File: iterator-graph.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 (106 lines) | stat: -rw-r--r-- 3,569 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env perl
use strict;
use warnings;
no warnings 'redefine';
use URI::file;
use Test::More tests => 21;
use Test::Exception;

use Data::Dumper;
use RDF::Trine;
use RDF::Trine::Iterator qw(sgrep smap swatch);
use RDF::Trine::Iterator::Graph;
use RDF::Trine::Statement;

my $p1		= RDF::Trine::Node::Resource->new('http://example.org/alice');
my $p2		= RDF::Trine::Node::Resource->new('http://example.org/eve');
my $p3		= RDF::Trine::Node::Resource->new('http://example.org/bob');
my $type	= RDF::Trine::Node::Resource->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');
my $person	= RDF::Trine::Node::Resource->new('http://xmlns.com/foaf/0.1/Person');

my $st1		= RDF::Trine::Statement->new( $p1, $type, $person );
my $st2		= RDF::Trine::Statement->new( $p2, $type, $person );
my $st3		= RDF::Trine::Statement->new( $p3, $type, $person );

{
	my $stream	= RDF::Trine::Iterator::Graph->new();
	isa_ok( $stream, 'RDF::Trine::Iterator::Graph' );
	ok( $stream->is_graph, 'is_graph' );
	my $data	= $stream->next;
	is( $data, undef );
}

{
	my $stream	= RDF::Trine::Iterator::Graph->new( [ $st1, $st2, $st3 ] );
	isa_ok( $stream, 'RDF::Trine::Iterator::Graph' );
	my $st		= $stream->next;
	isa_ok( $st, 'RDF::Trine::Statement' );
	is( $st->subject->uri_value, 'http://example.org/alice' );
}

{
	my $stream	= RDF::Trine::Iterator::Graph->new( [ $st1, $st2, $st3 ] );
	my $bindings	= $stream->as_bindings;
	isa_ok( $bindings, 'RDF::Trine::Iterator::Bindings' );
	my $hash		= $bindings->next;
	isa_ok( $hash, 'HASH' );
	is_deeply( $hash, { subject => $p1, predicate => $type, object => $person } );
}

{
	my $stream	= RDF::Trine::Iterator::Graph->new( [ $st1, $st2, $st3 ] );
	my @vars		= map { RDF::Trine::Node::Variable->new( $_ ) } qw(s p o);
	my $bindings	= $stream->as_bindings( @vars );
	isa_ok( $bindings, 'RDF::Trine::Iterator::Bindings' );
	my $hash		= $bindings->next;
	isa_ok( $hash, 'HASH' );
	is_deeply( $hash, { 's' => $p1, p => $type, o => $person } );
}

{
	my $stream	= RDF::Trine::Iterator::Graph->new( [ $st1, $st2, $st1, $st3, $st2, $st1 ] );
	my $unique	= $stream->unique;
	isa_ok( $unique, 'RDF::Trine::Iterator::Graph' );
	my $count	= 0;
	while (my $st = $unique->next) {
		$count++;
	}
	is( $count, 3, 'graph iterator unique' );
}

{
	my $stream	= RDF::Trine::Iterator::Graph->new( [ $st1, $st2 ] );
	my $xml		= $stream->as_xml;
	like( $xml, qr<rdf:Description.*http://example.org/alice.*type.*http://xmlns.com/foaf/0.1/Person>sm, 'graph iterator as_xml' );
	like( $xml, qr<http://example.org/eve>sm, 'graph iterator as_xml (second result)' );
}

{
	my $stream	= RDF::Trine::Iterator::Graph->new( [ $st1, $st2 ] );
	my $xml		= $stream->as_xml( 2 );
	my @matches	= ($xml =~ m<"http://example.org/([^"]+)">gsm);
	is( scalar(@matches), 2, 'graph iterator as_xml maxcount=2' );
}

{
	my $stream	= RDF::Trine::Iterator::Graph->new( [ $st1, $st2 ] );
	my $xml		= $stream->as_xml( 1 );
	my @matches	= ($xml =~ m<"http://example.org/([^"]+)">gsm);
	is( scalar(@matches), 1, 'graph iterator as_xml maxcount=1' );
}

{
	my $stream	= RDF::Trine::Iterator::Graph->new( [ $st1, $st2 ] );
	throws_ok {
		my $json	= $stream->as_json;
	} 'RDF::Trine::Error::SerializationError';
}

{
	my $stream1	= RDF::Trine::Iterator::Graph->new( [ $st2, $st3 ] );
	my $stream2	= RDF::Trine::Iterator::Graph->new( [ $st1, $st3 ] );
	my $mstream = $stream1->concat($stream2)->materialize;
	is($mstream->length, 4, 'Concatenated Graph Iterator has 4 statements');
	$mstream->reset;
	is($mstream->unique->length, 3, 'Concatenated Graph Iterator has 3 unique statements');
}