Test/PONAPI/Repository/MockDB.pm

filter: Statementstmt 95.7% 380 / 397 Branchbran 75.8% 94 / 124 Conditioncond 54.1% 33 / 61 MC/DCmcdc 23.5% 8 / 34 Subroutinesub 100% 39 / 39 Podpod 0.0% 0 / 15 totaltotal 82.6% 554 / 670 CC 100 SCAR 18.6
CC
100
Cov
87%
_db_execute
16.3
retrieve_relationships
11.9
_create_relationships
9.5
CRAP 6.4
« PONAPI/DAO/Request/Role/HasFields.pm ↑ Summary PONAPI/DAO/Request/Role/HasDataMethods.pm »
1 # ABSTRACT: mock repository class
2 package Test::PONAPI::Repository::MockDB;
3  
4 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # We MUST use DBD::SQLite before ::Constants to get anything useful!
7 8 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use DBD::SQLite::Constants qw/:result_codes/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use Test::PONAPI::Repository::MockDB::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶use Test::PONAPI::Repository::MockDB::Table::Articles;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Test::PONAPI::Repository::MockDB::Table::People;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Test::PONAPI::Repository::MockDB::Table::Comments;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 ▶use PONAPI::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use PONAPI::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 8 with 'PONAPI::Repository';
20  
21 8 has dbh => (
22 is => 'ro',
23 isa => 'DBI::db',
24 writer => '_set_dbh'
25 );
26  
27 has tables => (
28 is => 'ro',
29 isa => 'HashRef',
30 lazy => 1,
31 default => sub {
32 return +{
33 9 ▶ articles => Test::PONAPI::Repository::MockDB::Table::Articles->new,
Subroutine __ANON__: called CC=1 SCAR=0.0
34 people => Test::PONAPI::Repository::MockDB::Table::People->new,
35 comments => Test::PONAPI::Repository::MockDB::Table::Comments->new,
36 }
37 }
38 8 );
39  
40 sub BUILD {
41 13 ▶ my ($self, $params) = @_;
Subroutine BUILD: called CC=2 SCAR=7.0
Pod undocumented
42 13 my $loader = Test::PONAPI::Repository::MockDB::Loader->new;
43 13 ▶ $loader->load unless $params->{skip_data_load};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"skip_data_load"} 13 0 13
44 13 $self->_set_dbh( $loader->dbh );
45 }
46  
47 sub has_type {
48 257 ▶ my ( $self, $type ) = @_;
Subroutine has_type: called CC=1 SCAR=0.0
Pod undocumented
49 257 !! exists $self->tables->{$type};
50 }
51  
52 sub has_relationship {
53 117 ▶ my ( $self, $type, $rel_name ) = @_;
Subroutine has_relationship: called CC=2 SCAR=7.4
Pod undocumented
54 117 ▶ if ( my $table = $self->tables->{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $table = $self->tables->{$type}) 117 0 117
55 117 my $relations = $table->RELATIONS;
56 117 return !! exists $relations->{ $rel_name };
57 }
58 0 return 0;
59 }
60  
61 sub has_one_to_many_relationship {
62 160 ▶ my ( $self, $type, $rel_name ) = @_;
Subroutine has_one_to_many_relationship: called CC=3 SCAR=11.8
Pod undocumented
63 160 ▶ if ( my $table = $self->tables->{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $table = $self->tables->{$type}) 160 0 160
64 160 my $relations = $table->RELATIONS;
65 160 ▶ return if !exists $relations->{ $rel_name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $relations->{$rel_name} 0 160 160
66 160 return !$relations->{ $rel_name }->ONE_TO_ONE;
67 }
68 0 return;
69 }
70  
71 sub type_has_fields {
72 56 ▶ my ($self, $type, $fields) = @_;
Subroutine type_has_fields: called CC=2 SCAR=6.9
Pod undocumented
73  
74 # Check for invalid 'fields'
75 56 my $table_obj = $self->tables->{$type};
76 56 my %columns = map +($_=>1), @{ $table_obj->COLUMNS };
77  
78 56 ▶ return 1 unless grep !exists $columns{$_}, @$fields;
Branch: 1 branch
BranchTrueFalseTotal
unless grep((!exists $columns{$_}), @$fields) 46 10 56
79 10 return;
80 }
81  
82 sub retrieve_all {
83 54 ▶ my ( $self, %args ) = @_;
Subroutine retrieve_all: called CC=2 SCAR=6.9
Pod undocumented
84 54 my $type = $args{type};
85  
86 54 ▶ $self->_validate_page($args{page}) if $args{page};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"page"} 5 49 54
87  
88 54 my $stmt = $self->tables->{$type}->select_stmt(%args);
89 54 $self->_add_resources( stmt => $stmt, %args );
90 }
91  
92 sub retrieve {
93 37 ▶ my ( $self, %args ) = @_;
Subroutine retrieve: called CC=1 SCAR=0.0
Pod undocumented
94 37 $args{filter}{id} = delete $args{id};
95 37 $self->retrieve_all(%args);
96 }
97  
98 sub retrieve_relationships {
99 4 ▶ my ( $self, %args ) = @_;
Subroutine retrieve_relationships: called CC=11 SCAR=24.8
Pod undocumented
100 4 my ($type, $rel_type, $doc) = @args{qw/type rel_type document/};
101  
102 4 my $page = $args{page};
103 4 ▶ $self->_validate_page($page) if $page;
Branch: 1 branch
BranchTrueFalseTotal
if $page 1 3 4
104  
105 4 ▶ my $sort = $args{sort} || [];
Condition50%1/2
$args{'sort'} || []
l!l
40
MC/DC0%0/1
$args{'sort'} || []
$args{'sort'}
Truth table50%1/2
A
A= $args{'sort'}
Aresult
11
00
106 4 ▶ if ( @$sort ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$sort) 1 3 4
107 1 ▶ PONAPI::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
if @$sort > 1 or not $sort->[0] =~ /\A(-)?id\z/ 0 1 1
Condition33%1/3
@$sort > 1 or not $sort->[0] =~ /\A(-)?id\z/
l!l&&r!l&&!r
001
MC/DC0%0/2
@$sort > 1 or not $sort->[0] =~ /\A(-)?id\z/
@$sort > 1 not $sort->[0] =~ /\A(-)?id\z/
Truth table33%1/3
A or not(B)
A= @$sort > 1B= not $sort->[0] =~ /\A(-)?id\z/
ABresult
1X1
011
000
108 message => "You can only sort by id in retrieve_relationships"
109 ) if @$sort > 1 || $sort->[0] !~ /\A(-)?id\z/;
110  
111 1 my $desc = !!$1;
112  
113 1 my $table_obj = $self->tables->{$type};
114 1 my $relation_obj = $table_obj->RELATIONS->{$rel_type};
115 1 my $id_column = $relation_obj->REL_ID_COLUMN;
116  
117 1 ▶ @$sort = ($desc ? '-' : '') . $id_column;
Branch: 1 branch
BranchTrueFalseTotal
$desc ? : 1 0 1
118 }
119  
120 4 my $rels = $self->_find_resource_relationships(
121 %args,
122 # No need to fetch other relationship types
123 fields => { $type => [ $rel_type ] },
124 );
125  
126 4 ▶ return unless @{ $rels || [] };
Branch: 2 branches
BranchTrueFalseTotal
unless @{$rels or [];} 0 4 4
$rels or [] 0 4 4
127  
128 4 $doc->add_resource( %$_ ) for @$rels;
129  
130 4 ▶ $self->_add_pagination_links(
Branch: 1 branch
BranchTrueFalseTotal
if $page 1 3 4
131 page => $page,
132 document => $doc,
133 ) if $page;
134  
135 }
136  
137 sub retrieve_by_relationship {
138 6 ▶ my ( $self, %args ) = @_;
Subroutine retrieve_by_relationship: called CC=4 SCAR=13.9
Pod undocumented
139 6 my ( $doc, $type, $rel_type, $fields, $include ) = @args{qw< document type rel_type fields include >};
140  
141 6 ▶ my $sort = delete $args{sort} || [];
Condition50%1/2
delete $args{'sort'} || []
l!l
60
MC/DC0%0/1
delete $args{'sort'} || []
delete $args{'sort'}
Truth table50%1/2
A
A= delete $args{'sort'}
Aresult
11
00
142 6 my $page = delete $args{page};
143 6 ▶ $self->_validate_page($page) if $page;
Branch: 1 branch
BranchTrueFalseTotal
if $page 1 5 6
144  
145 # We need to avoid passing sort and page here, since sort
146 # will have columns for the actual data, not the relationship
147 # table, and page needs to happen after sorting
148 6 my $rels = $self->_find_resource_relationships(
149 %args,
150 # No need to fetch other relationship types
151 fields => { $type => [ $rel_type ] },
152 );
153  
154 6 ▶ return unless @$rels;
Branch: 1 branch
BranchTrueFalseTotal
unless @$rels 0 6 6
155  
156 6 my $q_type = $rels->[0]{type};
157 6 my $q_ids = [ map { $_->{id} } @{$rels} ];
158  
159 6 my $stmt = $self->tables->{$q_type}->select_stmt(
160 type => $q_type,
161 fields => $fields,
162 filter => { id => $q_ids },
163 sort => $sort,
164 page => $page,
165 );
166  
167 6 $self->_add_resources(
168 document => $doc,
169 stmt => $stmt,
170 type => $q_type,
171 fields => $fields,
172 include => $include,
173 page => $page,
174 sort => $sort,
175 );
176 }
177  
178 sub create {
179 5 ▶ my ( $self, %args ) = @_;
Subroutine create: called CC=4 SCAR=13.9
Pod undocumented
180  
181 5 my $dbh = $self->dbh;
182 5 $dbh->begin_work;
183  
184 5 my ($e, $failed);
185 {
186 5 local $@;
187 5 eval { $self->_create( %args ); 1; }
188 5 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_create(%args); 1 } } or do { ($failed, $e) = (1, $@ || "Unknown error") } 2 3 5
189 2 ▶ ($failed, $e) = (1, $@||'Unknown error');
Condition50%1/2
$@ || 'Unknown error'
l!l
20
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
190 };
191 }
192 5 ▶ if ( $failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 2 3 5
193 2 $dbh->rollback;
194 2 die $e;
195 }
196  
197 3 $dbh->commit;
198  
199 3 return;
200 }
201  
202 sub _create {
203 5 ▶ my ( $self, %args ) = @_;
Subroutine _create: called CC=5 SCAR=16.1
204 5 my ( $doc, $type, $data ) = @args{qw< document type data >};
205  
206 5 ▶ my $attributes = $data->{attributes} || {};
Condition50%1/2
$data->{'attributes'} || {}
l!l
50
MC/DC0%0/1
$data->{'attributes'} || {}
$data->{'attributes'}
Truth table50%1/2
A
A= $data->{'attributes'}
Aresult
11
00
207 5 ▶ my $relationships = delete $data->{relationships} || {};
Condition100%2/2
delete $data->{'relationships'} || {}
l!l
32
MC/DC100%1/1
delete $data->{'relationships'} || {}
delete $data->{'relationships'}
Truth table100%2/2
A
A= delete $data->{'relationships'}
Aresult
11
00
208  
209 5 my $table_obj = $self->tables->{$type};
210 5 my ($stmt, $return, $extra) = $table_obj->insert_stmt(
211 table => $type,
212 values => $attributes,
213 );
214  
215 5 $self->_db_execute( $stmt );
216  
217 4 my $new_id = $self->dbh->last_insert_id("","","","");
218  
219 4 foreach my $rel_type ( keys %$relationships ) {
220 2 my $rel_data = $relationships->{$rel_type};
221 2 ▶ $rel_data = [ $rel_data ] if ref($rel_data) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rel_data ne "ARRAY" 2 0 2
222 2 $self->_create_relationships(
223 %args,
224 id => $new_id,
225 rel_type => $rel_type,
226 data => $rel_data,
227 );
228 }
229  
230 # Spec says we MUST return this, both here and in the Location header;
231 # the DAO takes care of the header, but we need to put it in the doc
232 3 $doc->add_resource( type => $type, id => $new_id );
233  
234 3 return;
235 }
236  
237 sub _create_relationships {
238 6 ▶ my ( $self, %args ) = @_;
Subroutine _create_relationships: called CC=9 SCAR=22.5
239 6 my ( $type, $id, $rel_type, $data ) = @args{qw< type id rel_type data >};
240  
241 6 my $table_obj = $self->tables->{$type};
242 6 my $relation_obj = $table_obj->RELATIONS->{$rel_type};
243  
244 6 my $rel_table = $relation_obj->TABLE;
245 6 my $key_type = $relation_obj->TYPE;
246  
247 6 my $id_column = $relation_obj->ID_COLUMN;
248 6 my $rel_id_column = $relation_obj->REL_ID_COLUMN;
249  
250 6 my @all_values;
251 6 foreach my $orig ( @$data ) {
252 7 my $relationship = { %$orig };
253 7 my $data_type = delete $relationship->{type};
254  
255 7 ▶ if ( $data_type ne $key_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type ne $key_type) 2 5 7
256 2 PONAPI::Exception->throw(
257 message => "Data has type `$data_type`, but we were expecting `$key_type`",
258 bad_request_data => 1,
259 );
260 }
261  
262 5 $relationship->{$id_column} = $id;
263 5 $relationship->{$rel_id_column} = delete $relationship->{id};
264  
265 5 push @all_values, $relationship;
266 }
267  
268 4 my $one_to_one = !$self->has_one_to_many_relationship($type, $rel_type);
269  
270 4 foreach my $values ( @all_values ) {
271 5 my ($stmt, $return, $extra) = $relation_obj->insert_stmt(
272 table => $rel_table,
273 values => $values,
274 );
275  
276 5 my ($failed, $e);
277 {
278 5 local $@;
279 5 eval { $self->_db_execute( $stmt ); 1; }
280 5 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_db_execute($stmt); 1 } } or do { () = (1, $@ || "Unknown error") } 1 4 5
281 1 ▶ ($failed, $e) = (1, $@||'Unknown error');
Condition50%1/2
$@ || 'Unknown error'
l!l
10
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
282 };
283 }
284 5 ▶ if ( $failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 1 4 5
285 1 ▶ if ( $one_to_one && do { local $@; eval { $e->sql_error } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($one_to_one and do { local $@; eval { do { $e->sql_error } } }) { } 0 1 1
Condition33%1/3
$one_to_one and do {
!ll&&!rl&&r
100
MC/DC0%0/2
$one_to_one and do { local $@; eval { do { $e->sql_error } } }
$one_to_one do { local $@; eval { do { $e->sql_error } } }
Truth table33%1/3
A and B
A= $one_to_oneB= do { local $@; eval { do { $e->sql_error } } }
ABresult
0X0
100
111
286 # Can't quite do ::Upsert
287 0 $stmt = $relation_obj->update_stmt(
288 table => $rel_table,
289 values => [ %$values ],
290 where => { $id_column => $id },
291 driver => 'sqlite',
292 );
293 0 $self->_db_execute( $stmt );
294 }
295 else {
296 1 die $e;
297 }
298 };
299 }
300  
301 3 return PONAPI_UPDATED_NORMAL;
302 }
303  
304 sub create_relationships {
305 4 ▶ my ($self, %args) = @_;
Subroutine create_relationships: called CC=4 SCAR=13.9
Pod undocumented
306  
307 4 my $dbh = $self->dbh;
308 4 $dbh->begin_work;
309  
310 4 my ($ret, $e, $failed);
311 {
312 4 local $@;
313 4 eval { $ret = $self->_create_relationships( %args ); 1; }
314 4 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $ret = $self->_create_relationships(%args); 1 } } or do { ($failed, $e) = (1, $@ || "Unknown error") } 2 2 4
315 2 ▶ ($failed, $e) = (1, $@||'Unknown error');
Condition50%1/2
$@ || 'Unknown error'
l!l
20
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
316 };
317 }
318 4 ▶ if ( $failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 2 2 4
319 2 $dbh->rollback;
320 2 die $e;
321 }
322  
323 2 $dbh->commit;
324 2 return $ret;
325 }
326  
327 sub update {
328 12 ▶ my ( $self, %args ) = @_;
Subroutine update: called CC=4 SCAR=13.9
Pod undocumented
329  
330 12 my $dbh = $self->dbh;
331 12 $dbh->begin_work;
332  
333 12 my ($ret, $e, $failed);
334 {
335 12 local $@;
336 12 eval { $ret = $self->_update( %args ); 1 }
337 12 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $ret = $self->_update(%args); 1 } } or do { ($failed, $e) = (1, $@ || "Unknown error") } 1 11 12
338 1 ▶ ($failed, $e) = (1, $@||'Unknown error');
Condition50%1/2
$@ || 'Unknown error'
l!l
10
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
339 };
340 }
341 12 ▶ if ( $failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 1 11 12
342 1 $dbh->rollback;
343 1 die $e;
344 }
345  
346 11 $dbh->commit;
347 11 return $ret;
348 }
349  
350 sub _update {
351 12 ▶ my ( $self, %args ) = @_;
Subroutine _update: called CC=8 SCAR=20.9
352 12 my ( $type, $id, $data ) = @args{qw< type id data >};
353 12 ▶ my ($attributes, $relationships) = map $_||{}, @{ $data }{qw/ attributes relationships /};
Condition100%2/2
$_ || {}
l!l
1410
MC/DC100%1/1
$_ || {}
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
354  
355 12 my $return = PONAPI_UPDATED_NORMAL;
356 12 ▶ if ( %$attributes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$attributes) 8 4 12
357 8 my $table_obj = $self->tables->{$type};
358 # Per the spec, the api behaves *very* differently if ->update does extra things
359 # under the hood. Case point: the updated column in Articles
360 8 my ($stmt, $extra_return, $msg) = $table_obj->update_stmt(
361 table => $type,
362 where => { $table_obj->ID_COLUMN => $id },
363 values => $attributes,
364 );
365  
366 8 ▶ $return = $extra_return if defined $extra_return;
Branch: 1 branch
BranchTrueFalseTotal
if defined $extra_return 7 1 8
367  
368 8 my $sth = $self->_db_execute( $stmt );
369  
370 # We had a successful update, but it updated nothing
371 8 ▶ if ( !$sth->rows ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->rows) 3 5 8
372 3 $return = PONAPI_UPDATED_NOTHING;
373 }
374 }
375  
376 12 foreach my $rel_type ( keys %$relationships ) {
377 my $update_rel_return = $self->_update_relationships(
378 type => $type,
379 id => $id,
380 rel_type => $rel_type,
381 9 data => $relationships->{$rel_type},
382 );
383  
384 # We tried updating the attributes but
385 8 ▶ $return = $update_rel_return
Branch: 1 branch
BranchTrueFalseTotal
if $return == 102 and $update_rel_return != 102 0 8 8
Condition33%1/3
$return == 102 and $update_rel_return != 102
!ll&&!rl&&r
800
MC/DC0%0/2
$return == 102 and $update_rel_return != 102
$return == 102 $update_rel_return != 102
Truth table33%1/3
A and B
A= $return == 102B= $update_rel_return != 102
ABresult
0X0
100
111
386 if $return == PONAPI_UPDATED_NOTHING
387 && $update_rel_return != PONAPI_UPDATED_NOTHING;
388 }
389  
390 11 return $return;
391 }
392  
393 sub _update_relationships {
394 13 ▶ my ($self, %args) = @_;
Subroutine _update_relationships: called CC=6 SCAR=17.9
395 13 my ( $type, $id, $rel_type, $data ) = @args{qw< type id rel_type data >};
396  
397 13 my $table_obj = $self->tables->{$type};
398 13 my $relation_obj = $table_obj->RELATIONS->{$rel_type};
399  
400 13 my $column_rel_type = $relation_obj->TYPE;
401 13 my $rel_table = $relation_obj->TABLE;
402  
403 13 my $id_column = $relation_obj->ID_COLUMN;
404 13 my $rel_id_column = $relation_obj->REL_ID_COLUMN;
405  
406 # Let's have an arrayref
407 13 ▶ $data = $data
Branch: 3 branches
BranchTrueFalseTotal
$data ? : 10 3 13
ref $data eq 'HASH' ? : 4 6 10
keys %$data ? : 4 0 4
408 ? ref($data) eq 'HASH' ? [ keys(%$data) ? $data : () ] : $data
409 : [];
410  
411 # Let's start by clearing all relationships; this way
412 # we can implement the SQL below without adding special cases
413 # for ON DUPLICATE KEY UPDATE and sosuch.
414 13 my $stmt = $relation_obj->delete_stmt(
415 table => $rel_table,
416 where => { $id_column => $id },
417 );
418 13 $self->_db_execute( $stmt );
419  
420 13 my $return = PONAPI_UPDATED_NORMAL;
421 13 foreach my $insert ( @$data ) {
422 my ($stmt, $insert_return, $extra) = $table_obj->insert_stmt(
423 table => $rel_table,
424 values => {
425 $id_column => $id,
426 $rel_id_column => $insert->{id},
427 },
428 9 );
429 9 $self->_db_execute( $stmt );
430  
431 8 ▶ $return = $insert_return if $insert_return;
Branch: 1 branch
BranchTrueFalseTotal
if $insert_return 0 8 8
432 }
433  
434 12 return $return;
435 }
436  
437 sub update_relationships {
438 4 ▶ my ( $self, %args ) = @_;
Subroutine update_relationships: called CC=4 SCAR=15.2
Pod undocumented
439  
440 4 my $dbh = $self->dbh;
441 4 $dbh->begin_work;
442  
443 4 my ($ret, $e, $failed);
444 {
445 4 local $@;
446 4 eval { $ret = $self->_update_relationships( %args ); 1 }
447 4 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $ret = $self->_update_relationships(%args); 1 } } or do { ($failed, $e) = (1, $@ || "Unknown error") } 0 4 4
448 0 ▶ ($failed, $e) = (1, $@||'Unknown error');
Condition0%0/2
$@ || 'Unknown error'
l!l
00
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
449 };
450 }
451 4 ▶ if ( $failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 0 4 4
452 0 $dbh->rollback;
453 0 die $e;
454 }
455  
456 4 $dbh->commit;
457  
458 4 return $ret;
459 }
460  
461 sub delete : method {
462 3 ▶ my ( $self, %args ) = @_;
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
463 3 my ( $type, $id ) = @args{qw< type id >};
464  
465 3 my $table_obj = $self->tables->{$type};
466 3 my $stmt = $table_obj->delete_stmt(
467 table => $type,
468 where => { id => $id },
469 );
470  
471 3 my $sth = $self->_db_execute( $stmt );
472  
473 3 return;
474 }
475  
476 sub delete_relationships {
477 4 ▶ my ( $self, %args ) = @_;
Subroutine delete_relationships: called CC=4 SCAR=15.2
Pod undocumented
478  
479 4 my $dbh = $self->dbh;
480 4 $dbh->begin_work;
481  
482 4 my ($ret, $e, $failed);
483 {
484 4 local $@;
485 4 eval { $ret = $self->_delete_relationships( %args ); 1 }
486 4 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $ret = $self->_delete_relationships(%args); 1 } } or do { ($failed, $e) = (1, $@ || "Unknown error") } 0 4 4
487 0 ▶ ($failed, $e) = (1, $@||'Unknown error');
Condition0%0/2
$@ || 'Unknown error'
l!l
00
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
488 };
489 }
490 4 ▶ if ( $failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 0 4 4
491 0 $dbh->rollback;
492 0 die $e;
493 }
494  
495 4 $dbh->commit;
496  
497 4 return $ret;
498 }
499  
500 sub _delete_relationships {
501 4 ▶ my ( $self, %args ) = @_;
Subroutine _delete_relationships: called CC=5 SCAR=16.1
502 4 my ( $type, $id, $rel_type, $data ) = @args{qw< type id rel_type data >};
503  
504 4 my $table_obj = $self->tables->{$type};
505 4 my $relation_obj = $table_obj->RELATIONS->{$rel_type};
506  
507 4 my $table = $relation_obj->TABLE;
508 4 my $key_type = $relation_obj->TYPE;
509  
510 4 my $id_column = $relation_obj->ID_COLUMN;
511 4 my $rel_id_column = $relation_obj->REL_ID_COLUMN;
512  
513 4 my @all_values;
514 4 foreach my $resource ( @$data ) {
515 4 my $data_type = $resource->{type};
516  
517 4 ▶ if ( $data_type ne $key_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type ne $key_type) 0 4 4
518 0 PONAPI::Exception->throw(
519 message => "Data has type `$data_type`, but we were expecting `$key_type`",
520 bad_request_data => 1,
521 );
522 }
523  
524 my $delete_where = {
525 $id_column => $id,
526 $rel_id_column => $resource->{id},
527 4 };
528  
529 4 push @all_values, $delete_where;
530 }
531  
532 4 my $ret = PONAPI_UPDATED_NORMAL;
533  
534 4 my $rows_modified = 0;
535 DELETE:
536 4 foreach my $where ( @all_values ) {
537 4 my $stmt = $relation_obj->delete_stmt(
538 table => $table,
539 where => $where,
540 );
541  
542 4 my $sth = $self->_db_execute( $stmt );
543 4 $rows_modified += $sth->rows;
544 }
545  
546 4 ▶ $ret = PONAPI_UPDATED_NOTHING if !$rows_modified;
Branch: 1 branch
BranchTrueFalseTotal
unless $rows_modified 2 2 4
547  
548 4 return $ret;
549 }
550  
551  
552 ## --------------------------------------------------------
553  
554 sub _add_resources {
555 60 ▶ my ( $self, %args ) = @_;
Subroutine _add_resources: called CC=4 SCAR=13.9
556 my ( $doc, $stmt, $type ) =
557 60 @args{qw< document stmt type >};
558  
559 60 my $sth = $self->_db_execute( $stmt );
560  
561 60 ▶ while ( my $row = $sth->fetchrow_hashref() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 78 60 138
562 78 my $id = delete $row->{id};
563 78 my $rec = $doc->add_resource( type => $type, id => $id );
564 78 $rec->add_attribute( $_ => $row->{$_} ) for keys %{$row};
565 78 $rec->add_self_link;
566  
567 78 $self->_add_resource_relationships($rec, %args);
568 }
569  
570 $self->_add_pagination_links(
571 page => $args{page},
572 rows => scalar $sth->rows,
573 document => $doc,
574 60 ▶ ) if $args{page};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"page"} 6 54 60
575  
576 60 return;
577 }
578  
579 sub _add_pagination_links {
580 7 ▶ my ($self, %args) = @_;
Subroutine _add_pagination_links: called CC=4 SCAR=13.9
581 7 my ($page, $rows_fetched, $document) = @args{qw/page rows document/};
582 7 ▶ $rows_fetched ||= -1;
Condition100%2/2
$rows_fetched ||= -1
l!l
61
MC/DC100%1/1
$rows_fetched ||= -1
$rows_fetched
Truth table100%2/2
A
A= $rows_fetched
Aresult
11
00
583  
584 7 my ($offset, $limit) = @{$page}{qw/offset limit/};
585  
586 7 my %current = %$page;
587 7 my %first = ( %current, offset => 0, );
588 7 my (%previous, %next);
589  
590 7 ▶ if ( ($offset - $limit) >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset - $limit >= 0) 4 3 7
591 4 %previous = %current;
592 4 $previous{offset} -= $current{limit};
593 }
594  
595 7 ▶ if ( $rows_fetched >= $limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows_fetched >= $limit) 6 1 7
596 6 %next = %current;
597 6 $next{offset} += $limit;
598 }
599  
600 $document->add_pagination_links(
601 7 first => \%first,
602 self => \%current,
603 prev => \%previous,
604 next => \%next,
605 );
606 }
607  
608 sub _validate_page {
609 7 ▶ my ($self, $page) = @_;
Subroutine _validate_page: called CC=6 SCAR=18.7
610  
611 exists $page->{limit}
612 7 ▶ or PONAPI::Exception->throw(message => "Limit missing for `page`");
Branch: 1 branch
BranchTrueFalseTotal
exists $page->{"limit"} or "PONAPI::Exception"->throw("message", "Limit missing for `page`") 0 7 7
613  
614 7 ▶ $page->{limit} =~ /\A[0-9]+\z/
Branch: 1 branch
BranchTrueFalseTotal
$page->{"limit"} =~ /\A[0-9]+\z/ or "PONAPI::Exception"->throw("message", qq[Bad limit value ($page->{"limit"}) in `page`]) 0 7 7
615 or PONAPI::Exception->throw(message => "Bad limit value ($page->{limit}) in `page`");
616  
617 7 ▶ !exists $page->{offset} || ($page->{offset} =~ /\A[0-9]+\z/)
Branch: 1 branch
BranchTrueFalseTotal
not exists $page->{"offset"} or $page->{"offset"} =~ /\A[0-9]+\z/ or "PONAPI::Exception"->throw("message", "Bad offset value in `page`") 0 7 7
Condition66%2/3
not exists $page->{"offset"} or $page->{"offset"} =~ /\A[0-9]+\z/
l!l&&r!l&&!r
160
MC/DC0%0/2
not exists $page->{"offset"} or $page->{"offset"} =~ /\A[0-9]+\z/
not exists $page->{"offset"} $page->{"offset"} =~ /\A[0-9]+\z/
Truth table66%2/3
not(A) or B
A= not exists $page->{"offset"}B= $page->{"offset"} =~ /\A[0-9]+\z/
ABresult
1X1
011
000
618 or PONAPI::Exception->throw(message => "Bad offset value in `page`");
619  
620 7 ▶ $page->{offset} ||= 0;
Condition100%2/2
$page->{"offset"} ||= 0
l!l
52
MC/DC100%1/1
$page->{"offset"} ||= 0
$page->{"offset"}
Truth table100%2/2
A
A= $page->{"offset"}
Aresult
11
00
621  
622 7 return;
623 }
624  
625 sub _add_resource_relationships {
626 78 ▶ my ( $self, $rec, %args ) = @_;
Subroutine _add_resource_relationships: called CC=8 SCAR=20.8
627 78 my $doc = $rec->find_root;
628 78 my $type = $rec->type;
629 78 my $fields = $args{fields};
630 78 my %include = map { $_ => 1 } @{ $args{include} };
631  
632 # Do not add sort or page here -- those were for the primary resource
633 # *only*.
634 78 my $rels = $self->_fetchall_relationships(
635 type => $type,
636 id => $rec->id,
637 document => $doc,
638 fields => $fields,
639 );
640 78 ▶ $rels or return;
Branch: 1 branch
BranchTrueFalseTotal
$rels or return 0 78 78
641  
642 78 for my $r ( keys %$rels ) {
643 122 my $relationship = $rels->{$r};
644 122 ▶ @$relationship or next;
Branch: 1 branch
BranchTrueFalseTotal
@$relationship or next 24 98 122
645  
646 98 my $rel_type = $relationship->[0]{type};
647  
648 # skipping the relationship if the type has an empty `fields` set
649 98 ▶ next if exists $fields->{$rel_type} and !@{ $fields->{$rel_type} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $fields->{$rel_type} and not @{$fields->{$rel_type};} 2 96 98
Condition100%3/3
exists $fields->{$rel_type} and not @{$fields->{$rel_type};}
!ll&&!rl&&r
9242
MC/DC100%2/2
exists $fields->{$rel_type} and not @{$fields->{$rel_type};}
exists $fields->{$rel_type} not @{$fields->{$rel_type};}
Truth table100%3/3
A and not(B)
A= exists $fields->{$rel_type}B= not @{$fields->{$rel_type};}
ABresult
0X0
100
111
650  
651 96 my $one_to_many = $self->has_one_to_many_relationship($type, $r);
652 96 for ( @$relationship ) {
653 119 $rec->add_relationship( $r, $_, $one_to_many )
654 ->add_self_link
655 ->add_related_link;
656 }
657  
658 $self->_add_included(
659 $rel_type, # included type
660 19 +[ map { $_->{id} } @$relationship ], # included ids
661 %args # filters / fields / etc.
662 96 ▶ ) if exists $include{$r};
Branch: 1 branch
BranchTrueFalseTotal
if exists $include{$r} 16 80 96
663 }
664  
665 78 return;
666 }
667  
668 sub _add_included {
669 16 ▶ my ( $self, $type, $ids, %args ) = @_;
Subroutine _add_included: called CC=2 SCAR=6.9
670 16 my ( $doc, $filter, $fields ) = @args{qw< document filter fields >};
671  
672 16 $filter->{id} = $ids;
673  
674 # Do NOT add sort -- sort here was for the *main* resource!
675 16 my $stmt = $self->tables->{$type}->select_stmt(
676 type => $type,
677 filter => $filter,
678 fields => $fields,
679 );
680  
681 16 my $sth = $self->_db_execute( $stmt );
682  
683 16 ▶ while ( my $inc = $sth->fetchrow_hashref() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $inc = $sth->fetchrow_hashref) 18 16 34
684 18 my $id = delete $inc->{id};
685 $doc->add_included( type => $type, id => $id )
686 18 ->add_attributes( %{$inc} )
687 ->add_self_link;
688 }
689 }
690  
691 sub _find_resource_relationships {
692 10 ▶ my ( $self, %args ) = @_;
Subroutine _find_resource_relationships: called CC=4 SCAR=16.4
693 10 my $rel_type = $args{rel_type};
694  
695 10 ▶ if ( $rel_type and my $rels = $self->_fetchall_relationships(%args) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_type and my $rels = $self->_fetchall_relationships(%args)) 10 0 10
Condition33%1/3
$rel_type and my $rels = $self->_fetchall_relationships(%args)
!ll&&!rl&&r
0010
MC/DC0%0/2
$rel_type and my $rels = $self->_fetchall_relationships(%args)
$rel_type my $rels = $self->_fetchall_relationships(%args)
Truth table33%1/3
A and B
A= $rel_typeB= my $rels = $self->_fetchall_relationships(%args)
ABresult
0X0
100
111
696 10 ▶ return $rels->{$rel_type} if exists $rels->{$rel_type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rels->{$rel_type} 10 0 10
697 }
698  
699 0 return [];
700 }
701  
702 sub _fetchall_relationships {
703 88 ▶ my ( $self, %args ) = @_;
Subroutine _fetchall_relationships: called CC=5 SCAR=16.1
704 88 my ( $type, $id ) = @args{qw< type id >};
705  
706 # we don't want to autovivify $args{fields}{$type}
707 # since it will be checked in order to know whether
708 # the key existed in the original fields argument
709 my %type_fields = exists $args{fields}{$type}
710 88 ▶ ? map { $_ => 1 } @{ $args{fields}{$type} }
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'fields'}{$type} ? : 20 68 88
711 : ();
712  
713 88 my %ret;
714 my @errors;
715  
716 88 for my $name ( keys %{ $self->tables->{$type}->RELATIONS } ) {
717 # If we have fields, and this relationship is not mentioned, skip
718 # it.
719 157 ▶ next if keys %type_fields > 0 and !exists $type_fields{$name};
Branch: 1 branch
BranchTrueFalseTotal
if keys %type_fields > 0 and not exists $type_fields{$name} 25 132 157
Condition100%3/3
keys %type_fields > 0 and not exists $type_fields{$name}
!ll&&!rl&&r
1191325
MC/DC100%2/2
keys %type_fields > 0 and not exists $type_fields{$name}
keys %type_fields > 0 not exists $type_fields{$name}
Truth table100%3/3
A and not(B)
A= keys %type_fields > 0B= not exists $type_fields{$name}
ABresult
0X0
100
111
720  
721 132 my $table_obj = $self->tables->{$type};
722 132 my $rel_table_obj = $table_obj->RELATIONS->{$name};
723 132 my $rel_type = $rel_table_obj->TYPE;
724 132 my $rel_table = $rel_table_obj->TABLE;
725 132 my $id_column = $rel_table_obj->ID_COLUMN;
726 132 my $rel_id_column = $rel_table_obj->REL_ID_COLUMN;
727  
728 132 my $stmt = $rel_table_obj->select_stmt(
729 %args,
730 type => $rel_table,
731 filter => { $id_column => $id },
732 fields => [ $rel_id_column ],
733 );
734  
735 132 my $sth = $self->_db_execute( $stmt );
736  
737 $ret{$name} = +[
738 map +{ type => $rel_type, id => $_->{$rel_id_column} },
739 132 @{ $sth->fetchall_arrayref({}) }
740 ];
741 }
742  
743 88 return \%ret;
744 }
745  
746 # Might not be there?
747 8 my $sqlite_constraint_failed = do {
748 8 local $@;
749 8 ▶ eval { SQLITE_CONSTRAINT() } // undef;
Condition50%1/2
eval { // undef
l!l
80
MC/DC0%0/1
eval { do { &SQLITE_CONSTRAINT() } } // undef
eval { do { &SQLITE_CONSTRAINT() } }
Truth table50%1/2
A
A= eval { do { &SQLITE_CONSTRAINT() } }
Aresult
11
00
750 };
751 sub _db_execute {
752 255 ▶ my ( $self, $stmt ) = @_;
Subroutine _db_execute: called CC=12 SCAR=27.9
753  
754 255 my ($sth, $ret, $failed, $e);
755 {
756 255 local $@;
757 eval {
758 255 $sth = $self->dbh->prepare($stmt->{sql});
759 255 ▶ $ret = $sth->execute(@{ $stmt->{bind} || [] });
Branch: 1 branch
BranchTrueFalseTotal
$stmt->{"bind"} or [] 0 255 255
760 # This should never happen, since the DB handle is
761 # created with RaiseError.
762 252 ▶ die $DBI::errstr if !$ret;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 252 252
763 252 1;
764 255 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $sth = $self->dbh->prepare($stmt->{"sql"}); $ret = $sth->execute(@{$stmt->{"bind"} or [];}); die $DBI::errstr unless $ret; 1 } } or do { $failed = 1; $e = $@ || "Unknown error" } 3 252 255
765 3 $failed = 1;
766 3 ▶ $e = $@ || 'Unknown error';
Condition50%1/2
$@ || 'Unknown error'
l!l
30
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
767 };
768 };
769 255 ▶ if ( $failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 3 252 255
770 3 ▶ my $errstr = $DBI::errstr || "Unknown SQL error";
Condition50%1/2
$DBI::errstr || 'Unknown SQL error'
l!l
30
MC/DC0%0/1
$DBI::errstr || 'Unknown SQL error'
$DBI::errstr
Truth table50%1/2
A
A= $DBI::errstr
Aresult
11
00
771 3 ▶ my $err_id = $DBI::err || 0;
Condition50%1/2
$DBI::err || 0
l!l
30
MC/DC0%0/1
$DBI::err || 0
$DBI::err
Truth table50%1/2
A
A= $DBI::err
Aresult
11
00
772  
773 3 my $message;
774 3 ▶ if ( $sqlite_constraint_failed && $err_id && $err_id == $sqlite_constraint_failed ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sqlite_constraint_failed and $err_id and $err_id == $sqlite_constraint_failed) { } 3 0 3
elsif ($err_id) { } 0 0 0
Condition33%2/6
$sqlite_constraint_failed and $err_id and $err_id == $sqlite_constraint_failed
!ll&&!rl&&r
003
$sqlite_constraint_failed and $err_id
!ll&&!rl&&r
003
MC/DC0%0/3
$sqlite_constraint_failed and $err_id and $err_id == $sqlite_constraint_failed
$sqlite_constraint_failed $err_id $err_id == $sqlite_constraint_failed
Truth table25%1/4
A and B and C
A= $sqlite_constraint_failedB= $err_idC= $err_id == $sqlite_constraint_failed
ABCresult
0XX0
10X0
1100
1111
775 3 PONAPI::Exception->throw(
776 message => "Table constraint failed: $errstr",
777 sql_error => 1,
778 status => 409,
779 );
780 }
781 elsif ( $err_id ) {
782 0 PONAPI::Exception->throw(
783 message => $errstr,
784 sql_error => 1,
785 );
786 }
787 else {
788 0 PONAPI::Exception->throw(
789 message => "Non-SQL error while running query? $e"
790 )
791 }
792 };
793  
794 252 return $sth;
795 }
796  
797 8 __PACKAGE__->meta->make_immutable;
798 8 ▶no Moose; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
799  
800 __END__
801  
802 =pod
803  
804 =encoding UTF-8
805  
806 =head1 NAME
807  
808 Test::PONAPI::Repository::MockDB - mock repository class
809  
810 =head1 VERSION
811  
812 version 0.003002
813  
814 =head1 AUTHORS
815  
816 =over 4
817  
818 =item *
819  
820 Mickey Nasriachi <mickey@cpan.org>
821  
822 =item *
823  
824 Stevan Little <stevan@cpan.org>
825  
826 =item *
827  
828 Brian Fraser <hugmeir@cpan.org>
829  
830 =back
831  
832 =head1 COPYRIGHT AND LICENSE
833  
834 This software is copyright (c) 2017 by Mickey Nasriachi, Stevan Little, Brian Fraser.
835  
836 This is free software; you can redistribute it and/or modify it under
837 the same terms as the Perl 5 programming language system itself.
838  
839 =cut
« PONAPI/DAO/Request/Role/HasFields.pm ↑ Summary PONAPI/DAO/Request/Role/HasDataMethods.pm »