| 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 Module::Load::Conditional qw(can_load); | ||
| 21 | use Regexp::Common qw /URI/; | ||
| 22 | use Scalar::Util qw(blessed reftype); | ||
| 23 | use List::Util qw(all any); | ||
| 24 | use Test::More; | ||
| 25 | use Text::CSV; | ||
| 26 | use Try::Tiny; | ||
| 27 | use URI::file; | ||
| 28 | use File::Spec; | ||
| 29 | use Types::Standard qw(Str Bool ArrayRef HashRef InstanceOf ConsumerOf); | ||
| 30 | require XML::Simple; | ||
| 31 | |||
| 32 | my $XSD = 'http://www.w3.org/2001/XMLSchema#'; | ||
| 33 | my $RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; | ||
| 34 | my $RDFS = 'http://www.w3.org/2000/01/rdf-schema#'; | ||
| 35 | my $RDFT = 'http://www.w3.org/ns/rdftest#'; | ||
| 36 | my $RS = 'http://www.w3.org/2001/sw/DataAccess/tests/result-set#'; | ||
| 37 | my $MF = 'http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#'; | ||
| 38 | my $UT = 'http://www.w3.org/2009/sparql/tests/test-update#'; | ||
| 39 | my $RQ = 'http://www.w3.org/2001/sw/DataAccess/tests/test-query#'; | ||
| 40 | my $DAWGT = 'http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#'; | ||
| 41 | my $FORMATS = 'http://www.w3.org/ns/formats/'; | ||
| 42 | |||
| 43 | my $direct = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#DirectGraphIdentification'); | ||
| 44 | my $indirect = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#IndirectGraphIdentification'); | ||
| 45 | my $post_creation = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#POSTGraphCreation'); | ||
| 46 | |||
| 47 | with 'Test::Attean::W3CManifestTestSuite'; | ||
| 48 | |||
| 49 | sub manifest_paths { | ||
| 50 | my $self = shift; | ||
| 51 | my $dir = $self->tests_dir; | ||
| 52 | unless (defined($dir)) { | ||
| 53 | plan skip_all => "No manifest directory given"; | ||
| 54 | exit(0); | ||
| 55 | } | ||
| 56 | |||
| 57 | unless (-d $dir and -r $dir) { | ||
| 58 | plan skip_all => "Manifest directory not readable: $dir"; | ||
| 59 | exit(1); | ||
| 60 | } | ||
| 61 | |||
| 62 | my $manifest = File::Spec->catfile($dir, 'manifest.ttl'); | ||
| 63 | my $manifestall = File::Spec->catfile($dir, 'manifest-all.ttl'); | ||
| 64 | my @manifests = grep { -r $_ } ($manifest, $manifestall); | ||
| 65 | unless (scalar(@manifests)) { | ||
| 66 | die "No readable manifest: $dir\n"; | ||
| 67 | } | ||
| 68 | return @manifests; | ||
| 69 | } | ||
| 70 | |||
| 71 | # Look up the test in the model, and determine: | ||
| 72 | # | ||
| 73 | # * the test type (eval, syntax, prot, gsp, or sd) | ||
| 74 | # * whether the test is a query test | ||
| 75 | # * whether the test is an update test | ||
| 76 | # * whether the test is a negative test (implies a syntax test) | ||
| 77 | # | ||
| 78 | # If the test is not a recognized type, this method dies. Calling code is responsible | ||
| 79 | # for catching that error and properly recording a test failure. | ||
| 80 | sub query_test_type { | ||
| 81 | my $self = shift; | ||
| 82 | my $model = shift; | ||
| 83 | my $test = shift; | ||
| 84 | |||
| 85 | my %types = map { $_->value => $_ } $model->type_hierarchy( $test ); | ||
| 86 | # warn $test->value . ' has types: ' . join(', ', keys %types); | ||
| 87 | |||
| 88 | my $is_eval = $types{ "${MF}TestEval" }; | ||
| 89 | my $is_syntax = $types{ "${MF}TestSyntax" }; | ||
| 90 | my $is_prot = $types{ "${MF}ProtocolTest" }; | ||
| 91 | my $is_gsp = $types{ "${MF}GraphStoreProtocolTest" }; | ||
| 92 | my $is_sd = $types{ "${MF}ServiceDescriptionTest" }; | ||
| 93 | |||
| 94 | my $is_query = any { $types{ "${MF}$_" } or $types{ "${UT}$_" } } qw(QueryEvaluationTest PositiveSyntaxTest PositiveSyntaxTest11 NegativeSyntaxTest NegativeSyntaxTest11 CSVResultFormatTest); | ||
| 95 | my $is_update = any { $types{ "${MF}$_" } or $types{ "${UT}$_" } } qw(UpdateEvaluationTest PositiveUpdateSyntaxTest PositiveUpdateSyntaxTest11 NegativeUpdateSyntaxTest NegativeUpdateSyntaxTest11); | ||
| 96 | |||
| 97 | my $is_neg = any { /Negative/ } keys %types; | ||
| 98 | $is_syntax++ if ($is_neg); # to avoid having to do multiple subClassOf hops on these tests to find rdft:TextSyntax | ||
| 99 | |||
| 100 | my $type; | ||
| 101 | $type = 'eval' if ($is_eval); | ||
| 102 | $type = 'syntax' if ($is_syntax); | ||
| 103 | $type = 'prot' if ($is_prot); | ||
| 104 | $type = 'gsp' if ($is_gsp); | ||
| 105 | $type = 'sd' if ($is_sd); | ||
| 106 | |||
| 107 | unless (defined($type)) { | ||
| 108 | my $types = join(',', keys %types); | ||
| 109 | die "Unhandled type for " . $test->value . " ; types=$types"; | ||
| 110 | } | ||
| 111 | |||
| 112 | return ($type, $is_query, $is_update, $is_neg); | ||
| 113 | } | ||
| 114 | |||
| 115 | ############################################################################### | ||
| 116 | |||
| 117 | Test::Roo::top_test 'SPARQL tests' => sub { | ||
| 118 | my $self = shift; | ||
| 119 | my $PATTERN = $self->pattern; | ||
| 120 | my @manifests = @{ $self->manifests }; | ||
| 121 | my $model = $self->model; | ||
| 122 | foreach my $m (@manifests) { | ||
| 123 | # warn "Manifest: " . $m->as_string . "\n" if ($self->debug); | ||
| 124 | my $specVersion = '1.1'; | ||
| 125 | my ($sv) = $model->objects( $m, iri("${MF}specVersion") )->elements; | ||
| 126 | if ($sv) { | ||
| 127 | my $v = $sv->value; | ||
| 128 | if ($v =~ m<http://www.w3.org/ns/sparql#version-(.*)>) { | ||
| 129 | $specVersion = $1; | ||
| 130 | } else { | ||
| 131 | warn "*** Unexpected specVersion $v\n"; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | my ($list) = $model->objects( $m, iri("${MF}entries") )->elements; | ||
| 135 | my @tests = $model->get_list( $self->default_graph, $list )->elements; | ||
| 136 | foreach my $test (@tests) { | ||
| 137 | SKIP: { | ||
| 138 | unless ($test->value =~ /$PATTERN/) { | ||
| 139 | next; | ||
| 140 | } | ||
| 141 | |||
| 142 | my ($test_type, $is_query_test, $is_update_test, $is_neg) = $self->query_test_type($model, $test); | ||
| 143 | if ($is_query_test) { | ||
| 144 | my ($name) = $model->objects( $test, iri("${MF}name") )->elements; | ||
| 145 | if ($self->run_query_tests) { | ||
| 146 | if ($test_type eq 'eval') { | ||
| 147 | # Evaluation Tests | ||
| 148 | warn "### query eval test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug); | ||
| 149 | $self->query_eval_test( $model, $test, specVersion => $specVersion ); | ||
| 150 | } elsif ($test_type eq 'syntax') { | ||
| 151 | # Syntax Tests | ||
| 152 | warn "### query syntax test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug); | ||
| 153 | $self->sparql_syntax_test( 'query', $model, $test, specVersion => $specVersion ); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | next; | ||
| 157 | } elsif ($is_update_test) { | ||
| 158 | my ($name) = $model->objects( $test, iri("${MF}name") )->elements; | ||
| 159 | if ($self->run_update_tests) { | ||
| 160 | if ($test_type eq 'eval') { | ||
| 161 | # Evaluation Tests | ||
| 162 | warn "### update eval test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug); | ||
| 163 | $self->update_eval_test( $model, $test, specVersion => $specVersion ); | ||
| 164 | } elsif ($test_type eq 'syntax') { | ||
| 165 | # Syntax Tests | ||
| 166 | warn "### query syntax test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug); | ||
| 167 | $self->sparql_syntax_test( 'update', $model, $test, specVersion => $specVersion ); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | next; | ||
| 171 | } elsif ($test_type eq 'prot' or $test_type eq 'gsp' or $test_type eq 'sd') { | ||
| 172 | unless (can_load( modules => { 'AtteanX::Endpoint' => 0, 'LWP::Protocol::PSGI' => 0 })) { | ||
| 173 | $self->record_result('unhandled', 0, $test->as_string); | ||
| 174 | my $type = ($test_type eq 'prot') ? 'Protocol' : 'GSP'; | ||
| 175 | skip("${type} test is not handled (cannot load necessary modules; ensure AtteanX::Endpoint and LWP::Protocol::PSGI are available)"); | ||
| 176 | next; | ||
| 177 | } | ||
| 178 | |||
| 179 | if ($test_type eq 'sd') { | ||
| 180 | # $self->record_result('unhandled', 0, $test->as_string); | ||
| 181 | # skip("Service Description test is not handled"); | ||
| 182 | $self->sparql_service_description_test($model, $test, specVersion => $specVersion); | ||
| 183 | } else { | ||
| 184 | if ($test_type eq 'gsp') { | ||
| 185 | my (@requirements) = $model->objects($test, iri("${MF}requires"))->elements; | ||
| 186 | foreach my $r (@requirements) { | ||
| 187 | skip("Not configured to run GSP direct-identification tests") if ($direct->equals($r) and not($self->run_gsp_direct)); | ||
| 188 | skip("Not configured to run GSP indirect-identification tests") if ($indirect->equals($r) and not($self->run_gsp_indirect)); | ||
| 189 | skip("Not configured to run GSP tests requiring POST to create fresh graph names") if ($post_creation->equals($r) and not($self->run_gsp_postcreate)); | ||
| 190 | } | ||
| 191 | |||
| 192 | } | ||
| 193 | |||
| 194 | subtest $test->value => sub { | ||
| 195 | $self->sparql_protocol_test($test_type, $model, $test, specVersion => $specVersion); | ||
| 196 | }; | ||
| 197 | } | ||
| 198 | next; | ||
| 199 | } | ||
| 200 | |||
| 201 | diag("Test is not handled: " . $test->value); | ||
| 202 | $self->record_result('unhandled', 0, $test->as_string); | ||
| 203 | } | ||
| 204 | } | ||
| 205 | } | ||
| 206 | }; | ||
| 207 | |||
| 208 | |||
| 209 | 1; |