File Coverage

blib/lib/Test/Attean/Store/SPARQL/Role/CreateStore.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Test::Attean::Store::SPARQL::Role::CreateStore;
2 1     1   865975 use strict;
  1         1  
  1         26  
3 1     1   3 use warnings;
  1         1  
  1         20  
4 1     1   465 use RDF::Trine::Model;
  1         825063  
  1         27  
5 1     1   7 use RDF::Trine qw(statement iri blank literal);
  1         1  
  1         54  
6 1     1   453 use RDF::Endpoint;
  0            
  0            
7             use Test::LWP::UserAgent;
8             use HTTP::Message::PSGI;
9             use Moo::Role;
10              
11             sub create_store {
12             my $self = shift;
13             my %args = @_;
14             my $triples = $args{triples} // [];
15             my $model = RDF::Trine::Model->temporary_model; # For creating endpoint
16             foreach my $atteantriple (@{$triples}) {
17             my $s = iri($atteantriple->subject->value);
18             if ($atteantriple->subject->is_blank) {
19             $s = blank($atteantriple->subject->value);
20             }
21             my $p = iri($atteantriple->predicate->value);
22             my $o = iri($atteantriple->object->value);
23             if ($atteantriple->object->is_literal) {
24             # difference with RDF 1.0 vs RDF 1.1 datatype semantics
25             if ($atteantriple->object->datatype->value eq 'http://www.w3.org/2001/XMLSchema#string') {
26             $o = literal($atteantriple->object->value, $atteantriple->object->language);
27             } else {
28             $o = literal($atteantriple->object->value, $atteantriple->object->language, $atteantriple->object->datatype->value);
29             }
30             } elsif ($atteantriple->object->is_blank) {
31             $o = blank($atteantriple->object->value);
32             }
33             $model->add_statement(statement($s, $p, $o));
34             }
35             my $end = RDF::Endpoint->new($model);
36             my $app = sub {
37             my $env = shift;
38             my $req = Plack::Request->new($env);
39             my $resp = $end->run( $req );
40             return $resp->finalize;
41             };
42             my $useragent = Test::LWP::UserAgent->new;
43             $useragent->register_psgi('localhost', $app);
44             # Now, we should just have had a URL of the endpoint
45             my $url = 'http://localhost:5000/sparql';
46             my $store = Attean->get_store('SPARQL')->new(endpoint_url => $url,
47             ua => $useragent
48             );
49             return $store;
50             }
51              
52             1;
53              
54             =pod
55              
56             =head1 NAME
57              
58             Test::Attean::Store::SPARQL::Role::CreateStore - Create a SPARQL store for tests
59              
60             =head1 SYNOPSIS
61              
62             Either:
63              
64             use Test::More;
65             use Test::Roo;
66             with 'Test::Attean::TripleStore', 'Test::Attean::Store::SPARQL::Role::CreateStore';
67             run_me;
68             done_testing;
69              
70             or:
71              
72             package TestCreateStore {
73             use Moo;
74             with 'Test::Attean::Store::SPARQL::Role::CreateStore';
75             };
76             my $triples = [
77             triple(iri('http://example.org/bar'), iri('http://example.org/c'), iri('http://example.org/foo')),
78             # [...]
79             ];
80              
81             my $test = TestCreateStore->new;
82             my $store = $test->create_store(triples => $triples);
83              
84              
85             =head1 DESCRIPTION
86              
87              
88             There are two ways of using this. The original idea is to use it to
89             test a triple/quad that uses L<Test::Attean::TripleStore>, like in the
90             first example in the synopsis.
91              
92             It is also possible to utilize this role like in the second example to
93             create a store for testing other parts of the code too. In that
94             example, first wrap a class around the role, then create an arrayref
95             of triples, which should be used to populate the store. Then,
96             instantiate an object of the class, and call it's C<create_store>
97             method with the triples. Now, you have a proper store that can be used
98             in tests.
99              
100             =head1 AUTHOR
101              
102             Kjetil Kjernsmo E<lt>kjetilk@cpan.orgE<gt>.
103              
104             =head1 COPYRIGHT AND LICENCE
105              
106             This software is copyright (c) 2015, 2016 by Kjetil Kjernsmo.
107              
108             This is free software; you can redistribute it and/or modify it under
109             the same terms as the Perl 5 programming language system itself.