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
|
#!/usr/bin/env perl
use strict;
use warnings;
no warnings 'redefine';
use URI::file;
use lib qw(. t);
BEGIN { require "models.pl"; }
use Test::More;
################################################################################
# Log::Log4perl::init( \q[
# log4perl.category.rdf.trine.store.dbi = TRACE, Screen
# log4perl.category.rdf.query = TRACE, Screen
# log4perl.appender.Screen = Log::Log4perl::Appender::Screen
# log4perl.appender.Screen.stderr = 0
# log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout
# ] );
################################################################################
my @models = test_models();
my $alice = URI::file->new_abs( 'data/named_graphs/alice.rdf' );
my $bob = URI::file->new_abs( 'data/named_graphs/bob.rdf' );
my $meta = URI::file->new_abs( 'data/named_graphs/meta.rdf' );
use_ok( 'RDF::Query' );
foreach my $model (@models) {
print "\n#################################\n";
print "### Using model: $model\n";
SKIP: {
{
print "# variable named graph\n";
my $query = new RDF::Query ( <<"END", undef, undef, 'sparql' );
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?src ?name
FROM NAMED <${alice}>
WHERE {
GRAPH ?src { ?x foaf:name ?name }
}
END
my ($src, $name) = $query->get( $model );
ok( $src, 'got source' );
ok( $name, 'got name' );
is( $src->uri_value, $alice, 'graph uri' );
is( $name->literal_value, 'Alice', 'name literal' );
}
{
print "# uri named graph (fail: graph)\n";
my $query = new RDF::Query ( <<"END", undef, undef, 'sparql' );
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
FROM NAMED <${alice}>
WHERE {
GRAPH <foo:bar> { ?x foaf:name ?name }
}
END
my $stream = $query->execute( $model );
my $row = $stream->next;
is( $row, undef, 'no results' );
}
{
print "# uri named graph (fail: pattern)\n";
my $query = new RDF::Query ( <<"END", undef, undef, 'sparql' );
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?src ?name
FROM NAMED <${alice}>
WHERE {
GRAPH ?src { ?x <foo:bar> ?name }
}
END
my ($plan, $ctx) = $query->prepare( $model );
my $stream = $query->execute_plan( $plan, $ctx );
my $row = $stream->next;
is( $row, undef, 'no results' );
}
{
print "# uri named graph with multiple graphs\n";
my $query = new RDF::Query ( <<"END", undef, undef, 'sparql' );
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?mbox
FROM NAMED <${alice}>
FROM NAMED <${bob}>
WHERE {
GRAPH <$bob> { ?x foaf:mbox ?mbox } .
}
END
my $count = 0;
my $stream = $query->execute( $model );
while (my $row = $stream->next) {
isa_ok( $row, 'HASH' );
my $mbox = $row->{mbox};
ok( $mbox, 'got mbox' );
my $uri = $mbox->uri_value;
is( $uri, 'mailto:bob@oldcorp.example.org', "mbox uri: $uri" );
$count++;
}
is( $count, 1, 'one result' );
}
{
print "# variable named graph with multiple graphs; select from one\n";
my $query = new RDF::Query ( <<"END", undef, undef, 'sparql' );
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?src ?mbox
FROM NAMED <${alice}>
FROM NAMED <${bob}>
WHERE {
GRAPH ?src { ?x foaf:name "Alice"; foaf:mbox ?mbox } .
}
END
my ($plan, $ctx) = $query->prepare( $model );
my $iter = $query->execute_plan( $plan, $ctx );
while (my $row = $iter->next) {
my $src = $row->{src};
my $mbox = $row->{mbox};
ok( $src, 'got source' );
ok( $mbox, 'got mbox' );
is( $src->uri_value, $alice, 'graph uri' );
is( $mbox->uri_value, 'mailto:alice@work.example', 'mbox uri' );
}
is( $iter->seen_count, 1, 'expected result count' );
}
{
print "# variable named graph with multiple graphs; select from both\n";
my $query = new RDF::Query ( <<"END", undef, undef, 'sparql' );
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?g ?name
FROM NAMED <${alice}>
FROM NAMED <${bob}>
FROM <${meta}>
WHERE {
GRAPH ?g { ?x foaf:name ?name } .
}
END
my %expected = (
$alice => "Alice",
$bob => "Bob",
);
my $count = 0;
my $stream = $query->execute( $model );
while (my $row = $stream->current) {
$stream->next;
isa_ok( $row, 'HASH' );
my ($graph, $name) = @{ $row }{qw(g name)};
my $uri = $graph->uri_value;
ok( exists $expected{ $uri }, "Known GRAPH: $uri" );
my $expect = $expected{ $uri };
ok( $name, 'got name' );
my $l_name = $name->literal_value;
is( $l_name, $expect, "got name: $l_name" );
$count++;
}
is( $count, 2, 'got results' );
}
{
print "# variable named graph with multiple graphs; non-named graph triples\n";
my $query = new RDF::Query ( <<"END", undef, undef, 'sparql' );
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?g ?name ?topic
FROM NAMED <${alice}>
FROM NAMED <${bob}>
FROM <${meta}>
WHERE {
GRAPH ?g { ?x foaf:name ?name } .
?g foaf:topic ?topic .
}
END
my %expected = (
$alice => "Alice",
$bob => "Bob",
);
my $stream = $query->execute( $model );
while (my $row = $stream->next) {
isa_ok( $row, 'HASH' );
my ($graph, $name, $topic) = @{ $row }{qw(g name topic)};
my $uri = $graph->uri_value;
ok( exists $expected{ $uri }, "Known GRAPH: $uri" );
my $expect = $expected{ $uri };
ok( $name, 'got name' );
ok( $topic, 'got topic' );
my $l_name = $name->literal_value;
my $l_topic = $topic->literal_value;
is( $l_name, $expect, "got name: $l_name" );
is( $l_topic, $expect, "got topic: $l_topic" );
}
is( $stream->seen_count, 2, 'got results' );
}
}
{
print "# graph-1\n";
my $foaf = URI::file->new_abs( "data/foaf.xrdf" );
my $about = URI::file->new_abs( "data/about.xrdf" );
my $query = new RDF::Query ( <<"END", undef, undef, 'sparql' );
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT DISTINCT ?s ?o
FROM <$foaf>
FROM NAMED <$about>
WHERE {
?s dcterms:spatial ?o
}
END
my $stream = $query->execute();
isa_ok( $stream, 'RDF::Trine::Iterator' );
my $count = 0;
while (my $data = $stream->next) {
$count++;
}
is( $count, 0, 'graph-1: BGP does not match NAMED data' );
}
{
print "# graph-2\n";
my $foaf = URI::file->new_abs( "data/foaf.xrdf" );
my $about = URI::file->new_abs( "data/about.xrdf" );
my $query = new RDF::Query ( <<"END", undef, undef, 'sparql' );
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT DISTINCT ?g ?s
FROM <$foaf>
FROM NAMED <$about>
WHERE {
GRAPH ?g { ?s foaf:firstName "Gary" }
}
END
my $stream = $query->execute();
isa_ok( $stream, 'RDF::Trine::Iterator' );
my $count = 0;
while (my $data = $stream->next) {
$count++;
}
is( $count, 0, 'graph-2: GRAPH does not match non-NAMED data' );
}
{
print "# graph-3\n";
my $foaf = URI::file->new_abs( "data/foaf.xrdf" );
my $about = URI::file->new_abs( "data/about.xrdf" );
my $query = new RDF::Query ( <<"END", undef, undef, 'sparql' );
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?p ?g ?img
FROM <$foaf>
FROM NAMED <$about>
WHERE {
?p a foaf:Person .
GRAPH ?g { ?img foaf:maker ?p } .
}
END
my $stream = $query->execute( $model );
isa_ok( $stream, 'RDF::Trine::Iterator' );
my $count = 0;
while ($stream and not $stream->finished) {
my $row = $stream->current;
my ($p,$g,$i) = @{ $row }{qw(p g img)};
ok( $g->isa('RDF::Trine::Node::Resource'), 'graph-3: context is resource' );
ok( $p->isa('RDF::Trine::Node::Resource'), 'graph-3: person is resource' );
is( $p->uri_value, 'http://kasei.us/about/foaf.xrdf#greg', 'graph-3: correct person uri' );
like( $i->uri_value, qr/[.]jpg/, 'graph-3: made image' );
$count++;
} continue { $stream->next }
is( $count, 4, 'graph-3: expected count' );
}
{
print "# find all graph names\n";
my $query = new RDF::Query ( <<"END", undef, undef, 'sparql' ) or die RDF::Query->error;
SELECT ?g
FROM NAMED <${alice}>
FROM NAMED <${bob}>
WHERE {
GRAPH ?g {} .
}
END
my $count = 0;
my $stream = $query->execute( $model );
while (my $row = $stream->next) {
isa_ok( $row, 'HASH' );
my $g = $row->{g};
isa_ok( $g, 'RDF::Query::Node::Resource' );
like( $g->uri_value, qr/(alice|bob).rdf$/, 'expected graph name' );
$count++;
}
is( $count, 2, 'two expected graph names' );
}
}
done_testing();
|