File: protocol-serialization.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 (139 lines) | stat: -rw-r--r-- 4,097 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
#!/usr/bin/env perl
use strict;
use warnings;
no warnings 'redefine';
use URI::file;

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

my @files	= map { "data/$_" } qw(about.xrdf foaf.xrdf Flower-2.rdf);
my @models	= test_models( @files );

use Test::More;

eval "use Test::JSON 0.03; use JSON 2.0;";
my $run_json_tests	= (not $@) ? 1 : 0;
my $tests_per_model	= 7 + ($run_json_tests ? 6 : 0);

plan tests => 1 + ($tests_per_model * scalar(@models));

use_ok( 'RDF::Query' );
foreach my $model (@models) {
	print "\n#################################\n";
	print "### Using model: $model\n";
	
	{
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql' );
			PREFIX	foaf: <http://xmlns.com/foaf/0.1/>
			SELECT	?person ?homepage
			WHERE	{
						?person foaf:name "Gregory Todd Williams" .
						?person foaf:homepage ?homepage .
						FILTER REGEX(STR(?homepage), "kasei")
					}
			LIMIT 1
END
		my $stream	= $query->execute( $model );
		ok( $stream->is_bindings, 'Bindings result' );
		my $xml		= $stream->as_xml;
		$xml		=~ s/^.*<sparql/<sparql/smo;	# remove xml declaration
		my $expect	= <<"END";
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
	<variable name="person"/>
	<variable name="homepage"/>
</head>
<results>
		<result>
			<binding name="person"><uri>http://kasei.us/about/foaf.xrdf#greg</uri></binding>
			<binding name="homepage"><uri>http://kasei.us/</uri></binding>
		</result>
</results>
</sparql>
END
		is( $xml, $expect, 'XML Bindings Results formatting' );
	}
	
	{
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql' );
			PREFIX	foaf: <http://xmlns.com/foaf/0.1/>
			ASK { ?person foaf:name "Gregory Todd Williams" }
END
		my $stream	= $query->execute( $model );
		ok( $stream->is_boolean, 'Boolean result' );
		my $xml		= $stream->as_xml;
		like( $xml, qr%<boolean>true</boolean>%sm, 'XML Boolean Results formatting' );
	}
	
	{
		my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql' );
			PREFIX foaf: <http://xmlns.com/foaf/0.1/>
			PREFIX dc: <http://purl.org/dc/elements/1.1/>
			CONSTRUCT	{ _:somebody foaf:name ?name; foaf:made ?thing }
			WHERE		{ ?thing dc:creator ?name }
END
		my $stream	= $query->execute( $model );
		ok( $stream->is_graph, 'Graph result' );
		
		my $xml		= $stream->as_xml;	# XXX remove eval when removing the TODO!
		no warnings 'uninitialized';
		like( $xml, qr%name.*?>Greg Williams<%ms, 'XML Results formatting' );
		like( $xml, qr%made\s+.*?rdf:resource="http://kasei\.us/pictures/2004/20040909-Ireland/images/DSC_5705\.jpg"%ms, 'XML Results formatting' );
	}
	
	### JSON Tests
	
	sub JSON::true;
	sub JSON::false;
	
	if ($run_json_tests) {
		{
			my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql' );
				PREFIX	foaf: <http://xmlns.com/foaf/0.1/>
				SELECT ?person ?homepage
				WHERE	{
							?person foaf:name "Gregory Todd Williams" .
							?person foaf:homepage ?homepage .
							FILTER REGEX(STR(?homepage), "kasei")
						}
				ORDER BY ?homepage
				LIMIT 1
END
			my $stream	= $query->execute( $model );
			ok( $stream->is_bindings, 'Bindings result' );
			my $js		= $stream->as_json();
			my $expect	= {
							head	=> { vars => [qw(person homepage)] },
							results	=> {
								ordered 	=> JSON::true,
								distinct	=> JSON::false,
								bindings	=> [
									{
										person		=> { type => 'uri', value => 'http://kasei.us/about/foaf.xrdf#greg' },
										homepage	=> { type => 'uri', value => 'http://kasei.us/' },
									}
								],
							}
						};
			is_valid_json( $js, 'valid json syntax' );
			is_json( $js, to_json($expect), 'expected json results' );
		}
	
		{
			my $query	= new RDF::Query ( <<"END", undef, undef, 'sparql' );
				PREFIX	foaf: <http://xmlns.com/foaf/0.1/>
				ASK { ?person foaf:name "Gregory Todd Williams" }
END
			my $stream	= $query->execute( $model );
			ok( $stream->is_boolean, 'Boolean result' );
			my $js		= $stream->as_json;
			my $expect	= {
							head	=> { vars => [] },
							boolean	=> JSON::true,
						};
			is_valid_json( $js, 'valid json syntax' );
			is_json( $js, to_json($expect), 'expected json results' );
		}
	}
}