File: model.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 (335 lines) | stat: -rw-r--r-- 11,878 bytes parent folder | download | duplicates (5)
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
use Test::More;
use Test::Exception;

use utf8;
use strict;
use warnings;
no warnings 'redefine';

use DBI;
use RDF::Trine qw(literal);
use RDF::Trine::Model;
use RDF::Trine::Node;
use RDF::Trine::Pattern;
use RDF::Trine::Namespace;
use RDF::Trine::Store::DBI;
use RDF::Trine::Statement;
use File::Temp qw(tempfile);

my $rdf		= RDF::Trine::Namespace->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
my $foaf	= RDF::Trine::Namespace->new('http://xmlns.com/foaf/0.1/');
my $xsd		= RDF::Trine::Namespace->new('http://www.w3.org/2001/XMLSchema#');
my $kasei	= RDF::Trine::Namespace->new('http://kasei.us/');
my $b		= RDF::Trine::Node::Blank->new();
my $p		= RDF::Trine::Node::Resource->new('http://kasei.us/about/foaf.xrdf#greg');
my $intval	= RDF::Trine::Node::Literal->new('23',undef,$xsd->int);
my $langval	= RDF::Trine::Node::Literal->new('gwilliams','en');
my $st0		= RDF::Trine::Statement->new( $p, $rdf->type, $foaf->Person );
my $st1		= RDF::Trine::Statement->new( $p, $foaf->name, RDF::Trine::Node::Literal->new('Gregory Todd Williams') );
my $st2		= RDF::Trine::Statement->new( $b, $rdf->type, $foaf->Person );
my $st3		= RDF::Trine::Statement->new( $b, $foaf->name, RDF::Trine::Node::Literal->new('Eve') );
my $st4		= RDF::Trine::Statement->new( $p, $foaf->knows, $b );
my $st5		= RDF::Trine::Statement->new( $p, $foaf->nick, $langval );
my $st6		= RDF::Trine::Statement->new( $p, $foaf->age, $intval);

my ($stores, $remove)	= stores();

plan tests => 7 + 85 * scalar(@$stores);

print "### Testing auto-creation of store\n";
isa_ok( RDF::Trine::Model->new( 'Memory' ), 'RDF::Trine::Model' );

foreach my $store (@$stores) {
	print "### Testing store " . ref($store) . "\n";
	isa_ok( $store, 'RDF::Trine::Store' );
	my $model	= RDF::Trine::Model->new( $store );
	isa_ok( $model, 'RDF::Trine::Model' );
	$model->add_statement( $_ ) for ($st0, $st1, $st2, $st3);
	
	{
		is( $model->count_statements(), 4, 'model size' );
		$model->add_statement( $_ ) for ($st0);
		is( $model->count_statements(), 4, 'model size after duplicate statements' );
		is( $model->count_statements( undef, $foaf->name, undef ), 2, 'count of foaf:name statements' );
	}
	
	{
		my $stream	= $model->get_statements( $p, $foaf->name, RDF::Trine::Node::Variable->new('name') );
		my $st		= $stream->next;
		is_deeply( $st, $st1, 'foaf:name statement' );
		is( $stream->next, undef, 'end-of-stream' );
	}
	
	{
		throws_ok {
			my $iter	= $model->get_statements('<foo>');
		} 'RDF::Trine::Error::MethodInvocationError', 'get_statements called with non-object argument';
		
	}
	
	{
		throws_ok {
			$model->add_statement($p, $rdf->type, $foaf->Person);
		} 'RDF::Trine::Error::MethodInvocationError', 'add_statement called with 3 nodes, not a statement';
		throws_ok {
			$model->add_statement($p, $rdf->type, $foaf->Person, $p);
		} 'RDF::Trine::Error::MethodInvocationError', 'add_statement called with 4 nodes, not a statement';
		throws_ok {
			$model->add_statement('http://example.org/subject', 'http://example.org/predicate', 'String');
		} 'RDF::Trine::Error::MethodInvocationError', 'add_statement called with strings, not a statement';
	}


	{
		my $stream	= $model->get_statements( $b, $foaf->name, RDF::Trine::Node::Variable->new('name') );
		my $st		= $stream->next;
		is_deeply( $st, $st3, 'foaf:name statement (with bnode in triple)' );
		is( $stream->next, undef, 'end-of-stream' );
	}
	
	{
		my $stream	= $model->get_statements( RDF::Trine::Node::Variable->new('p'), $foaf->name, RDF::Trine::Node::Literal->new('Gregory Todd Williams') );
		my $st		= $stream->next;
		is_deeply( $st, $st1, 'foaf:name statement (with literal in triple)' );
		is( $stream->next, undef, 'end-of-stream' );
	}

	{
		my $stream	= $model->get_statements( RDF::Trine::Node::Variable->new('p'), $foaf->name, RDF::Trine::Node::Variable->new('name') );
		my $count	= 0;
		while (my $st = $stream->next) {
			my $subj	= $st->subject;
			isa_ok( $subj, 'RDF::Trine::Node' );
			$count++;
		}
		is( $count, 2, 'expected result count (2 people) 1' );
	}
	
	{
		my $p1		= RDF::Trine::Statement->new( RDF::Trine::Node::Variable->new('p'), $rdf->type, $foaf->Person );
		my $p2		= RDF::Trine::Statement->new( RDF::Trine::Node::Variable->new('p'), $foaf->name, RDF::Trine::Node::Variable->new('name') );
		my $pattern	= RDF::Trine::Pattern->new( $p1, $p2 );
		
		{
			my $stream	= $model->get_pattern( $pattern );
			my $count	= 0;
			while (my $b = $stream->next) {
				isa_ok( $b, 'HASH' );
				isa_ok( $b->{p}, 'RDF::Trine::Node', 'node person' );
				isa_ok( $b->{name}, 'RDF::Trine::Node::Literal', 'literal name' );
				like( $b->{name}->literal_value, qr/Eve|Gregory/, 'name pattern' );
				$count++;
			}
			is( $count, 2, 'expected result count (2 people) 2' );
		}
	
		{
			my $stream	= $model->get_pattern( $pattern, undef, orderby => [ 'name', 'ASC' ] );
			is_deeply( [ $stream->sorted_by ], ['name', 'ASC'], 'results sort order' );
			my $count	= 0;
			my @expect	= ('Eve', 'Gregory Todd Williams');
			while (my $b = $stream->next) {
				isa_ok( $b, 'HASH' );
				isa_ok( $b->{p}, 'RDF::Trine::Node', 'node person' );
				my $name	= shift(@expect);
				is( $b->{name}->literal_value, $name, 'name pattern' );
				$count++;
			}
			is( $count, 2, 'expected result count (2 people) 3' );
		}

		{
			my $stream	= $model->get_pattern( $pattern, undef, orderby => [ qw(name DESC p ASC) ] );
			is_deeply( [ $stream->sorted_by ], ['name', 'DESC', 'p', 'ASC'], 'results sort order' );
			my $count	= 0;
			my @expect	= ('Gregory Todd Williams', 'Eve');
			while (my $b = $stream->next) {
				isa_ok( $b, 'HASH' );
				isa_ok( $b->{p}, 'RDF::Trine::Node', 'node person' );
				my $name	= shift(@expect);
				is( $b->{name}->literal_value, $name, 'name pattern' );
				$count++;
			}
			is( $count, 2, 'expected result count (2 people) 4' );
		}

		{
			my $stream	= $model->get_pattern( $pattern, undef, orderby => [ 'date', 'ASC' ] );
			is_deeply( [ $stream->sorted_by ], [], 'results sort order for unknown binding' );
		}
		
		{
			throws_ok {
				my $stream	= $model->get_pattern( $pattern, undef, orderby => [ 'name' ] );
			} 'RDF::Trine::Error::MethodInvocationError', 'bad ordering request throws exception';
		}
	}
	
	{
		my $stream	= $model->get_pattern( $st0 );
		my $empty	= $stream->next;
		is_deeply( $empty, RDF::Trine::VariableBindings->new({}), 'empty binding on no-variable pattern' );
		is( $stream->next, undef, 'end-of-stream' );
	}
	
	{
		my $stream	= $model->as_stream();
		isa_ok( $stream, 'RDF::Trine::Iterator::Graph' );
		my $count	= 0;
		while (my $st = $stream->next) {
			my $p	= $st->predicate;
			like( $p->uri_value, qr<(#type|/name)$>, 'as_stream statement' );
			$count++;
		}
		is( $count, 4, 'expected model statement count (4)' );
	}
	
	{
		{
			my @subj	= $model->subjects( $rdf->type );
			my @preds	= $model->predicates( $p );
			my @objs	= $model->objects( $p );
			is( scalar(@subj), 2, "expected subject count on rdf:type" );
			is( scalar(@preds), 2, "expected predicate count on " . $p->uri_value );
			is( scalar(@objs), 2, "expected objects count on " . $p->uri_value );
		}
		{
			my @subjs	= $model->subjects( $foaf->name, literal('Eve') );
			my @preds	= $model->predicates( $p, $foaf->Person );
			my @objs	= $model->objects( $p, $rdf->type );
			is( scalar(@subjs), 1, "expected subject count on rdf:type" );
			ok( $subjs[0]->isa('RDF::Trine::Node::Blank'), 'expected subject' );
			is( scalar(@preds), 1, "expected predicate count on " . $p->uri_value );
			ok( $preds[0]->equal( $rdf->type ), 'expected predicate' );
			is( scalar(@objs), 1, "expected objects count on " . $p->uri_value );
			ok( $objs[0]->equal( $foaf->Person ), 'expected object' );
		}
		{
			my $subjs	= $model->subjects( $rdf->type );
			my $preds	= $model->predicates( $p );
			my $objs	= $model->objects( $p );
			isa_ok( $subjs, 'RDF::Trine::Iterator', 'expected iterator from subjects()' );
			isa_ok( $preds, 'RDF::Trine::Iterator', 'expected iterator from predicates()' );
			isa_ok( $objs, 'RDF::Trine::Iterator', 'expected iterator from objects()' );
		}
	}
	
	{
		my $st5		= RDF::Trine::Statement->new( $p, $foaf->name, RDF::Trine::Node::Literal->new('グレゴリ ウィリアムス', 'jp') );
		$model->add_statement( $st5 );
		
		my $pattern	= RDF::Trine::Statement->new( $p, $foaf->name, RDF::Trine::Node::Variable->new('name') );
		my $stream	= $model->get_pattern( $pattern );
		my $count	= 0;
		while (my $b = $stream->next) {
			isa_ok( $b, 'HASH' );
			isa_ok( $b->{name}, 'RDF::Trine::Node::Literal', 'literal name' );
			my $value	= $b->{name}->literal_value;
			like( $value, qr/Gregory|グレゴリ/, 'name pattern with language-tagged result' );
			$count++;
		}
		is( $count, 2, 'expected result count (2 names)' );
		is( $model->count_statements(), 5, 'model size' );
		$model->remove_statement( $st5 );
		is( $model->count_statements(), 4, 'model size after remove_statement' );
	}
	
	{
		my $st6		= RDF::Trine::Statement->new( $p, $foaf->name, RDF::Trine::Node::Literal->new('Gregory Todd Williams', undef, 'http://www.w3.org/2000/01/rdf-schema#Literal') );
		$model->add_statement( $st6 );
		
		my $pattern	= RDF::Trine::Statement->new( $p, $foaf->name, RDF::Trine::Node::Variable->new('name') );
		my $stream	= $model->get_pattern( $pattern );
		my $count	= 0;
		my $dt		= 0;
		while (my $b = $stream->next) {
			my $name	= $b->{name};
			isa_ok( $b, 'HASH' );
			isa_ok( $name, 'RDF::Trine::Node::Literal', 'literal name' );
			is( $name->literal_value, 'Gregory Todd Williams', 'name pattern with datatyped result' );
			if (my $type = $name->literal_datatype) {
				is( $type, 'http://www.w3.org/2000/01/rdf-schema#Literal', 'datatyped literal' );
				$dt++;
			}
			$count++;
		}
		is( $count, 2, 'expected result count (2 names)' );
		is( $dt, 1, 'expected result count (1 datatyped literal)' );
	}
	
	{
		$model->remove_statements( $p );
		is( $model->count_statements(), 2, 'model size after remove_statements' );
	}
	
	{
		throws_ok {
			my $pattern	= RDF::Trine::Pattern->new();
			my $stream	= $model->get_pattern( $pattern );
		} 'RDF::Trine::Error::CompilationError', 'empty GGP throws exception';
	}
}

foreach my $file (@$remove) {
	unlink( $file );
}

{ # test optional parameters of RDF::Trine::Model::objects
	my $model = RDF::Trine::Model->new;
	$model->add_statement( $_ ) for ($st0, $st1, $st3, $st4, $st5, $st6);
	my %types = (blank => 1, literal => 3, resource => 1);
	while (my ($type,$count) = each(%types)) {
		my @objs	= $model->objects( $p, undef, type => $type );
		is( scalar(@objs), $count, "expected objects count on type $type");
	}
	my @objs	= $model->objects( $p, undef, language => 'en' );
	ok( $objs[0]->equal( $langval ), 'expected integer value as object' );
	foreach my $dt ( $xsd->int, $xsd->int->uri_value ) { 
		@objs	= $model->objects( $p, undef, datatype => $dt );
		ok( $objs[0]->equal( $intval ), 'expected integer value as object' );
	}
}


sub stores {
	my @stores;
	my @removeme;
	push(@stores, RDF::Trine::Store::Memory->temporary_store());
	
	{
		my $store	= RDF::Trine::Store::DBI->new();
		$store->init();
		push(@stores, $store);
	}
	
	{
		my ($fh, $filename) = tempfile();
		undef $fh;
		my $dbh		= DBI->connect( "dbi:SQLite:dbname=${filename}", '', '' );
		my $store	= RDF::Trine::Store::DBI->new( 'model', $dbh );
		$store->init();
		push(@stores, $store);
		push(@removeme, $filename);
	}
	
	{
		my ($fh, $filename) = tempfile();
		undef $fh;
		my $dsn		= "dbi:SQLite:dbname=${filename}";
		my $store	= RDF::Trine::Store::DBI->new( 'model', $dsn, '', '' );
		$store->init();
		push(@stores, $store);
		push(@removeme, $filename);
	}
	return (\@stores, \@removeme);
}

sub debug {
	my $store	= shift;
	my $dbh		= $store->dbh;
	my $sth		= $dbh->prepare( "SELECT * FROM Statements15799945864759145248" );
	$sth->execute();
	while (my $row = $sth->fetchrow_hashref) {
		warn Dumper($row);
	}
}