| 1 | package Test::Attean::W3CManifestTestSuite; | ||
| 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::TestSimpleQueryEvaluator; | ||
| 11 | use Test::Attean::TestIDPQueryPlanner; | ||
| 12 | use Module::Load::Conditional qw[can_load]; | ||
| 13 | |||
| 14 | require XML::Simple; | ||
| 15 | use Carp; | ||
| 16 | use Data::Dumper; | ||
| 17 | use Encode qw(encode encode_utf8 decode_utf8); | ||
| 18 | use File::Slurp; | ||
| 19 | use File::Spec; | ||
| 20 | use FindBin qw($Bin); | ||
| 21 | use Getopt::Long; | ||
| 22 | use HTTP::Message::PSGI; | ||
| 23 | use HTTP::Request; | ||
| 24 | use HTTP::Response; | ||
| 25 | use HTTP::Status; | ||
| 26 | use JSON qw(decode_json); | ||
| 27 | use List::Util qw(all any); | ||
| 28 | use Regexp::Common qw /URI/; | ||
| 29 | use Scalar::Util qw(blessed reftype); | ||
| 30 | use Test::Modern; | ||
| 31 | use Test::Roo::Role; | ||
| 32 | use Text::CSV; | ||
| 33 | use Text::Table; | ||
| 34 | use Try::Tiny; | ||
| 35 | use Types::Standard qw(Str Bool Int ArrayRef HashRef InstanceOf ConsumerOf); | ||
| 36 | use URI::file; | ||
| 37 | use UUID::Tiny ':std'; | ||
| 38 | |||
| 39 | my $XSD = 'http://www.w3.org/2001/XMLSchema#'; | ||
| 40 | my $RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; | ||
| 41 | my $RDFS = 'http://www.w3.org/2000/01/rdf-schema#'; | ||
| 42 | my $RDFT = 'http://www.w3.org/ns/rdftest#'; | ||
| 43 | my $RS = 'http://www.w3.org/2001/sw/DataAccess/tests/result-set#'; | ||
| 44 | my $MF = 'http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#'; | ||
| 45 | my $UT = 'http://www.w3.org/2009/sparql/tests/test-update#'; | ||
| 46 | my $RQ = 'http://www.w3.org/2001/sw/DataAccess/tests/test-query#'; | ||
| 47 | my $SD = 'http://www.w3.org/ns/sparql-service-description#'; | ||
| 48 | my $DAWGT = 'http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#'; | ||
| 49 | my $FORMATS = 'http://www.w3.org/ns/formats/'; | ||
| 50 | |||
| 51 | my $absolutePath = iri('http://www.w3.org/2011/http#absolutePath'); | ||
| 52 | my $authority = iri('http://www.w3.org/2011/http#connectionAuthority'); | ||
| 53 | my $cnt_chars = iri('http://www.w3.org/2011/content#chars'); | ||
| 54 | my $cnt_encoding = iri('http://www.w3.org/2011/content#characterEncoding'); | ||
| 55 | my $entries = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#entries'); | ||
| 56 | my $expectedBoolean = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#expectedBoolean'); | ||
| 57 | my $expectedFormat = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#expectedFormat'); | ||
| 58 | my $expectedSchemaConformance = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#expectedSchemaConformance'); | ||
| 59 | my $expectedSPARQLMatch = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#expectedSPARQLMatch'); | ||
| 60 | my $expectedLocation= iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#expectedLocation'); | ||
| 61 | my $expectedStatus = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#expectedStatus'); | ||
| 62 | my $expectedStatusClass = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#expectedStatusClass'); | ||
| 63 | my $ht_body = iri('http://www.w3.org/2011/http#body'); | ||
| 64 | my $ht_fieldName = iri('http://www.w3.org/2011/http#fieldName'); | ||
| 65 | my $ht_fieldValue = iri('http://www.w3.org/2011/http#fieldValue'); | ||
| 66 | my $ht_headers = iri('http://www.w3.org/2011/http#headers'); | ||
| 67 | my $ht_resp = iri('http://www.w3.org/2011/http#resp'); | ||
| 68 | my $Manifest = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#Manifest'); | ||
| 69 | my $methodName = iri('http://www.w3.org/2011/http#methodName'); | ||
| 70 | my $mf_action = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#action'); | ||
| 71 | my $mf_expectation = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#expectation'); | ||
| 72 | my $mf_name = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#name'); | ||
| 73 | my $rdf_type = iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'); | ||
| 74 | my $rdfs_label = iri('http://www.w3.org/2000/01/rdf-schema#label'); | ||
| 75 | my $requests = iri('http://www.w3.org/2011/http#requests'); | ||
| 76 | my $ut_graph = iri('http://www.w3.org/2009/sparql/tests/test-update#graph'); | ||
| 77 | my $ut_graphData = iri('http://www.w3.org/2009/sparql/tests/test-update#graphData'); | ||
| 78 | my $followLinkType = iri('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#followLinkTypeRedirect'); | ||
| 79 | |||
| 80 | requires 'test_model'; | ||
| 81 | requires 'manifest_paths'; | ||
| 82 | |||
| 83 | has run_update_tests => (is => 'rw', isa => Bool, default => 1); | ||
| 84 | has run_query_tests => (is => 'rw', isa => Bool, default => 1); | ||
| 85 | has run_syntax_tests => (is => 'rw', isa => Bool, default => 1); | ||
| 86 | has run_gsp_direct => (is => 'rw', isa => Bool, default => 1); | ||
| 87 | has run_gsp_indirect => (is => 'rw', isa => Bool, default => 1); | ||
| 88 | has run_gsp_postcreate => (is => 'rw', isa => Bool, default => 1); | ||
| 89 | |||
| 90 | |||
| 91 | |||
| 92 | has debug => (is => 'rw', isa => Bool, default => 0); | ||
| 93 | has results => (is => 'rw', isa => Bool, default => 0); | ||
| 94 | has summary => (is => 'rw', isa => Bool, default => 0); | ||
| 95 | has strict_approval => (is => 'rw', isa => Bool, default => 0); | ||
| 96 | has use_idp_planner => (is => 'rw', isa => Bool, default => 1); | ||
| 97 | has pattern => (is => 'rw', isa => Str, default => ''); | ||
| 98 | |||
| 99 | has tests_dir => (is => 'rw', required => 1, default => sub { $ENV{ATTEAN_SPARQL_TESTS_DIR} }); | ||
| 100 | has model => (is => 'rw', isa => ConsumerOf['Attean::API::Model'], init_arg => undef); | ||
| 101 | has manifests => (is => 'rw', isa => ArrayRef, init_arg => undef); | ||
| 102 | has default_graph => (is => 'rw'); | ||
| 103 | has failures => (is => 'rw', isa => HashRef, default => sub { +{} }); | ||
| 104 | has supported_extensions => (is => 'rw', isa => HashRef, default => sub { +{} }); | ||
| 105 | |||
| 106 | has timestamp => (is => 'rw', isa => Int, default => sub { scalar(time) }); | ||
| 107 | has earl => (is => 'rw', isa => InstanceOf['RDF::EARL']); | ||
| 108 | has earl_filename => (is => 'rw', isa => Str); | ||
| 109 | |||
| 110 | has load_ontology_files => (is => 'rw', isa => ArrayRef[Str]); | ||
| 111 | |||
| 112 | after 'BUILD' => sub { | ||
| 113 | my $self = shift; | ||
| 114 | if ($self->debug) { | ||
| 115 | $self->results(1); | ||
| 116 | } | ||
| 117 | |||
| 118 | $self->earl_filename(sprintf('.sparql-test-suite-%d', $self->timestamp)); | ||
| 119 | my @files = @{ $self->load_ontology_files || [] }; | ||
| 120 | unless (scalar(@files)) { | ||
| 121 | my $env_path = $ENV{'ATTEAN_LOAD_PATH'}; | ||
| 122 | my $base = (defined($env_path) and -d $env_path) ? $env_path : File::Spec->catfile( $Bin, 'data', 'test-rdf' ); | ||
| 123 | push(@files, glob("${base}/*.ttl")); | ||
| 124 | $self->load_ontology_files(\@files); | ||
| 125 | } | ||
| 126 | |||
| 127 | }; | ||
| 128 | |||
| 129 | sub memory_model { | ||
| 130 | my $self = shift; | ||
| 131 | my $store = Attean->get_store('Memory')->new(); | ||
| 132 | my $model = Attean::MutableQuadModel->new( store => $store ); | ||
| 133 | return $model; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub setup { | ||
| 137 | my $self = shift; | ||
| 138 | $self->default_graph(iri('http://graph/')); | ||
| 139 | |||
| 140 | warn 'setting up sparql test harness' if ($self->debug); | ||
| 141 | my @manifests_iris = $self->manifest_paths(); | ||
| 142 | unless (scalar(@manifests_iris)) { | ||
| 143 | my $dir = $self->tests_dir; | ||
| 144 | plan skip_all => "No manifest files found in $dir"; | ||
| 145 | exit(0); | ||
| 146 | } | ||
| 147 | |||
| 148 | my $model = $self->memory_model(); | ||
| 149 | foreach my $f (@{ $self->load_ontology_files }) { | ||
| 150 | my $url = "file://$f"; | ||
| 151 | try { | ||
| 152 | warn "Loading $url\n" if ($self->debug); | ||
| 153 | $model->load_urls_into_graph($self->default_graph, $url); | ||
| 154 | } catch { | ||
| 155 | warn "Failed to load $url\n"; | ||
| 156 | }; | ||
| 157 | } | ||
| 158 | |||
| 159 | $self->model($model); | ||
| 160 | my $class = Attean->get_parser("turtle") || die "Failed to load parser for 'turtle'"; | ||
| 161 | |||
| 162 | my %loaded; | ||
| 163 | |||
| 164 | my %manifests; | ||
| 165 | my @load = map { iri("file://" . File::Spec->rel2abs($_)) } @manifests_iris; | ||
| 166 | while (scalar(@load)) { | ||
| 167 | foreach my $iri (@load) { | ||
| 168 | warn "Loading " . $iri->value . "\n" if ($self->debug); | ||
| 169 | $loaded{ $iri->value }++; | ||
| 170 | } | ||
| 171 | $model->load_urls_into_graph($self->default_graph, @load); | ||
| 172 | @load = (); | ||
| 173 | |||
| 174 | my $subjects = $model->subjects( iri("${RDF}type"), iri("${MF}Manifest") ); | ||
| 175 | my @manifest_matches = $subjects->elements; | ||
| 176 | foreach my $m (@manifest_matches) { | ||
| 177 | if (not exists $manifests{ $m->value }) { | ||
| 178 | $manifests{ $m->value } = $m; | ||
| 179 | my @list_heads = $model->objects( $m, iri("${MF}include") )->elements; | ||
| 180 | my @elements = map { $model->get_list(undef, $_)->elements() } @list_heads; | ||
| 181 | push(@load, grep { not exists($loaded{$_->value}) } @elements); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | my @manifests = values %manifests; | ||
| 187 | |||
| 188 | if ($self->debug) { | ||
| 189 | warn "done parsing manifests" if $self->debug; | ||
| 190 | warn "setting manifests:\n"; | ||
| 191 | foreach my $m (@manifests) { | ||
| 192 | warn "- " . $m->as_string . "\n"; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | $self->manifests(\@manifests); | ||
| 196 | } | ||
| 197 | |||
| 198 | sub setup_endpoint { | ||
| 199 | my $self = shift; | ||
| 200 | my $config = shift // { | ||
| 201 | store => 'Memory2', | ||
| 202 | endpoint => { | ||
| 203 | service_description => { | ||
| 204 | named_graphs => 1, | ||
| 205 | default => 1, | ||
| 206 | }, | ||
| 207 | load_data => 0, | ||
| 208 | update => 1, | ||
| 209 | } | ||
| 210 | }; | ||
| 211 | my $end = AtteanX::Endpoint->new( $config ); | ||
| 212 | my $app = sub { | ||
| 213 | my $env = shift; | ||
| 214 | my $req = Plack::Request->new($env); | ||
| 215 | my $resp = $end->run( $req ); | ||
| 216 | return $resp->finalize; | ||
| 217 | }; | ||
| 218 | |||
| 219 | my $host = 'localhost:3000'; | ||
| 220 | LWP::Protocol::PSGI->register($app, host => $host); | ||
| 221 | |||
| 222 | my $sparql_endpoint = "http://${host}/sparql"; | ||
| 223 | my $gsp_endpoint = "http://${host}/gsp"; | ||
| 224 | return ($sparql_endpoint, $gsp_endpoint); | ||
| 225 | } | ||
| 226 | |||
| 227 | sub sparql_service_description_test { | ||
| 228 | my $self = shift; | ||
| 229 | my $model = shift; | ||
| 230 | my $test = shift; | ||
| 231 | my %args = @_; | ||
| 232 | my $specVersion = $args{'specVersion'} // '1.1'; | ||
| 233 | |||
| 234 | my $testname = $test->value; | ||
| 235 | my ($sparql_endpoint, $gsp_endpoint) = $self->setup_endpoint(); | ||
| 236 | my $ua = LWP::UserAgent->new(); | ||
| 237 | # fail("$testname - TODO"); | ||
| 238 | |||
| 239 | my ($action) = $model->objects($test, $mf_action)->elements; | ||
| 240 | unless ($action) { | ||
| 241 | return fail('no action data for test ' . $testname); | ||
| 242 | } | ||
| 243 | my ($reqs) = $model->objects($action, $requests)->elements; | ||
| 244 | my @reqs = $model->get_list($self->default_graph, $reqs)->elements; | ||
| 245 | my ($auth) = $model->objects($action, $authority)->elements; | ||
| 246 | my $auth_v = $auth->value; | ||
| 247 | my $uri = new URI($sparql_endpoint); | ||
| 248 | my $real_auth = $uri->authority; | ||
| 249 | |||
| 250 | say scalar(@reqs) . ' request(s) in test' if ($self->debug); | ||
| 251 | my $req_number = 0; | ||
| 252 | foreach my $req (@reqs) { | ||
| 253 | $req_number++; | ||
| 254 | my ($follow) = $model->objects($req, $followLinkType)->elements; | ||
| 255 | my $follow_type = ($follow) ? $follow->value : undef; | ||
| 256 | subtest $testname => sub { | ||
| 257 | my $m = $self->make_protocol_request('sd', $sparql_endpoint, $model, $req, $auth_v, $real_auth); | ||
| 258 | # next unless ($m->method eq 'GET'); | ||
| 259 | warn "REQUEST:\n" . $m->as_string . "\n" if ($self->debug); | ||
| 260 | my $resp = $ua->request($m); | ||
| 261 | warn "RESPONSE:\n" . $resp->as_string if ($self->debug); | ||
| 262 | diag(sprintf('%s %s', $m->method, $m->uri)); | ||
| 263 | |||
| 264 | if ($follow_type) { | ||
| 265 | # Redirect if the endpoint returns a Link header pointing to the service description | ||
| 266 | diag("FOLLOW TYPE: ${follow_type}"); | ||
| 267 | my $type = quotemeta($follow_type); | ||
| 268 | if (my $link = $resp->header('Link')) { | ||
| 269 | if ($link =~ m/^<([^>]+)>[ \t]*;[ \t]*rel[ \t]*=[ \t]*(${type}|\"${type}\")$/) { | ||
| 270 | my ($url) = $1; | ||
| 271 | my $base = $m->uri->as_string; | ||
| 272 | my $uri = URI->new_abs($url, $base); | ||
| 273 | my $uri_s = $uri->as_string; | ||
| 274 | my $m = HTTP::Request->new(GET => $uri_s); | ||
| 275 | diag("Redirected to Service Description via Link header: ${uri_s}"); | ||
| 276 | $resp = $ua->request($m); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | $self->validate_protocol_response('sd', $sparql_endpoint, $model, $test, $req, $resp, $auth_v, $real_auth); | ||
| 282 | if ($self->debug) { | ||
| 283 | warn "--------------------------------- after request:\n"; | ||
| 284 | $self->dump_protocol_dataset($sparql_endpoint, $ua); | ||
| 285 | warn "---------------------------------\n"; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | } | ||
| 291 | |||
| 292 | sub sparql_protocol_test { | ||
| 293 | my $self = shift; | ||
| 294 | my $test_type = shift; | ||
| 295 | my $model = shift; | ||
| 296 | my $test = shift; | ||
| 297 | my %args = @_; | ||
| 298 | my $specVersion = $args{'specVersion'} // '1.1'; | ||
| 299 | |||
| 300 | my $testname = $test->value; | ||
| 301 | my ($sparql_endpoint, $gsp_endpoint) = $self->setup_endpoint(); | ||
| 302 | my $ua = LWP::UserAgent->new(); | ||
| 303 | subtest $testname => sub { | ||
| 304 | if ($test_type eq 'prot') { | ||
| 305 | eval { $self->run_protocol_test($test_type, $ua, $model, $test, $sparql_endpoint) }; | ||
| 306 | if ($@) { | ||
| 307 | warn "*** protocol test failed: $@"; | ||
| 308 | $self->record_result('unhandled', 0, $test->as_string); | ||
| 309 | fail($testname); | ||
| 310 | } | ||
| 311 | } else { | ||
| 312 | eval { $self->run_gsp_test($test_type, $ua, $model, $test, $sparql_endpoint, $gsp_endpoint) }; | ||
| 313 | if ($@) { | ||
| 314 | warn "*** protocol test failed: $@"; | ||
| 315 | $self->record_result('unhandled', 0, $test->as_string); | ||
| 316 | fail($testname); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | }; | ||
| 320 | } | ||
| 321 | |||
| 322 | sub setup_protocol_dataset { | ||
| 323 | my $self = shift; | ||
| 324 | my $ua = shift; | ||
| 325 | my $endpoint = shift; | ||
| 326 | my $model = shift; | ||
| 327 | my $test = shift; | ||
| 328 | warn "Clearing dataset ...\n" if ($self->debug); | ||
| 329 | warn "REQUEST: POST $endpoint update=DROP ALL\n" if ($self->debug); | ||
| 330 | my $resp = $ua->post($endpoint, {update => 'DROP ALL'}); | ||
| 331 | warn "RESPONSE:\n" . $resp->as_string . "\n" if ($self->debug); | ||
| 332 | die $resp->status_line unless ($resp->is_success); | ||
| 333 | |||
| 334 | my (@graphs) = $model->objects($test, $ut_graphData)->elements; | ||
| 335 | if (scalar(@graphs)) { | ||
| 336 | foreach my $graph (@graphs) { | ||
| 337 | # loading contents of $uri into graph named $name | ||
| 338 | my ($name) = map { $_->value } $model->objects($graph, $rdfs_label)->elements; | ||
| 339 | my ($uri) = map { $_->value } $model->objects($graph, $ut_graph)->elements; | ||
| 340 | my $update = "LOAD <$uri> INTO GRAPH <$name>"; | ||
| 341 | warn "Loading data <$name> ...\n" if ($self->debug); | ||
| 342 | my $resp = $ua->post($endpoint, {update => $update}); | ||
| 343 | die $resp->status_line unless ($resp->is_success); | ||
| 344 | } | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | sub run_gsp_test { | ||
| 349 | my $self = shift; | ||
| 350 | my $test_type = shift; | ||
| 351 | my $ua = shift; | ||
| 352 | my $model = shift; | ||
| 353 | my $test = shift; | ||
| 354 | my $prot_endpoint = shift; | ||
| 355 | my $endpoint = shift; | ||
| 356 | my $testname = $test->value; | ||
| 357 | say "# $testname" if ($self->debug); | ||
| 358 | my ($action) = $model->objects($test, $mf_action)->elements; | ||
| 359 | unless ($action) { | ||
| 360 | return fail('no action data for test ' . $testname); | ||
| 361 | } | ||
| 362 | my ($reqs) = $model->objects($action, $requests)->elements; | ||
| 363 | my @reqs = $model->get_list($self->default_graph, $reqs)->elements; | ||
| 364 | my ($auth) = $model->objects($action, $authority)->elements; | ||
| 365 | my $auth_v = $auth->value; | ||
| 366 | my $uri = new URI($endpoint); | ||
| 367 | my $real_auth = $uri->authority; | ||
| 368 | |||
| 369 | $ua->get("$prot_endpoint?================================================================NEWTEST"); | ||
| 370 | $self->setup_protocol_dataset($ua, $prot_endpoint, $model, $test); | ||
| 371 | |||
| 372 | say scalar(@reqs) . ' request(s) in test' if ($self->debug); | ||
| 373 | my $req_number = 1; | ||
| 374 | my $templates = {}; | ||
| 375 | foreach my $req (@reqs) { | ||
| 376 | my $name = "Request " . $req_number++; | ||
| 377 | subtest $name => sub { | ||
| 378 | say "--------------------------------- $name ---------------------------------" if ($self->debug); | ||
| 379 | my $m = $self->make_protocol_request($test_type, $endpoint, $model, $req, $auth_v, $real_auth, $templates); | ||
| 380 | say "Request [[" . $m->method . " " . $m->uri . "]]\n" if ($self->debug); | ||
| 381 | # next unless ($m->method eq 'GET'); | ||
| 382 | say "REQUEST:\n" . $m->as_string if ($self->debug); | ||
| 383 | my $resp = $ua->request($m); | ||
| 384 | say "RESPONSE:\n" . $resp->as_string if ($self->debug); | ||
| 385 | diag(sprintf('%s %s', $m->method, $m->uri)); | ||
| 386 | $self->validate_protocol_response($test_type, $endpoint, $model, $test, $req, $resp, $auth_v, $real_auth, $templates); | ||
| 387 | }; | ||
| 388 | last; | ||
| 389 | } | ||
| 390 | } | ||
| 391 | |||
| 392 | sub run_protocol_test { | ||
| 393 | my $self = shift; | ||
| 394 | my $test_type = shift; | ||
| 395 | my $ua = shift; | ||
| 396 | my $model = shift; | ||
| 397 | my $test = shift; | ||
| 398 | my $prot_endpoint = shift; | ||
| 399 | my $testname = $test->value; | ||
| 400 | my ($action) = $model->objects($test, $mf_action)->elements; | ||
| 401 | unless ($action) { | ||
| 402 | return fail('no action data for test ' . $testname); | ||
| 403 | } | ||
| 404 | my ($reqs) = $model->objects($action, $requests)->elements; | ||
| 405 | my @reqs = $model->get_list($self->default_graph, $reqs)->elements; | ||
| 406 | my ($auth) = $model->objects($action, $authority)->elements; | ||
| 407 | my $auth_v = $auth->value; | ||
| 408 | my $uri = new URI($prot_endpoint); | ||
| 409 | my $real_auth = $uri->authority; | ||
| 410 | |||
| 411 | $self->setup_protocol_dataset($ua, $prot_endpoint, $model, $test); | ||
| 412 | |||
| 413 | say scalar(@reqs) . ' request(s) in test' if ($self->debug); | ||
| 414 | my $req_number = 0; | ||
| 415 | foreach my $req (@reqs) { | ||
| 416 | $req_number++; | ||
| 417 | my $m = $self->make_protocol_request($test_type, $prot_endpoint, $model, $req, $auth_v, $real_auth); | ||
| 418 | # next unless ($m->method eq 'GET'); | ||
| 419 | warn "REQUEST:\n" . $m->as_string . "\n" if ($self->debug); | ||
| 420 | my $resp = $ua->request($m); | ||
| 421 | warn "RESPONSE:\n" . $resp->as_string if ($self->debug); | ||
| 422 | diag(sprintf('%s %s', $m->method, $m->uri)); | ||
| 423 | $self->validate_protocol_response($test_type, $prot_endpoint, $model, $test, $req, $resp, $auth_v, $real_auth); | ||
| 424 | if ($self->debug) { | ||
| 425 | warn "--------------------------------- after request:\n"; | ||
| 426 | $self->dump_protocol_dataset($prot_endpoint, $ua); | ||
| 427 | warn "---------------------------------\n"; | ||
| 428 | } | ||
| 429 | } | ||
| 430 | } | ||
| 431 | |||
| 432 | sub dump_protocol_dataset { | ||
| 433 | my $self = shift; | ||
| 434 | my $endpoint = shift; | ||
| 435 | my $ua = shift; | ||
| 436 | my $resp = $ua->post($endpoint, {query => 'SELECT * WHERE { { GRAPH ?g { ?s ?p ?o } } UNION { ?s ?p ?o } }'}, 'Accept' => 'application/sparql-results+json'); | ||
| 437 | my $body = $resp->decoded_content; | ||
| 438 | my $j = decode_json($body); | ||
| 439 | my $r = $j->{results}{bindings}; | ||
| 440 | my $tb = Text::Table->new(qw(s p o g)); | ||
| 441 | my @rows; | ||
| 442 | foreach my $r (@$r) { | ||
| 443 | push(@rows, [map { $r->{$_}{'value'} } qw(s p o g)]); | ||
| 444 | } | ||
| 445 | $tb->load(@rows); | ||
| 446 | say $tb; | ||
| 447 | } | ||
| 448 | |||
| 449 | sub make_protocol_request { | ||
| 450 | my $self = shift; | ||
| 451 | my $test_type = shift; | ||
| 452 | my $endpoint = shift; | ||
| 453 | my $model = shift; | ||
| 454 | my $req = shift; | ||
| 455 | my $auth_v = shift; | ||
| 456 | my $real_auth = shift; | ||
| 457 | my $templates = shift; | ||
| 458 | my ($path) = $model->objects($req, $absolutePath)->elements; | ||
| 459 | my ($method) = $model->objects($req, $methodName)->elements; | ||
| 460 | my ($body) = $model->objects($req, $ht_body)->elements; | ||
| 461 | my ($headers) = $model->objects($req, $ht_headers)->elements; | ||
| 462 | my $path_v = $path->value; | ||
| 463 | foreach my $t (keys %$templates) { | ||
| 464 | my $v = $templates->{$t}; | ||
| 465 | my $i = index($path_v, $t); | ||
| 466 | if ($i >= 0) { | ||
| 467 | substr($path_v, $i, length($t), $v); | ||
| 468 | } | ||
| 469 | } | ||
| 470 | |||
| 471 | my $pathuri = new URI($path_v); | ||
| 472 | |||
| 473 | my $m = HTTP::Request->new(); | ||
| 474 | $m->method($method->value); | ||
| 475 | my $uri = new URI($endpoint); | ||
| 476 | |||
| 477 | if ($test_type eq 'gsp') { | ||
| 478 | my $test_path = $pathuri->path; | ||
| 479 | $test_path =~ s#/gsp#$uri->path#e; | ||
| 480 | $uri->path($test_path); | ||
| 481 | } | ||
| 482 | $uri->query($pathuri->query); | ||
| 483 | |||
| 484 | my $uri_v = $uri->as_string; | ||
| 485 | |||
| 486 | # fixup | ||
| 487 | $uri_v =~ s/$auth_v/$real_auth/e; | ||
| 488 | |||
| 489 | |||
| 490 | $m->uri(URI->new($uri_v)); | ||
| 491 | if ($body) { | ||
| 492 | my ($content) = $model->objects($body, $cnt_chars)->elements; | ||
| 493 | my ($encoding) = $model->objects($body, $cnt_encoding)->elements; | ||
| 494 | |||
| 495 | # fixup | ||
| 496 | $content =~ s/$auth_v/$real_auth/ge; | ||
| 497 | ######### TODO CONTINUE HERE ############## | ||
| 498 | |||
| 499 | my $bytes = encode($encoding->value, $content->value); | ||
| 500 | $m->content($bytes); | ||
| 501 | } | ||
| 502 | |||
| 503 | my @headers = $model->get_list($self->default_graph, $headers)->elements; | ||
| 504 | foreach my $h (@headers) { | ||
| 505 | my ($key) = $model->objects($h, $ht_fieldName)->elements; | ||
| 506 | my ($value) = $model->objects($h, $ht_fieldValue)->elements; | ||
| 507 | $m->header($key->value => $value->value); | ||
| 508 | } | ||
| 509 | return $m; | ||
| 510 | } | ||
| 511 | |||
| 512 | sub validate_protocol_response { | ||
| 513 | my $self = shift; | ||
| 514 | my $test_type = shift; | ||
| 515 | my $endpoint = shift; | ||
| 516 | my $model = shift; | ||
| 517 | my $test = shift; | ||
| 518 | my $req = shift; | ||
| 519 | my $m = shift; | ||
| 520 | my $auth_v = shift; | ||
| 521 | my $real_auth = shift; | ||
| 522 | my $templates = shift; | ||
| 523 | |||
| 524 | warn "Validating response..." if ($self->debug); | ||
| 525 | |||
| 526 | my ($resp) = $model->objects($req, $ht_resp)->elements; | ||
| 527 | unless ($resp) { | ||
| 528 | return fail('No expected response data for test'); | ||
| 529 | } | ||
| 530 | |||
| 531 | my (@expected_location) = $model->objects($resp, $expectedLocation)->elements; | ||
| 532 | foreach my $loc (@expected_location) { | ||
| 533 | my $header = $m->header('Location'); | ||
| 534 | ok($header, 'Location header value'); | ||
| 535 | my $var = $loc->value; | ||
| 536 | $templates->{ $var } = $header; | ||
| 537 | } | ||
| 538 | |||
| 539 | my (@expected_status) = $model->objects($resp, $expectedStatus)->elements; | ||
| 540 | if (scalar(@expected_status)) { | ||
| 541 | my %ok = map { $_ => 1 } map { $_->value } @expected_status; | ||
| 542 | my $code = $m->code; | ||
| 543 | my $digit = int($code / 100); | ||
| 544 | my $message = status_message($code); | ||
| 545 | $message =~ s/ //g; | ||
| 546 | my $actual_iri = "http://www.w3.org/2011/http-statusCodes#$message"; | ||
| 547 | if (exists $ok{$actual_iri}) { | ||
| 548 | pass("status code matches ($code)") | ||
| 549 | } else { | ||
| 550 | fail("got status code: " . $m->status_line . ' but expecting one of: ' . join(',', keys %ok)) | ||
| 551 | } | ||
| 552 | } | ||
| 553 | |||
| 554 | my (@expected_status_class) = $model->objects($resp, $expectedStatusClass)->elements; | ||
| 555 | if (scalar(@expected_status_class)) { | ||
| 556 | my %ok = map { $_ => 1 } map { $_->value } @expected_status_class; | ||
| 557 | my $code = $m->code; | ||
| 558 | my $digit = int($code / 100); | ||
| 559 | my $actual_class_iri = "http://www.w3.org/2011/http-statusCodes#StatusCode${digit}xx"; | ||
| 560 | if (exists $ok{$actual_class_iri}) { | ||
| 561 | pass("status code matches class ${digit}xx") | ||
| 562 | } else { | ||
| 563 | fail("got status code: " . $m->status_line . ' but expecting one of: ' . join(',', keys %ok)) | ||
| 564 | } | ||
| 565 | } | ||
| 566 | |||
| 567 | my ($expected_format) = $model->objects($resp, $expectedFormat)->elements; | ||
| 568 | if ($expected_format) { | ||
| 569 | my $ct = $m->header('Content-Type'); | ||
| 570 | my $expected = $expected_format->value; | ||
| 571 | if ($expected eq 'tabular') { | ||
| 572 | like($ct, qr#^application/sparql-results[+](json|xml)|text/csv|text/tab-separated-values#, "expected tabular result; got $ct"); | ||
| 573 | } elsif ($expected eq 'boolean') { | ||
| 574 | like($ct, qr#^application/sparql-results[+](json|xml)|text/plain#, "expected boolean result; got $ct"); | ||
| 575 | } elsif ($expected eq 'RDF') { | ||
| 576 | like($ct, qr#^(application/(rdf-xml|n-triples))|text/((n-(triples|quads))|turtle)|application/ld[+]json#, "expected RDF result; got $ct") | ||
| 577 | } else { | ||
| 578 | die("Unexpected format value '$expected'"); | ||
| 579 | } | ||
| 580 | } | ||
| 581 | |||
| 582 | my ($schema) = $model->objects($resp, $expectedSchemaConformance)->elements; | ||
| 583 | if ($schema) { | ||
| 584 | pass('Schema conformance (no-op)'); | ||
| 585 | } | ||
| 586 | |||
| 587 | my ($expected_ask) = $model->objects($resp, $expectedSPARQLMatch)->elements; | ||
| 588 | if ($expected_ask) { | ||
| 589 | ### Need to replace the test endpoint with the real endpoint in the query string | ||
| 590 | my $sparql = $expected_ask->value; | ||
| 591 | $sparql =~ s/$auth_v/$real_auth/ge; | ||
| 592 | |||
| 593 | my $ct = $m->header('Content-Type'); | ||
| 594 | my $pclass = Attean->get_parser(media_type => $ct); | ||
| 595 | die 'no suitable parser found for SD RDF' unless ($pclass); | ||
| 596 | my $parser = $pclass->new(); | ||
| 597 | my $store = Attean->get_store('Memory')->new(); | ||
| 598 | my $body = $m->decoded_content; | ||
| 599 | my $iter = $parser->parse_iter_from_bytes(encode_utf8($body)); | ||
| 600 | my $graph = iri('http://graph-name/'); | ||
| 601 | my $quads = $iter->as_quads($graph); | ||
| 602 | $store->add_iter($quads); | ||
| 603 | my $model = Attean::QuadModel->new( store => $store ); | ||
| 604 | my $s = Attean->get_parser('SPARQL')->new(); | ||
| 605 | my ($algebra) = $s->parse($sparql); | ||
| 606 | my $results = $model->evaluate($algebra, $graph); | ||
| 607 | my $result = $results->next; | ||
| 608 | fail('expectedSPARQLMatch') unless ($result); | ||
| 609 | my $ok = $result->boolean_value; | ||
| 610 | unless ($ok) { | ||
| 611 | diag("RDF content did not match query `${sparql}`: [[${body}]]") | ||
| 612 | } | ||
| 613 | ok($ok, 'expectedSPARQLMatch'); | ||
| 614 | } | ||
| 615 | |||
| 616 | my ($expected_bool) = $model->objects($resp, $expectedBoolean)->elements; | ||
| 617 | if ($expected_bool) { | ||
| 618 | my $ct = $m->header('Content-Type'); | ||
| 619 | my $body = $m->decoded_content; | ||
| 620 | my $expected = $expected_bool->value; | ||
| 621 | if ($ct eq 'application/sparql-results+json') { | ||
| 622 | like($body, qr#"boolean"\s*:\s*${expected}#sm, "expected boolean $expected"); | ||
| 623 | } elsif ($ct eq 'application/sparql-results+xml') { | ||
| 624 | like($body, qr#<boolean>${expected}</boolean>#sm, "expected boolean $expected"); | ||
| 625 | } elsif ($ct =~ 'text/plain') { | ||
| 626 | like($body, qr#${expected}#sm, "expected boolean $expected"); | ||
| 627 | } else { | ||
| 628 | die("validate expected boolean for [$ct]: " . $expected_bool->value); | ||
| 629 | } | ||
| 630 | } | ||
| 631 | |||
| 632 | my ($expected_headers) = $model->objects($resp, $ht_headers)->elements; | ||
| 633 | if ($expected_headers) { | ||
| 634 | my @headers = $model->get_list($self->default_graph, $expected_headers)->elements; | ||
| 635 | foreach my $h (@headers) { | ||
| 636 | my ($key) = $model->objects($h, $ht_fieldName)->elements; | ||
| 637 | my ($expected_value_l) = $model->objects($h, $ht_fieldValue)->elements; | ||
| 638 | my $expected_value = $expected_value_l->value; | ||
| 639 | my $actual_value = $m->header($key->value); | ||
| 640 | if ($expected_value =~ m<$actual_value; charset=utf-8>i) { | ||
| 641 | pass('content-type with ignored charset=utf-8'); | ||
| 642 | } else { | ||
| 643 | $actual_value =~ s/ //g; | ||
| 644 | $expected_value =~ s/ //g; | ||
| 645 | is($actual_value, $expected_value, "expected header: " . $key->value); | ||
| 646 | } | ||
| 647 | } | ||
| 648 | } | ||
| 649 | |||
| 650 | my ($expected_body) = $model->objects($resp, $ht_body)->elements; | ||
| 651 | if ($expected_body) { | ||
| 652 | my $ct = $m->header('Content-Type'); | ||
| 653 | if (not defined($ct)) { | ||
| 654 | fail('No content-type in response'); | ||
| 655 | return; | ||
| 656 | } | ||
| 657 | $ct =~ s/;.*//; | ||
| 658 | |||
| 659 | warn "Body content-type: $ct\n"; | ||
| 660 | my $pclass = Attean->get_parser( media_type => $ct ); | ||
| 661 | unless ($pclass) { | ||
| 662 | fail("Unrecognized media type for response: $ct"); | ||
| 663 | return; | ||
| 664 | } | ||
| 665 | my $parser = $pclass->new(); | ||
| 666 | my ($chars) = $model->objects($expected_body, $cnt_chars)->elements; | ||
| 667 | my $actual = $m->decoded_content; | ||
| 668 | my $expected = $chars->value; | ||
| 669 | |||
| 670 | # fixup | ||
| 671 | $actual =~ s/$auth_v/$real_auth/ge; | ||
| 672 | $expected =~ s/$auth_v/$real_auth/ge; | ||
| 673 | |||
| 674 | warn "Actual response body: " . Dumper($actual) if ($self->debug); | ||
| 675 | warn "Expected response body: " . Dumper($expected) if ($self->debug); | ||
| 676 | |||
| 677 | my $actual_iter = $parser->parse_iter_from_bytes(encode_utf8($actual)); | ||
| 678 | my $expected_iter = $parser->parse_iter_from_bytes(encode_utf8($expected)); | ||
| 679 | my $eqtest = Attean::BindingEqualityTest->new(); | ||
| 680 | my $ok = eval { ok( $eqtest->equals( $actual_iter, $expected_iter ), 'expected triples' ) or diag($eqtest->error) }; | ||
| 681 | # unless ($ok) { | ||
| 682 | # warn Dumper({expected => $expected, actual => $actual}); | ||
| 683 | # } | ||
| 684 | if ($@) { | ||
| 685 | diag($@); | ||
| 686 | } | ||
| 687 | unless ($ok) { | ||
| 688 | $self->record_result('protocol', $ok, $test->as_string); | ||
| 689 | fail($test->value); | ||
| 690 | return; | ||
| 691 | } | ||
| 692 | } | ||
| 693 | |||
| 694 | my ($expectation) = $model->objects($resp, $mf_expectation)->elements; | ||
| 695 | if ($expectation) { | ||
| 696 | $self->record_result('protocol', 0, $test->as_string); | ||
| 697 | fail($test->value . " - validate expectation: " . $expectation->value); | ||
| 698 | return; | ||
| 699 | } | ||
| 700 | |||
| 701 | # pass($test->value); | ||
| 702 | } | ||
| 703 | |||
| 704 | sub sparql_syntax_test { | ||
| 705 | my $self = shift; | ||
| 706 | my $test_type = shift; | ||
| 707 | my $model = shift; | ||
| 708 | my $test = shift; | ||
| 709 | my %args = @_; | ||
| 710 | my $specVersion = $args{'specVersion'} // '1.1'; | ||
| 711 | |||
| 712 | my $handled = 0; | ||
| 713 | |||
| 714 | my $type = iri( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ); | ||
| 715 | my $mfname = iri( "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#name" ); | ||
| 716 | my ($queryd) = $model->objects( $test, iri("${MF}action") )->elements; | ||
| 717 | my ($approved) = $model->objects( $test, iri("${DAWGT}approval") )->elements; | ||
| 718 | my ($name) = $model->objects( $test, $mfname )->elements; | ||
| 719 | my $testname = $test->value; | ||
| 720 | $testname =~ s{^.*[/#]}{}; | ||
| 721 | |||
| 722 | if ($self->strict_approval) { | ||
| 723 | unless ($approved) { | ||
| 724 | warn "- skipping test because it isn't approved\n" if ($self->debug); | ||
| 725 | return; | ||
| 726 | } | ||
| 727 | if ($approved->equal("${DAWGT}NotClassified")) { | ||
| 728 | warn "- skipping test because its approval is dawgt:NotClassified\n" if ($self->debug); | ||
| 729 | return; | ||
| 730 | } | ||
| 731 | } | ||
| 732 | |||
| 733 | my $is_pos_query = $model->count_quads($test, $type, iri("${MF}PositiveSyntaxTest")) + $model->count_quads($test, $type, iri("${MF}PositiveSyntaxTest11")); | ||
| 734 | my $is_pos_update = $model->count_quads($test, $type, iri("${MF}PositiveUpdateSyntaxTest")) + $model->count_quads($test, $type, iri("${MF}PositiveUpdateSyntaxTest11")); | ||
| 735 | my $is_neg_query = $model->count_quads($test, $type, iri("${MF}NegativeSyntaxTest")) + $model->count_quads($test, $type, iri("${MF}NegativeSyntaxTest11")); | ||
| 736 | my $is_neg_update = $model->count_quads($test, $type, iri("${MF}NegativeUpdateSyntaxTest")) + $model->count_quads($test, $type, iri("${MF}NegativeUpdateSyntaxTest11")); | ||
| 737 | |||
| 738 | my $uri = URI->new( $queryd->value ); | ||
| 739 | my $filename = $uri->file; | ||
| 740 | my (undef,$base,undef) = File::Spec->splitpath( $filename ); | ||
| 741 | $base = "file://${base}"; | ||
| 742 | # warn "Loading SPARQL query from file $filename" if ($self->debug); | ||
| 743 | my $sparql = eval { read_file($filename, { binmode => ':utf8' }) }; | ||
| 744 | if ($@) { | ||
| 745 | warn("$!: $filename; " . $test->as_string); | ||
| 746 | $self->record_result('syntax', 0, $test->as_string, $TODO); | ||
| 747 | return; | ||
| 748 | } | ||
| 749 | my $bytes = encode_utf8($sparql); | ||
| 750 | |||
| 751 | if ($self->debug) { | ||
| 752 | my $q = $sparql; | ||
| 753 | $q =~ s/\s+/ /g; | ||
| 754 | warn "### test : " . $test->as_string . "\n"; | ||
| 755 | warn "# sparql : $q\n"; | ||
| 756 | warn "# file : $filename\n"; | ||
| 757 | } | ||
| 758 | |||
| 759 | my $pclass = Attean->get_parser('SPARQL'); | ||
| 760 | my $parser = $pclass->new(version => $specVersion); | ||
| 761 | if ($test_type eq 'update') { | ||
| 762 | $parser->update(1); | ||
| 763 | } | ||
| 764 | if ($is_pos_query or $is_pos_update) { | ||
| 765 | my ($query) = eval { $parser->parse_list_from_bytes($bytes) }; | ||
| 766 | my $ok = blessed($query); | ||
| 767 | $self->record_result('syntax', $ok, $test->as_string, $TODO); | ||
| 768 | $handled++; | ||
| 769 | if ($ok) { | ||
| 770 | pass("$testname -Â $filename"); | ||
| 771 | } else { | ||
| 772 | fail("$testname -Â $filename: $@"); | ||
| 773 | } | ||
| 774 | } elsif ($is_neg_query or $is_neg_update) { | ||
| 775 | my ($query) = eval { $parser->parse_list_from_bytes($bytes) }; | ||
| 776 | my $ok = $@ ? 1 : 0; | ||
| 777 | $self->record_result('syntax', $ok, $test->as_string, $TODO); | ||
| 778 | $handled++; | ||
| 779 | if ($ok) { | ||
| 780 | pass("$testname -Â $filename"); | ||
| 781 | } else { | ||
| 782 | if ($self->debug) { | ||
| 783 | warn $query->as_string; | ||
| 784 | } | ||
| 785 | fail("$testname -Â $filename (unexpected successful parse)"); | ||
| 786 | } | ||
| 787 | } | ||
| 788 | |||
| 789 | unless ($handled) { | ||
| 790 | fail("Unhandled test: $testname"); | ||
| 791 | } | ||
| 792 | |||
| 793 | } | ||
| 794 | |||
| 795 | # Look up the test in the model, and determine: | ||
| 796 | # | ||
| 797 | # * the media type of the syntax being tested | ||
| 798 | # * whether the test is an eval test (compared against a base nt/nq file) | ||
| 799 | # * whether the test is a syntax test | ||
| 800 | # * whether the test is a canonicalization test | ||
| 801 | # * whether the test is a negative test (implies a syntax test) | ||
| 802 | # * whether the syntax being tested is a triples-based format (being false implies that the format is a quad-based format) | ||
| 803 | # | ||
| 804 | # Not all of these pieces of information are derivable from the RDF data, | ||
| 805 | # so some of it is heuristics based on the naming of the test type. | ||
| 806 | # | ||
| 807 | # If the test is not a recognized type, this method dies. Calling code is responsible | ||
| 808 | # for catching that error and properly recording a test failure. | ||
| 809 | sub syntax_test_type { | ||
| 810 | my $self = shift; | ||
| 811 | my $model = shift; | ||
| 812 | my $test = shift; | ||
| 813 | my @types = $model->objects( $test, iri("${RDF}type") )->elements; | ||
| 814 | my @formats = $model->objects( \@types, iri("${RDFT}syntaxFormat") )->elements; | ||
| 815 | my @mediatypes = $model->objects( \@formats, iri("${FORMATS}media_type") )->elements; | ||
| 816 | unless (scalar(@mediatypes) == 1) { | ||
| 817 | my $types = join(',', map { $_->value } @types); | ||
| 818 | my $formats = join(',', map { $_->value } @formats); | ||
| 819 | die "No syntax test type for " . $test->value . " ; types=$types ; formats=$formats"; | ||
| 820 | } | ||
| 821 | my $mediatype = $mediatypes[0]->value; | ||
| 822 | |||
| 823 | my $is_eval = $model->count_quads( \@types, iri("${RDFS}subClassOf"), iri("${RDFT}TestEval") ); | ||
| 824 | my $is_syntax = $model->count_quads( \@types, iri("${RDFS}subClassOf"), iri("${RDFT}TestSyntax") ); | ||
| 825 | my $is_c14n = $model->count_quads( \@types, iri("${RDFS}subClassOf"), iri("${RDFT}TestC14N") ); | ||
| 826 | my $is_neg = $model->count_quads( \@types, iri("${RDFS}subClassOf"), iri("${RDFT}TestNegativeSyntax") ); | ||
| 827 | $is_syntax++ if ($is_neg); # to avoid having to do multiple subClassOf hops on these tests to find rdft:TextSyntax | ||
| 828 | |||
| 829 | my $triples = 0; | ||
| 830 | $triples++ if ($mediatype eq 'text/turtle'); | ||
| 831 | $triples++ if ($mediatype eq 'application/n-triples'); | ||
| 832 | $triples++ if ($mediatype eq 'application/rdf+xml'); | ||
| 833 | |||
| 834 | return ($mediatype, $is_eval, $is_syntax, $is_c14n, $is_neg, $triples); | ||
| 835 | } | ||
| 836 | |||
| 837 | sub data_syntax_eval_test { | ||
| 838 | my $self = shift; | ||
| 839 | my $model = shift; | ||
| 840 | my $test = shift; | ||
| 841 | my $count = shift // 1; | ||
| 842 | |||
| 843 | my $type = iri( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ); | ||
| 844 | my $mfname = iri( "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#name" ); | ||
| 845 | my ($queryd) = $model->objects( $test, iri("${MF}action") )->elements; | ||
| 846 | my ($resultd) = $model->objects( $test, iri("${MF}result") )->elements; | ||
| 847 | my ($approved) = $model->objects( $test, iri("${DAWGT}approval") )->elements; | ||
| 848 | my ($name) = $model->objects( $test, $mfname )->elements; | ||
| 849 | my $testname = $test->value; | ||
| 850 | $testname =~ s{^.*[/#]([^/#]+[/#])}{$1}; | ||
| 851 | |||
| 852 | if ($self->strict_approval) { | ||
| 853 | unless ($approved) { | ||
| 854 | warn "- skipping test because it isn't approved\n" if ($self->debug); | ||
| 855 | return; | ||
| 856 | } | ||
| 857 | if ($approved->equal("${DAWGT}NotClassified")) { | ||
| 858 | warn "- skipping test because its approval is dawgt:NotClassified\n" if ($self->debug); | ||
| 859 | return; | ||
| 860 | } | ||
| 861 | } | ||
| 862 | |||
| 863 | my ($mediatype, $is_eval, $is_syntax, $is_c14n, $is_neg, $is_triples) = eval { $self->syntax_test_type($model, $test) }; | ||
| 864 | if ($@) { | ||
| 865 | $self->record_result('syntax', 0, $test->as_string, $TODO); | ||
| 866 | fail("$testname -Â Unexpected format for test: $@"); | ||
| 867 | return; | ||
| 868 | } | ||
| 869 | my $is_quads = not($is_triples); | ||
| 870 | |||
| 871 | my $base; | ||
| 872 | my ($result_filename, $expected_bytes, $nt_content); | ||
| 873 | if ($is_eval) { | ||
| 874 | my $result_uri = URI->new( $resultd->value ); | ||
| 875 | $result_filename = $result_uri->file; | ||
| 876 | (undef,$base,undef) = File::Spec->splitpath( $result_filename ); | ||
| 877 | $base = "file://${base}"; | ||
| 878 | warn "Loading expected RDF data from file $result_filename\n" if ($self->debug); | ||
| 879 | $nt_content = eval { read_file($result_filename, { binmode => ':utf8' }) }; | ||
| 880 | if ($@) { | ||
| 881 | warn("$!: $result_filename; " . $test->as_string); | ||
| 882 | $self->record_result('evaluation', 0, $test->as_string, $TODO); | ||
| 883 | return; | ||
| 884 | } | ||
| 885 | unless (defined($nt_content)) { | ||
| 886 | warn "*** Undefined expected data from $result_filename...\n"; | ||
| 887 | } | ||
| 888 | $expected_bytes = encode_utf8($nt_content); | ||
| 889 | } | ||
| 890 | |||
| 891 | my $uri = URI->new( $queryd->value ); | ||
| 892 | my $filename = $uri->file; | ||
| 893 | (undef,$base,undef) = File::Spec->splitpath( $filename ); | ||
| 894 | $base = "file://${base}"; | ||
| 895 | warn "Loading test RDF data from file $filename\n" if ($self->debug); | ||
| 896 | my $content = eval { read_file($filename, { binmode => ':utf8' }) }; | ||
| 897 | if ($@) { | ||
| 898 | warn("$!: $filename; " . $test->as_string); | ||
| 899 | $self->record_result('syntax', 0, $test->as_string, $TODO); | ||
| 900 | return; | ||
| 901 | } | ||
| 902 | my $bytes = encode_utf8($content); | ||
| 903 | |||
| 904 | if ($self->debug) { | ||
| 905 | my $q = $content; | ||
| 906 | $q =~ s/\s+/ /g; | ||
| 907 | |||
| 908 | warn "### test : " . $test->as_string . "\n"; | ||
| 909 | warn "# file : $filename\n"; | ||
| 910 | warn "# content : $q\n"; | ||
| 911 | if ($is_eval) { | ||
| 912 | my $expected = $nt_content; | ||
| 913 | $expected =~ s/\s+/ /g; | ||
| 914 | warn "# expected : $result_filename\n"; | ||
| 915 | warn "# expected : $expected\n"; | ||
| 916 | } | ||
| 917 | } | ||
| 918 | |||
| 919 | my $test_uri = $uri->as_string; | ||
| 920 | my $test_base = $uri->as_string; | ||
| 921 | $test_base = $self->fix_local_to_w3c($test_base); | ||
| 922 | warn "Test base: " . $test_base . "\n" if ($self->debug); | ||
| 923 | |||
| 924 | my $pclass = Attean->get_parser(media_type => $mediatype); | ||
| 925 | unless ($pclass) { | ||
| 926 | die "No parser found for type $mediatype"; | ||
| 927 | } | ||
| 928 | my $test_parser = $pclass->new(base => $test_base); | ||
| 929 | my $nt_parser = Attean->get_parser('NTriples')->new(); | ||
| 930 | my $nq_parser = Attean->get_parser('NQuads')->new(); | ||
| 931 | SKIP: { | ||
| 932 | if ($is_c14n) { | ||
| 933 | skip("$testname - C14N tests not supported"); | ||
| 934 | } elsif ($is_neg) { | ||
| 935 | my (@triples) = eval { $test_parser->parse_list_from_bytes($bytes) }; | ||
| 936 | my $ok = $@ ? 1 : 0; | ||
| 937 | $self->record_result('syntax', $ok, $test->as_string, $TODO); | ||
| 938 | if ($ok) { | ||
| 939 | pass("$testname -Â $filename"); | ||
| 940 | } else { | ||
| 941 | if ($self->debug) { | ||
| 942 | warn "Unexpected successful parse of:\n" . $content; | ||
| 943 | } | ||
| 944 | fail("$testname - $filename (unexpected successful parse)"); | ||
| 945 | } | ||
| 946 | } else { | ||
| 947 | my (@actual_statements) = eval { $test_parser->parse_iter_from_bytes($bytes)->uniq()->elements() }; | ||
| 948 | my $parse_error = $@; | ||
| 949 | my $ser = Attean->get_serializer($is_triples ? 'NTriples' : 'NQuads')->new(); | ||
| 950 | my $nq_parser = Attean->get_parser('NQuads')->new(); | ||
| 951 | my $expected_parser = $is_triples ? $nt_parser : $nq_parser; | ||
| 952 | my $role = $is_triples ? 'Attean::API::Triple' : 'Attean::API::TripleOrQuad'; | ||
| 953 | my $eq_test = Attean::BindingEqualityTest->new(); | ||
| 954 | if ($is_eval) { | ||
| 955 | my @expected_statements = eval { $expected_parser->parse_iter_from_bytes($expected_bytes)->uniq()->elements() }; | ||
| 956 | $parse_error ||= $@; | ||
| 957 | if ($parse_error) { | ||
| 958 | $self->record_result('evaluation', 0, $test->as_string, $TODO); | ||
| 959 | fail("$testname -Â Failed to parse expected data from $result_filename: $parse_error"); | ||
| 960 | return; | ||
| 961 | } | ||
| 962 | my $expected_iter = Attean::ListIterator->new( values => \@expected_statements, item_type => $role ); | ||
| 963 | |||
| 964 | my $actual_iter = Attean::ListIterator->new( values => \@actual_statements, item_type => $role ); | ||
| 965 | my $ok = $eq_test->equals($actual_iter, $expected_iter); | ||
| 966 | $self->record_result('evaluation', $ok, $test->as_string, $TODO); | ||
| 967 | ok($ok, "$testname -Â $filename"); | ||
| 968 | if (not $ok) { | ||
| 969 | warn "BindingEqualityTest failures: " . $eq_test->error(); | ||
| 970 | warn "Expecting " . scalar(@expected_statements) . " statements:\n"; | ||
| 971 | if (scalar(@expected_statements)) { | ||
| 972 | warn $ser->serialize_list_to_bytes(@expected_statements); | ||
| 973 | } | ||
| 974 | warn "But found " . scalar(@actual_statements) . " statements:\n"; | ||
| 975 | my $bytes = $ser->serialize_list_to_bytes(@actual_statements); | ||
| 976 | warn((length($bytes)) ? decode_utf8($bytes) : '(empty content)'); | ||
| 977 | } | ||
| 978 | } else { | ||
| 979 | my $ok = scalar(not $parse_error); | ||
| 980 | $self->record_result('syntax', $ok, $test->as_string, $TODO); | ||
| 981 | if ($ok) { | ||
| 982 | pass("$testname -Â $filename"); | ||
| 983 | } else { | ||
| 984 | fail("$testname -Â $filename: $parse_error"); | ||
| 985 | } | ||
| 986 | } | ||
| 987 | } | ||
| 988 | } | ||
| 989 | } | ||
| 990 | |||
| 991 | sub update_eval_test { | ||
| 992 | my $self = shift; | ||
| 993 | my $model = shift; | ||
| 994 | my $test = shift; | ||
| 995 | my %args = @_; | ||
| 996 | my $specVersion = $args{'specVersion'} // '1.1'; | ||
| 997 | |||
| 998 | my ($action) = $model->objects( $test, iri("${MF}action") )->elements; | ||
| 999 | my ($result) = $model->objects( $test, iri("${MF}result") )->elements; | ||
| 1000 | my @reqs = $model->objects( $test, iri("${MF}requires") )->elements; | ||
| 1001 | my ($approved) = $model->objects( $test, iri("${DAWGT}approval") )->elements; | ||
| 1002 | my ($queryd) = $model->objects( $action, iri("${UT}request") )->elements; | ||
| 1003 | my @data = $model->objects( $action, iri("${UT}data") )->elements; | ||
| 1004 | my @gdata = $model->objects( $action, iri("${UT}graphData") )->elements; | ||
| 1005 | |||
| 1006 | if ($self->strict_approval) { | ||
| 1007 | unless ($approved) { | ||
| 1008 | warn "- skipping test because it isn't approved\n" if ($self->debug); | ||
| 1009 | return; | ||
| 1010 | } | ||
| 1011 | if ($approved->equal(iri("${DAWGT}NotClassified"))) { | ||
| 1012 | warn "- skipping test because its approval is dawgt:NotClassified\n" if ($self->debug); | ||
| 1013 | return; | ||
| 1014 | } | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | my $uri = URI->new( $queryd->value ); | ||
| 1018 | my $filename = $uri->file; | ||
| 1019 | my (undef,$base,undef) = File::Spec->splitpath( $filename ); | ||
| 1020 | $base = "file://${base}"; | ||
| 1021 | warn "Loading SPARQL query from file $filename" if ($self->debug); | ||
| 1022 | my $sparql = eval { read_file($filename, { binmode => ':utf8' }) }; | ||
| 1023 | if ($@) { | ||
| 1024 | warn("$!: $filename; " . $test->as_string); | ||
| 1025 | $self->record_result('update', 0, $test->as_string, $TODO); | ||
| 1026 | return; | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | my $q = $sparql; | ||
| 1030 | $q =~ s/\s+/ /g; | ||
| 1031 | if ($self->debug) { | ||
| 1032 | warn "### test : " . $test->value . "\n"; | ||
| 1033 | warn "# sparql : $q\n"; | ||
| 1034 | warn "# file : $filename\n"; | ||
| 1035 | foreach my $data (@data) { | ||
| 1036 | warn "# data : " . $data->value . "\n" if (blessed($data)); | ||
| 1037 | } | ||
| 1038 | warn "# graph data : " . $_->value . "\n" for (@gdata); | ||
| 1039 | warn "# result : " . $result->value . "\n"; | ||
| 1040 | foreach my $req (@reqs) { | ||
| 1041 | warn "# requires : " . $req->value . "\n" if (blessed($req)); | ||
| 1042 | } | ||
| 1043 | } | ||
| 1044 | |||
| 1045 | my $todo_because_of_test_req = 0; | ||
| 1046 | my @unsupported_reqs; | ||
| 1047 | foreach my $req (@reqs) { | ||
| 1048 | unless (exists ${ $self->supported_extensions }{ $req->value }) { | ||
| 1049 | $todo_because_of_test_req++; | ||
| 1050 | push(@unsupported_reqs, $req->value); | ||
| 1051 | } | ||
| 1052 | } | ||
| 1053 | |||
| 1054 | |||
| 1055 | |||
| 1056 | # TODO: set up remote endpoint mock | ||
| 1057 | TODO: { | ||
| 1058 | local($TODO) = $todo_because_of_test_req ? "requires " . join(', ', @unsupported_reqs) : ''; | ||
| 1059 | |||
| 1060 | warn "constructing model...\n" if ($self->debug); | ||
| 1061 | my $test_model = $self->test_model(); | ||
| 1062 | foreach my $data (@data) { | ||
| 1063 | eval { | ||
| 1064 | if (blessed($data)) { | ||
| 1065 | $test_model->load_urls_into_graph($self->default_graph, $data); | ||
| 1066 | } | ||
| 1067 | }; | ||
| 1068 | |||
| 1069 | if ($self->debug) { | ||
| 1070 | warn "TEST MODEL AFTER DEFAULT GRAPH LOAD:\n"; | ||
| 1071 | $self->debug_model($test_model); | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | if ($@) { | ||
| 1075 | fail($test->value); | ||
| 1076 | $self->record_result('update', 0, $test->value, $TODO); | ||
| 1077 | print "# died: " . $test->value . ": $@\n"; | ||
| 1078 | return; | ||
| 1079 | } | ||
| 1080 | } | ||
| 1081 | foreach my $gdata (@gdata) { | ||
| 1082 | my ($data) = ($model->objects( $gdata, iri("${UT}data") )->elements)[0] || ($model->objects( $gdata, iri("${UT}graph") )->elements)[0]; | ||
| 1083 | my ($graph) = $model->objects( $gdata, iri("${RDFS}label") )->elements; | ||
| 1084 | my $uri = $graph->value; | ||
| 1085 | eval { | ||
| 1086 | $test_model->load_urls_into_graph(iri($uri), $data); | ||
| 1087 | }; | ||
| 1088 | if ($@) { | ||
| 1089 | fail($test->as_string); | ||
| 1090 | $self->record_result('update', 0, $test->value, $TODO); | ||
| 1091 | print "# died: " . $test->value . ": $@\n"; | ||
| 1092 | return; | ||
| 1093 | }; | ||
| 1094 | } | ||
| 1095 | |||
| 1096 | my ($result_status) = $model->objects( $result, iri("${UT}result") )->elements; | ||
| 1097 | my @resgdata = $model->objects( $result, iri("${UT}graphData") )->elements; | ||
| 1098 | my ($resdata) = $model->objects( $result, iri("${UT}data") )->elements; | ||
| 1099 | my $expected_model = memory_model; | ||
| 1100 | eval { | ||
| 1101 | if (blessed($resdata)) { | ||
| 1102 | $expected_model->load_urls_into_graph($self->default_graph, $resdata); | ||
| 1103 | } | ||
| 1104 | }; | ||
| 1105 | if ($@) { | ||
| 1106 | fail($test->as_string); | ||
| 1107 | $self->record_result('update', 0, $test->value, $TODO); | ||
| 1108 | print "# died: " . $test->value . ": $@\n"; | ||
| 1109 | return; | ||
| 1110 | }; | ||
| 1111 | foreach my $gdata (@resgdata) { | ||
| 1112 | my ($data) = ($model->objects( $gdata, iri("${UT}data") )->elements)[0] || ($model->objects( $gdata, iri("${UT}graph") )->elements)[0]; | ||
| 1113 | my ($graph) = $model->objects( $gdata, iri("${RDFS}label") )->elements; | ||
| 1114 | my $uri = $graph->value; | ||
| 1115 | my $return = 0; | ||
| 1116 | if ($data) { | ||
| 1117 | eval { | ||
| 1118 | $expected_model->load_urls_into_graph(iri($uri), $data); | ||
| 1119 | }; | ||
| 1120 | if ($@) { | ||
| 1121 | fail($test->as_string); | ||
| 1122 | $self->record_result('update', 0, $test->value, $TODO); | ||
| 1123 | print "# died: " . $test->value . ": $@\n"; | ||
| 1124 | $return = 1; | ||
| 1125 | }; | ||
| 1126 | return if ($return); | ||
| 1127 | } | ||
| 1128 | } | ||
| 1129 | |||
| 1130 | if ($self->debug) { | ||
| 1131 | warn "Dataset before update operation:\n"; | ||
| 1132 | warn $self->model_as_string($test_model); | ||
| 1133 | } | ||
| 1134 | |||
| 1135 | my $ok = 0; | ||
| 1136 | eval { | ||
| 1137 | my $algebra = eval { Attean->get_parser('SPARQL')->parse_update($sparql) }; | ||
| 1138 | if ($@) { | ||
| 1139 | warn "Failed to parse query $filename: $@"; | ||
| 1140 | die $@; | ||
| 1141 | } | ||
| 1142 | unless ($algebra) { | ||
| 1143 | warn "No algebra generated for update\n"; | ||
| 1144 | fail($test->value); | ||
| 1145 | return; | ||
| 1146 | } | ||
| 1147 | if ($self->debug) { | ||
| 1148 | warn "# Algebra:\n" . $algebra->as_string . "\n"; | ||
| 1149 | } | ||
| 1150 | |||
| 1151 | my $default_graphs = [$self->default_graph]; | ||
| 1152 | my $planner = Attean::IDPQueryPlanner->new(); | ||
| 1153 | my $plan = $planner->plan_for_algebra($algebra, $test_model, $default_graphs); | ||
| 1154 | if ($self->debug) { | ||
| 1155 | warn "# Plan:\n" . $plan->as_string . "\n"; | ||
| 1156 | } | ||
| 1157 | |||
| 1158 | if ($self->debug) { | ||
| 1159 | warn "Running update...\n"; | ||
| 1160 | } | ||
| 1161 | my $iter = $plan->evaluate($test_model); | ||
| 1162 | $iter->elements; | ||
| 1163 | if ($self->debug) { | ||
| 1164 | warn "done.\n"; | ||
| 1165 | } | ||
| 1166 | |||
| 1167 | if ($self->debug) { | ||
| 1168 | warn "Comparing results...\n"; | ||
| 1169 | } | ||
| 1170 | my $eqtest = Attean::BindingEqualityTest->new(); | ||
| 1171 | my $eq = $eqtest->equals($test_model, $expected_model); | ||
| 1172 | if ($self->debug) { | ||
| 1173 | warn "done.\n"; | ||
| 1174 | } | ||
| 1175 | $ok = is( $eq, 1, $test->value ); | ||
| 1176 | unless ($ok) { | ||
| 1177 | warn $eqtest->error; | ||
| 1178 | warn "Got model:\n" . $self->model_as_string($test_model); | ||
| 1179 | warn "Expected model:\n" . $self->model_as_string($expected_model); | ||
| 1180 | } | ||
| 1181 | }; | ||
| 1182 | if ($@) { | ||
| 1183 | warn "Failed to execute update: $@"; | ||
| 1184 | fail($test->value); | ||
| 1185 | } | ||
| 1186 | if (not($ok)) { | ||
| 1187 | print "# failed: " . $test->value . "\n"; | ||
| 1188 | } | ||
| 1189 | |||
| 1190 | $self->record_result('update', $ok, $test->value, $TODO); | ||
| 1191 | warn "ok\n" if ($self->debug); | ||
| 1192 | } | ||
| 1193 | } | ||
| 1194 | |||
| 1195 | sub fix_local_to_w3c { | ||
| 1196 | my $self = shift; | ||
| 1197 | my $file_url = shift; | ||
| 1198 | $file_url =~ s{^.*/rdf-tests/}{https://w3c.github.io/rdf-tests/}; # XXX hack | ||
| 1199 | return $file_url; | ||
| 1200 | } | ||
| 1201 | |||
| 1202 | sub construct_data { | ||
| 1203 | my $self = shift; | ||
| 1204 | my $model = shift; | ||
| 1205 | my $action = shift; | ||
| 1206 | my %args = @_; | ||
| 1207 | my $specVersion = $args{'specVersion'} // '1.1'; | ||
| 1208 | |||
| 1209 | my ($queryd) = $model->objects( $action, iri("${RQ}query") )->elements; | ||
| 1210 | my @data = $model->objects( $action, iri("${RQ}data") )->elements; | ||
| 1211 | my @gdata = $model->objects( $action, iri("${RQ}graphData") )->elements; | ||
| 1212 | my @sdata = $model->objects( $action, iri("${RQ}serviceData") )->elements; | ||
| 1213 | my @cdata = $model->objects( $action, iri("${RQ}constructDataFile") )->elements; | ||
| 1214 | my ($fnode) = $model->objects( $action, iri("${RQ}format") )->elements; | ||
| 1215 | |||
| 1216 | my $format = blessed($fnode) ? $fnode->value : 'text/turtle'; | ||
| 1217 | |||
| 1218 | my $uri = URI->new( $queryd->value ); | ||
| 1219 | my $filename = $uri->file; | ||
| 1220 | my (undef,$base,undef) = File::Spec->splitpath( $filename ); | ||
| 1221 | $base = "file://${base}"; | ||
| 1222 | warn "Loading SPARQL query from file $filename" if ($self->debug); | ||
| 1223 | my $sparql = eval { read_file($filename, { binmode => ':utf8' }) }; | ||
| 1224 | if ($@) { | ||
| 1225 | warn "$!: $filename"; | ||
| 1226 | return; | ||
| 1227 | } | ||
| 1228 | |||
| 1229 | my $test_model = $self->test_model(); | ||
| 1230 | foreach my $data (@data) { | ||
| 1231 | if (blessed($data)) { | ||
| 1232 | $test_model->load_urls_into_graph($self->default_graph, $data); | ||
| 1233 | } | ||
| 1234 | } | ||
| 1235 | foreach my $g (@gdata) { | ||
| 1236 | my $start = $test_model->size; | ||
| 1237 | my $file_url = $g->value; | ||
| 1238 | $file_url = $self->fix_local_to_w3c($file_url); | ||
| 1239 | $test_model->load_urls_into_graph(iri($file_url), $g); | ||
| 1240 | my $end = $test_model->size; | ||
| 1241 | unless ($start < $end) { | ||
| 1242 | warn "*** Loading file did not result in any new quads: " . $g; | ||
| 1243 | } | ||
| 1244 | } | ||
| 1245 | foreach my $n (@cdata) { | ||
| 1246 | my ($bytes, $format) = $self->construct_data($model, $n, %args); | ||
| 1247 | my $p = Attean->get_parser(media_type => $format)->new(); | ||
| 1248 | my $iter = $p->parse_iter_from_bytes($bytes); | ||
| 1249 | $test_model->add_iter($iter->as_quads($self->default_graph)); | ||
| 1250 | } | ||
| 1251 | |||
| 1252 | print STDERR "ok\n" if ($self->debug); | ||
| 1253 | |||
| 1254 | if ($self->debug) { | ||
| 1255 | my $q = $sparql; | ||
| 1256 | $q =~ s/([\x{256}-\x{1000}])/'\x{' . sprintf('%x', ord($1)) . '}'/eg; | ||
| 1257 | warn $q; | ||
| 1258 | } | ||
| 1259 | |||
| 1260 | my $test_base = $base; | ||
| 1261 | $test_base = $self->fix_local_to_w3c($test_base); | ||
| 1262 | |||
| 1263 | my ($iter, $type) = $self->get_actual_results( $filename, $test_model, $sparql, $test_base, $model, \@sdata, %args ); | ||
| 1264 | my $sclass = Attean->get_serializer(media_type => $format); | ||
| 1265 | my $s = $sclass->new(); | ||
| 1266 | my $bytes = $s->serialize_iter_to_bytes($iter); | ||
| 1267 | return ($bytes, $format); | ||
| 1268 | } | ||
| 1269 | |||
| 1270 | sub query_eval_test { | ||
| 1271 | my $self = shift; | ||
| 1272 | my $model = shift; | ||
| 1273 | my $test = shift; | ||
| 1274 | my %args = @_; | ||
| 1275 | my $specVersion = $args{'specVersion'} // '1.1'; | ||
| 1276 | my $testname = $test->value; | ||
| 1277 | |||
| 1278 | my ($action) = $model->objects( $test, iri("${MF}action") )->elements; | ||
| 1279 | my ($result) = $model->objects( $test, iri("${MF}result") )->elements; | ||
| 1280 | my ($req) = $model->objects( $test, iri("${MF}requires") )->elements; | ||
| 1281 | my ($approved) = $model->objects( $test, iri("${DAWGT}approval") )->elements; | ||
| 1282 | my ($res_card) = $model->objects( $test, iri("${MF}resultCardinality") )->elements; | ||
| 1283 | my ($queryd) = $model->objects( $action, iri("${RQ}query") )->elements; | ||
| 1284 | my @data = $model->objects( $action, iri("${RQ}data") )->elements; | ||
| 1285 | my @gdata = $model->objects( $action, iri("${RQ}graphData") )->elements; | ||
| 1286 | my @sdata = $model->objects( $action, iri("${RQ}serviceData") )->elements; | ||
| 1287 | my @cdata = $model->objects( $action, iri("${RQ}constructDataFile") )->elements; | ||
| 1288 | my @entailment = $model->objects( $action, [iri("${SD}entailmentRegime"), iri("${SD}EntailmentProfile")] )->elements; | ||
| 1289 | my $reduced_mode = (defined($res_card) and $res_card->value eq "${MF}LaxCardinality"); | ||
| 1290 | |||
| 1291 | if ($self->strict_approval) { | ||
| 1292 | unless ($approved) { | ||
| 1293 | warn "- skipping test because it isn't approved\n" if ($self->debug); | ||
| 1294 | return; | ||
| 1295 | } | ||
| 1296 | if ($approved->equal("${DAWGT}NotClassified")) { | ||
| 1297 | warn "- skipping test because its approval is dawgt:NotClassified\n" if ($self->debug); | ||
| 1298 | return; | ||
| 1299 | } | ||
| 1300 | } | ||
| 1301 | |||
| 1302 | if (scalar(@entailment)) { | ||
| 1303 | $self->record_result('unhandled', 0, $test->as_string, $TODO); | ||
| 1304 | return; | ||
| 1305 | } | ||
| 1306 | |||
| 1307 | my $uri = URI->new( $queryd->value ); | ||
| 1308 | my $filename = $uri->file; | ||
| 1309 | my (undef,$base,undef) = File::Spec->splitpath( $filename ); | ||
| 1310 | $base = "file://${base}"; | ||
| 1311 | warn "Loading SPARQL query from file $filename\n" if ($self->debug); | ||
| 1312 | |||
| 1313 | my $test_base = $base; | ||
| 1314 | $test_base = $self->fix_local_to_w3c($test_base); | ||
| 1315 | |||
| 1316 | my $sparql = eval { read_file($filename, { binmode => ':utf8' }) }; | ||
| 1317 | if ($@) { | ||
| 1318 | warn("$!: $filename; " . $test->as_string); | ||
| 1319 | $self->record_result('evaluation', 0, $test->as_string, $TODO); | ||
| 1320 | return; | ||
| 1321 | } | ||
| 1322 | |||
| 1323 | my $q = $sparql; | ||
| 1324 | $q =~ s/\s+/ /g; | ||
| 1325 | if ($self->debug) { | ||
| 1326 | warn "### test : " . $test->value . "\n"; | ||
| 1327 | warn "# sparql : $q\n"; | ||
| 1328 | warn "# file : $filename\n"; | ||
| 1329 | foreach my $data (@data) { | ||
| 1330 | warn "# data : " . ($data->value =~ s#file://##r) . "\n" if (blessed($data)); | ||
| 1331 | } | ||
| 1332 | warn "# graph data : " . ($_->value =~ s#file://##r) . "\n" for (@gdata); | ||
| 1333 | warn "# constructed data : " . ($_->value =~ s#file://##r) . "\n" for (@cdata); | ||
| 1334 | warn "# result : " . ($result->value =~ s#file://##r) . "\n"; | ||
| 1335 | warn "# requires : " . ($req->value =~ s#file://##r) . "\n" if (blessed($req)); | ||
| 1336 | } | ||
| 1337 | |||
| 1338 | print STDERR "constructing model...\n" if ($self->debug); | ||
| 1339 | my $test_model = $self->test_model(); | ||
| 1340 | TODO: { | ||
| 1341 | local($TODO) = (blessed($req)) ? "requires " . $req->value : ''; | ||
| 1342 | try { | ||
| 1343 | foreach my $data (@data) { | ||
| 1344 | my $file_url = $data->value; | ||
| 1345 | $file_url = $self->fix_local_to_w3c($file_url); | ||
| 1346 | if (blessed($data)) { | ||
| 1347 | $test_model->load_urls_into_graph_with_base($self->default_graph, [$data], $file_url); | ||
| 1348 | } | ||
| 1349 | } | ||
| 1350 | |||
| 1351 | if ($self->debug) { | ||
| 1352 | warn "TEST MODEL AFTER DEFAULT GRAPH LOAD:\n"; | ||
| 1353 | $self->debug_model($test_model); | ||
| 1354 | } | ||
| 1355 | |||
| 1356 | foreach my $g (@gdata) { | ||
| 1357 | my $start = $test_model->size; | ||
| 1358 | my $file_url = $g->value; | ||
| 1359 | $file_url = $self->fix_local_to_w3c($file_url); | ||
| 1360 | $test_model->load_urls_into_graph_with_base(iri($file_url), [$g], $file_url); | ||
| 1361 | my $end = $test_model->size; | ||
| 1362 | unless ($start < $end) { | ||
| 1363 | warn "*** Loading file did not result in any new quads: " . $g; | ||
| 1364 | } | ||
| 1365 | } | ||
| 1366 | foreach my $n (@cdata) { | ||
| 1367 | my ($bytes, $format) = $self->construct_data($model, $n, %args); | ||
| 1368 | my $p = Attean->get_parser(media_type => $format)->new(); | ||
| 1369 | my $iter = $p->parse_iter_from_bytes($bytes); | ||
| 1370 | $test_model->add_iter($iter->as_quads($self->default_graph)); | ||
| 1371 | } | ||
| 1372 | } catch { | ||
| 1373 | fail($test->value); | ||
| 1374 | $self->record_result('evaluation', 0, $test->value, $TODO); | ||
| 1375 | print "# died: " . $test->value . ": $_\n"; | ||
| 1376 | return; | ||
| 1377 | }; | ||
| 1378 | print STDERR "ok\n" if ($self->debug); | ||
| 1379 | |||
| 1380 | my $resuri = URI->new( $result->value ); | ||
| 1381 | my $resfilename = $resuri->file; | ||
| 1382 | |||
| 1383 | my $comment; | ||
| 1384 | eval { | ||
| 1385 | if ($self->debug) { | ||
| 1386 | my $q = $sparql; | ||
| 1387 | $q =~ s/([\x{256}-\x{1000}])/'\x{' . sprintf('%x', ord($1)) . '}'/eg; | ||
| 1388 | warn $q; | ||
| 1389 | } | ||
| 1390 | |||
| 1391 | my ($actual, $type); | ||
| 1392 | { | ||
| 1393 | local($::DEBUG) = 1; | ||
| 1394 | print STDERR "getting actual results...\n" if ($self->debug); | ||
| 1395 | ($actual, $type) = eval { $self->get_actual_results( $filename, $test_model, $sparql, $test_base, $model, \@sdata, %args ) }; | ||
| 1396 | if ($@) { | ||
| 1397 | warn $@; | ||
| 1398 | fail($test->value); | ||
| 1399 | $self->record_result('evaluation', 0, $test->value, $TODO); | ||
| 1400 | return; | ||
| 1401 | } | ||
| 1402 | print STDERR "ok\n" if ($self->debug); | ||
| 1403 | } | ||
| 1404 | |||
| 1405 | print STDERR "getting expected results...\n" if ($self->debug); | ||
| 1406 | my $expected = $self->get_expected_results( $resfilename, $type ); | ||
| 1407 | print STDERR "ok\n" if ($self->debug); | ||
| 1408 | |||
| 1409 | # warn "comparing results..."; | ||
| 1410 | $self->compare_results( $expected, $actual, $test->value, \$comment, $reduced_mode, $TODO ); | ||
| 1411 | }; | ||
| 1412 | |||
| 1413 | my $ok = not($@); | ||
| 1414 | unless ($ok) { | ||
| 1415 | warn $@; | ||
| 1416 | fail($test->value); | ||
| 1417 | $self->record_result('evaluation', 0, $test->value, $TODO); | ||
| 1418 | }; | ||
| 1419 | if ($ok) { | ||
| 1420 | } else { | ||
| 1421 | print "# failed: " . $test->value . "\n"; | ||
| 1422 | } | ||
| 1423 | } | ||
| 1424 | } | ||
| 1425 | |||
| 1426 | sub mock_endpoints { | ||
| 1427 | my $self = shift; | ||
| 1428 | my $mock = shift; | ||
| 1429 | my $model = shift; | ||
| 1430 | my $sdata = shift; | ||
| 1431 | |||
| 1432 | foreach my $sd (@$sdata) { | ||
| 1433 | my ($e) = $model->objects( $sd, iri("${RQ}endpoint") )->elements; | ||
| 1434 | my @data = $model->objects( $sd, iri("${RQ}data") )->elements; | ||
| 1435 | my @gdata = $model->objects( $sd, iri("${RQ}graphData") )->elements; | ||
| 1436 | |||
| 1437 | my $endpoint = $e->value; | ||
| 1438 | my $test_model = $self->test_model(); | ||
| 1439 | foreach my $data (@data) { | ||
| 1440 | if (blessed($data)) { | ||
| 1441 | $test_model->load_urls_into_graph($self->default_graph, $data); | ||
| 1442 | } | ||
| 1443 | } | ||
| 1444 | foreach my $g (@gdata) { | ||
| 1445 | my $start = $test_model->size; | ||
| 1446 | $test_model->load_urls_into_graph($g, $g); | ||
| 1447 | my $end = $test_model->size; | ||
| 1448 | unless ($start < $end) { | ||
| 1449 | warn "*** Loading file did not result in any new quads: " . $g; | ||
| 1450 | } | ||
| 1451 | } | ||
| 1452 | $mock->register_test_endpoint($endpoint, [$test_model, $self->default_graph]); | ||
| 1453 | } | ||
| 1454 | } | ||
| 1455 | |||
| 1456 | sub debug_model { | ||
| 1457 | my $self = shift; | ||
| 1458 | my $model = shift; | ||
| 1459 | if (shift || $self->debug) { | ||
| 1460 | my $iter = $model->get_quads; | ||
| 1461 | warn "Dataset:\n-------------\n"; | ||
| 1462 | my $s = Attean->get_serializer('TextTable')->new(); | ||
| 1463 | my $table = decode_utf8($s->serialize_iter_to_bytes($iter)); | ||
| 1464 | warn $table; | ||
| 1465 | warn "-------------\n"; | ||
| 1466 | } | ||
| 1467 | } | ||
| 1468 | |||
| 1469 | sub get_actual_results { | ||
| 1470 | my $self = shift; | ||
| 1471 | my $filename = shift; | ||
| 1472 | my $model = shift; | ||
| 1473 | my $sparql = shift; | ||
| 1474 | my $base = shift; | ||
| 1475 | my $manifest_model = shift; | ||
| 1476 | my $sdata = shift; | ||
| 1477 | my %args = @_; | ||
| 1478 | my $specVersion = $args{'specVersion'} // '1.1'; | ||
| 1479 | |||
| 1480 | my $bytes = encode_utf8($sparql); | ||
| 1481 | |||
| 1482 | my $s = AtteanX::Parser::SPARQL->new(base => $base, version => $specVersion); | ||
| 1483 | my $algebra; | ||
| 1484 | eval { | ||
| 1485 | ($algebra) = $s->parse_list_from_bytes($bytes); | ||
| 1486 | }; | ||
| 1487 | if ($@) { | ||
| 1488 | warn "Failed to parse query $filename: $@"; | ||
| 1489 | warn "--------- [contents of $filename]\n"; | ||
| 1490 | warn $bytes . "\n"; | ||
| 1491 | warn "---------\n"; | ||
| 1492 | die $@; | ||
| 1493 | } | ||
| 1494 | |||
| 1495 | if ($self->debug) { | ||
| 1496 | warn "Walking algebra:\n"; | ||
| 1497 | warn $algebra->as_string; | ||
| 1498 | } | ||
| 1499 | |||
| 1500 | $self->debug_model($model); | ||
| 1501 | |||
| 1502 | my $testns = 'http://example.com/test-results#'; | ||
| 1503 | my $rmodel = memory_model(); | ||
| 1504 | |||
| 1505 | my $results; | ||
| 1506 | if ($self->use_idp_planner) { | ||
| 1507 | my $default_graphs = [$self->default_graph]; | ||
| 1508 | my $planner = Test::Attean::TestIDPQueryPlanner->new(); | ||
| 1509 | $self->mock_endpoints($planner, $manifest_model, $sdata); | ||
| 1510 | my $plan = $planner->plan_for_algebra($algebra, $model, $default_graphs); | ||
| 1511 | if ($self->debug) { | ||
| 1512 | warn "Walking plan:\n"; | ||
| 1513 | warn $plan->as_string; | ||
| 1514 | } | ||
| 1515 | $results = eval { $plan->evaluate($model) }; | ||
| 1516 | warn $@ if $@; | ||
| 1517 | } else { | ||
| 1518 | my $e = Test::Attean::TestSimpleQueryEvaluator->new( model => $model, default_graph => $self->default_graph ); | ||
| 1519 | $self->mock_endpoints($e, $manifest_model, $sdata); | ||
| 1520 | $results = eval { $e->evaluate($algebra, $self->default_graph) }; | ||
| 1521 | warn $@ if $@; | ||
| 1522 | } | ||
| 1523 | my $count = 1; | ||
| 1524 | |||
| 1525 | unless (blessed($results)) { | ||
| 1526 | die 'Failed to produce actual results'; | ||
| 1527 | } | ||
| 1528 | |||
| 1529 | $results = $results->materialize; | ||
| 1530 | my $item = $results->peek; | ||
| 1531 | |||
| 1532 | my $type = 'bindings'; | ||
| 1533 | if ($item) { | ||
| 1534 | if ($item->does('Attean::API::Triple')) { | ||
| 1535 | $type = 'graph'; | ||
| 1536 | } elsif ($item->does('Attean::API::Term')) { | ||
| 1537 | $type = 'boolean'; | ||
| 1538 | } | ||
| 1539 | } | ||
| 1540 | |||
| 1541 | $self->print_results("Actual results", \$results) if ($self->results); | ||
| 1542 | return ($results, $type); | ||
| 1543 | |||
| 1544 | if ($results->is_bindings) { | ||
| 1545 | return ($results, 'bindings'); | ||
| 1546 | } elsif ($results->is_boolean) { | ||
| 1547 | $rmodel->add_statement( triple( iri("${testns}result"), iri("${testns}boolean"), literal(($results->get_boolean ? 'true' : 'false'), undef, "${XSD}boolean") ) ); | ||
| 1548 | return ($rmodel->get_statements, 'boolean'); | ||
| 1549 | } elsif ($results->is_graph) { | ||
| 1550 | return ($results, 'graph'); | ||
| 1551 | } else { | ||
| 1552 | warn "unknown result type: " . Dumper($results); | ||
| 1553 | } | ||
| 1554 | } | ||
| 1555 | |||
| 1556 | sub print_results { | ||
| 1557 | my $self = shift; | ||
| 1558 | my $name = shift; | ||
| 1559 | my $results = shift; | ||
| 1560 | $$results = $$results->materialize; | ||
| 1561 | print "$name:\n"; | ||
| 1562 | my $count = 1; | ||
| 1563 | while (my $r = $$results->next) { | ||
| 1564 | printf("%3d %s\n", $count++, $r->as_string); | ||
| 1565 | } | ||
| 1566 | $$results->reset; | ||
| 1567 | } | ||
| 1568 | |||
| 1569 | sub get_expected_results { | ||
| 1570 | my $self = shift; | ||
| 1571 | my $file = shift; | ||
| 1572 | my $type = shift; | ||
| 1573 | |||
| 1574 | if ($type eq 'graph') { | ||
| 1575 | my $model = memory_model(); | ||
| 1576 | $model->load_urls_into_graph($self->default_graph, iri("file://$file")); | ||
| 1577 | my $results = $model->get_quads->map(sub { shift->as_triple }, 'Attean::API::Triple'); | ||
| 1578 | $self->print_results("Expected results", \$results) if ($self->results); | ||
| 1579 | return $results; | ||
| 1580 | } elsif ($file =~ /[.](srj|json)/) { | ||
| 1581 | my $model = memory_model(); | ||
| 1582 | open(my $fh, '<', $file) or die $!; | ||
| 1583 | my $parser = Attean->get_parser('SPARQLJSON')->new(); | ||
| 1584 | my $results = $parser->parse_iter_from_io($fh)->materialize; | ||
| 1585 | my $item = $results->peek; | ||
| 1586 | if (blessed($item) and $item->does('Attean::API::Term')) { | ||
| 1587 | if ($self->results) { | ||
| 1588 | warn "Expected result: " . $item->as_string . "\n"; | ||
| 1589 | } | ||
| 1590 | return $results; | ||
| 1591 | } else { | ||
| 1592 | $self->print_results("Expected results", \$results) if ($self->results); | ||
| 1593 | return $results; | ||
| 1594 | } | ||
| 1595 | } elsif ($file =~ /[.]srx/) { | ||
| 1596 | my $model = memory_model(); | ||
| 1597 | my $parser = Attean->get_parser('sparqlxml')->new(); | ||
| 1598 | open(my $fh, '<', $file); | ||
| 1599 | my $results = $parser->parse_iter_from_io($fh); | ||
| 1600 | |||
| 1601 | $self->print_results("Expected results", \$results) if ($self->results); | ||
| 1602 | return $results; | ||
| 1603 | } elsif ($file =~ /[.]csv/) { | ||
| 1604 | my $csv = Text::CSV->new({binary => 1}); | ||
| 1605 | open( my $fh, "<:encoding(utf8)", $file ) or die $!; | ||
| 1606 | my $header = $csv->getline($fh); | ||
| 1607 | my @vars = @$header; | ||
| 1608 | my @data; | ||
| 1609 | while (my $row = $csv->getline($fh)) { | ||
| 1610 | my %result; | ||
| 1611 | foreach my $i (0 .. $#vars) { | ||
| 1612 | my $var = $vars[$i]; | ||
| 1613 | my $value = $row->[ $i ]; | ||
| 1614 | # XXX @@ heuristics that won't always work. | ||
| 1615 | # XXX @@ expected to work on the test suite, though | ||
| 1616 | if ($value =~ /^_:(\w+)$/) { | ||
| 1617 | $value = blank($1); | ||
| 1618 | } elsif ($value =~ /$RE{URI}/) { | ||
| 1619 | $value = iri($value); | ||
| 1620 | } elsif (defined($value) and length($value)) { | ||
| 1621 | $value = literal($value); | ||
| 1622 | } | ||
| 1623 | if (ref($value)) { | ||
| 1624 | $result{ $var } = $value; | ||
| 1625 | } | ||
| 1626 | } | ||
| 1627 | push(@data, Attean::Result->new( bindings => \%result )); | ||
| 1628 | } | ||
| 1629 | my $results = Attean::ListIterator->new(values => \@data, item_type => 'Attean::API::Result', variables => \@vars); | ||
| 1630 | $self->print_results("Expected results", \$results) if ($self->results); | ||
| 1631 | return $results; | ||
| 1632 | } elsif ($file =~ /[.]tsv/) { | ||
| 1633 | my $parser = Attean->get_parser('SPARQLTSV')->new(); | ||
| 1634 | open( my $fh, "<:encoding(utf8)", $file ) or die $!; | ||
| 1635 | my $iter = $parser->parse_iter_from_io($fh); | ||
| 1636 | return $iter; | ||
| 1637 | } elsif ($file =~ /[.](ttl|rdf|nt)/) { | ||
| 1638 | my $model = memory_model(); | ||
| 1639 | my $file_url = "file://$file"; | ||
| 1640 | my $base = $file_url; | ||
| 1641 | $base = $self->fix_local_to_w3c($base); | ||
| 1642 | $model->load_urls_into_graph_with_base($self->default_graph, [iri($file_url)], iri($base)); | ||
| 1643 | my ($res) = $model->subjects( iri("${RDF}type"), iri("${RS}ResultSet") )->elements; | ||
| 1644 | if (my($b) = $model->objects( $res, iri("${RS}boolean") )->elements) { | ||
| 1645 | my $bool = $b->value; | ||
| 1646 | my $term = literal(value => $bool, datatype => "${XSD}boolean"); | ||
| 1647 | if ($self->results) { | ||
| 1648 | warn "Expected result: " . $term->as_string . "\n"; | ||
| 1649 | } | ||
| 1650 | return Attean::ListIterator->new(values => [$term], item_type => 'Attean::API::Term'); | ||
| 1651 | } else { | ||
| 1652 | my @vars = $model->objects( $res, iri("${RS}resultVariable") )->elements; | ||
| 1653 | my @sols = $model->objects( $res, iri("${RS}solution") )->elements; | ||
| 1654 | my @names = map { $_->value } @vars; | ||
| 1655 | my @bindings; | ||
| 1656 | my %vars; | ||
| 1657 | foreach my $r (@sols) { | ||
| 1658 | my %data; | ||
| 1659 | my @b = $model->objects( $r, iri("${RS}binding") )->elements; | ||
| 1660 | foreach my $b (@b) { | ||
| 1661 | my ($value) = $model->objects( $b, iri("${RS}value") )->elements; | ||
| 1662 | my ($var) = $model->objects( $b, iri("${RS}variable") )->elements; | ||
| 1663 | $data{ $var->value } = $value; | ||
| 1664 | $vars{ $var->value }++; | ||
| 1665 | } | ||
| 1666 | push(@bindings, Attean::Result->new( bindings => \%data )); | ||
| 1667 | } | ||
| 1668 | my $results = Attean::ListIterator->new(values => \@bindings, item_type => 'Attean::API::Result', variables => [keys %vars]); | ||
| 1669 | $self->print_results("Expected results", \$results) if ($self->results); | ||
| 1670 | return $results; | ||
| 1671 | } | ||
| 1672 | } else { | ||
| 1673 | die "Unrecognized type of expected results: $file"; | ||
| 1674 | } | ||
| 1675 | } | ||
| 1676 | |||
| 1677 | sub compare_results { | ||
| 1678 | my $self = shift; | ||
| 1679 | my $expected = shift->canonicalize->materialize; | ||
| 1680 | my $actual = shift->canonicalize->materialize; | ||
| 1681 | my $test = shift; | ||
| 1682 | my $comment = shift || do { my $foo; \$foo }; | ||
| 1683 | my $reduced_mode = shift; | ||
| 1684 | my $todo = shift; | ||
| 1685 | if ($reduced_mode) { | ||
| 1686 | for ($actual, $expected) { | ||
| 1687 | $_ = $_->uniq(); | ||
| 1688 | } | ||
| 1689 | } | ||
| 1690 | |||
| 1691 | if ($reduced_mode and $self->debug) { | ||
| 1692 | warn "Comparing data using REDUCED cardinality semantics\n"; | ||
| 1693 | } | ||
| 1694 | |||
| 1695 | if ($actual->does('Attean::API::ResultIterator') or $actual->does('Attean::API::TripleIterator')) { | ||
| 1696 | my $eqtest = Attean::BindingEqualityTest->new(); | ||
| 1697 | if ($test =~ /csv0/) { | ||
| 1698 | # CSV is a lossy format, so strip the languages and datatypes off of literals in the actual results (so that they'll match up with the (lossy) expected results | ||
| 1699 | my $mapper = Attean::TermMap->new(mapper => sub { | ||
| 1700 | my $term = shift; | ||
| 1701 | if ($term->does('Attean::API::Literal')) { | ||
| 1702 | return Attean::Literal->new(value => $term->value); | ||
| 1703 | } | ||
| 1704 | return $term; | ||
| 1705 | }); | ||
| 1706 | $actual = $actual->map($mapper->binding_mapper); | ||
| 1707 | } | ||
| 1708 | |||
| 1709 | my $ok = eval { ok( $eqtest->equals( $actual, $expected ), $test ) or diag($eqtest->error) }; | ||
| 1710 | if ($@) { | ||
| 1711 | diag($@); | ||
| 1712 | } | ||
| 1713 | $self->record_result('evaluation', $ok, $test, $todo); | ||
| 1714 | return $ok; | ||
| 1715 | } elsif ($actual->does('Attean::API::TermIterator')) { | ||
| 1716 | my $a = $actual->next; | ||
| 1717 | my $e = $expected->next; | ||
| 1718 | my $name = $self->debug ? sprintf("$test: %s == %s", $a->as_string, $e->as_string) : $test; | ||
| 1719 | my $eq = $a->equals($e); | ||
| 1720 | my $ok = ok( $eq, $name ); | ||
| 1721 | $self->record_result('evaluation', $ok, $test, $todo); | ||
| 1722 | return $ok; | ||
| 1723 | } else { | ||
| 1724 | die "Unexpected result type $actual"; | ||
| 1725 | } | ||
| 1726 | } | ||
| 1727 | |||
| 1728 | sub record_result { | ||
| 1729 | my $self = shift; | ||
| 1730 | my $type = shift; | ||
| 1731 | my $ok = shift; | ||
| 1732 | my $name = shift; | ||
| 1733 | my $todo = shift; | ||
| 1734 | $name = $self->fix_local_to_w3c($name); | ||
| 1735 | unless ($ok) { | ||
| 1736 | if ($todo) { | ||
| 1737 | push(@{ $self->failures->{'TODO'}{$type} }, $name); | ||
| 1738 | } else { | ||
| 1739 | push(@{ $self->failures->{$type} }, $name); | ||
| 1740 | } | ||
| 1741 | } | ||
| 1742 | |||
| 1743 | if (my $earl = $self->earl) { | ||
| 1744 | if ($ok) { | ||
| 1745 | $earl->pass($name); | ||
| 1746 | } else { | ||
| 1747 | $earl->fail($name); | ||
| 1748 | } | ||
| 1749 | } | ||
| 1750 | } | ||
| 1751 | |||
| 1752 | sub model_as_string { | ||
| 1753 | my $self = shift; | ||
| 1754 | my $model = shift; | ||
| 1755 | my $ser = Attean->get_serializer('nquads'); | ||
| 1756 | my $sep = ('####' x 25) . "\n"; | ||
| 1757 | my $s = sprintf("Model with %d quads:\n", $model->size); | ||
| 1758 | $s .= $ser->serialize_iter_to_bytes($model->get_quads); | ||
| 1759 | return $sep . $s . $sep; | ||
| 1760 | } | ||
| 1761 | |||
| 1762 | sub enable_earl { | ||
| 1763 | my $self = shift; | ||
| 1764 | my %args = @_; | ||
| 1765 | my $conf = $args{ conf } || {}; | ||
| 1766 | if (my $filename = $args{ filename }) { | ||
| 1767 | $self->earl_filename($filename); | ||
| 1768 | } | ||
| 1769 | |||
| 1770 | if (can_load( modules => { 'RDF::EARL' => 0.002 })) { | ||
| 1771 | my $v = $Attean::VERSION; | ||
| 1772 | $v =~ s/[.]/-/; | ||
| 1773 | my $def_subject = "http://purl.org/NET/cpan-uri/dist/Attean/v_$v"; | ||
| 1774 | my $subject = $conf->{ subject } || $def_subject; | ||
| 1775 | my $assertor = $conf->{ assertor } || "${def_subject}#Test-Attean-SPARQLSuite"; | ||
| 1776 | $assertor = iri($assertor) unless (ref($assertor)); | ||
| 1777 | $subject = iri($subject) unless (ref($subject)); | ||
| 1778 | |||
| 1779 | my $EARL = 'http://www.w3.org/ns/earl#'; | ||
| 1780 | my $foaf = 'http://xmlns.com/foaf/0.1/'; | ||
| 1781 | my $type = iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'); | ||
| 1782 | my $partOf = iri('http://purl.org/dc/terms/isPartOf'); | ||
| 1783 | my $desc = iri('http://purl.org/dc/elements/1.1/description'); | ||
| 1784 | |||
| 1785 | my @triples; | ||
| 1786 | my @config_notes; | ||
| 1787 | if (my $store = $conf->{store}) { | ||
| 1788 | push(@config_notes, "Configured with the ${store} quadstore"); | ||
| 1789 | } | ||
| 1790 | if (my $eval = $conf->{'eval'}) { | ||
| 1791 | push(@config_notes, "Configured with ${eval}-based evaluation"); | ||
| 1792 | } | ||
| 1793 | if (scalar(@config_notes)) { | ||
| 1794 | my $proj = $subject; | ||
| 1795 | my $uuid = 'urn:uuid:' . uc(uuid_to_string(create_uuid())); | ||
| 1796 | $subject = iri($uuid); | ||
| 1797 | push(@triples, triple($proj, $type, iri("${EARL}Software"))); | ||
| 1798 | push(@triples, triple($subject, $partOf, $proj)); | ||
| 1799 | foreach my $note (@config_notes) { | ||
| 1800 | push(@triples, triple($subject, $desc, literal($note))); | ||
| 1801 | } | ||
| 1802 | } | ||
| 1803 | |||
| 1804 | my $earl = RDF::EARL->new( subject => $subject, assertor => $assertor ); | ||
| 1805 | $self->earl($earl); | ||
| 1806 | |||
| 1807 | my $graph = $earl->graph; | ||
| 1808 | my $model = $earl->model; | ||
| 1809 | foreach my $t (@triples) { | ||
| 1810 | # warn "Adding metadata: " . $t->as_string . "\n"; | ||
| 1811 | $model->add_quad($t->as_quad($graph)); | ||
| 1812 | } | ||
| 1813 | foreach my $t ($self->earl_metadata($subject->value)) { | ||
| 1814 | # warn "Adding metadata: " . $t->as_string . "\n"; | ||
| 1815 | $model->add_quad($t->as_quad($graph)); | ||
| 1816 | } | ||
| 1817 | |||
| 1818 | return $earl; | ||
| 1819 | } | ||
| 1820 | return; | ||
| 1821 | } | ||
| 1822 | |||
| 1823 | sub earl_metadata { | ||
| 1824 | my $self = shift; | ||
| 1825 | my $subj = shift; | ||
| 1826 | my $prefixes = q[ | ||
| 1827 | @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | ||
| 1828 | @prefix doap: <http://usefulinc.com/ns/doap#>. | ||
| 1829 | @prefix foaf: <http://xmlns.com/foaf/0.1/> . | ||
| 1830 | ]; | ||
| 1831 | |||
| 1832 | my $ver = $Attean::VERSION; | ||
| 1833 | my $release = 'urn:uuid:' . uc(uuid_to_string(create_uuid(UUID_V5, "${subj}-${ver}"))); | ||
| 1834 | my $ttl = $prefixes . <<"END"; | ||
| 1835 | <${subj}> a doap:Project ; | ||
| 1836 | doap:name "Attean" ; | ||
| 1837 | doap:homepage <https://github.com/kasei/attean> ; | ||
| 1838 | rdfs:seeAlso <irc://irc.perl.org/perlrdf> ; | ||
| 1839 | doap:license <http://opensource.org/licenses/Artistic>; | ||
| 1840 | doap:programming-language "Perl" ; | ||
| 1841 | doap:developer <http://kasei.us/about/#greg> ; | ||
| 1842 | doap:release <${release}> ; | ||
| 1843 | . | ||
| 1844 | |||
| 1845 | <${release}> | ||
| 1846 | a doap:Release ; | ||
| 1847 | doap:revision "${ver}" ; | ||
| 1848 | . | ||
| 1849 | |||
| 1850 | <http://kasei.us/about/#greg> | ||
| 1851 | a foaf:Person ; | ||
| 1852 | foaf:name "Gregory Todd Williams" ; | ||
| 1853 | foaf:mbox_sha1sum "f80a0f19d2a0897b89f48647b2fb5ca1f0bc1cb8" ; | ||
| 1854 | foaf:homepage <http://kasei.us/> ; | ||
| 1855 | rdfs:seeAlso <http://kasei.us/about/foaf.ttl> ; | ||
| 1856 | foaf:account [ | ||
| 1857 | a foaf:OnlineAccount ; | ||
| 1858 | foaf:accountName "kasei" ; | ||
| 1859 | foaf:accountServiceHomepage <http://github.com/> | ||
| 1860 | ] ; | ||
| 1861 | . | ||
| 1862 | END | ||
| 1863 | my $parser = Attean->get_parser('Turtle')->new(); | ||
| 1864 | my $iter = $parser->parse_iter_from_bytes(encode_utf8($ttl)); | ||
| 1865 | return $iter->elements; | ||
| 1866 | } | ||
| 1867 | |||
| 1868 | sub DESTROY { | ||
| 1869 | my $self = shift; | ||
| 1870 | my $count = 0; | ||
| 1871 | my $todo = 0; | ||
| 1872 | while (my ($type, $failures) = each(%{ $self->failures })) { | ||
| 1873 | if ($type eq 'TODO') { | ||
| 1874 | while (my ($type, $failures) = each(%$failures)) { | ||
| 1875 | $todo += scalar(@$failures); | ||
| 1876 | } | ||
| 1877 | next; | ||
| 1878 | } | ||
| 1879 | $count += scalar(@$failures); | ||
| 1880 | } | ||
| 1881 | my $timestamp = $self->timestamp; | ||
| 1882 | if ($self->summary or $self->debug) { | ||
| 1883 | if ($self->run_query_tests and ($count or $todo)) { | ||
| 1884 | my $d = Data::Dumper->new([$self->failures], [qw(tests)]); | ||
| 1885 | $d->Sortkeys(1)->Indent(2); | ||
| 1886 | my $msg = "Failing and/or TODO tests: " . $d->Dump; | ||
| 1887 | warn $msg; | ||
| 1888 | unless ($self->pattern) { | ||
| 1889 | open(my $fh, '>', sprintf('.sparql-test-suite-%d', $timestamp)) or die $!; | ||
| 1890 | while (my ($type, $failures) = each(%{ $self->failures })) { | ||
| 1891 | if ($type eq 'TODO') { | ||
| 1892 | while (my ($type, $failures) = each(%$failures)) { | ||
| 1893 | say $fh "$type - TODO"; | ||
| 1894 | say $fh join("\n", sort @$failures); | ||
| 1895 | } | ||
| 1896 | next; | ||
| 1897 | } | ||
| 1898 | say $fh $type; | ||
| 1899 | say $fh join("\n", sort @$failures); | ||
| 1900 | } | ||
| 1901 | } | ||
| 1902 | } | ||
| 1903 | } | ||
| 1904 | if (my $earl = $self->earl and my $filename = $self->earl_filename) { | ||
| 1905 | open(my $fh, '>', $filename) or die $!; | ||
| 1906 | my $model = $earl->model; | ||
| 1907 | my $iter = $model->get_quads()->as_triples(); | ||
| 1908 | my $rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; | ||
| 1909 | my $EARL = 'http://www.w3.org/ns/earl#'; | ||
| 1910 | my $map = URI::NamespaceMap->new( { earl => iri($EARL), rdf => iri($rdf) } ); | ||
| 1911 | my $ser = Attean->get_serializer('Turtle')->new(namespaces => $map); | ||
| 1912 | $ser->serialize_iter_to_io($fh, $iter); | ||
| 1913 | } | ||
| 1914 | } | ||
| 1915 | |||
| 1916 | ############################################################################### | ||
| 1917 | |||
| 1918 | 1; |