| 1 | package Test::Attean::SPARQLSuite; | ||
| 2 | |||
| 3 | use v5.14; | ||
| 4 | use warnings; | ||
| 5 | use Test::Roo::Role; | ||
| 6 | |||
| 7 | use Attean; | ||
| 8 | use Attean::RDF; | ||
| 9 | use AtteanX::Parser::SPARQL; | ||
| 10 | use Attean::SimpleQueryEvaluator; | ||
| 11 | use Test::Attean::W3CManifestTestSuite; | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | use HTTP::Request; | ||
| 15 | use HTTP::Response; | ||
| 16 | use HTTP::Message::PSGI; | ||
| 17 | use Data::Dumper; | ||
| 18 | use Encode qw(encode encode_utf8); | ||
| 19 | use Getopt::Long; | ||
| 20 | use Regexp::Common qw /URI/; | ||
| 21 | use Scalar::Util qw(blessed reftype); | ||
| 22 | use List::Util qw(all); | ||
| 23 | use Test::More; | ||
| 24 | use Text::CSV; | ||
| 25 | use Try::Tiny; | ||
| 26 | use URI::file; | ||
| 27 | use File::Spec; | ||
| 28 | use Types::Standard qw(Str Bool ArrayRef HashRef InstanceOf ConsumerOf); | ||
| 29 | require XML::Simple; | ||
| 30 | |||
| 31 | my $XSD = 'http://www.w3.org/2001/XMLSchema#'; | ||
| 32 | my $RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; | ||
| 33 | my $RDFS = 'http://www.w3.org/2000/01/rdf-schema#'; | ||
| 34 | my $RS = 'http://www.w3.org/2001/sw/DataAccess/tests/result-set#'; | ||
| 35 | my $MF = 'http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#'; | ||
| 36 | my $UT = 'http://www.w3.org/2009/sparql/tests/test-update#'; | ||
| 37 | my $RQ = 'http://www.w3.org/2001/sw/DataAccess/tests/test-query#'; | ||
| 38 | my $DAWGT = 'http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#'; | ||
| 39 | |||
| 40 | with 'Test::Attean::W3CManifestTestSuite'; | ||
| 41 | |||
| 42 | sub manifest_paths { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | my $dir = $self->tests_dir; | ||
| 46 | unless (defined($dir)) { | ||
| 47 | plan skip_all => "No manifest directory given"; | ||
| 48 | exit(0); | ||
| 49 | } | ||
| 50 | |||
| 51 | my $manifest = File::Spec->catfile($dir, 'manifest.ttl'); | ||
| 52 | if (-r $manifest) { | ||
| 53 | return ($manifest); | ||
| 54 | } else { | ||
| 55 | die "Manifest is not readable: $manifest\n"; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub recursively_find_manifest_files { | ||
| 60 | my $self = shift; | ||
| 61 | my $manifest = shift; | ||
| 62 | |||
| 63 | my $model = $self->memory_model(); | ||
| 64 | my $class = Attean->get_parser("turtle") || die "Failed to load parser for 'turtle'"; | ||
| 65 | |||
| 66 | my %loaded; | ||
| 67 | |||
| 68 | my %manifests; | ||
| 69 | my %manifest_files = ($manifest => 1); | ||
| 70 | my @load = map { iri("file://" . File::Spec->rel2abs($_)) } ($manifest); | ||
| 71 | while (scalar(@load)) { | ||
| 72 | foreach my $iri (@load) { | ||
| 73 | # warn "Loading " . $iri->value . "\n" if ($self->debug); | ||
| 74 | $loaded{ $iri->value }++; | ||
| 75 | } | ||
| 76 | $model->load_urls_into_graph($self->default_graph, @load); | ||
| 77 | @load = (); | ||
| 78 | |||
| 79 | my $subjects = $model->subjects( iri("${RDF}type"), iri("${MF}Manifest") ); | ||
| 80 | my @manifest_matches = $subjects->elements; | ||
| 81 | foreach my $m (@manifest_matches) { | ||
| 82 | if (not exists $manifests{ $m->value }) { | ||
| 83 | $manifests{ $m->value } = $m; | ||
| 84 | my @list_heads = $model->objects( $m, iri("${MF}include") )->elements; | ||
| 85 | my @elements = map { $model->get_list(undef, $_)->elements() } @list_heads; | ||
| 86 | my @new = grep { not exists($loaded{$_->value}) } @elements; | ||
| 87 | push(@load, @new); | ||
| 88 | foreach my $n (@new) { | ||
| 89 | my $url = $n->value; | ||
| 90 | $url =~ s{file://}{}; | ||
| 91 | $manifest_files{ $url }++; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | return keys %manifest_files; | ||
| 98 | } | ||
| 99 | |||
| 100 | ############################################################################### | ||
| 101 | |||
| 102 | |||
| 103 | Test::Roo::top_test 'SPARQL tests' => sub { | ||
| 104 | my $self = shift; | ||
| 105 | my $PATTERN = $self->pattern; | ||
| 106 | my @manifests = @{ $self->manifests }; | ||
| 107 | my $model = $self->model; | ||
| 108 | foreach my $m (@manifests) { | ||
| 109 | # warn "Manifest: " . $m->as_string . "\n" if ($self->debug); | ||
| 110 | my $specVersion = '1.1'; | ||
| 111 | my ($sv) = $model->objects( $m, iri("${MF}specVersion") )->elements; | ||
| 112 | if ($sv) { | ||
| 113 | my $v = $sv->value; | ||
| 114 | if ($v =~ m<http://www.w3.org/ns/sparql#version-(.*)>) { | ||
| 115 | $specVersion = $1; | ||
| 116 | } else { | ||
| 117 | warn "*** Unexpected specVersion $v\n"; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | my ($list) = $model->objects( $m, iri("${MF}entries") )->elements; | ||
| 121 | # unless (blessed($list)) { | ||
| 122 | # warn "No mf:entries found for manifest " . $m->as_string . "\n" if ($self->debug); | ||
| 123 | # } | ||
| 124 | my @tests = $model->get_list( $self->default_graph, $list )->elements; | ||
| 125 | foreach my $test (@tests) { | ||
| 126 | unless ($test->value =~ /$PATTERN/) { | ||
| 127 | next; | ||
| 128 | } | ||
| 129 | # if ($LIST_TESTS) { | ||
| 130 | # say $test->value; | ||
| 131 | # } | ||
| 132 | if ($self->run_query_tests) { | ||
| 133 | { | ||
| 134 | # Evaluation Tests | ||
| 135 | my $et = $model->count_quads($test, iri("${RDF}type"), iri("${MF}QueryEvaluationTest")); | ||
| 136 | my $ct = $model->count_quads($test, iri("${RDF}type"), iri("${MF}CSVResultFormatTest")); | ||
| 137 | if ($et + $ct) { | ||
| 138 | my ($name) = $model->objects( $test, iri("${MF}name") )->elements; | ||
| 139 | warn "### query eval test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug); | ||
| 140 | $self->query_eval_test( $model, $test, specVersion => $specVersion ); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | { | ||
| 145 | # Syntax Tests | ||
| 146 | my $total = 0; | ||
| 147 | foreach my $type (qw(PositiveSyntaxTest PositiveSyntaxTest11 NegativeSyntaxTest NegativeSyntaxTest11)) { | ||
| 148 | $total += $model->count_quads($test, iri("${RDF}type"), iri("${MF}$type")); | ||
| 149 | } | ||
| 150 | |||
| 151 | if ($total) { | ||
| 152 | my ($name) = $model->objects( $test, iri("${MF}name") )->elements; | ||
| 153 | warn "### query syntax test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug); | ||
| 154 | $self->sparql_syntax_test( 'query', $model, $test, specVersion => $specVersion ); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | if ($self->run_update_tests) { | ||
| 160 | { | ||
| 161 | # Evaluation Tests | ||
| 162 | if ($model->count_quads($test, iri("${RDF}type"), iri("${UT}UpdateEvaluationTest")) or $model->count_quads($test, iri("${RDF}type"), iri("${MF}UpdateEvaluationTest"))) { | ||
| 163 | my ($name) = $model->objects( $test, iri("${MF}name") )->elements; | ||
| 164 | unless ($test->value =~ /$PATTERN/) { | ||
| 165 | next; | ||
| 166 | } | ||
| 167 | warn "### update eval test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug); | ||
| 168 | $self->update_eval_test( $model, $test, specVersion => $specVersion ); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | { | ||
| 173 | # Syntax Tests | ||
| 174 | my $total = 0; | ||
| 175 | foreach my $type (qw(PositiveUpdateSyntaxTest PositiveUpdateSyntaxTest11 NegativeUpdateSyntaxTest NegativeUpdateSyntaxTest11)) { | ||
| 176 | $total += $model->count_quads($test, iri("${RDF}type"), iri("${MF}$type")); | ||
| 177 | } | ||
| 178 | |||
| 179 | if ($total) { | ||
| 180 | my ($name) = $model->objects( $test, iri("${MF}name") )->elements; | ||
| 181 | warn "### query syntax test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug); | ||
| 182 | $self->sparql_syntax_test( 'update', $model, $test, specVersion => $specVersion ); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | ||
| 188 | }; | ||
| 189 | |||
| 190 | |||
| 191 | 1; |