1use Test::More tests => 18;
2use Test::Exception;
3
4use strict;
5use warnings;
6use File::Spec;
7use Data::Dumper;
8
9use RDF::Trine qw(iri variable statement);
10use RDF::Trine::Namespace qw(rdf foaf);
11use RDF::Trine::Error qw(:try);
12use RDF::Trine::Parser;
13
14my $store	= RDF::Trine::Store::SPARQL->new('http://myrdf.us/sparql11');
15my $model	= RDF::Trine::Model->new( $store );
16
17throws_ok { $store->add_statement() } 'RDF::Trine::Error::MethodInvocationError', 'add_statement throws error with no statement';
18throws_ok { $store->remove_statement() } 'RDF::Trine::Error::MethodInvocationError', 'remove_statement throws error with no statement';
19# throws_ok { $store->remove_statements(iri('asdfkj')) } 'RDF::Trine::Error::UnimplementedError', 'remove_statements throws unimplemented error';
20
21my $subject = iri('http://example.org/resource/1');
22my $subject2 = iri('http://example.org/resource/2');
23my $predicate = iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');
24my $object = iri('http://example.org/vocab/Record');
25my $statement = statement($subject, $predicate, $object);
26my $statement2 = statement($subject2, $predicate, $object);
27
28GROUP_BULK_OPS: {
29    my @bulk_ops = ();
30    push(@bulk_ops, ['_add_statements', $statement, undef]);
31    push(@bulk_ops, ['_add_statements', $statement2, undef]);
32    my @aggops = $store->_group_bulk_ops( @bulk_ops );
33    is(scalar @aggops, 1, "There should just be one _add_statements aggregated operation");
34    my $aggop = shift @aggops;
35    my ($type, $ops) = @$aggop;
36    is($type, '_add_statements', "Type is correct: _add_statements");
37    is(scalar @$ops, 2, 'There should be two ops here');
38}
39
40BULK_OPS_INTEGRATION: {
41    no warnings 'redefine';
42    local *RDF::Trine::Store::SPARQL::_get_post_iterator = sub {
43        my ($self,$sparql) = @_;
44        my @statements = ();
45        if ($sparql =~ /INSERT DATA [{](.*)[}]/s){
46            my $ntriples = $1;
47            if ($ntriples){
48                my $parser = RDF::Trine::Parser->new('ntriples');
49                $parser->parse(undef, $ntriples, sub { my $st  = shift; push(@statements,$st); } );
50            }
51        }
52        is(@statements,2,"There should be two statements being posted.");
53
54        #This boolean isn't really checked so just default to true
55        return RDF::Trine::Iterator::Boolean->new( [ 1 ] );
56     };
57    $store->_begin_bulk_ops;
58    $store->add_statement($statement);
59    $store->add_statement($statement2);
60    $store->_end_bulk_ops;
61}
62
63SKIP: {
64	unless ($ENV{RDFTRINE_NETWORK_TESTS}) {
65		skip( "No network. Set RDFTRINE_NETWORK_TESTS to run these tests.", 11 );
66	}
67
68	{
69		my $iter	= $model->get_statements( undef, $rdf->type, $foaf->Person );
70		isa_ok( $iter, 'RDF::Trine::Iterator' );
71		my $st		= $iter->next;
72		isa_ok( $st, 'RDF::Trine::Statement' );
73		my $p		= $st->subject;
74		isa_ok( $p, 'RDF::Trine::Node' );
75	}
76
77	{
78		my $iter	= $model->get_statements( variable('s'), $rdf->type, $foaf->Person );
79		isa_ok( $iter, 'RDF::Trine::Iterator' );
80		my $st		= $iter->next;
81		isa_ok( $st, 'RDF::Trine::Statement' );
82		my $p		= $st->subject;
83		isa_ok( $p, 'RDF::Trine::Node' );
84	}
85
86	{
87		my $count	= $model->size;
88		cmp_ok( $count, '>', 0, 'size' );
89	}
90
91	{
92		my $count	= $model->count_statements( undef, $rdf->type, $foaf->Person );
93		cmp_ok( $count, '>', 0, 'count_statements' );
94	}
95
96	{
97		my $pattern	= statement( variable('p'), $rdf->type, $foaf->Person );
98		my $iter	= $model->get_pattern( $pattern );
99		isa_ok( $iter, 'RDF::Trine::Iterator' );
100		my $b	= $iter->next;
101		isa_ok( $b, 'HASH' );
102		isa_ok( $b->{p}, 'RDF::Trine::Node' );
103	}
104
105	if (0) {
106		my @ctx	= $model->get_contexts;
107		is_deeply( \@ctx, [], 'empty get_contexts' );
108	}
109}
110
111subtest 'test remove_statements interface' => sub {
112    plan tests => 8;
113
114    no warnings 'redefine';
115    my $cached_sparql;
116    local *RDF::Trine::Store::SPARQL::_get_post_iterator = sub {
117        my ($self,$sparql) = @_;
118        #Cache the SPARQL at subroutine scope for testing
119        $cached_sparql = $sparql;
120        #This boolean isn't really checked so just default to true
121        return RDF::Trine::Iterator::Boolean->new( [ 1 ] );
122    };
123
124    my $store   = RDF::Trine::Store::SPARQL->new('http://localhost/sparql');
125
126    my $subject = iri('http://example.org/resource/1');
127    my $predicate = iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');
128    my $object = iri('http://example.org/vocab/Record');
129    my $context = iri('http://example.org/graph/1');
130    my $triple = statement($subject, $predicate, $object);
131    my $quad = statement($subject, $predicate, $object, $context);
132
133    lives_ok {  $store->remove_statements($triple) } 'Use non-standard interface for remove_statements w/ triple';
134    is( $cached_sparql, 'DELETE WHERE { <http://example.org/resource/1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/vocab/Record> . }','SPARQL OK');
135
136    lives_ok {  $store->remove_statements($quad) } 'Use non-standard interface for remove_statements w/ quad';
137    is( $cached_sparql, 'DELETE WHERE { GRAPH <http://example.org/graph/1> { <http://example.org/resource/1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/vocab/Record> } }', 'SPARQL OK');
138
139    lives_ok {  $store->remove_statements($subject, $predicate, $object, $context) } 'Use standard interface for remove_statements w/ context';
140    is( $cached_sparql, 'DELETE WHERE { GRAPH <http://example.org/graph/1> { <http://example.org/resource/1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/vocab/Record> } }', 'SPARQL OK');
141
142    lives_ok {  $store->remove_statements($subject, $predicate, $object, undef) } 'Use standard interface for remove_statements w/o context';
143    is( $cached_sparql, 'DELETE WHERE { <http://example.org/resource/1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/vocab/Record> . }','SPARQL OK');
144};
145