File: resultforms.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 (227 lines) | stat: -rw-r--r-- 6,679 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
#!/usr/bin/env perl
use strict;
use warnings;
no warnings 'redefine';
use Test::More;

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

my @files	= map { "data/$_" } qw(about.xrdf foaf.xrdf);
my @models	= test_models( @files );
my $tests	= 1 + (scalar(@models) * 26);
plan tests => $tests;

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

	{
		my %seen;
		{
			print "# foaf:Person ORDER BY name with LIMIT\n";
			my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql' );
				PREFIX	foaf: <http://xmlns.com/foaf/0.1/>
				SELECT	DISTINCT ?p ?name
				WHERE	{
							?p a foaf:Person; foaf:name ?name .
							FILTER(lang(?name) = "")
						}
				ORDER BY ?name
				LIMIT 2
END
			my $stream	= $query->execute( $model );
			isa_ok( $stream, 'RDF::Trine::Iterator' );
			my ($count, $last);
			while (my $row = $stream->next) {
				my ($p, $node)	= @{ $row }{qw(p name)};
				my $name	= $node->literal_value;
				$seen{ $name }++;
				if (defined($last)) {
					cmp_ok( $name, 'ge', $last, "In order: $name (" . $p->as_string . ")" );
				} else {
					ok( $name, "First: $name (" . $p->as_string . ")" );
				}
				$last	= $name;
			} continue { ++$count };
			is( $count, 2, 'good LIMIT' );
		}
		
		{
			print "# foaf:Person ORDER BY name with LIMIT and OFFSET\n";
			my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql' );
				PREFIX	foaf: <http://xmlns.com/foaf/0.1/>
				SELECT	DISTINCT ?p ?name
				WHERE	{
							?p a foaf:Person; foaf:name ?name .
							FILTER(lang(?name) = "")
						}
				ORDER BY ?name
				LIMIT 2
				OFFSET 2
END
			my $stream	= $query->execute( $model );
			isa_ok( $stream, 'RDF::Trine::Iterator' );
			my ($count, $last);
			while (my $row = $stream->next) {
				my ($p, $node)	= @{ $row }{qw(p name)};
				my $name	= $node->literal_value;
				is( exists($seen{ $name }), '', "not seen before with offset: $name" );
				if (defined($last)) {
					cmp_ok( $name, 'ge', $last, "In order: $name (" . $p->as_string . ")" );
				} else {
					ok( $name, "First: $name (" . $p->as_string . ")" );
				}
				$last	= $name;
			} continue { ++$count };
			is( $count, 1, 'good LIMIT' );
		}
	}
	
	{
		print "# foaf:Person with LIMIT\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql' );
			PREFIX	foaf: <http://xmlns.com/foaf/0.1/>
			SELECT	?p ?name
			WHERE	{
						?p a foaf:Person; foaf:name ?name
					}
			LIMIT 2
END
		my $stream	= $query->execute( $model );
		isa_ok( $stream, 'RDF::Trine::Iterator' );
		my ($count);
		while (my $row = $stream->next) {
			my ($p, $node)	= @{ $row }{qw(p name)};
			my $name	= $node->literal_value;
			ok( $name, "First: $name (" . $p->as_string . ")" );
		} continue { ++$count };
		is( $count, 2, 'good LIMIT' );
	}
	
	{
		print "# foaf:Person with ORDER BY and OFFSET\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql' );
			PREFIX	foaf: <http://xmlns.com/foaf/0.1/>
			SELECT	DISTINCT ?p ?name
			WHERE	{
						?p a foaf:Person; foaf:nick ?nick; foaf:name ?name
					}
			ORDER BY ?name
			OFFSET 1
END
		my $stream	= $query->execute( $model );
		isa_ok( $stream, 'RDF::Trine::Iterator' );
		my ($count);
		while (my $row = $stream->next) {
			my ($p, $node)	= @{ $row }{qw(p name)};
			my $name	= $node->literal_value;
			ok( $name, "Got person with nick: $name (" . $p->as_string . ")" );
		} continue { ++$count };
		is( $count, 1, "Good DISTINCT with OFFSET" );
	}
	
	{
		print "# foaf:Image with OFFSET [2]\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql' );
			PREFIX	foaf: <http://xmlns.com/foaf/0.1/>
			PREFIX	exif: <http://www.kanzaki.com/ns/exif#>
			PREFIX	dc: <http://purl.org/dc/elements/1.1/>
			SELECT	DISTINCT ?name ?camera
			WHERE	{
						?img a foaf:Image .
						?img dc:creator ?name .
						?img exif:model ?camera
					}
			OFFSET 1
END
		my $stream	= $query->execute( $model );
		isa_ok( $stream, 'RDF::Trine::Iterator' );
		my ($count);
		while (my $row = $stream->next) {
			my ($n, $c)	= @{ $row }{qw(name camera)};
			my $name	= $n->literal_value;
			ok( $name, "Got image creator: $name" );
		} continue { ++$count };
		is( $count, 1, "Good DISTINCT with LIMIT" );
	}
	
	{
		print "# foaf:Image with ORDER BY ASC(expression) [1]\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql' );
			PREFIX	foaf: <http://xmlns.com/foaf/0.1/>
			PREFIX	dcterms: <http://purl.org/dc/terms/>
			PREFIX	geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
			PREFIX	exif: <http://www.kanzaki.com/ns/exif#>
			PREFIX	dc: <http://purl.org/dc/elements/1.1/>
			PREFIX	xsd: <http://www.w3.org/2001/XMLSchema#>
			SELECT	DISTINCT ?img ?long
			WHERE	{
						?img a foaf:Image .
						?img dcterms:spatial ?point .
						?point geo:long ?long .
					}
			ORDER BY ASC(xsd:float(xsd:decimal(?long) * -1))
END
		my $stream	= $query->execute( $model );
		isa_ok( $stream, 'RDF::Trine::Iterator' );
		my $count	= 0;
		
		my $min;
		while (my $row = $stream->next) {
			my ($i, $l)	= @{ $row }{qw(img long)};
			my $image	= $i->uri_value;
			my $long	= $l->literal_value;
			if (defined($min)) {
				cmp_ok( $long, '<=', $min, "decreasing longitude $long on $image" );
				if ($long <= $min) {
					$min	= $long;
				}
			} else {
				is( $image, 'http://kasei.us/pictures/2004/20040909-Ireland/images/DSC_5705.jpg' );
				$min	= $long;
			}
		} continue { last if ++$count == 2 };
		is( $count, 2, "Good ORDER BY ASC(expression) [1]" );
	}
	
	{
		print "# foaf:Image with ORDER BY DESC(expression) [2]\n";
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql' );
			PREFIX	foaf: <http://xmlns.com/foaf/0.1/>
			PREFIX	dcterms: <http://purl.org/dc/terms/>
			PREFIX	geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
			PREFIX	exif: <http://www.kanzaki.com/ns/exif#>
			PREFIX	dc: <http://purl.org/dc/elements/1.1/>
			PREFIX	xsd: <http://www.w3.org/2001/XMLSchema#>
			SELECT	DISTINCT ?img ?long
			WHERE	{
						?img a foaf:Image .
						?img dcterms:spatial ?point .
						?point geo:long ?long .
					}
			ORDER BY DESC(xsd:float(xsd:decimal(?long) * -1))
END
		my $stream	= $query->execute( $model );
		isa_ok( $stream, 'RDF::Trine::Iterator' );
		my $count	= 0;
		
		my $max;
		while (my $row = $stream->next) {
			my ($i, $l)	= @{ $row }{qw(img long)};
			my $image	= $i->uri_value;
			my $long	= $l->literal_value;
			if (defined($max)) {
				cmp_ok( $long, '>=', $max, "increasing longitude $long on $image" );
				if ($long >= $max) {
					$max	= $long;
				}
			} else {
				cmp_ok( $long, '==', -71.3924 );
				$max	= $long;
			}
		} continue { last if ++$count == 2 };
		is( $count, 2, "Good ORDER BY DESC(expression) [2]" );
	}
}