File Coverage

blib/lib/Test/Attean/Store/LDF/Role/CreateStore.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Test::Attean::Store::LDF::Role::CreateStore;
2 2     2   922881 use strict;
  2         2  
  2         49  
3 2     2   6 use warnings;
  2         2  
  2         37  
4 2     2   7 use Moo::Role;
  2         2  
  2         11  
5 2     2   1335 use RDF::Trine::Model;
  2         1949731  
  2         55  
6 2     2   12 use RDF::Trine qw(statement iri blank literal);
  2         3  
  2         106  
7 2     2   883 use RDF::LinkedData;
  0            
  0            
8             use Test::LWP::UserAgent;
9             use Plack::Request;
10             use HTTP::Message::PSGI;
11             use Data::Dumper;
12             use namespace::clean;
13              
14             our $AUTHORITY = 'cpan:KJETILK';
15             our $VERSION = '0.04';
16              
17             sub create_store {
18             my $self = shift;
19             my %args = @_;
20             my $triples = $args{triples} // [];
21             my $model = RDF::Trine::Model->temporary_model; # For creating endpoint
22             foreach my $atteantriple (@{$triples}) {
23             my $s = iri($atteantriple->subject->value);
24             if ($atteantriple->subject->is_blank) {
25             $s = blank($atteantriple->subject->value);
26             }
27             my $p = iri($atteantriple->predicate->value);
28             my $o = iri($atteantriple->object->value);
29             if ($atteantriple->object->is_literal) {
30             # difference with RDF 1.0 vs RDF 1.1 datatype semantics
31             if ($atteantriple->object->datatype->value eq 'http://www.w3.org/2001/XMLSchema#string') {
32             $o = literal($atteantriple->object->value, $atteantriple->object->language);
33             } else {
34             $o = literal($atteantriple->object->value, $atteantriple->object->language, $atteantriple->object->datatype->value);
35             }
36             } elsif ($atteantriple->object->is_blank) {
37             $o = blank($atteantriple->object->value);
38             }
39             $model->add_statement(statement($s, $p, $o));
40             }
41              
42             my $ld = RDF::LinkedData->new(
43             model => $model ,
44             base_uri => 'http://example.org' ,
45             void_config => {
46             urispace => 'http://example.org/'
47             } ,
48             fragments_config => {
49             fragments_path => '/fragments' ,
50             allow_dump_dataset => 1,
51             } ,
52             );
53              
54             my $endpoint = 'http://example.org/fragments';
55              
56             my $useragent = Test::LWP::UserAgent->new;
57             $useragent->register_psgi('example.org', sub {
58             my $env = shift;
59             $ld->request(Plack::Request->new($env));
60             my $uri = $endpoint;
61             $uri .= sprintf "?%s" , $env->{QUERY_STRING} if length $env->{QUERY_STRING};
62             return $ld->response($uri)->finalize;
63             });
64              
65             my $store = Attean->get_store('LDF')->new(start_url => $endpoint);
66              
67             RDF::Trine->default_useragent($useragent);
68              
69             return $store;
70             }
71              
72             1;
73              
74             =pod
75              
76             =head1 NAME
77              
78             Test::Attean::Store::LDF::Role::CreateStore - Create a LDF store for tests
79              
80             =head1 SYNOPSIS
81              
82             Either:
83              
84             use Test::More;
85             use Test::Roo;
86             with 'Test::Attean::TripleStore', 'Test::Attean::Store::LDF::Role::CreateStore';
87             run_me;
88             done_testing;
89              
90             or:
91              
92             package TestCreateStore {
93             use Moo;
94             with 'Test::Attean::Store::LDF::Role::CreateStore';
95             };
96             my $triples = [
97             triple(iri('http://example.org/bar'), iri('http://example.org/c'), iri('http://example.org/foo')),
98             # [...]
99             ];
100              
101             my $test = TestCreateStore->new;
102             my $store = $test->create_store(triples => $triples);
103              
104              
105             =head1 DESCRIPTION
106              
107              
108             There are two ways of using this. The original idea is to use it to
109             test a triple/quad that uses L<Test::Attean::TripleStore>, like in the
110             first example in the synopsis.
111              
112             It is also possible to utilize this role like in the second example to
113             create a store for testing other parts of the code too. In that
114             example, first wrap a class around the role, then create an arrayref
115             of triples, which should be used to populate the store. Then,
116             instantiate an object of the class, and call it's C<create_store>
117             method with the triples. Now, you have a proper store that can be used
118             in tests.
119              
120             =head1 AUTHOR
121              
122             Kjetil Kjernsmo E<lt>kjetilk@cpan.orgE<gt>.
123             Patrick Hochstenbach C<< <patrick.hochstenbach@ugent.be> >>
124              
125             =head1 COPYRIGHT AND LICENCE
126              
127             This software is copyright (c) 2015 by Patrick Hochstenbach.
128             This software is copyright (c) 2015, 2016 by Kjetil Kjernsmo.
129              
130             This is free software; you can redistribute it and/or modify it under
131             the same terms as the Perl 5 programming language system itself.