| 1 | package Test::Attean::SPARQLStarSuite; | ||
| 2 | |||
| 3 | use v5.14; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Attean; | ||
| 7 | use Attean::RDF; | ||
| 8 | use AtteanX::Parser::SPARQL; | ||
| 9 | use Attean::SimpleQueryEvaluator; | ||
| 10 | use Test::Attean::W3CManifestTestSuite; | ||
| 11 | |||
| 12 | use Carp; | ||
| 13 | use HTTP::Request; | ||
| 14 | use HTTP::Response; | ||
| 15 | use HTTP::Message::PSGI; | ||
| 16 | use Data::Dumper; | ||
| 17 | use Encode qw(encode encode_utf8); | ||
| 18 | use Getopt::Long; | ||
| 19 | use Regexp::Common qw /URI/; | ||
| 20 | use Scalar::Util qw(blessed reftype); | ||
| 21 | use List::Util qw(all); | ||
| 22 | use Test::More; | ||
| 23 | use Test::Roo::Role; | ||
| 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 | my $dir = $self->tests_dir; | ||
| 45 | unless (defined($dir)) { | ||
| 46 | plan skip_all => "No manifest directory given"; | ||
| 47 | exit(0); | ||
| 48 | } | ||
| 49 | |||
| 50 | unless (-d $dir and -r $dir) { | ||
| 51 | plan skip_all => "Manifest directory not readable: $dir"; | ||
| 52 | exit(0); | ||
| 53 | } | ||
| 54 | |||
| 55 | my $manifest = File::Spec->catfile($dir, 'manifest.ttl'); | ||
| 56 | my $manifestall = File::Spec->catfile($dir, 'manifest-all.ttl'); | ||
| 57 | return grep { -r $_ } ($manifest, $manifestall); | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | ############################################################################### | ||
| 62 | |||
| 63 | |||
| 64 | Test::Roo::top_test 'SPARQL 1.2 tests' => sub { | ||
| 65 | my $self = shift; | ||
| 66 | my $PATTERN = $self->pattern; | ||
| 67 | my @manifests = @{ $self->manifests }; | ||
| 68 | my $model = $self->model; | ||
| 69 | |||
| 70 | foreach my $m (@manifests) { | ||
| 71 | warn "Manifest: " . $m->as_string . "\n" if ($self->debug); | ||
| 72 | my ($list) = $model->objects( $m, iri("${MF}entries") )->elements; | ||
| 73 | unless (blessed($list)) { | ||
| 74 | warn "No mf:entries found for manifest " . $m->as_string . "\n" if ($self->debug); | ||
| 75 | } | ||
| 76 | my @tests = $model->get_list( $self->default_graph, $list )->elements; | ||
| 77 | foreach my $test (@tests) { | ||
| 78 | unless ($test->value =~ /$PATTERN/) { | ||
| 79 | next; | ||
| 80 | } | ||
| 81 | warn "Test: " . $test->as_string . "\n" if ($self->debug); | ||
| 82 | # if ($LIST_TESTS) { | ||
| 83 | # say $test->value; | ||
| 84 | # } | ||
| 85 | |||
| 86 | my $handled = 0; | ||
| 87 | if ($self->run_query_tests) { | ||
| 88 | { | ||
| 89 | # Evaluation Tests | ||
| 90 | my $et = $model->count_quads($test, iri("${RDF}type"), iri("${MF}QueryEvaluationTest")); | ||
| 91 | my $ct = $model->count_quads($test, iri("${RDF}type"), iri("${MF}CSVResultFormatTest")); | ||
| 92 | if ($et + $ct) { | ||
| 93 | $handled++; | ||
| 94 | my ($name) = $model->objects( $test, iri("${MF}name") )->elements; | ||
| 95 | warn "### query eval test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug); | ||
| 96 | $self->query_eval_test( $model, $test ); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | { | ||
| 101 | # Syntax Tests | ||
| 102 | my $total = 0; | ||
| 103 | foreach my $type (qw(PositiveSyntaxTest NegativeSyntaxTest)) { | ||
| 104 | $total += $model->count_quads($test, iri("${RDF}type"), iri("${MF}$type")); | ||
| 105 | } | ||
| 106 | |||
| 107 | if ($total) { | ||
| 108 | $handled++; | ||
| 109 | my ($name) = $model->objects( $test, iri("${MF}name") )->elements; | ||
| 110 | warn "### query syntax test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug); | ||
| 111 | $self->sparql_syntax_test( 'query', $model, $test ); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | if ($self->run_update_tests) { | ||
| 117 | { | ||
| 118 | # Evaluation Tests | ||
| 119 | if ($model->count_quads($test, iri("${RDF}type"), iri("${UT}UpdateEvaluationTest")) or $model->count_quads($test, iri("${RDF}type"), iri("${MF}UpdateEvaluationTest"))) { | ||
| 120 | $handled++; | ||
| 121 | my ($name) = $model->objects( $test, iri("${MF}name") )->elements; | ||
| 122 | unless ($test->value =~ /$PATTERN/) { | ||
| 123 | next; | ||
| 124 | } | ||
| 125 | warn "### update eval test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug); | ||
| 126 | $self->update_eval_test( $model, $test, ); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | { | ||
| 131 | # Syntax Tests | ||
| 132 | my $total = 0; | ||
| 133 | foreach my $type (qw(PositiveUpdateSyntaxTest NegativeUpdateSyntaxTest)) { | ||
| 134 | $total += $model->count_quads($test, iri("${RDF}type"), iri("${MF}$type")); | ||
| 135 | } | ||
| 136 | |||
| 137 | if ($total) { | ||
| 138 | $handled++; | ||
| 139 | my ($name) = $model->objects( $test, iri("${MF}name") )->elements; | ||
| 140 | warn "### query syntax test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug); | ||
| 141 | $self->sparql_syntax_test( 'update', $model, $test ); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | unless ($handled) { | ||
| 147 | my (@types) = $model->objects( $test, iri("${RDF}type") )->elements; | ||
| 148 | foreach my $t (@types) { | ||
| 149 | diag("type: " . $t->value); | ||
| 150 | } | ||
| 151 | fail("Unhandled test: " . $test->value); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | }; | ||
| 156 | |||
| 157 | |||
| 158 | 1; |