/usr/share/perl5/Test/Attean/SPARQLSuite.pm is in libattean-perl 0.019-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 | package Test::Attean::SPARQLSuite;
use v5.14;
use warnings;
use Test::Roo::Role;
use Attean;
use Attean::RDF;
use AtteanX::Parser::SPARQL;
use Attean::SimpleQueryEvaluator;
use Carp;
use HTTP::Request;
use HTTP::Response;
use HTTP::Message::PSGI;
use Data::Dumper;
use Encode qw(encode);
use Getopt::Long;
use Regexp::Common qw /URI/;
use Scalar::Util qw(blessed reftype);
use List::MoreUtils qw(all);
use Test::Modern;
use Text::CSV;
use Try::Tiny;
use URI::file;
use File::Spec;
use Types::Standard qw(Str Bool ArrayRef HashRef InstanceOf ConsumerOf);
require XML::Simple;
my $XSD = 'http://www.w3.org/2001/XMLSchema#';
my $RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
my $RDFS = 'http://www.w3.org/2000/01/rdf-schema#';
my $RS = 'http://www.w3.org/2001/sw/DataAccess/tests/result-set#';
my $MF = 'http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#';
my $UT = 'http://www.w3.org/2009/sparql/tests/test-update#';
my $RQ = 'http://www.w3.org/2001/sw/DataAccess/tests/test-query#';
my $DAWGT = 'http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#';
requires 'test_model';
has run_update_tests => (is => 'rw', isa => Bool, default => 1);
has run_query_tests => (is => 'rw', isa => Bool, default => 1);
has debug => (is => 'rw', isa => Bool, default => 0);
has results => (is => 'rw', isa => Bool, default => 0);
has strict_approval => (is => 'rw', isa => Bool, default => 0);
has use_idp_planner => (is => 'rw', isa => Bool, default => 1);
has pattern => (is => 'rw', isa => Str, default => '');
has tests_dir => (is => 'rw', required => 1, default => sub { $ENV{ATTEAN_SPARQL_TESTS_DIR} });
has model => (is => 'rw', isa => ConsumerOf['Attean::API::Model'], init_arg => undef);
has manifests => (is => 'rw', isa => ArrayRef, init_arg => undef);
has default_graph => (is => 'rw');
has failures => (is => 'rw', isa => HashRef, default => sub { +{} });
sub BUILD {
my $self = shift;
if ($self->pattern) {
$self->results(1);
}
}
sub memory_model {
my $self = shift;
my $store = Attean->get_store('Memory')->new();
my $model = Attean::MutableQuadModel->new( store => $store );
return $model;
}
sub setup {
my $self = shift;
$self->default_graph(iri('http://graph/'));
warn 'setting up sparql test harness' if ($self->debug);
my @files;
if ($self->run_query_tests) {
push(@files, qw(
aggregates
bind
bindings
construct
csv-tsv-res
exists
functions
grouping
json-res
negation
project-expression
property-path
subquery
));
push(@files, qw(
aggregates
construct
delete-insert
grouping
syntax-query
syntax-fed
syntax-update-1
syntax-update-2
));
}
if ($self->run_update_tests) {
push(@files, qw(
add
basic-update
clear
copy
delete
delete-data
delete-insert
delete-where
drop
move
update-silent
));
}
my $dir = $self->tests_dir;
unless (defined($dir)) {
plan skip_all => "No manifest directory given";
exit(0);
}
unless (-d $dir and -r $dir) {
plan skip_all => "Manifest directory not readable: $dir";
exit(0);
}
my @manifests = grep { -r $_ } map { File::Spec->catfile($dir, $_, 'manifest.ttl') } @files;
unless (scalar(@manifests)) {
plan skip_all => "No manifest files found in $dir";
exit(0);
}
my $model = $self->memory_model();
my $class = Attean->get_parser("turtle") || die "Failed to load parser for 'turtle'";
my @load = map { iri("file://" . File::Spec->rel2abs($_)) } @manifests;
$model->load_urls_into_graph($self->default_graph, @load);
warn "done parsing manifests" if $self->debug;
$self->model($model);
$self->manifests([$model->subjects( iri("${RDF}type"), iri("${MF}Manifest") )->elements]);
}
sub syntax_test {
my $self = shift;
my $test_type = shift;
my $model = shift;
my $test = shift;
my $count = shift // 1;
my $type = iri( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" );
my $mfname = iri( "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#name" );
my ($queryd) = $model->objects( $test, iri("${MF}action") )->elements;
my ($approved) = $model->objects( $test, iri("${DAWGT}approval") )->elements;
my ($name) = $model->objects( $test, $mfname )->elements;
my $namevalue = $name->value;
if ($self->strict_approval) {
unless ($approved) {
warn "- skipping test because it isn't approved\n" if ($self->debug);
return;
}
if ($approved->equal("${DAWGT}NotClassified")) {
warn "- skipping test because its approval is dawgt:NotClassified\n" if ($self->debug);
return;
}
}
my $is_pos_query = $model->count_quads($test, $type, iri("${MF}PositiveSyntaxTest11"));
my $is_pos_update = $model->count_quads($test, $type, iri("${MF}PositiveUpdateSyntaxTest11"));
my $is_neg_query = $model->count_quads($test, $type, iri("${MF}NegativeSyntaxTest")) + $model->count_quads($test, $type, iri("${MF}NegativeSyntaxTest11"));
my $is_neg_update = $model->count_quads($test, $type, iri("${MF}NegativeUpdateSyntaxTest")) + $model->count_quads($test, $type, iri("${MF}NegativeUpdateSyntaxTest11"));
my $uri = URI->new( $queryd->value );
my $filename = $uri->file;
my (undef,$base,undef) = File::Spec->splitpath( $filename );
$base = "file://${base}";
warn "Loading SPARQL query from file $filename" if ($self->debug);
my $sparql = do { local($/) = undef; open(my $fh, '<:utf8', $filename) or do { warn("$!: $filename; " . $test->as_string); return }; <$fh> };
if ($self->debug) {
my $q = $sparql;
$q =~ s/\s+/ /g;
warn "### test : " . $test->as_string . "\n";
warn "# file : $filename\n";
warn "# sparql : $q\n";
}
my $pclass = Attean->get_parser('SPARQL');
my $parser = $pclass->new();
if ($test_type eq 'update') {
$parser->update(1);
}
if ($is_pos_query or $is_pos_update) {
my ($query) = eval { $parser->parse_list_from_bytes($sparql) };
my $ok = blessed($query);
$self->record_result('syntax', $ok, $test->as_string);
if ($ok) {
pass("syntax $namevalue: $filename");
} else {
fail("syntax $namevalue; $filename: $@");
}
} elsif ($is_neg_query or $is_neg_update) {
my ($query) = eval { $parser->parse_list_from_bytes($sparql) };
my $ok = $@ ? 1 : 0;
$self->record_result('syntax', $ok, $test->as_string);
if ($ok) {
pass("syntax $namevalue: $filename");
} else {
if ($self->debug) {
warn $query->as_string;
}
fail("syntax $namevalue; $filename (unexpected successful parse)");
}
}
}
sub update_eval_test {
my $self = shift;
my $model = shift;
my $test = shift;
my $count = shift // 1;
my ($action) = $model->objects( $test, iri("${MF}action") )->elements;
my ($result) = $model->objects( $test, iri("${MF}result") )->elements;
my ($req) = $model->objects( $test, iri("${MF}requires") )->elements;
my ($approved) = $model->objects( $test, iri("${DAWGT}approval") )->elements;
my ($queryd) = $model->objects( $action, iri("${UT}request") )->elements;
my ($data) = $model->objects( $action, iri("${UT}data") )->elements;
my @gdata = $model->objects( $action, iri("${UT}graphData") )->elements;
if ($self->strict_approval) {
unless ($approved) {
warn "- skipping test because it isn't approved\n" if ($self->debug);
return;
}
if ($approved->equal(iri("${DAWGT}NotClassified"))) {
warn "- skipping test because its approval is dawgt:NotClassified\n" if ($self->debug);
return;
}
}
my $uri = URI->new( $queryd->value );
my $filename = $uri->file;
my (undef,$base,undef) = File::Spec->splitpath( $filename );
$base = "file://${base}";
warn "Loading SPARQL query from file $filename" if ($self->debug);
my $sparql = do { local($/) = undef; open(my $fh, '<', $filename) or do { fail("$!: $filename; " . $test->as_string); return }; binmode($fh, ':utf8'); <$fh> };
my $q = $sparql;
$q =~ s/\s+/ /g;
if ($self->debug) {
warn "### test : " . $test->value . "\n";
warn "# sparql : $q\n";
warn "# data : " . $data->value . "\n" if (blessed($data));
warn "# graph data : " . $_->value . "\n" for (@gdata);
warn "# result : " . $result->value . "\n";
warn "# requires : " . $req->value . "\n" if (blessed($req));
}
# TODO: set up remote endpoint mock
warn "constructing model...\n" if ($self->debug);
my $test_model = $self->test_model();
eval {
if (blessed($data)) {
$test_model->load_urls_into_graph($self->default_graph, $data);
}
};
if ($@) {
fail($test->value);
print "# died: " . $test->value . ": $@\n";
return;
}
foreach my $gdata (@gdata) {
my ($data) = ($model->objects( $gdata, iri("${UT}data") )->elements)[0] || ($model->objects( $gdata, iri("${UT}graph") )->elements)[0];
my ($graph) = $model->objects( $gdata, iri("${RDFS}label") )->elements;
my $uri = $graph->value;
eval {
$test_model->load_urls_into_graph(iri($uri), $data);
};
if ($@) {
fail($test->as_string);
print "# died: " . $test->value . ": $@\n";
return;
};
}
my ($result_status) = $model->objects( $result, iri("${UT}result") )->elements;
my @resgdata = $model->objects( $result, iri("${UT}graphData") )->elements;
my ($resdata) = $model->objects( $result, iri("${UT}data") )->elements;
my $expected_model = memory_model;
eval {
if (blessed($resdata)) {
$expected_model->load_urls_into_graph($self->default_graph, $resdata);
}
};
if ($@) {
fail($test->as_string);
print "# died: " . $test->value . ": $@\n";
return;
};
foreach my $gdata (@resgdata) {
my ($data) = ($model->objects( $gdata, iri("${UT}data") )->elements)[0] || ($model->objects( $gdata, iri("${UT}graph") )->elements)[0];
my ($graph) = $model->objects( $gdata, iri("${RDFS}label") )->elements;
my $uri = $graph->value;
my $return = 0;
if ($data) {
eval {
$expected_model->load_urls_into_graph(iri($uri), $data);
};
if ($@) {
fail($test->as_string);
print "# died: " . $test->value . ": $@\n";
$return = 1;
};
return if ($return);
}
}
if ($self->debug) {
warn "Dataset before update operation:\n";
warn $self->model_as_string($test_model);
}
my $ok = 0;
eval {
my $algebra = eval { Attean->get_parser('SPARQL')->parse_update($sparql) };
if ($@) {
warn "Failed to parse query $filename: $@";
die $@;
}
unless ($algebra) {
warn "No algebra generated for update\n";
fail($test->value);
return;
}
if ($self->debug) {
warn "# Algebra:\n" . $algebra->as_string . "\n";
}
my $default_graphs = [$self->default_graph];
my $planner = Attean::IDPQueryPlanner->new();
my $plan = $planner->plan_for_algebra($algebra, $test_model, $default_graphs);
if ($self->debug) {
warn "# Plan:\n" . $plan->as_string . "\n";
}
if ($self->debug) {
warn "Running update...\n";
}
my $iter = $plan->evaluate($test_model);
$iter->elements;
if ($self->debug) {
warn "done.\n";
}
if ($self->debug) {
warn "Comparing results...\n";
}
my $eqtest = Attean::BindingEqualityTest->new();
my $eq = $eqtest->equals($test_model, $expected_model);
if ($self->debug) {
warn "done.\n";
}
$ok = is( $eq, 1, $test->value );
unless ($ok) {
warn $eqtest->error;
warn "Got model:\n" . $self->model_as_string($test_model);
warn "Expected model:\n" . $self->model_as_string($expected_model);
}
};
if ($@) {
warn "Failed to execute update: $@";
fail($test->value);
}
if (not($ok)) {
print "# failed: " . $test->value . "\n";
}
warn "ok\n" if ($self->debug);
}
sub query_eval_test {
my $self = shift;
my $model = shift;
my $test = shift;
my $count = shift // 1;
my ($action) = $model->objects( $test, iri("${MF}action") )->elements;
my ($result) = $model->objects( $test, iri("${MF}result") )->elements;
my ($req) = $model->objects( $test, iri("${MF}requires") )->elements;
my ($approved) = $model->objects( $test, iri("${DAWGT}approval") )->elements;
my ($queryd) = $model->objects( $action, iri("${RQ}query") )->elements;
my ($data) = $model->objects( $action, iri("${RQ}data") )->elements;
my @gdata = $model->objects( $action, iri("${RQ}graphData") )->elements;
my @sdata = $model->objects( $action, iri("${RQ}serviceData") )->elements;
if ($self->strict_approval) {
unless ($approved) {
warn "- skipping test because it isn't approved\n" if ($self->debug);
return;
}
if ($approved->equal("${DAWGT}NotClassified")) {
warn "- skipping test because its approval is dawgt:NotClassified\n" if ($self->debug);
return;
}
}
my $uri = URI->new( $queryd->value );
my $filename = $uri->file;
my (undef,$base,undef) = File::Spec->splitpath( $filename );
$base = "file://${base}";
warn "Loading SPARQL query from file $filename" if ($self->debug);
my $sparql = do { local($/) = undef; open(my $fh, '<', $filename) or do { warn("$!: $filename; " . $test->value); return }; binmode($fh, ':utf8'); <$fh> };
my $q = $sparql;
$q =~ s/\s+/ /g;
if ($self->debug) {
warn "### test : " . $test->value . "\n";
warn "# sparql : $q\n";
warn "# data : " . ($data->value =~ s#file://##r) if (blessed($data));
warn "# graph data : " . ($_->value =~ s#file://##r) for (@gdata);
warn "# result : " . ($result->value =~ s#file://##r);
warn "# requires : " . ($req->value =~ s#file://##r) if (blessed($req));
}
STRESS: foreach (1 .. $count) {
print STDERR "constructing model... " if ($self->debug);
my $test_model = $self->test_model();
try {
if (blessed($data)) {
$test_model->load_urls_into_graph($self->default_graph, $data);
}
foreach my $g (@gdata) {
$test_model->load_urls_into_graph($g, $g);
}
} catch {
fail($test->value);
$self->record_result('evaluation', 0, $test->value);
print "# died: " . $test->value . ": $_\n";
next STRESS;
};
print STDERR "ok\n" if ($self->debug);
my $resuri = URI->new( $result->value );
my $resfilename = $resuri->file;
TODO: {
local($TODO) = (blessed($req)) ? "requires " . $req->value : '';
my $comment;
eval {
if ($self->debug) {
my $q = $sparql;
$q =~ s/([\x{256}-\x{1000}])/'\x{' . sprintf('%x', ord($1)) . '}'/eg;
warn $q;
}
my ($actual, $type);
{
local($::DEBUG) = 1;
print STDERR "getting actual results... " if ($self->debug);
($actual, $type) = $self->get_actual_results( $filename, $test_model, $sparql, $base );
print STDERR "ok\n" if ($self->debug);
}
print STDERR "getting expected results... " if ($self->debug);
my $expected = $self->get_expected_results( $resfilename, $type );
print STDERR "ok\n" if ($self->debug);
# warn "comparing results...";
$self->compare_results( $expected, $actual, $test->value, \$comment );
};
my $ok = not($@);
unless ($ok) {
warn $@;
fail($test->value);
$self->record_result('evaluation', 0, $test->value);
};
if ($ok) {
} else {
print "# failed: " . $test->value . "\n";
}
}
}
}
sub get_actual_results {
my $self = shift;
my $filename = shift;
my $model = shift;
my $sparql = shift;
my $base = shift;
my $s = AtteanX::Parser::SPARQL->new(base => $base);
my $algebra;
eval {
($algebra) = $s->parse_list_from_bytes($sparql);
};
if ($@) {
warn "Failed to parse query $filename: $@";
die $@;
}
if ($self->debug) {
warn "Walking algebra:\n";
warn $algebra->as_string;
}
if ($self->debug) {
my $iter = $model->get_quads;
warn "Dataset:\n-------------\n";
while (my $q = $iter->next) {
say $q->as_string;
}
warn "-------------\n";
}
my $testns = 'http://example.com/test-results#';
my $rmodel = memory_model();
my $results;
if ($self->use_idp_planner) {
my $default_graphs = [$self->default_graph];
my $planner = Attean::IDPQueryPlanner->new();
my $plan = $planner->plan_for_algebra($algebra, $model, $default_graphs);
if ($self->debug) {
warn "Walking plan:\n";
warn $plan->as_string;
}
$results = $plan->evaluate($model);
} else {
my $e = Attean::SimpleQueryEvaluator->new( model => $model, default_graph => $self->default_graph );
$results = $e->evaluate($algebra, $self->default_graph);
}
my $count = 1;
$results = $results->materialize;
my $item = $results->peek;
my $type = 'bindings';
if ($item) {
if ($item->does('Attean::API::Triple')) {
$type = 'graph';
} elsif ($item->does('Attean::API::Term')) {
$type = 'boolean';
}
}
$self->print_results("Actual results", \$results) if ($self->results);
return ($results, $type);
if ($results->is_bindings) {
return ($results, 'bindings');
} elsif ($results->is_boolean) {
$rmodel->add_statement( triple( iri("${testns}result"), iri("${testns}boolean"), literal(($results->get_boolean ? 'true' : 'false'), undef, "${XSD}boolean") ) );
return ($rmodel->get_statements, 'boolean');
} elsif ($results->is_graph) {
return ($results, 'graph');
} else {
warn "unknown result type: " . Dumper($results);
}
}
sub print_results {
my $self = shift;
my $name = shift;
my $results = shift;
$$results = $$results->materialize;
warn "$name:\n";
my $count = 1;
while (my $r = $$results->next) {
printf("%3d %s\n", $count++, $r->as_string);
}
$$results->reset;
}
sub get_expected_results {
my $self = shift;
my $file = shift;
my $type = shift;
if ($type eq 'graph') {
my $model = memory_model();
$model->load_urls_into_graph($self->default_graph, iri("file://$file"));
my $results = $model->get_quads->map(sub { shift->as_triple }, 'Attean::API::Triple');
$self->print_results("Expected results", \$results) if ($self->results);
return $results;
} elsif ($file =~ /[.](srj|json)/) {
my $model = memory_model();
open(my $fh, '<', $file) or die $!;
my $parser = Attean->get_parser('SPARQLJSON')->new();
my $results = $parser->parse_iter_from_io($fh)->materialize;
my $item = $results->peek;
if ($item->does('Attean::API::Term')) {
if ($self->results) {
warn "Expected result: " . $item->as_string . "\n";
}
return $results;
} else {
$self->print_results("Expected results", \$results) if ($self->results);
return $results;
}
} elsif ($file =~ /[.]srx/) {
my $model = memory_model();
my $parser = Attean->get_parser('sparqlxml')->new();
open(my $fh, '<', $file);
my $results = $parser->parse_iter_from_io($fh);
$self->print_results("Expected results", \$results) if ($self->results);
return $results;
} elsif ($file =~ /[.]csv/) {
my $csv = Text::CSV->new({binary => 1});
open( my $fh, "<:encoding(utf8)", $file ) or die $!;
my $header = $csv->getline($fh);
my @vars = @$header;
my @data;
while (my $row = $csv->getline($fh)) {
my %result;
foreach my $i (0 .. $#vars) {
my $var = $vars[$i];
my $value = $row->[ $i ];
# XXX @@ heuristics that won't always work.
# XXX @@ expected to work on the test suite, though
if ($value =~ /^_:(\w+)$/) {
$value = blank($1);
} elsif ($value =~ /$RE{URI}/) {
$value = iri($value);
} elsif (defined($value) and length($value)) {
$value = literal($value);
}
if (ref($value)) {
$result{ $var } = $value;
}
}
push(@data, Attean::Result->new( bindings => \%result ));
}
my $results = Attean::ListIterator->new(values => \@data, item_type => 'Attean::API::Result', variables => \@vars);
$self->print_results("Expected results", \$results) if ($self->results);
return $results;
} elsif ($file =~ /[.]tsv/) {
my $parser = Attean->get_parser('SPARQLTSV')->new();
open( my $fh, "<:encoding(utf8)", $file ) or die $!;
my $iter = $parser->parse_iter_from_io($fh);
return $iter;
} elsif ($file =~ /[.](ttl|rdf|nt)/) {
my $model = memory_model();
$model->load_urls_into_graph($self->default_graph, iri("file://$file"));
my ($res) = $model->subjects( iri("${RDF}type"), iri("${RS}ResultSet") )->elements;
if (my($b) = $model->objects( $res, iri("${RS}boolean") )->elements) {
my $bool = $b->value;
my $term = literal(value => $bool, datatype => "${XSD}boolean");
if ($self->results) {
warn "Expected result: " . $term->as_string . "\n";
}
return Attean::ListIterator->new(values => [$term], item_type => 'Attean::API::Term');
} else {
my @vars = $model->objects( $res, iri("${RS}resultVariable") )->elements;
my @sols = $model->objects( $res, iri("${RS}solution") )->elements;
my @names = map { $_->value } @vars;
my @bindings;
my %vars;
foreach my $r (@sols) {
my %data;
my @b = $model->objects( $r, iri("${RS}binding") )->elements;
foreach my $b (@b) {
my ($value) = $model->objects( $b, iri("${RS}value") )->elements;
my ($var) = $model->objects( $b, iri("${RS}variable") )->elements;
$data{ $var->value } = $value;
$vars{ $var->value }++;
}
push(@bindings, Attean::Result->new( bindings => \%data ));
}
my $results = Attean::ListIterator->new(values => \@bindings, item_type => 'Attean::API::Result', variables => [keys %vars]);
$self->print_results("Expected results", \$results) if ($self->results);
return $results;
}
} else {
die "Unrecognized type of expected results: $file";
}
}
sub compare_results {
my $self = shift;
my $expected = shift->canonicalize->materialize;
my $actual = shift->canonicalize->materialize;
my $test = shift;
my $comment = shift || do { my $foo; \$foo };
my $TODO = shift;
if ($actual->does('Attean::API::ResultIterator') or $actual->does('Attean::API::TripleIterator')) {
my $eqtest = Attean::BindingEqualityTest->new();
if ($test =~ /csv0/) {
# 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
my $mapper = Attean::TermMap->new(mapper => sub {
my $term = shift;
if ($term->does('Attean::API::Literal')) {
return Attean::Literal->new(value => $term->value);
}
return $term;
});
$actual = $actual->map($mapper->binding_mapper);
}
my $ok = ok( $eqtest->equals( $actual, $expected ), $test ) or diag($eqtest->error);
$self->record_result('evaluation', $ok, $test);
return $ok;
} elsif ($actual->does('Attean::API::TermIterator')) {
my $a = $actual->next;
my $e = $expected->next;
my $name = $self->debug ? sprintf("$test: %s == %s", $a->as_string, $e->as_string) : $test;
my $ok = ok( $a->equals($e), $name );
$self->record_result('evaluation', $ok, $test);
return $ok;
} else {
die "Unexpected result type $actual";
}
}
sub record_result {
my $self = shift;
my $type = shift;
my $ok = shift;
my $name = shift;
unless ($ok) {
push(@{ $self->failures->{$type} }, $name);
}
}
sub model_as_string {
my $self = shift;
my $model = shift;
my $ser = Attean->get_serializer('nquads');
my $sep = ('####' x 25) . "\n";
my $s = sprintf("Model with %d quads:\n", $model->size);
$s .= $ser->serialize_iter_to_bytes($model->get_quads);
return $sep . $s . $sep;
}
sub DESTROY {
my $self = shift;
my $count = 0;
while (my ($type, $failures) = each(%{ $self->failures })) {
$count += scalar(@$failures);
}
if ($self->run_query_tests and $count) {
my $d = Data::Dumper->new([$self->failures], [qw(failures)]);
$d->Sortkeys(1)->Indent(2);
my $msg = "Failing tests: " . $d->Dump;
warn $msg;
unless ($self->pattern) {
open(my $fh, '>', sprintf('.sparql-test-suite-%d', scalar(time)));
while (my ($type, $failures) = each(%{ $self->failures })) {
say $fh $type;
say $fh join("\n", sort @$failures);
}
}
}
}
###############################################################################
Test::Roo::top_test 'SPARQL 1.1 tests' => sub {
my $self = shift;
my $PATTERN = $self->pattern;
my @manifests = @{ $self->manifests };
my $model = $self->model;
foreach my $m (@manifests) {
# warn "Manifest: " . $m->as_string . "\n" if ($self->debug);
my ($list) = $model->objects( $m, iri("${MF}entries") )->elements;
unless (blessed($list)) {
warn "No mf:entries found for manifest " . $m->as_string . "\n";
}
my @tests = $model->get_list( $self->default_graph, $list )->elements;
foreach my $test (@tests) {
unless ($test->value =~ /$PATTERN/) {
next;
}
# if ($LIST_TESTS) {
# say $test->value;
# }
if ($self->run_query_tests) {
{
# Evaluation Tests
my $et = $model->count_quads($test, iri("${RDF}type"), iri("${MF}QueryEvaluationTest"));
my $ct = $model->count_quads($test, iri("${RDF}type"), iri("${MF}CSVResultFormatTest"));
if ($et + $ct) {
my ($name) = $model->objects( $test, iri("${MF}name") )->elements;
warn "### query eval test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug);
$self->query_eval_test( $model, $test );
}
}
{
# Syntax Tests
my $total = 0;
foreach my $type (qw(PositiveSyntaxTest11 NegativeSyntaxTest11)) {
$total += $model->count_quads($test, iri("${RDF}type"), iri("${MF}$type"));
}
if ($total) {
my ($name) = $model->objects( $test, iri("${MF}name") )->elements;
warn "### query syntax test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug);
$self->syntax_test( 'query', $model, $test );
}
}
}
if ($self->run_update_tests) {
{
# Evaluation Tests
if ($model->count_quads($test, iri("${RDF}type"), iri("${UT}UpdateEvaluationTest")) or $model->count_quads($test, iri("${RDF}type"), iri("${MF}UpdateEvaluationTest"))) {
my ($name) = $model->objects( $test, iri("${MF}name") )->elements;
unless ($test->value =~ /$PATTERN/) {
next;
}
warn "### update eval test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug);
$self->update_eval_test( $model, $test, );
}
}
{
# Syntax Tests
my $total = 0;
foreach my $type (qw(PositiveUpdateSyntaxTest11 NegativeUpdateSyntaxTest11)) {
$total += $model->count_quads($test, iri("${RDF}type"), iri("${MF}$type"));
}
if ($total) {
my ($name) = $model->objects( $test, iri("${MF}name") )->elements;
warn "### query syntax test: " . $test->as_string . " >>> " . $name->value . "\n" if ($self->debug);
$self->syntax_test( 'update', $model, $test );
}
}
}
}
}
};
1;
|