File: sparql11-aggregates.t

package info (click to toggle)
librdf-query-perl 2.919-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,580 kB
  • sloc: perl: 30,628; javascript: 131; sh: 13; makefile: 2
file content (458 lines) | stat: -rw-r--r-- 14,176 bytes parent folder | download | duplicates (7)
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
use strict;
use warnings;
no warnings 'redefine';
use Test::More;
use Test::Exception;
use Scalar::Util qw(blessed);
use RDF::Trine qw(literal);

use lib qw(. t);
require "models.pl";

use RDF::Query;

################################################################################
# Log::Log4perl::init( \q[
# 	log4perl.category.rdf.query.plan.exists          = TRACE, Screen
# 	
# 	log4perl.appender.Screen         = Log::Log4perl::Appender::Screen
# 	log4perl.appender.Screen.stderr  = 0
# 	log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout
# ] );
################################################################################

my @files	= map { "data/$_" } qw(t-sparql11-aggregates-1.rdf foaf.xrdf about.xrdf);
my @models	= test_models( @files );
my $tests	= (scalar(@models) * 91);
plan tests => $tests;

foreach my $model (@models) {
	print "\n#################################\n";
	print "### Using model: $model\n\n";

	
	{
		print "# SELECT SUM aggregate with GROUP BY and HAVING\n";
		my $query	= new RDF::Query ( <<"END", { lang => 'sparql11' } );
	PREFIX : <http://books.example/>
	SELECT (SUM(?lprice) AS ?totalPrice)
	WHERE {
	  ?org :affiliates ?auth .
	  ?auth :writesBook ?book .
	  ?book :price ?lprice .
	}
	GROUP BY ?org
	HAVING (SUM(?lprice) > 10)
END
		warn RDF::Query->error unless ($query);
		my ($plan, $ctx)	= $query->prepare( $model );
		my $pattern			= $query->pattern;
		my $stream	= $query->execute_plan( $plan, $ctx );
		isa_ok( $stream, 'RDF::Trine::Iterator' );
		my $count	= 0;
		while (my $row = $stream->next) {
			$count++;
			my $tp	= $row->{totalPrice};
			isa_ok( $tp, 'RDF::Trine::Node::Literal', 'got ?totalPrice value' );
			is( $tp->literal_value, '21', 'expected literal value' );
			is( $tp->literal_datatype, 'http://www.w3.org/2001/XMLSchema#integer', 'expected integer datatype' );
		}
		is( $count, 1, 'expected result count with aggregation' );
	}

	{
		print "# SELECT GROUPED Variable with GROUP BY and HAVING\n";
		my $query	= new RDF::Query ( <<"END", { lang => 'sparql11' } );
	PREFIX : <http://books.example/>
	SELECT ?org
	WHERE {
	  ?org :affiliates ?auth .
	  ?auth :writesBook ?book .
	  ?book :price ?lprice .
	}
	GROUP BY ?org
	HAVING (SUM(?lprice) > 10)
END
		warn RDF::Query->error unless ($query);
		my ($plan, $ctx)	= $query->prepare( $model );
		my $pattern			= $query->pattern;
		my $stream	= $query->execute_plan( $plan, $ctx );
		isa_ok( $stream, 'RDF::Trine::Iterator' );
		my $count	= 0;
		while (my $row = $stream->next) {
			$count++;
			my $org	= $row->{org};
			isa_ok( $org, 'RDF::Trine::Node::Resource', 'got ?org value' );
		}
		is( $count, 1, 'expected result count with aggregation' );
	}

	{
		print "# SELECT MIN with GROUP BY\n";
		my $query	= new RDF::Query ( <<"END", { lang => 'sparql11' } );
	PREFIX : <http://books.example/>
	SELECT ?auth (MIN(?lprice) AS ?min)
	WHERE {
	  ?org :affiliates ?auth .
	  ?auth :writesBook ?book .
	  ?book :price ?lprice .
	}
	GROUP BY ?auth
END
		warn RDF::Query->error unless ($query);
		my ($plan, $ctx)	= $query->prepare( $model );
		my $pattern			= $query->pattern;
		my $stream	= $query->execute_plan( $plan, $ctx );
		isa_ok( $stream, 'RDF::Trine::Iterator' );
		my $count	= 0;
		my %expect	= (
			'http://books.example/auth1'	=> 5,
			'http://books.example/auth2'	=> 7,
			'http://books.example/auth3'	=> 7,
		);
		while (my $row = $stream->next) {
			$count++;
			my $auth	= $row->{auth};
			my $val		= $row->{min};
			isa_ok( $auth, 'RDF::Trine::Node::Resource', 'got ?auth value' );
			isa_ok( $val, 'RDF::Trine::Node::Literal', 'got ?min value' );
			my $expect	= $expect{ $auth->uri_value };
			cmp_ok( $val->literal_value, '==', $expect, 'expected MIN value' );
		}
		is( $count, 3, 'expected result count with aggregation' );
	}

	{
		print "# SELECT MAX with GROUP BY\n";
		my $query	= new RDF::Query ( <<"END", { lang => 'sparql11' } );
	PREFIX : <http://books.example/>
	SELECT ?auth (MAX(?lprice) AS ?max)
	WHERE {
	  ?org :affiliates ?auth .
	  ?auth :writesBook ?book .
	  ?book :price ?lprice .
	}
	GROUP BY ?auth
END
		warn RDF::Query->error unless ($query);
		my ($plan, $ctx)	= $query->prepare( $model );
		my $pattern			= $query->pattern;
		my $stream	= $query->execute_plan( $plan, $ctx );
		isa_ok( $stream, 'RDF::Trine::Iterator' );
		my $count	= 0;
		my %expect	= (
			'http://books.example/auth1'	=> 9,
			'http://books.example/auth2'	=> 7,
			'http://books.example/auth3'	=> 7,
		);
		while (my $row = $stream->next) {
			$count++;
			my $auth	= $row->{auth};
			my $val		= $row->{max};
			isa_ok( $auth, 'RDF::Trine::Node::Resource', 'got ?auth value' );
			isa_ok( $val, 'RDF::Trine::Node::Literal', 'got ?max value' );
			my $expect	= $expect{ $auth->uri_value };
			cmp_ok( $val->literal_value, '==', $expect, 'expected MAX value' );
		}
		is( $count, 3, 'expected result count with aggregation' );
	}

	{
		print "# SELECT MAX with GROUP BY and ORDER BY DESC\n";
		my $query	= new RDF::Query ( <<"END", { lang => 'sparql11' } );
	PREFIX : <http://books.example/>
	SELECT ?auth (MAX(?lprice) AS ?max)
	WHERE {
	  ?org :affiliates ?auth .
	  ?auth :writesBook ?book .
	  ?book :price ?lprice .
	}
	GROUP BY ?auth
	ORDER BY DESC(?max)
END
		warn RDF::Query->error unless ($query);
		my ($plan, $ctx)	= $query->prepare( $model );
		my $pattern			= $query->pattern;
		my $stream	= $query->execute_plan( $plan, $ctx );
		isa_ok( $stream, 'RDF::Trine::Iterator' );
		my $count	= 0;
		my @expect	= (9, 7, 7);
		while (my $row = $stream->next) {
			$count++;
			my $val		= $row->{max};
			isa_ok( $val, 'RDF::Trine::Node::Literal', 'got ?max value' );
			my $expect	= shift(@expect);
			cmp_ok( $val->literal_value, '==', $expect, 'expected DESC MAX value' );
		}
		is( $count, 3, 'expected result count with aggregation' );
	}

	{
		print "# SELECT MAX with GROUP BY and ORDER BY ASC\n";
		my $query	= new RDF::Query ( <<"END", { lang => 'sparql11' } );
	PREFIX : <http://books.example/>
	SELECT ?auth (MAX(?lprice) AS ?max)
	WHERE {
	  ?org :affiliates ?auth .
	  ?auth :writesBook ?book .
	  ?book :price ?lprice .
	}
	GROUP BY ?auth
	ORDER BY ASC(?max)
END
		warn RDF::Query->error unless ($query);
		my ($plan, $ctx)	= $query->prepare( $model );
		my $pattern			= $query->pattern;
		my $stream	= $query->execute_plan( $plan, $ctx );
		isa_ok( $stream, 'RDF::Trine::Iterator' );
		my $count	= 0;
		my @expect	= (7, 7, 9);
		while (my $row = $stream->next) {
			$count++;
			my $val		= $row->{max};
			isa_ok( $val, 'RDF::Trine::Node::Literal', 'got ?max value' );
			my $expect	= shift(@expect);
			cmp_ok( $val->literal_value, '==', $expect, 'expected ASC MAX value' );
		}
		is( $count, 3, 'expected result count with aggregation' );
	}
	
	{
		print "# SELECT COUNT(VAR)\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql11' );
			PREFIX exif: <http://www.kanzaki.com/ns/exif#>
			PREFIX foaf: <http://xmlns.com/foaf/0.1/>
			SELECT (COUNT(?aperture) AS ?count)
			WHERE {
				?image a foaf:Image ; exif:fNumber ?aperture
			}
END
		isa_ok( $query, 'RDF::Query' ) or warn RDF::Query->error;
		
		my $stream	= $query->execute( $model );
		my $count	= 0;
		while (my $row = $stream->next) {
			my $expect	= RDF::Query::VariableBindings->new({ count => literal('4', undef, 'http://www.w3.org/2001/XMLSchema#integer') });
			is_deeply( $row, $expect, 'value for count apertures' );
			$count++;
		}
		is( $count, 1, 'one aggreate row' );
	}
	
	{
		print "# SELECT COUNT(DISTINCT VAR)\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql11' );
			PREFIX exif: <http://www.kanzaki.com/ns/exif#>
			PREFIX foaf: <http://xmlns.com/foaf/0.1/>
			SELECT (COUNT(DISTINCT ?aperture) AS ?count)
			WHERE {
				?image a foaf:Image ; exif:fNumber ?aperture
			}
END
		isa_ok( $query, 'RDF::Query' );
		
		my $stream	= $query->execute( $model );
		my $count	= 0;
		while (my $row = $stream->next) {
			my $expect	= RDF::Query::VariableBindings->new({ count => literal('2', undef, 'http://www.w3.org/2001/XMLSchema#integer') });
			is_deeply( $row, $expect, 'value for count distinct apertures' );
			$count++;
		}
		is( $count, 1, 'one aggreate row' );
	}
	
	{
		print "# SELECT MIN(STR)\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql11' );
			PREFIX foaf: <http://xmlns.com/foaf/0.1/>
			SELECT (MIN(?mbox) AS ?min)
			WHERE {
				[ a foaf:Person ; foaf:mbox_sha1sum ?mbox ]
			}
END
		isa_ok( $query, 'RDF::Query' );
		
		my $stream	= $query->execute( $model );
		my $count	= 0;
		while (my $row = $stream->next) {
			my $expect	= RDF::Query::VariableBindings->new({ min => literal('19fc9d0234848371668cf10a1b71ac9bd4236806') });
			is_deeply( $row, $expect, 'value for min mbox_sha1sum' );
			$count++;
		}
		is( $count, 1, 'one aggreate row' );
	}
	
	{
		print "# SELECT COUNT(VAR)\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql11' );
			PREFIX foaf: <http://xmlns.com/foaf/0.1/>
			SELECT (COUNT(?nick) AS ?count)
			WHERE {
				?p a foaf:Person .
				OPTIONAL {
					?p foaf:nick ?nick
				}
			}
END
		isa_ok( $query, 'RDF::Query' );
		my $stream	= $query->execute( $model );
		my $count	= 0;
		while (my $row = $stream->next) {
			my $expect	= RDF::Query::VariableBindings->new({ count => RDF::Query::Node::Literal->new('3', undef, 'http://www.w3.org/2001/XMLSchema#integer') });
			is_deeply( $row, $expect, 'COUNT() on sometimes unbound variable' );
			$count++;
		}
		is( $count, 1, 'one aggreate row' );
	}
	
	{
		print "# SELECT COUNT(VAR) with GROUP BY\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql11' );
			PREFIX foaf: <http://xmlns.com/foaf/0.1/>
			SELECT ?name (COUNT(?nick) AS ?count)
			WHERE {
				?p a foaf:Person ;
					foaf:name ?name;
					foaf:nick ?nick .
			}
			GROUP BY ?name
END
		isa_ok( $query, 'RDF::Query' );
		my $stream	= $query->execute( $model );
		my $count	= 0;
		
		my %expect	= ( 'Gregory Todd Williams' => 2, 'Gary P' => 1 );
		while (my $row = $stream->next) {
			my $name	= $row->{name}->literal_value;
			my $expect	= $expect{ $name };
			cmp_ok( $row->{count}->literal_value, '==', $expect, 'expected COUNT() value for variable GROUP' );
			$count++;
		}
		is( $count, 2, 'two aggreate groups' );
	}
	
	{
		print "# SELECT AVG(STR)\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql11' );
			PREFIX exif: <http://www.kanzaki.com/ns/exif#>
			SELECT (AVG(?f) AS ?avg)
			WHERE {
				?image exif:fNumber ?f
			}
END
		isa_ok( $query, 'RDF::Query' );
		
		my $stream	= $query->execute( $model );
		my $count	= 0;
		while (my $row = $stream->next) {
			my $value	= (values %$row)[0];
			isa_ok( $value, 'RDF::Query::Node::Literal' );
			ok( $value->is_numeric_type, 'avg produces a numeric type' );
			is( $value->numeric_value, 6.125, 'expected average value' );
			$count++;
		}
		is( $count, 1, 'one aggreate row' );
	}
	
	{
		print "# SELECT MIN(STR), MAX(STR)\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql11' );
			PREFIX foaf: <http://xmlns.com/foaf/0.1/>
			PREFIX exif: <http://www.kanzaki.com/ns/exif#>
			SELECT (MIN(?e) AS ?min) (MAX(?e) AS ?max)
			WHERE {
				[] foaf:mbox_sha1sum ?e
			}
END
		isa_ok( $query, 'RDF::Query' );
		
		my $stream	= $query->execute( $model );
		my $count	= 0;
		while (my $row = $stream->next) {
			my $min	= $row->{min};
			my $max	= $row->{max};
			isa_ok( $min, 'RDF::Query::Node::Literal' );
			isa_ok( $max, 'RDF::Query::Node::Literal' );
			is( $min->literal_value, '19fc9d0234848371668cf10a1b71ac9bd4236806', 'expected MIN plain-literal' );
			is( $max->literal_value, 'f8677979059b73385c9d14cadf7d1e3652b205a8', 'expected MAX plain-literal' );
			$count++;
		}
		is( $count, 1, 'one aggreate row' );
	}
	
	{
		print "# SELECT MIN(STR), MAX(STR)\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql11' );
			PREFIX dc: <http://purl.org/dc/elements/1.1/>
			SELECT (MIN(?d) AS ?min) (MAX(?d) AS ?max)
			WHERE {
				[] dc:date ?d
			}
END
		isa_ok( $query, 'RDF::Query' );
		
		# without strict errors, non-datatyped dates (in human readable form) will
		# be string-compared with dateTime-datatyped W3CDTF literals
		my $stream	= $query->execute( $model );
		my $count	= 0;
		while (my $row = $stream->next) {
			my $min	= $row->{min};
			my $max	= $row->{max};
			isa_ok( $min, 'RDF::Query::Node::Literal' );
			isa_ok( $max, 'RDF::Query::Node::Literal' );
 			is( $min->literal_value, '2004-09-06T15:19:20+01:00', 'expected MIN plain-literal' );
 			is( $max->literal_value, 'Sat, 4 Oct 2003 20:02:22 PDT-0700', 'expected MAX plain-literal' );
			$count++;
		}
		is( $count, 1, 'one aggreate row' );
	}
	
	{
		print "# SELECT GROUP_CONCAT(STR)\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql11' );
			PREFIX dc: <http://purl.org/dc/elements/1.1/>
			SELECT (GROUP_CONCAT(?d) AS ?dates)
			WHERE {
				[] dc:date ?d
			}
END
		isa_ok( $query, 'RDF::Query' );
		
		my $stream	= $query->execute( $model );
		my $count	= 0;
		while (my $row = $stream->next) {
			my $dates	= $row->{dates};
			isa_ok( $dates, 'RDF::Query::Node::Literal' );
			my $lit	= $dates->literal_value;
			like( $lit, qr/2004-09-06T15:19:20[+]01:00/, 'expected GROUP_CONCAT plain-literal' );
			like( $lit, qr/2005-04-07T18:27:37-04:00/, 'expected GROUP_CONCAT plain-literal' );
			like( $lit, qr/2005-04-07T18:27:50-04:00/, 'expected GROUP_CONCAT plain-literal' );
			like( $lit, qr/2005-04-07T18:27:56-04:00/, 'expected GROUP_CONCAT plain-literal' );
			like( $lit, qr/Sat, 4 Oct 2003 20:02:22 PDT-0700/, 'expected GROUP_CONCAT plain-literal' );
			$count++;
		}
		is( $count, 1, 'one aggreate row' );
	}
	
	{
		print "# SELECT SAMPLE(STR)\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql11' );
			PREFIX dc: <http://purl.org/dc/elements/1.1/>
			SELECT (SAMPLE(?d) AS ?date)
			WHERE {
				[] dc:date ?d
			}
END
		isa_ok( $query, 'RDF::Query' );
		
		my $stream	= $query->execute( $model );
		my $count	= 0;
		while (my $row = $stream->next) {
			my $date	= $row->{date};
			isa_ok( $date, 'RDF::Query::Node::Literal' );
 			is( $date->literal_value, '2004-09-06T15:19:20+01:00', 'expected SAMPLE plain-literal' );
			$count++;
		}
		is( $count, 1, 'one aggreate row' );
	}
}