File: store-hexastore.t

package info (click to toggle)
librdf-trine-perl 1.015-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,188 kB
  • ctags: 1,059
  • sloc: perl: 17,584; makefile: 30; sql: 20
file content (232 lines) | stat: -rw-r--r-- 10,154 bytes parent folder | download | duplicates (6)
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
use Test::More qw(no_plan); # tests => 183;
use Test::Exception;

use strict;
use warnings;
no warnings 'redefine';
use Scalar::Util qw(blessed);

use RDF::Trine;
use RDF::Trine::Store::Hexastore;

my $store	= RDF::Trine::Store::Hexastore->new();
isa_ok( $store, 'RDF::Trine::Store::Hexastore' );

_add_rdf( $store, <<"END" );
<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:eg="http://example.org/">
 <rdf:Description rdf:about="http://example.org/foo">
   <eg:bar rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">23</eg:bar>
 </rdf:Description>
</rdf:RDF>
END

{
	my @nodes	= (
		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'),
	);
	my $st		= RDF::Trine::Statement->new( @nodes );
	my $iter	= $store->get_statements( @nodes );
	isa_ok( $iter, 'RDF::Trine::Iterator' );
	my $next	= $iter->next;
	is_deeply( $next, $st, 'got expected statement by 3-bound query' );
}

{
	my @nodes	= (
		RDF::Trine::Node::Resource->new('http://example.org/foo'),
		RDF::Trine::Node::Resource->new('http://example.org/bar'),
		RDF::Trine::Node::Literal->new('00', undef, 'http://www.w3.org/2001/XMLSchema#integer'),
	);
	my $st		= RDF::Trine::Statement->new( @nodes );
	my $iter	= $store->get_statements( @nodes );
	isa_ok( $iter, 'RDF::Trine::Iterator' );
	my $next	= $iter->next;
	is_deeply( $next, undef, 'got expected empty statement iterator by 3-bound query' );
}

{
	my @nodes	= (
		RDF::Trine::Node::Resource->new('http://example.org/foo'),
		RDF::Trine::Node::Resource->new('http://example.org/bar'),
		undef,
	);
	my $st		= RDF::Trine::Statement->new( @nodes[0,1], RDF::Trine::Node::Literal->new('23', undef, 'http://www.w3.org/2001/XMLSchema#integer') );
	my $iter	= $store->get_statements( @nodes );
	isa_ok( $iter, 'RDF::Trine::Iterator' );
	my $count	= 0;
	while (my $next = $iter->next) {
		is_deeply( $next, $st, 'got expected statement by 2-bound query' );
		$count++;
	}
	is( $count, 1, 'iterator had expected single element' );
}

_add_rdf( $store, <<"END" );
<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:eg="http://example.org/">
 <rdf:Description rdf:about="http://example.org/zzz">
   <eg:bar rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">999</eg:bar>
 </rdf:Description>
 <rdf:Description rdf:about="http://example.org/foo">
   <eg:bar rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">24</eg:bar>
   <eg:baz>quux</eg:baz>
 </rdf:Description>
</rdf:RDF>
END

{
	my @nodes	= (
		RDF::Trine::Node::Resource->new('http://example.org/foo'),
		RDF::Trine::Node::Resource->new('http://example.org/bar'),
		undef,
	);
	my @expect	= (
		RDF::Trine::Node::Literal->new('23', undef, 'http://www.w3.org/2001/XMLSchema#integer'),
		RDF::Trine::Node::Literal->new('24', undef, 'http://www.w3.org/2001/XMLSchema#integer'),
	);
	my $iter	= $store->get_statements( @nodes );
	isa_ok( $iter, 'RDF::Trine::Iterator' );
	my $count	= 0;
	while (my $next = $iter->next) {
		my $e		= shift(@expect);
		unless (blessed($e)) {
			fail('no more nodes are expected, but one was found');
		}
		my $st		= RDF::Trine::Statement->new( @nodes[0,1], $e );
		is_deeply( $next, $st, 'got expected statement by 2-bound query' );
		$count++;
	}
	is( $count, 2, 'iterator had 2 expected element (after adding new data)' );
}

{
	my @nodes	= (
		RDF::Trine::Node::Resource->new('http://example.org/foo'),
		undef,
		undef,
	);
	my @expect	= (
		[ RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('23', undef, 'http://www.w3.org/2001/XMLSchema#integer') ],
		[ RDF::Trine::Node::Resource->new('http://example.org/bar'), RDF::Trine::Node::Literal->new('24', undef, 'http://www.w3.org/2001/XMLSchema#integer') ],
		[ RDF::Trine::Node::Resource->new('http://example.org/baz'), RDF::Trine::Node::Literal->new('quux') ],
	);
	my $iter	= $store->get_statements( @nodes );
	isa_ok( $iter, 'RDF::Trine::Iterator' );
	my $count	= 0;
	while (my $next = $iter->next) {
		my $e		= shift(@expect);
		unless (ref($e)) {
			fail('no more nodes are expected, but one was found');
		}
		my $st		= RDF::Trine::Statement->new( $nodes[0], @$e );
		is_deeply( $next, $st, 'got expected statement by 1-bound query' );
		$count++;
	}
	is( $count, 3, 'iterator had 3 expected element' );
}

{
	my @expect	= (
		[ 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') ],
		[ 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') ],
		[ RDF::Trine::Node::Resource->new('http://example.org/foo'), RDF::Trine::Node::Resource->new('http://example.org/baz'), RDF::Trine::Node::Literal->new('quux') ],
		[ 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') ],
	);
	my $iter	= $store->get_statements();
	isa_ok( $iter, 'RDF::Trine::Iterator' );
	my $count	= 0;
	while (my $next = $iter->next) {
		my $e		= shift(@expect);
		unless (ref($e)) {
			fail('no more nodes are expected, but one was found');
		}
		my $st		= RDF::Trine::Statement->new( @$e );
		is_deeply( $next, $st, 'got expected statement by all wildcard query' );
		$count++;
	}
	is( $count, 4, 'iterator had 4 expected element' );
}


is( $store->count_statements( RDF::Trine::Node::Resource->new('http://example.org/foo') ), 3, 'count_statements(bff) returned 3' );
is( $store->count_statements( RDF::Trine::Node::Resource->new('http://example.org/zzz') ), 1, 'count_statements(bff) returned 1' );
is( $store->count_statements( undef, RDF::Trine::Node::Resource->new('http://example.org/bar'), undef ), 3, 'count_statements(fbf) returned 3' );
is( $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' );
is( $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' );
is( $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' );
is( $store->count_statements, 4, 'count_statements(fff) returned expected 4' );

$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') ) );

{
	my @expect	= (
		[ 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') ],
		[ RDF::Trine::Node::Resource->new('http://example.org/foo'), RDF::Trine::Node::Resource->new('http://example.org/baz'), RDF::Trine::Node::Literal->new('quux') ],
		[ 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') ],
	);
	my $iter	= $store->get_statements();
	isa_ok( $iter, 'RDF::Trine::Iterator' );
	my $count	= 0;
	while (my $next = $iter->next) {
		my $e		= shift(@expect);
		unless (ref($e)) {
			fail('no more nodes are expected, but one was found');
		}
		my $st		= RDF::Trine::Statement->new( @$e );
		is_deeply( $next, $st, 'got expected statement by all wildcard query (after removing a statement)' );
		$count++;
	}
	is( $count, 3, 'iterator had 3 expected element' );
}

$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') ) );

{
	my @expect	= (
		[ 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') ],
		[ 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') ],
	);
	my $iter	= $store->get_statements();
	isa_ok( $iter, 'RDF::Trine::Iterator' );
	my $count	= 0;
	while (my $next = $iter->next) {
# 		use Data::Dumper;
# 		warn Dumper($next);
		my $e		= shift(@expect);
		unless (ref($e)) {
			fail('no more nodes are expected, but one was found');
		}
		my $st		= RDF::Trine::Statement->new( @$e );
		is_deeply( $next, $st, 'got expected statement by all wildcard query (after removing a second statement)' );
		$count++;
	}
	is( $count, 2, 'iterator had 2 expected element' );
}


{
	is($store->size, 2, 'pre-nuke size test shows 2 statements in store');
	$store->nuke;
	is($store->size, 0, 'post-nuke size test shows 0 statements in store');
	my $count	= $store->count_statements( undef, undef, undef, undef );
	is($count, 0, 'post-nuke count test shows 0 statements in store');
	my $iter	= $store->get_statements( undef, undef, undef, undef );
	my $next	= $iter->next;
	ok(! defined($next), 'Iterator gave no result' );
}



################

sub _add_rdf {
	my $store	= shift;
	my $data	= shift;
	my $base	= shift || 'http://example.org/';
	my $parser	= RDF::Trine::Parser::RDFXML->new();
	$parser->parse_into_model( $base, $data, $store );
}