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