1use Test::More qw(no_plan); # tests => 183; 2use Test::Exception; 3 4use strict; 5use warnings; 6no warnings 'redefine'; 7use Scalar::Util qw(blessed); 8 9use RDF::Trine; 10use RDF::Trine::Store::Hexastore; 11 12my $store = RDF::Trine::Store::Hexastore->new(); 13isa_ok( $store, 'RDF::Trine::Store::Hexastore' ); 14 15_add_rdf( $store, <<"END" ); 16<?xml version="1.0"?> 17<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:eg="http://example.org/"> 18 <rdf:Description rdf:about="http://example.org/foo"> 19 <eg:bar rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">23</eg:bar> 20 </rdf:Description> 21</rdf:RDF> 22END 23 24{ 25 my @nodes = ( 26 RDF::Trine::Node::Resource->new('http://example.org/foo'), 27 RDF::Trine::Node::Resource->new('http://example.org/bar'), 28 RDF::Trine::Node::Literal->new('23', undef, 'http://www.w3.org/2001/XMLSchema#integer'), 29 ); 30 my $st = RDF::Trine::Statement->new( @nodes ); 31 my $iter = $store->get_statements( @nodes ); 32 isa_ok( $iter, 'RDF::Trine::Iterator' ); 33 my $next = $iter->next; 34 is_deeply( $next, $st, 'got expected statement by 3-bound query' ); 35} 36 37{ 38 my @nodes = ( 39 RDF::Trine::Node::Resource->new('http://example.org/foo'), 40 RDF::Trine::Node::Resource->new('http://example.org/bar'), 41 RDF::Trine::Node::Literal->new('00', undef, 'http://www.w3.org/2001/XMLSchema#integer'), 42 ); 43 my $st = RDF::Trine::Statement->new( @nodes ); 44 my $iter = $store->get_statements( @nodes ); 45 isa_ok( $iter, 'RDF::Trine::Iterator' ); 46 my $next = $iter->next; 47 is_deeply( $next, undef, 'got expected empty statement iterator by 3-bound query' ); 48} 49 50{ 51 my @nodes = ( 52 RDF::Trine::Node::Resource->new('http://example.org/foo'), 53 RDF::Trine::Node::Resource->new('http://example.org/bar'), 54 undef, 55 ); 56 my $st = RDF::Trine::Statement->new( @nodes[0,1], RDF::Trine::Node::Literal->new('23', undef, 'http://www.w3.org/2001/XMLSchema#integer') ); 57 my $iter = $store->get_statements( @nodes ); 58 isa_ok( $iter, 'RDF::Trine::Iterator' ); 59 my $count = 0; 60 while (my $next = $iter->next) { 61 is_deeply( $next, $st, 'got expected statement by 2-bound query' ); 62 $count++; 63 } 64 is( $count, 1, 'iterator had expected single element' ); 65} 66 67_add_rdf( $store, <<"END" ); 68<?xml version="1.0"?> 69<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:eg="http://example.org/"> 70 <rdf:Description rdf:about="http://example.org/zzz"> 71 <eg:bar rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">999</eg:bar> 72 </rdf:Description> 73 <rdf:Description rdf:about="http://example.org/foo"> 74 <eg:bar rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">24</eg:bar> 75 <eg:baz>quux</eg:baz> 76 </rdf:Description> 77</rdf:RDF> 78END 79 80{ 81 my @nodes = ( 82 RDF::Trine::Node::Resource->new('http://example.org/foo'), 83 RDF::Trine::Node::Resource->new('http://example.org/bar'), 84 undef, 85 ); 86 my @expect = ( 87 RDF::Trine::Node::Literal->new('23', undef, 'http://www.w3.org/2001/XMLSchema#integer'), 88 RDF::Trine::Node::Literal->new('24', undef, 'http://www.w3.org/2001/XMLSchema#integer'), 89 ); 90 my $iter = $store->get_statements( @nodes ); 91 isa_ok( $iter, 'RDF::Trine::Iterator' ); 92 my $count = 0; 93 while (my $next = $iter->next) { 94 my $e = shift(@expect); 95 unless (blessed($e)) { 96 fail('no more nodes are expected, but one was found'); 97 } 98 my $st = RDF::Trine::Statement->new( @nodes[0,1], $e ); 99 is_deeply( $next, $st, 'got expected statement by 2-bound query' ); 100 $count++; 101 } 102 is( $count, 2, 'iterator had 2 expected element (after adding new data)' ); 103} 104 105{ 106 my @nodes = ( 107 RDF::Trine::Node::Resource->new('http://example.org/foo'), 108 undef, 109 undef, 110 ); 111 my @expect = ( 112 [ RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('23', undef, 'http://www.w3.org/2001/XMLSchema#integer') ], 113 [ RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('24', undef, 'http://www.w3.org/2001/XMLSchema#integer') ], 114 [ RDF::Trine::Node::Resource->new('http://example.org/baz'), RDF::Trine::Node::Literal->new('quux') ], 115 ); 116 my $iter = $store->get_statements( @nodes ); 117 isa_ok( $iter, 'RDF::Trine::Iterator' ); 118 my $count = 0; 119 while (my $next = $iter->next) { 120 my $e = shift(@expect); 121 unless (ref($e)) { 122 fail('no more nodes are expected, but one was found'); 123 } 124 my $st = RDF::Trine::Statement->new( $nodes[0], @$e ); 125 is_deeply( $next, $st, 'got expected statement by 1-bound query' ); 126 $count++; 127 } 128 is( $count, 3, 'iterator had 3 expected element' ); 129} 130 131{ 132 my @expect = ( 133 [ RDF::Trine::Node::Resource->new('http://example.org/foo'), RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('23', undef, 'http://www.w3.org/2001/XMLSchema#integer') ], 134 [ RDF::Trine::Node::Resource->new('http://example.org/foo'), RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('24', undef, 'http://www.w3.org/2001/XMLSchema#integer') ], 135 [ RDF::Trine::Node::Resource->new('http://example.org/foo'), RDF::Trine::Node::Resource->new('http://example.org/baz'), RDF::Trine::Node::Literal->new('quux') ], 136 [ RDF::Trine::Node::Resource->new('http://example.org/zzz'), RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('999', undef, 'http://www.w3.org/2001/XMLSchema#integer') ], 137 ); 138 my $iter = $store->get_statements(); 139 isa_ok( $iter, 'RDF::Trine::Iterator' ); 140 my $count = 0; 141 while (my $next = $iter->next) { 142 my $e = shift(@expect); 143 unless (ref($e)) { 144 fail('no more nodes are expected, but one was found'); 145 } 146 my $st = RDF::Trine::Statement->new( @$e ); 147 is_deeply( $next, $st, 'got expected statement by all wildcard query' ); 148 $count++; 149 } 150 is( $count, 4, 'iterator had 4 expected element' ); 151} 152 153 154is( $store->count_statements( RDF::Trine::Node::Resource->new('http://example.org/foo') ), 3, 'count_statements(bff) returned 3' ); 155is( $store->count_statements( RDF::Trine::Node::Resource->new('http://example.org/zzz') ), 1, 'count_statements(bff) returned 1' ); 156is( $store->count_statements( undef, RDF::Trine::Node::Resource->new('http://example.org/bar'), undef ), 3, 'count_statements(fbf) returned 3' ); 157is( $store->count_statements( RDF::Trine::Node::Resource->new('http://example.org/foo'), RDF::Trine::Node::Resource->new('http://example.org/bar'), undef ), 2, 'count_statements(bbf) returned 2' ); 158is( $store->count_statements( RDF::Trine::Node::Resource->new('http://example.org/foo'), RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('24', undef, 'http://www.w3.org/2001/XMLSchema#integer') ), 1, 'count_statements(bbb) returned 1' ); 159is( $store->count_statements( RDF::Trine::Node::Resource->new('http://example.org/foo'), RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('12345', undef, 'http://www.w3.org/2001/XMLSchema#integer') ), 0, 'count_statements(bbb) returned 0' ); 160is( $store->count_statements, 4, 'count_statements(fff) returned expected 4' ); 161 162$store->remove_statement( RDF::Trine::Statement->new( RDF::Trine::Node::Resource->new('http://example.org/foo'), RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('23', undef, 'http://www.w3.org/2001/XMLSchema#integer') ) ); 163 164{ 165 my @expect = ( 166 [ RDF::Trine::Node::Resource->new('http://example.org/foo'), RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('24', undef, 'http://www.w3.org/2001/XMLSchema#integer') ], 167 [ RDF::Trine::Node::Resource->new('http://example.org/foo'), RDF::Trine::Node::Resource->new('http://example.org/baz'), RDF::Trine::Node::Literal->new('quux') ], 168 [ RDF::Trine::Node::Resource->new('http://example.org/zzz'), RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('999', undef, 'http://www.w3.org/2001/XMLSchema#integer') ], 169 ); 170 my $iter = $store->get_statements(); 171 isa_ok( $iter, 'RDF::Trine::Iterator' ); 172 my $count = 0; 173 while (my $next = $iter->next) { 174 my $e = shift(@expect); 175 unless (ref($e)) { 176 fail('no more nodes are expected, but one was found'); 177 } 178 my $st = RDF::Trine::Statement->new( @$e ); 179 is_deeply( $next, $st, 'got expected statement by all wildcard query (after removing a statement)' ); 180 $count++; 181 } 182 is( $count, 3, 'iterator had 3 expected element' ); 183} 184 185$store->remove_statement( RDF::Trine::Statement->new( RDF::Trine::Node::Resource->new('http://example.org/foo'), RDF::Trine::Node::Resource->new('http://example.org/baz'), RDF::Trine::Node::Literal->new('quux') ) ); 186 187{ 188 my @expect = ( 189 [ RDF::Trine::Node::Resource->new('http://example.org/foo'), RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('24', undef, 'http://www.w3.org/2001/XMLSchema#integer') ], 190 [ RDF::Trine::Node::Resource->new('http://example.org/zzz'), RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('999', undef, 'http://www.w3.org/2001/XMLSchema#integer') ], 191 ); 192 my $iter = $store->get_statements(); 193 isa_ok( $iter, 'RDF::Trine::Iterator' ); 194 my $count = 0; 195 while (my $next = $iter->next) { 196# use Data::Dumper; 197# warn Dumper($next); 198 my $e = shift(@expect); 199 unless (ref($e)) { 200 fail('no more nodes are expected, but one was found'); 201 } 202 my $st = RDF::Trine::Statement->new( @$e ); 203 is_deeply( $next, $st, 'got expected statement by all wildcard query (after removing a second statement)' ); 204 $count++; 205 } 206 is( $count, 2, 'iterator had 2 expected element' ); 207} 208 209 210{ 211 is($store->size, 2, 'pre-nuke size test shows 2 statements in store'); 212 $store->nuke; 213 is($store->size, 0, 'post-nuke size test shows 0 statements in store'); 214 my $count = $store->count_statements( undef, undef, undef, undef ); 215 is($count, 0, 'post-nuke count test shows 0 statements in store'); 216 my $iter = $store->get_statements( undef, undef, undef, undef ); 217 my $next = $iter->next; 218 ok(! defined($next), 'Iterator gave no result' ); 219} 220 221 222 223################ 224 225sub _add_rdf { 226 my $store = shift; 227 my $data = shift; 228 my $base = shift || 'http://example.org/'; 229 my $parser = RDF::Trine::Parser::RDFXML->new(); 230 $parser->parse_into_model( $base, $data, $store ); 231} 232 233