Sims/Item.pm

filter: Statementstmt 90.2% 435 / 482 Branchbran 85.3% 198 / 232 Conditioncond 80.9% 85 / 105 MC/DCmcdc 66.6% 40 / 60 Subroutinesub 100% 41 / 41 Podpod 22.5% 7 / 31 totaltotal 84.7% 806 / 951 CC 171 SCAR 27.2
CC
171
Cov
88%
populate_parent
31.2
populate_column
27.8
resolve_direct_values
21.9
CRAP 15.2
« Sims.pm ↑ Summary Sims/Runner.pm »
1 # This class exists to represent a row requested (and subsequently created) by
2 # the Sims. It will have a link back to a Sims::Source which will have the link
3 # back to the $schema object.
4  
5 package DBIx::Class::Sims::Item;
6  
7 54 ▶use 5.010_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 54 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 54 ▶use DDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 54 ▶use List::PowerSet qw(powerset);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 54 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 54 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 54 ▶use DBIx::Class::Sims::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
18 normalize_aoh reftype compare_values
19 54 );
20  
21 872 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
22 872 my $class = shift;
23 872 my $self = bless {@_}, $class;
24 872 $self->initialize;
25 872 return $self;
26 }
27  
28 872 ▶sub initialize {
Subroutine initialize: called CC=2 SCAR=6.9
Pod undocumented
29 872 my $self = shift;
30  
31 872 $self->{original_spec} = MyCloner::clone($self->spec);
32  
33 # Lots of code assumes __META__ exists.
34 # TODO: Should we check for _META__ or __META_ or __MTA__ etc?
35 872 ▶ $self->{meta} = $self->spec->{__META__} // {};
Condition100%2/2
$self->spec->{"__META__"} // {}
l!l
79577
MC/DC100%1/1
$self->spec->{"__META__"} // {}
$self->spec->{"__META__"}
Truth table100%2/2
A
A= $self->spec->{"__META__"}
Aresult
11
00
36  
37 872 $self->{create} = {};
38 872 $self->{parents} = {};
39  
40 872 $self->{still_to_use} = { map { $_ => 1 } keys %{$self->spec} };
41 872 delete $self->{still_to_use}{__META__};
42  
43 872 $self->{skip_relationship} = {};
44  
45 872 return;
46 }
47  
48 7370 ▶sub runner { $_[0]{runner} }
Subroutine runner: called CC=1 SCAR=0.0
Pod undocumented
49 15105 ▶sub source { $_[0]{source} }
Subroutine source: called CC=1 SCAR=0.0
Pod documented
50 16950 ▶sub spec { $_[0]{spec} }
Subroutine spec: called CC=1 SCAR=0.0
Pod documented
51 1953 ▶sub meta { $_[0]{meta} }
Subroutine meta: called CC=1 SCAR=0.0
Pod undocumented
52  
53 1763 ▶sub source_name { shift->source->name }
Subroutine source_name: called CC=1 SCAR=0.0
Pod undocumented
54  
55 11 ▶sub allow_pk_set_value { shift->meta->{allow_pk_set_value} }
Subroutine allow_pk_set_value: called CC=1 SCAR=0.0
Pod undocumented
56 114 ▶sub set_allow_pk_to {
Subroutine set_allow_pk_to: called CC=2 SCAR=6.9
Pod undocumented
57 114 my $self = shift;
58 114 my ($proto) = @_;
59  
60 $self->meta->{allow_pk_set_value} = blessed($proto)
61 ? $proto->meta->{allow_pk_set_value}
62 114 ▶ : $proto;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($proto) ? : 93 21 114
63  
64 114 return;
65 }
66  
67 14463 ▶sub row {
Subroutine row: called CC=2 SCAR=6.9
Pod undocumented
68 14463 my $self = shift;
69 14463 ▶ $self->{row} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 761 13702 14463
70 14463 return $self->{row};
71 }
72  
73 5057 ▶sub has_value {
Subroutine has_value: called CC=2 SCAR=6.9
Pod documented
74 5057 my $self = shift;
75 5057 my ($col) = @_;
76 5057 ▶ return exists $self->{create}{$col} || exists $self->spec->{$col};
Condition100%3/3
exists $self->{'create'}{$col} || exists $self->spec->{$col}
l!l&&r!l&&!r
86211913004
MC/DC100%2/2
exists $self->{'create'}{$col} || exists $self->spec->{$col}
exists $self->{'create'}{$col} exists $self->spec->{$col}
Truth table100%3/3
A || B
A= exists $self->{'create'}{$col}B= exists $self->spec->{$col}
ABresult
1X1
011
000
77 }
78 1333 ▶sub value {
Subroutine value: called CC=3 SCAR=11.0
Pod documented
79 1333 my $self = shift;
80 1333 my ($col) = @_;
81  
82 1333 ▶ return unless $self->has_value($col);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_value($col) 47 1286 1333
83 return exists $self->{create}{$col}
84 ? $self->{create}{$col}
85 1286 ▶ : $self->spec->{$col};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'create'}{$col} ? : 518 768 1286
86 }
87 974 ▶sub set_value {
Subroutine set_value: called CC=1 SCAR=0.0
Pod documented
88 974 my $self = shift;
89 974 my ($col, $val) = @_;
90 974 $self->{create}{$col} = $val;
91 }
92  
93 90 ▶sub has_parent_values {
Subroutine has_parent_values: called CC=4 SCAR=13.9
Pod undocumented
94 90 my $self = shift;
95  
96 90 foreach my $r ( $self->source->parent_relationships ) {
97 # FIXME: Is there a problem if there's a multi-col relationship with the
98 # same name as another unrelated column?
99 22 ▶ return 1 if $self->spec->{$r->name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->spec->{$r->name} 7 15 22
100  
101 # We need to have an entry for all the columns in the parent relationship.
102 return 1 if ! grep {
103 15 ▶ ! exists $self->spec->{$_}
Branch: 1 branch
BranchTrueFalseTotal
unless grep {not exists $self->spec->{$_};} $r->self_fk_cols 1 14 15
104 } $r->self_fk_cols;
105 }
106  
107 82 return;
108 }
109  
110 1469 ▶sub make_jsonable {
Subroutine make_jsonable: called CC=5 SCAR=16.3
Pod undocumented
111 1469 my $self = shift;
112 1469 my ($item) = @_;
113  
114 # Deference all scalar references. This happens when we retrieve a row and
115 # it has something like { update_time => \'current_timestamp' }
116 $item->{$_} = reftype($item->{$_}) eq 'SCALAR'
117 0 ? ${$item->{$_}} : $item->{$_}
118 1469 ▶ for keys %{$item};
Branch: 1 branch
BranchTrueFalseTotal
reftype($item->{$_}) eq 'SCALAR' ? : 0 2711 2711
119  
120 # Stringify everything, otherwise JSON::MaybeXS gets confused
121 $item->{$_} = defined $item->{$_}
122 ? '' . $item->{$_} : undef
123 1469 ▶ foreach keys %{$item};
Branch: 1 branch
BranchTrueFalseTotal
defined $item->{$_} ? : 2693 18 2711
124  
125 1469 return $item;
126 }
127  
128 ################################################################################
129 #
130 # These are the helper methods
131 #
132 ################################################################################
133  
134 2999 ▶sub is_real_value {
Subroutine is_real_value: called CC=4 SCAR=13.9
Pod undocumented
135 2999 my $self = shift;
136 2999 my ($col) = @_;
137 2999 ▶ return unless $self->has_value($col);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_value($col) 2461 538 2999
138 538 my $v = $self->value($col);
139 538 ▶ return 1 unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 36 502 538
140 502 ▶ return if ref($v);
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 4 498 502
141 498 return 1;
142 }
143  
144 2696 ▶sub build_searcher_for_constraints {
Subroutine build_searcher_for_constraints: called CC=6 SCAR=17.9
Pod undocumented
145 2696 my $self = shift;
146 2696 my (@constraints) = @_;
147  
148 2696 my $to_find = {};
149 2696 my $matched_all_columns = 1;
150 2696 foreach my $c ( map { @$_ } @constraints ) {
151 2999 ▶ unless ($self->is_real_value($c->name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_real_value($c->name)) 2465 534 2999
152 2465 $matched_all_columns = 0;
153 2465 last;
154 }
155  
156 # Undefined values cannot be searched over because undefined values appear
157 # different to the UK, but appear the same in a query. Therefore, this
158 # searcher cannot work.
159 534 ▶ $to_find->{$c->name} = $self->value($c->name) // return;
Condition100%2/2
$self->value($c->name) // (return)
l!l
49836
MC/DC100%1/1
$self->value($c->name) // (return)
$self->value($c->name)
Truth table100%2/2
A
A= $self->value($c->name)
Aresult
11
00
160 }
161  
162 2660 ▶ return $to_find if keys(%$to_find) && $matched_all_columns;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$to_find and $matched_all_columns 195 2465 2660
Condition100%3/3
keys %$to_find and $matched_all_columns
!ll&&!rl&&r
2365100195
MC/DC100%2/2
keys %$to_find and $matched_all_columns
keys %$to_find $matched_all_columns
Truth table100%3/3
A and B
A= keys %$to_findB= $matched_all_columns
ABresult
0X0
100
111
163 2465 return;
164 }
165  
166 16 ▶sub unique_id {
Subroutine unique_id: called CC=2 SCAR=7.0
Pod undocumented
167 16 my $self = shift;
168 16 my ($row) = @_;
169  
170 16 my @cols = $row->result_source->columns;
171 16 my %data = $row->get_columns;
172 16 my @vals = @data{@cols};
173 16 ▶ return ref($row) . join(',',@cols) . join(',',map {$_//''} @vals);
Condition50%1/2
$_ // ""
l!l
600
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
174 }
175  
176 2344 ▶sub find_unique_match {
Subroutine find_unique_match: called CC=11 SCAR=24.0
Pod undocumented
177 2344 my $self = shift;
178  
179 2344 my @uniques = $self->source->uniques;
180  
181 2344 my $rs = $self->source->resultset;
182  
183 2344 ▶ if ( my $to_find = $self->build_searcher_for_constraints(@uniques) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $to_find = $self->build_searcher_for_constraints(@uniques)) 75 2269 2344
184 75 my $row = $rs->search($to_find, { rows => 1 })->first;
185 75 ▶ if ($row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row) 4 71 75
186 4 push @{$self->runner->{duplicates}{$self->source_name}}, {
187 criteria => [$to_find],
188 found => { $row->get_columns },
189 };
190 4 $self->row($row);
191  
192 4 $self->{trace}{find} = $self->{runner}{ids}{find}++;
193 4 $self->{trace}{row} = $self->make_jsonable( { $row->get_columns } );
194 4 $self->{trace}{criteria} = [$to_find];
195 4 $self->{trace}{unique} = 1;
196  
197 4 return;
198 }
199 }
200  
201 # Search through all the possible iterations of unique keys.
202 # * Don't populate $self->{create}
203 # * If found with all keys, great.
204 # * Otherwise, keep track of what we find for each combination (if at all)
205 # * If we have multiple finds, die.
206 # TODO: Use List::Powerset->powerset_lazy() instead of powerset()
207 2340 my %rows_found;
208 2340 foreach my $bundle (@{powerset(@uniques)}) {
209 # Skip the all (already handled) and the empty (unneeded).
210 5032 ▶ next if @$bundle == 0 || @$bundle == @uniques;
Branch: 1 branch
BranchTrueFalseTotal
if @$bundle == 0 or @$bundle == @uniques 4680 352 5032
Condition100%3/3
@$bundle == 0 or @$bundle == @uniques
l!l&&r!l&&!r
23402340352
MC/DC100%2/2
@$bundle == 0 or @$bundle == @uniques
@$bundle == 0 @$bundle == @uniques
Truth table100%3/3
A or B
A= @$bundle == 0B= @$bundle == @uniques
ABresult
1X1
011
000
211  
212 352 ▶ my $finder = $self->build_searcher_for_constraints(@$bundle)
Branch: 1 branch
BranchTrueFalseTotal
my $finder = $self->build_searcher_for_constraints(@$bundle) or next 120 232 352
213 or next;
214  
215 120 my $row = $rs->search($finder, { rows => 1 })->first;
216 120 ▶ if ($row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row) 16 104 120
217 16 my $unique_id = $self->unique_id($row);
218 16 ▶ $rows_found{$unique_id} //= {
Condition100%2/2
$rows_found{$unique_id} //= {"row", $row, "finders", []}
l!l
214
MC/DC100%1/1
$rows_found{$unique_id} //= {"row", $row, "finders", []}
$rows_found{$unique_id}
Truth table100%2/2
A
A= $rows_found{$unique_id}
Aresult
11
00
219 row => $row,
220 finders => [],
221 };
222 16 push @{$rows_found{$unique_id}{finders}}, $finder;
223 }
224 }
225  
226 2340 ▶ if (keys(%rows_found) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %rows_found > 1) 2 2338 2340
227 2 die "Rows found by multiple unique constraints";
228 }
229  
230 2338 ▶ if (keys(%rows_found) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %rows_found == 1) 10 2328 2338
231 10 my $x = (values %rows_found)[0];
232 10 my ($finders, $row) = @{$x}{qw(finders row)};
233 10 push @{$self->runner->{duplicates}{$self->source_name}}, {
234 criteria => $finders,
235 found => { $row->get_columns },
236 };
237 10 $self->row($row);
238  
239 10 $self->{trace}{find} = $self->{runner}{ids}{find}++;
240 10 $self->{trace}{row} = $self->make_jsonable( { $row->get_columns } );
241 10 $self->{trace}{criteria} = $finders;
242 10 $self->{trace}{unique} = 1;
243  
244 10 return;
245 }
246  
247 2328 return;
248 }
249  
250 132 ▶sub find_any_match {
Subroutine find_any_match: called CC=5 SCAR=16.1
Pod undocumented
251 132 my $self = shift;
252  
253 132 my $rs = $self->source->resultset;
254  
255 # This is for handling the case where a FK is within a UK in the child. This
256 # ensures we cannot select a parent whose PK is already in use in the child.
257 # We don't check for this in find_unique_match() because the UK is all that
258 # matters there.
259 132 ▶ if ($self->meta->{restriction}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->meta->{"restriction"}) 24 108 132
260 24 my $c = $self->meta->{restriction};
261 24 $rs = $rs->search( $c->{cond}, $c->{extra} );
262 }
263  
264 132 my $cond = {};
265 132 foreach my $colname ( map { $_->name } $self->source->columns ) {
266 286 ▶ next unless $self->has_value($colname);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_value($colname) 174 112 286
267 112 $cond->{'me.' . $colname} = $self->value($colname);
268 }
269  
270 132 my $row = $rs->search($cond, { rows => 1 })->single;
271 132 ▶ if ($row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row) 39 93 132
272 39 $self->row($row);
273  
274 39 $self->{trace}{find} = $self->{runner}{ids}{find}++;
275 39 $self->{trace}{row} = $self->make_jsonable( { $row->get_columns } );
276 39 $self->{trace}{criteria} = [ $cond ];
277 39 $self->{trace}{unique} = 0;
278 }
279  
280 132 return $self->row;
281 }
282  
283 3240 ▶sub attempt_to_find {
Subroutine attempt_to_find: called CC=20 SCAR=30.0
Pod undocumented
284 3240 my $self = shift;
285 3240 my ($opts) = @_;
286  
287 3240 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
2519721
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
288 3240 ▶ $opts->{unique} //= 1;
Condition100%2/2
$opts->{"unique"} //= 1
l!l
8712369
MC/DC100%1/1
$opts->{"unique"} //= 1
$opts->{"unique"}
Truth table100%2/2
A
A= $opts->{"unique"}
Aresult
11
00
289 3240 ▶ $opts->{any} //= 1;
Condition100%2/2
$opts->{"any"} //= 1
l!l
16481592
MC/DC100%1/1
$opts->{"any"} //= 1
$opts->{"any"}
Truth table100%2/2
A
A= $opts->{"any"}
Aresult
11
00
290 3240 ▶ $opts->{no_parent_values} //= 0;
Condition100%2/2
$opts->{"no_parent_values"} //= 0
l!l
8712369
MC/DC100%1/1
$opts->{"no_parent_values"} //= 0
$opts->{"no_parent_values"}
Truth table100%2/2
A
A= $opts->{"no_parent_values"}
Aresult
11
00
291  
292 # First, find_unique_match.
293 # If row, handle die_on_unique_mismatch
294 3240 ▶ if ($opts->{unique}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"unique"}) 2369 871 3240
295 2369 ▶ unless ($self->row) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->row) 2344 25 2369
296 2344 $self->find_unique_match;
297 2342 ▶ if ($self->row && $self->runner->{die_on_unique_mismatch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->row and $self->runner->{"die_on_unique_mismatch"}) 13 2329 2342
Condition100%3/3
$self->row and $self->runner->{"die_on_unique_mismatch"}
!ll&&!rl&&r
2328113
MC/DC100%2/2
$self->row and $self->runner->{"die_on_unique_mismatch"}
$self->row $self->runner->{"die_on_unique_mismatch"}
Truth table100%3/3
A and B
A= $self->rowB= $self->runner->{"die_on_unique_mismatch"}
ABresult
0X0
100
111
298 13 my @failed;
299 13 foreach my $c ( $self->source->columns ) {
300 46 my $col_name = $c->name;
301  
302 46 ▶ next unless $self->has_value($col_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_value($col_name) 27 19 46
303  
304 19 my $row_value = $self->row->get_column($col_name);
305 19 my $spec_value = $self->value($col_name);
306 19 ▶ unless (compare_values($row_value, $spec_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (compare_values($row_value, $spec_value)) 1 18 19
307 1 ▶ push @failed, "\t$col_name: row(@{[$row_value//'[undef]']}) spec(@{[$spec_value//'[undef]']})\n";
Condition50%2/4
$row_value // "[undef]"
l!l
10
$spec_value // "[undef]"
l!l
10
MC/DC0%0/1
$row_value // "[undef]"
$row_value
MC/DC0%0/1
$spec_value // "[undef]"
$spec_value
Truth table50%1/2
A
A= $row_value
Aresult
11
00
Truth table50%1/2
A
A= $spec_value
Aresult
11
00
308 }
309 }
310 13 ▶ if (@failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (@failed) 1 12 13
311 1 die "ERROR Retrieving unique @{[$self->source_name]} (".np($self->spec).") (".np($self->{create}).")\n" . join('', sort @failed) . $/ . np($self->runner->{duplicates}{$self->source_name}[-1]{criteria});
312 }
313 }
314 }
315 }
316  
317 # Else, find_any_match, but only if parent_values matches
318 3237 ▶ if ($opts->{any}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"any"}) 1592 1645 3237
319 1592 ▶ if ( ! $self->row && ! $self->meta->{create} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->row or $self->meta->{"create"}) 140 1452 1592
Condition100%3/3
$self->row or $self->meta->{"create"}
l!l&&r!l&&!r
131439140
MC/DC100%2/2
$self->row or $self->meta->{"create"}
$self->row $self->meta->{"create"}
Truth table100%3/3
A or B
A= $self->rowB= $self->meta->{"create"}
ABresult
1X1
011
000
320 140 ▶ if ( $opts->{no_parent_values} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'no_parent_values'}) { } 90 50 140
321 90 ▶ $self->find_any_match if ! $self->has_parent_values;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_parent_values 82 8 90
322 }
323 else {
324 50 $self->find_any_match;
325 }
326 }
327 }
328  
329 3237 return $self->row;
330 }
331  
332 832 ▶sub resolve_direct_values {
Subroutine resolve_direct_values: called CC=20 SCAR=30.9
Pod undocumented
333 832 my $self = shift;
334  
335 832 my @unknowns;
336 832 ▶ while ( my ($k,$v) = each %{$self->spec} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->spec;}) 1665 830 2495
337 # Ignore __META__
338 1665 ▶ next if $k eq '__META__';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "__META__" 791 874 1665
339  
340 # If $k is a relationship, handle that.
341 874 ▶ if ( my $r = $self->source->relationship($k) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $r = $self->source->relationship($k)) { } 73 801 874
elsif (my $c = $self->source->column($k)) { } 795 6 801
342 73 ▶ if (blessed($v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($v)) { } 4 69 73
elsif (ref $v eq 'SCALAR') { } 10 59 69
343 4 ▶ die "$k is a multi-column FK, so cannot be directly set\n"
Branch: 1 branch
BranchTrueFalseTotal
if $r->is_multi_col 0 4 4
344 if $r->is_multi_col;
345  
346 4 my $fkcol = $r->foreign_fk_col;
347 4 $self->set_value($r->self_fk_col, $v->get_column($fkcol));
348 4 $self->{skip_relationship}{$r->name} = 1;
349  
350 # Otherwise, the tracer will try and write out a blessed object.
351 4 $self->{trace}{spec}{$r->self_fk_col} = $self->{create}{$r->self_fk_col};
352 }
353 elsif (ref($v) eq 'SCALAR') {
354 10 ▶ die "$k is a multi-column FK, so cannot be directly set\n"
Branch: 1 branch
BranchTrueFalseTotal
if $r->is_multi_col 0 10 10
355 if $r->is_multi_col;
356  
357 10 my $fkcol = $r->foreign_fk_col;
358 $self->set_value($r->self_fk_col, $self->runner->convert_backreference(
359 $self->runner->backref_name($self, $k),
360 10 ${$v},
361 $fkcol,
362 ));
363 }
364 }
365 # If $k is a column,
366 elsif ( my $c = $self->source->column($k) ) {
367 # If $k is in a relationship, find the appropriate one via the class.
368 795 ▶ if ( $c->is_in_fk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->is_in_fk) { } 53 742 795
369 # Find the appropriate FK.
370 53 ▶ if ( my $kls = blessed $v ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $kls = builtin::blessed($v)) { } 46 7 53
elsif (ref $v eq 'SCALAR') { } 2 5 7
371 my @rels = grep {
372 46 $kls eq $_->foreign_class
373 } $c->fks;
374  
375 46 ▶ if ( @rels != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rels != 1) 0 46 46
376 0 die "ERROR: @{[$self->source_name]} Cannot figure out what relationship belongs to $k (@{[np $v]})!\n@{[join ',', sort map{$_->name}@rels]}";
377 }
378 46 my $r = $rels[0];
379  
380 46 ▶ die "$k is a multi-column FK, so cannot be directly set\n"
Branch: 1 branch
BranchTrueFalseTotal
if $r->is_multi_col 0 46 46
381 if $r->is_multi_col;
382  
383 46 $self->set_value($k, $v->get_column($r->foreign_fk_col));
384 46 $self->{skip_relationship}{$_->name} = 1 for $c->fks;
385  
386 # Otherwise, the tracer will try and write out a blessed object.
387 46 $self->{trace}{spec}{$k} = $self->{create}{$k};
388 }
389 elsif (ref($v) eq 'SCALAR') {
390 2 my ($kls_base) = ${$v} =~ /([^:]+)\[/;
391 my @rels = grep {
392 2 my ($k) = $_->foreign_class =~ /([^:]+)$/;
393 2 ${$v} =~ /$k/
394 } $c->fks;
395  
396 2 ▶ if ( @rels != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rels != 1) 0 2 2
397 0 die "ERROR: @{[$self->source_name]} Cannot figure out what relationship belongs to $k (@{[np $v]})!\n@{[join ',', sort map{$_->name}@rels]}";
398 }
399 2 my $r = $rels[0];
400  
401 2 ▶ die "$k is a multi-column FK, so cannot be directly set\n"
Branch: 1 branch
BranchTrueFalseTotal
if $r->is_multi_col 0 2 2
402 if $r->is_multi_col;
403  
404 $self->set_value($k, $self->runner->convert_backreference(
405 $self->runner->backref_name($self, $r->name),
406 2 ${$v},
407 $r->foreign_fk_col,
408 ));
409 }
410 }
411 # Else handle it via column
412 else {
413 742 ▶ if (reftype($v) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (reftype($v) eq "SCALAR") 5 737 742
414 $self->set_value($k, $self->runner->convert_backreference(
415 $self->runner->backref_name($self, $k),
416 5 ${$v},
417 ));
418 }
419 }
420 }
421 # Otherwise, DIE DIE DIE
422 else {
423 6 push @unknowns, $k;
424 }
425 }
426  
427 # Things were passed in, but don't exist in the table.
428 830 ▶ if (!$self->runner->{ignore_unknown_columns} && @unknowns) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->runner->{"ignore_unknown_columns"} and @unknowns) 3 827 830
Condition100%3/3
not $self->runner->{"ignore_unknown_columns"} and @unknowns
!ll&&!rl&&r
18263
MC/DC100%2/2
not $self->runner->{"ignore_unknown_columns"} and @unknowns
not $self->runner->{"ignore_unknown_columns"} @unknowns
Truth table100%3/3
not(A) and B
A= not $self->runner->{"ignore_unknown_columns"}B= @unknowns
ABresult
0X0
100
111
429 3 my $msg = "The following names are in the spec, but not the table @{[$self->source_name]}\n";
430 3 $msg .= join ',', sort @unknowns;
431 3 $msg .= "\n";
432 3 die $msg;
433 }
434  
435 827 return;
436 }
437  
438 ################################################################################
439 #
440 # These are the expected interface methods
441 #
442 ################################################################################
443  
444 872 ▶sub create {
Subroutine create: called CC=15 SCAR=29.8
Pod undocumented
445 872 my $self = shift;
446  
447 0 ▶ warn "Received @{[$self->source_name]}($self) (".np($self->spec).") (".np($self->{create}).")\n" if $ENV{SIMS_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SIMS_DEBUG"} 0 872 872
448  
449 # If, in the current stack of in-flight items, we've attempted to make this
450 # exact item, die because we've obviously entered an infinite loop.
451 872 ▶ if ($self->runner->has_item($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->runner->has_item($self)) 1 871 872
452 1 die "ERROR: @{[$self->source_name]} (".np($self->spec).") was seen more than once\n";
453 }
454 871 $self->runner->add_item($self);
455  
456 # Try to find a match with what was given if this is a parent request. But,
457 # we cannot do that if we have parent values because we haven't resolved FKs
458 # yet.
459 0 ▶ warn "Trying to find a parent match @{[$self->source_name]}($self) (".np($self->spec).") (".np($self->{create}).")\n" if $ENV{SIMS_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SIMS_DEBUG"} 0 871 871
460 871 ▶ if ( $self->attempt_to_find({ unique => 0, no_parent_values => 1 }) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->attempt_to_find({"unique", 0, "no_parent_values", 1})) 39 832 871
461 # If there are any children specified, figure them out here.
462 39 $self->build_children;
463  
464 39 $self->runner->remove_item($self);
465  
466 39 return $self->row;
467 }
468  
469 832 $self->runner->call_hook(preprocess =>
470 $self->source, $self->spec,
471 );
472 0 ▶ warn "After preprocess @{[$self->source_name]}($self) (".np($self->spec).") (".np($self->{create}).")\n" if $ENV{SIMS_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SIMS_DEBUG"} 0 832 832
473  
474 # This resolves all of the values that can be resolved immediately.
475 # * Back references
476 # * Objects
477 832 $self->resolve_direct_values;
478 0 ▶ warn "After RDV @{[$self->source_name]}($self) (".np($self->spec).") (".np($self->{create}).")\n" if $ENV{SIMS_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SIMS_DEBUG"} 0 827 827
479  
480 827 $self->attempt_to_find({ any => 0 });
481  
482 824 ▶ unless ($self->row) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->row) 812 12 824
483 812 $self->populate_parents(nullable => 0);
484 0 ▶ warn "After populate_parents @{[$self->source_name]}($self) (".np($self->spec).") (".np($self->{create}).")\n" if $ENV{SIMS_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SIMS_DEBUG"} 0 809 809
485 }
486  
487 821 $self->attempt_to_find({ any => 0 });
488  
489 821 ▶ unless ($self->row) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->row) 808 13 821
490 808 $self->populate_columns;
491 0 ▶ warn "After populate_columns @{[$self->source_name]}($self) (".np($self->spec).") (".np($self->{create}).")\n" if $ENV{SIMS_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SIMS_DEBUG"} 0 708 708
492 708 $self->oracle_ensure_populated_pk;
493 }
494  
495 721 $self->attempt_to_find;
496  
497 721 ▶ unless ($self->row) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->row) 708 13 721
498 708 $self->runner->call_hook(before_create =>
499 $self->source, $self,
500 );
501  
502 0 ▶ warn "Creating @{[$self->source_name]}($self) (".np($self->spec).") (".np($self->{create}).")\n" if $ENV{SIMS_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SIMS_DEBUG"} 0 708 708
503 708 my $row = eval {
504 #use Carp; local $SIG{__DIE__} = \&Carp::confess;
505 708 $self->source->resultset->create($self->{create});
506 708 ▶ }; if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 708 708
507 0 my $e = $@;
508 0 warn "ERROR Creating @{[$self->source_name]} (".np($self->spec).") (".np($self->{create}).")\n";
509 0 die $e;
510 }
511 708 $self->row($row);
512  
513 # This tracks everything that was created, not just what was requested.
514 708 $self->runner->{created}{$self->source_name}++;
515  
516 708 $self->{trace}{made} = $self->{runner}{ids}{made}++;
517 708 $self->{trace}{create_params} = $self->make_jsonable( $self->{create} );
518 708 $self->{trace}{row} = $self->make_jsonable( { $row->get_columns } );
519  
520 # This occurs when a FK condition was specified, but the column is
521 # nullable and we didn't find an existing parent row. We want to defer these
522 # because self-referential values need to be set after creation.
523 708 $self->populate_parents(nullable => 1);
524 }
525 721 $self->build_children;
526  
527 721 $self->runner->call_hook(postprocess =>
528 $self->source, $self->row,
529 );
530  
531 721 $self->runner->remove_item($self);
532  
533 721 ▶ if ($ENV{SIMS_DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"SIMS_DEBUG"}) 0 721 721
534 0 my %x = $self->row->get_columns;
535 0 warn "Finished @{[$self->source_name]}($self) (".np($self->spec).") (".np($self->{create}).") (" . np(%x) . ")\n";
536 }
537  
538 721 return $self->row;
539 }
540  
541 571 ▶sub value_from_spec {
Subroutine value_from_spec: called CC=11 SCAR=24.0
Pod undocumented
542 571 my $self = shift;
543 571 my ($c, $spec) = @_;
544  
545 # Try N times to find a value that's not in value_not
546  
547 571 my $n = 0;
548 571 my $max = 25;
549 571 my $v;
550 do {
551 3523 $n++;
552 3523 ▶ die "Cannot find a value for @{[$c->source->name]}\.@{[$c->name]} after $max tries" if $n >= $max;
Branch: 1 branch
BranchTrueFalseTotal
if $n >= $max 100 3423 3523
553  
554 3423 ▶ if ( ref($spec->{func} // '') eq 'CODE' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref($spec->{'func'} // '') eq 'CODE') { } 3 3420 3423
elsif (exists $spec->{'value'}) { } 3398 22 3420
elsif ($spec->{'type'}) { } 8 14 22
Condition100%2/2
$spec->{"func"} // ""
l!l
33420
MC/DC100%1/1
$spec->{"func"} // ""
$spec->{"func"}
Truth table100%2/2
A
A= $spec->{"func"}
Aresult
11
00
555 3 $v = $spec->{func}->($c->info);
556 }
557 elsif ( exists $spec->{value} ) {
558 3398 ▶ if (ref($spec->{value} // '') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($spec->{'value'} // '') eq 'ARRAY') { } 3355 43 3398
Condition50%1/2
$spec->{"value"} // ""
l!l
33980
MC/DC0%0/1
$spec->{"value"} // ""
$spec->{"value"}
Truth table50%1/2
A
A= $spec->{"value"}
Aresult
11
00
559 3355 $v = $c->random_item( $spec->{value} );
560 }
561 else {
562 43 $v = $spec->{value};
563 }
564 }
565 elsif ( $spec->{type} ) {
566 my $meth = $self->runner->parent->sim_type($spec->{type})
567 8 ▶ // die "Type '$spec->{type}' is not loaded";
Condition50%1/2
$self->runner->parent->sim_type($spec->{"type"}) // die(qq[Type '$spec->{"type"}' is not loaded])
l!l
80
MC/DC0%0/1
$self->runner->parent->sim_type($spec->{"type"}) // die(qq[Type '$spec->{"type"}' is not loaded])
$self->runner->parent->sim_type($spec->{"type"})
Truth table50%1/2
A
A= $self->runner->parent->sim_type($spec->{"type"})
Aresult
11
00
568 8 $v = $meth->($c->info, $spec, $c);
569 }
570 else {
571 14 $v = $c->generate_value(die_on_unknown => 0);
572 }
573 571 ▶ } while ( $spec->{value_not} && $spec->{value_not}->($v) );
Branch: 1 branch
BranchTrueFalseTotal
$spec->{"value_not"} and $spec->{"value_not"}->($v) and do { do { ++$n; die "Cannot find a value for @{[$c->source->name];}.@{[$c->name];} after $max tries" if $n >= $max; if (ref($spec->{"func"} // "") eq "CODE") { $v = $spec->{"func"}->($c->info); } elsif (exists $spec->{"value"}) { if (ref($spec->{"value"} // "") eq "ARRAY") { $v = $c->random_item($spec->{"value"}); } else { $v = $spec->{"value"}; } ; } elsif ($spec->{"type"}) { my $meth = $self->runner->parent->sim_type($spec->{"type"}) // die(qq[Type '$spec->{"type"}' is not loaded]); $v = &$meth($c->info, $spec, $c); } else { $v = $c->generate_value("die_on_unknown", 0); } } } 2952 471 3423
Condition100%3/3
$spec->{"value_not"} and $spec->{"value_not"}->($v)
!ll&&!rl&&r
714002952
MC/DC100%2/2
$spec->{"value_not"} and $spec->{"value_not"}->($v)
$spec->{"value_not"} $spec->{"value_not"}->($v)
Truth table100%3/3
A and B
A= $spec->{"value_not"}B= $spec->{"value_not"}->($v)
ABresult
0X0
100
111
574 471 return $v;
575 }
576  
577 1609 ▶sub populate_column {
Subroutine populate_column: called CC=27 SCAR=33.2
Pod documented
578 1609 my $self = shift;
579 1609 my ($c) = @_;
580  
581 1609 my $col_name = $c->name;
582 1609 ▶ return if exists $self->{create}->{$col_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"create"}{$col_name} 4 1605 1609
583  
584 1605 my $spec;
585 1605 ▶ if ( exists $self->spec->{$col_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->spec->{$col_name}) 712 893 1605
586 712 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($c->is_in_pk and $c->is_auto_increment and not $self->allow_pk_set_value) 4 708 712
Condition100%6/6
$c->is_in_pk and $c->is_auto_increment and not $self->allow_pk_set_value
!ll&&!rl&&r
70174
$c->is_in_pk and $c->is_auto_increment
!ll&&!rl&&r
696511
MC/DC100%3/3
$c->is_in_pk and $c->is_auto_increment and not $self->allow_pk_set_value
$c->is_in_pk $c->is_auto_increment not $self->allow_pk_set_value
Truth table100%4/4
A and B and not(C)
A= $c->is_in_pkB= $c->is_auto_incrementC= not $self->allow_pk_set_value
ABCresult
0XX0
10X0
1100
1111
587 $c->is_in_pk && $c->is_auto_increment &&
588 !$self->allow_pk_set_value
589 ) {
590 warn sprintf(
591 "Primary-key autoincrement columns should not be hardcoded in tests (%s.%s = %s)",
592 4 $self->source_name, $col_name, $self->spec->{$col_name},
593 );
594 }
595  
596 # This is the original way of specifying an override with a HASHREFREF.
597 # Reflection has realized it was an unnecessary distinction to a parent
598 # specification. Either it's a relationship hashref or a simspec hashref.
599 # We can never have both. It will be deprecated.
600 712 ▶ if (
Branch: 3 branches
BranchTrueFalseTotal
if (reftype($self->spec->{$col_name}) eq 'REF' and reftype(${$self->spec->{$col_name};}) eq 'HASH') { } 1 711 712
elsif (reftype($self->spec->{$col_name}) eq 'HASH' and not builtin::blessed($self->spec->{$col_name}) and not $c->is_inflated) { } 503 208 711
elsif (reftype($self->spec->{$col_name}) eq 'SCALAR') { } 0 208 208
Condition66%2/3
reftype($self->spec->{$col_name}) eq "REF" and reftype(${$self->spec->{$col_name};}) eq "HASH"
!ll&&!rl&&r
71101
MC/DC50%1/2
reftype($self->spec->{$col_name}) eq "REF" and reftype(${$self->spec->{$col_name};}) eq "HASH"
reftype($self->spec->{$col_name}) eq "REF" reftype(${$self->spec->{$col_name};}) eq "HASH"
Truth table66%2/3
A and B
A= reftype($self->spec->{$col_name}) eq "REF"B= reftype(${$self->spec->{$col_name};}) eq "HASH"
ABresult
0X0
100
111
601 reftype($self->spec->{$col_name}) eq 'REF' &&
602 1 reftype(${$self->spec->{$col_name}}) eq 'HASH'
603 ) {
604 1 warn "DEPRECATED: Use a regular HASHREF for overriding simspec. HASHREFREF will be removed in a future release.";
605 1 $spec = ${ $self->spec->{$col_name} };
606 }
607 ▶ elsif (
Condition83%5/6
reftype($self->spec->{$col_name}) eq "HASH" and not builtin::blessed($self->spec->{$col_name}) and not $c->is_inflated
!ll&&!rl&&r
2071503
reftype($self->spec->{$col_name}) eq "HASH" and not builtin::blessed($self->spec->{$col_name})
!ll&&!rl&&r
2070504
MC/DC66%2/3
reftype($self->spec->{$col_name}) eq "HASH" and not builtin::blessed($self->spec->{$col_name}) and not $c->is_inflated
reftype($self->spec->{$col_name}) eq "HASH" not builtin::blessed($self->spec->{$col_name}) not $c->is_inflated
Truth table75%3/4
A and not(B) and not(C)
A= reftype($self->spec->{$col_name}) eq "HASH"B= not builtin::blessed($self->spec->{$col_name})C= not $c->is_inflated
ABCresult
0XX0
10X0
1100
1111
608 reftype($self->spec->{$col_name}) eq 'HASH' &&
609 # Assume a blessed hash is a DBIC object
610 !blessed($self->spec->{$col_name}) &&
611 # Do not assume we understand something to be inflated/deflated
612 !$c->is_inflated
613 ) {
614 503 $spec = $self->spec->{$col_name};
615 }
616 elsif (reftype($self->spec->{$col_name}) eq 'SCALAR') {
617 $self->set_value($col_name, $self->runner->convert_backreference(
618 $self->runner->backref_name($self, $c->name),
619 0 ${$self->spec->{$col_name}},
620 ));
621 0 return;
622 }
623 else {
624 208 $self->set_value($col_name, $self->spec->{$col_name});
625 208 return;
626 }
627 }
628  
629 # If the spec is a hashref containing "value_not" and nothing else, then merge
630 # it with the spec from the column. Otherwise, it overrides the column.
631 1397 ▶ my $merge_spec = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
632 1397 my ($s) = @_;
633 1397 ▶ return unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 893 504 1397
634  
635 # At this point, we can presume that we have a HASHREF because the only way
636 # we get a per-entry spec is if it's a HASHREF.
637  
638 # Handle the optional plural
639 504 ▶ $s->{value_not} = delete $s->{values_not} if exists $s->{values_not};
Branch: 1 branch
BranchTrueFalseTotal
if exists $s->{"values_not"} 100 404 504
640  
641 504 ▶ return unless keys %$s == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$s == 1 0 504 504
642 504 ▶ return unless exists $s->{value_not};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $s->{"value_not"} 4 500 504
643 500 return 1;
644 1397 };
645  
646 1397 ▶ if ( $merge_spec->( $spec ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$merge_spec($spec)) { } 500 897 1397
647 500 my $merger = Hash::Merge->new('RIGHT_PRECEDENT');
648 500 ▶ $spec = $merger->merge( $c->sim_spec // {}, $spec );
Condition50%1/2
$c->sim_spec // {}
l!l
5000
MC/DC0%0/1
$c->sim_spec // {}
$c->sim_spec
Truth table50%1/2
A
A= $c->sim_spec
Aresult
11
00
649 }
650 else {
651 897 ▶ $spec //= $c->sim_spec;
Condition100%3/3
$spec //= $c->sim_spec
l!l&&r!l&&!r
471822
MC/DC100%2/2
$spec //= $c->sim_spec
$spec $c->sim_spec
Truth table100%3/3
A //= B
A= $specB= $c->sim_spec
ABresult
1X1
011
000
652 }
653  
654 1397 ▶ if ($spec) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec) { } 575 822 1397
elsif (not $c->is_nullable || $c->is_in_pk || $c->has_default_value) { } 133 689 822
655 575 ▶ if ( exists $spec->{value_not} && reftype($spec->{value_not}) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"value_not"} and reftype($spec->{"value_not"}) ne "CODE") 400 175 575
Condition100%3/3
exists $spec->{"value_not"} and reftype($spec->{"value_not"}) ne "CODE"
!ll&&!rl&&r
75100400
MC/DC100%2/2
exists $spec->{"value_not"} and reftype($spec->{"value_not"}) ne "CODE"
exists $spec->{"value_not"} reftype($spec->{"value_not"}) ne "CODE"
Truth table100%3/3
A and B
A= exists $spec->{"value_not"}B= reftype($spec->{"value_not"}) ne "CODE"
ABresult
0X0
100
111
656 400 ▶ if ( reftype($spec->{value_not}) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (reftype($spec->{"value_not"}) ne "ARRAY") 100 300 400
657 100 $spec->{value_not} = [ $spec->{value_not} ];
658 }
659  
660 400 my $x = $spec->{value_not};
661 3235 ▶ $spec->{value_not} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
662 3235 my ($v) = @_;
663 3235 return grep { $v eq $_ } @{$x};
664 400 };
665 }
666  
667 575 ▶ if (ref($spec // '') eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($spec // "") eq "HASH") 575 0 575
Condition50%1/2
$spec // ""
l!l
5750
MC/DC0%0/1
$spec // ""
$spec
Truth table50%1/2
A
A= $spec
Aresult
11
00
668 575 ▶ if ( exists $spec->{null_chance} && $c->is_nullable ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"null_chance"} and $c->is_nullable) 5 570 575
Condition66%2/3
exists $spec->{"null_chance"} and $c->is_nullable
!ll&&!rl&&r
57005
MC/DC50%1/2
exists $spec->{"null_chance"} and $c->is_nullable
exists $spec->{"null_chance"} $c->is_nullable
Truth table66%2/3
A and B
A= exists $spec->{"null_chance"}B= $c->is_nullable
ABresult
0X0
100
111
669 # Add check for not a number
670 5 ▶ if ( $c->random_choice($spec->{null_chance}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->random_choice($spec->{"null_chance"})) 5 0 5
671 5 $self->set_value($col_name, undef);
672 5 return;
673 }
674 }
675 570 $self->set_value($col_name, $self->value_from_spec($c, $spec));
676 }
677 }
678 ▶ elsif (
Condition100%6/6
$c->is_nullable || $c->is_in_pk || $c->has_default_value
l!l&&r!l&&!r
6863133
$c->is_nullable or $c->is_in_pk
l!l&&r!l&&!r
4682136
MC/DC100%3/3
$c->is_nullable || $c->is_in_pk || $c->has_default_value
$c->is_nullable $c->is_in_pk $c->has_default_value
Truth table100%4/4
A or B || C
A= $c->is_nullableB= $c->is_in_pkC= $c->has_default_value
ABCresult
1XX1
01X1
0011
0000
679 !$c->is_nullable &&
680 !$c->is_in_pk &&
681 !$c->has_default_value
682 # These clauses were in the original code. Do they still need to exist?
683 # && !$c->is_in_uk
684 ) {
685 133 $self->set_value($col_name, $c->generate_value(die_on_unknown => 1));
686 }
687  
688 1292 return;
689 }
690  
691 808 ▶sub populate_columns {
Subroutine populate_columns: called CC=2 SCAR=6.9
Pod undocumented
692 808 my $self = shift;
693  
694 808 foreach my $c ( $self->source->columns_not_in_parent_relationships ) {
695 1609 $self->populate_column($c);
696 } continue {
697 1509 delete $self->{still_to_use}{$c->name};
698 }
699  
700 708 return;
701 }
702  
703 1 ▶sub parent {
Subroutine parent: called CC=1 SCAR=0.0
Pod documented
704 1 my $self = shift;
705 1 my ($relname) = @_;
706  
707 1 return $self->{parents}{$relname};
708 }
709  
710 98 ▶sub populate_parent {
Subroutine populate_parent: called CC=22 SCAR=34.4
Pod undocumented
711 98 my $self = shift;
712 98 my ($r, %opts) = @_;
713  
714 98 my $col = $r->self_fk_col;
715  
716 # Assumptions:
717 # * If someone sets $col, then they intend to use that.
718 # * If someone sets $col and $col is for multiple relationships, use it.
719 # * If someone sets $col *and* $r->name, then we're confused. Raise error.
720 # - What happens if there are two parents for $col?
721 # - What happens if one of them is nullable?
722  
723 # TODO: Write a test if both the rel and the FK col are specified
724 98 ▶ my $proto = $self->has_value($col)
Branch: 1 branch
BranchTrueFalseTotal
$self->has_value($col) ? : 20 78 98
725 ? $self->value($col)
726 : $self->value($r->name);
727  
728 98 my $fkcol = $r->foreign_fk_col;
729  
730 98 my $spec;
731 98 ▶ if ($proto) {
Branch: 2 branches
BranchTrueFalseTotal
if ($proto) { } 51 47 98
elsif ($self->source->column($col)->sim_spec) { } 1 46 47
732 # Convert backreferences first.
733 51 ▶ if (ref($proto) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto eq "SCALAR") 0 51 51
734 0 $proto = $self->runner->convert_backreference(
735 $self->runner->backref_name($self, $r->name), $$proto, $fkcol,
736 );
737 }
738  
739 51 ▶ if (blessed($proto)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($proto)) 1 50 51
740 1 ▶ if ($opts{nullable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'nullable'}) { } 0 1 1
741 0 $self->row->set_column($col => $proto->get_column($fkcol));
742 0 $self->row->update;
743 }
744 else {
745 1 $self->set_value($col, $proto->get_column($fkcol));
746  
747 # Otherwise, the tracer will try and write out a blessed object.
748 1 warn "Converting $col to @{[$self->{create}{$col}]}\n";
749 1 $self->{trace}{spec}{$col} = $self->{create}{$col};
750 }
751 1 return;
752 }
753  
754 # Assume any hashref is a Sims specification
755 50 ▶ if (ref($proto) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $proto eq 'HASH') { } 30 20 50
elsif (not ref $proto) { } 20 0 20
756 30 $spec = $proto;
757 }
758 # Assume any unblessed scalar is a column value.
759 elsif (!ref($proto)) {
760 20 $spec = { $fkcol => $proto };
761 }
762 else {
763 0 die "Unsure what to do about @{[$r->full_name]}():" . np($proto);
764 }
765 }
766 elsif ($self->source->column($col)->sim_spec) {
767 1 my $c = $self->source->column($col);
768 1 my $sp = $c->sim_spec;
769 1 ▶ if ( exists $sp->{null_chance} && $c->is_nullable ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sp->{"null_chance"} and $c->is_nullable) 0 1 1
Condition33%1/3
exists $sp->{"null_chance"} and $c->is_nullable
!ll&&!rl&&r
100
MC/DC0%0/2
exists $sp->{"null_chance"} and $c->is_nullable
exists $sp->{"null_chance"} $c->is_nullable
Truth table33%1/3
A and B
A= exists $sp->{"null_chance"}B= $c->is_nullable
ABresult
0X0
100
111
770 # Add check for not a number
771 0 ▶ if ( $c->random_choice($sp->{null_chance}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->random_choice($sp->{"null_chance"})) 0 0 0
772 0 return;
773 }
774 }
775 $spec = {
776 1 $fkcol => $self->value_from_spec($c, $sp),
777 };
778 }
779  
780 97 ▶ unless ( $spec ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec) 46 51 97
781 46 ▶ if ( $self->source->column($col)->is_nullable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->source->column($col)->is_nullable) 4 42 46
782 4 return;
783 }
784  
785 42 $spec = {};
786 }
787  
788 93 my $fk_source = $r->target;
789 # If the child's column is within a UK, add a check to the $rs that ensures
790 # we cannot pick a parent that's already being used.
791 93 my @constraints = $self->source->unique_constraints_containing($col);
792 93 ▶ if (@constraints) {
Branch: 1 branch
BranchTrueFalseTotal
if (@constraints) 14 79 93
793 # First, find the inverse relationship. If it doesn't exist or if there
794 # is more than one, then die.
795 14 my @inverse = $self->source->find_inverse_relationships(
796 $fk_source, $fkcol,
797 );
798 14 ▶ if (@inverse == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@inverse == 0) { } 0 14 14
elsif (@inverse > 1) { } 0 14 14
799 0 die "Cannot find an inverse relationship for @{[$r->full_name]}\n";
800 }
801 elsif (@inverse > 1) {
802 0 die "Too many inverse relationships for @{[$r->full_name]} (@{[$fk_source->name]} / $fkcol)\n" . np(@inverse);
803 }
804  
805 # We cannot add this relationship to the $spec because that would result
806 # in an infinite loop. So, add a restriction to the parent's __META__
807 14 ▶ $spec->{__META__} //= {};
Condition50%1/2
$spec->{"__META__"} //= {}
l!l
014
MC/DC0%0/1
$spec->{"__META__"} //= {}
$spec->{"__META__"}
Truth table50%1/2
A
A= $spec->{"__META__"}
Aresult
11
00
808 $spec->{__META__}{restriction} = {
809 cond => { join('.', $inverse[0]{rel}, $inverse[0]{col}) => undef },
810 extra => { join => $inverse[0]{rel} },
811 14 };
812 }
813  
814 0 ▶ warn "Parent (@{[$fk_source->name]}): " . np($spec) .$/ if $ENV{SIMS_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SIMS_DEBUG"} 0 93 93
815 93 ▶ push @{$self->{runner}{traces}}, {
Condition50%1/2
$spec // {}
l!l
930
MC/DC0%0/1
$spec // {}
$spec
Truth table50%1/2
A
A= $spec
Aresult
11
00
816 table => $fk_source->name,
817 spec => MyCloner::clone($spec // {}),
818 seen => $self->{runner}{ids}{seen}++,
819 parent => $self->{trace}{seen},
820 93 via => "populate_parents/@{[$r->name]}",
821 };
822 my $fk_item = DBIx::Class::Sims::Item->new(
823 runner => $self->runner,
824 source => $fk_source,
825 spec => MyCloner::clone($spec // {}),
826 93 ▶ trace => $self->{runner}{traces}[-1],
Condition50%1/2
$spec // {}
l!l
930
MC/DC0%0/1
$spec // {}
$spec
Truth table50%1/2
A
A= $spec
Aresult
11
00
827 );
828 93 $fk_item->set_allow_pk_to($self);
829 93 $fk_item->create;
830  
831 91 $self->{parents}{$r->name} = $fk_item;
832  
833 91 ▶ if ($opts{nullable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'nullable'}) { } 4 87 91
834 4 $self->row->set_column($col => $fk_item->row->get_column($fkcol));
835 4 $self->row->update;
836 }
837 else {
838 87 $self->set_value($col, $fk_item->row->get_column($fkcol));
839 }
840 }
841  
842 1520 ▶sub populate_parents {
Subroutine populate_parents: called CC=9 SCAR=22.0
Pod undocumented
843 1520 my $self = shift;
844 1520 my %opts = @_;
845  
846 62 ▶ my $has_value = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
847 62 my $r = shift;
848 62 ▶ return $self->has_value($r->self_fk_col) || $self->has_value($r->name);
Condition100%3/3
$self->has_value($r->self_fk_col) || $self->has_value($r->name)
l!l&&r!l&&!r
43514
MC/DC100%2/2
$self->has_value($r->self_fk_col) || $self->has_value($r->name)
$self->has_value($r->self_fk_col) $self->has_value($r->name)
Truth table100%3/3
A || B
A= $self->has_value($r->self_fk_col)B= $self->has_value($r->name)
ABresult
1X1
011
000
849 1520 };
850  
851 RELATIONSHIP:
852 1520 foreach my $r (
853 sort {
854 31 $has_value->($b) <=> $has_value->($a)
855 } $self->source->parent_relationships
856 ) {
857 297 my $col = $r->self_fk_col;
858  
859 297 ▶ if ( $opts{nullable} xor $self->source->column($col)->is_nullable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"nullable"} xor $self->source->column($col)->is_nullable) 146 151 297
Condition100%4/4
$opts{"nullable"} xor $self->source->column($col)->is_nullable
l&&rl&&!r!l&&r!l&&!r
1013610141
MC/DC100%2/2
$opts{"nullable"} xor $self->source->column($col)->is_nullable
$opts{"nullable"} $self->source->column($col)->is_nullable
Truth table100%4/4
A xor B
A= $opts{"nullable"}B= $self->source->column($col)->is_nullable
ABresult
110
101
011
000
860 146 next RELATIONSHIP;
861 }
862  
863 151 delete $self->{still_to_use}{$_} for ($r->name, $col);
864  
865 151 ▶ if (!$self->runner->{allow_relationship_column_names}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->runner->{"allow_relationship_column_names"}) 1 150 151
866 1 ▶ if ($col ne $r->name && exists $self->spec->{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col ne $r->name and exists $self->spec->{$col}) 1 0 1
Condition33%1/3
$col ne $r->name and exists $self->spec->{$col}
!ll&&!rl&&r
001
MC/DC0%0/2
$col ne $r->name and exists $self->spec->{$col}
$col ne $r->name exists $self->spec->{$col}
Truth table33%1/3
A and B
A= $col ne $r->nameB= exists $self->spec->{$col}
ABresult
0X0
100
111
867 1 die "Cannot use column $col - use relationship @{[$r->name]}";
868 }
869 }
870  
871 150 ▶ if ($self->{skip_relationship}{$r->name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"skip_relationship"}{$r->name}) 52 98 150
872 52 next RELATIONSHIP;
873 }
874  
875 98 $self->populate_parent($r, %opts);
876 }
877  
878 1517 return;
879 }
880  
881 760 ▶sub build_children {
Subroutine build_children: called CC=9 SCAR=22.0
Pod undocumented
882 760 my $self = shift;
883  
884 # 1. If we have something, then:
885 # a. If it's not an array, then make it an array
886 # 2. If we don't have something,
887 # a. Make an array with an empty item
888 # XXX This is more than one item would be supported
889 # In all cases, make sure to add { $fkcol => $row->get_column($col) } to the
890 # child's $item
891 760 foreach my $r ( $self->source->child_relationships ) {
892 214 ▶ if ($r->constraints) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->constraints) 10 204 214
893 10 $self->runner->ensure_children($self, $r, $r->constraints);
894 }
895  
896 214 ▶ next unless $self->has_value($r->name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_value($r->name) 184 30 214
897  
898 my $normalized = normalize_aoh($self->value($r->name))
899 0 ▶ or die "Don't know what to do with @{[$r->full_name]}\n\t".np($self->{original_spec});
Branch: 1 branch
BranchTrueFalseTotal
my $normalized = normalize_aoh($self->value($r->name)) or die "Don't know what to do with @{[$r->full_name];}\n\t" . &np(\$self->{"original_spec"}) 30 0 30
900  
901 30 my @specified = grep { keys %$_ } @$normalized;
902  
903 # Only run everything through ensure_children() if all the children are
904 # unspecified. We do need to figure out how to handle specified children,
905 # but this should be "good nuff" for now.
906 # In essence, this is saying x => [ {}, {} ] is equivalent to x => 2
907 30 ▶ unless (@specified) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@specified) 9 21 30
908 9 $self->runner->ensure_children(
909 $self, $r, @$normalized + 0,
910 );
911 9 next;
912 }
913  
914 21 my $fkcol = $r->foreign_fk_col;
915 21 my $fk_source = $r->target;
916  
917 21 my @inverse = $self->source->find_inverse_relationships(
918 $fk_source, $fkcol,
919 );
920  
921 21 foreach my $child (@{$normalized}) {
922 # FIXME $child is a hashref, not a ::Item. add_child() needs to be able to
923 # handle ::Item's, which requires ::Item's to be Comparable. It also means
924 # the ::Runner's spec has been converted to ::Item before iteration.
925 26 ▶ ($child->{__META__} //= {})->{allow_pk_set_value} = 1;
Condition50%1/2
$child->{"__META__"} //= {}
l!l
026
MC/DC0%0/1
$child->{"__META__"} //= {}
$child->{"__META__"}
Truth table50%1/2
A
A= $child->{"__META__"}
Aresult
11
00
926  
927 # If there isn't an inverse relationship from the child back to here, then
928 # we need to specifically set the column. This could happen when you have
929 # a "types" or "preferences" table that's used for many tables.
930 26 ▶ if ( @inverse == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@inverse == 0) { } 2 24 26
931 2 $child->{$fkcol} = $self->row->get_column($r->self_fk_col);
932 }
933 # But, if there *is* any inverse relationship (even if there's several),
934 # do not do $self->row->get_column($col). This causes an infinite loop
935 # because the child then needs a parent ::Item that tries to create a
936 # child, and so forth.
937 else {
938 24 $child->{$fkcol} = $self->row;
939 }
940  
941 $self->runner->add_child({
942 adder => $self->source_name,
943 source => $fk_source,
944 fkcol => $fkcol,
945 child => $child,
946 trace => {
947 table => $fk_source->name,
948 spec => MyCloner::clone($child),
949 seen => $self->{runner}{ids}{seen}++,
950 parent => $self->{trace}{seen},
951 26 via => 'add_child',
952 },
953 });
954 }
955 } continue {
956 214 delete $self->{still_to_use}{$r->name};
957 }
958 }
959  
960 708 ▶sub oracle_ensure_populated_pk {
Subroutine oracle_ensure_populated_pk: called CC=4 SCAR=22.9
Pod undocumented
961 708 my $self = shift;
962  
963 # Oracle does not allow the "INSERT INTO x DEFAULT VALUES" syntax that DBIC
964 # wants to use. Therefore, find a PK column and set it to NULL. If there
965 # isn't one, complain loudly.
966 0 ▶ if ($self->runner->is_oracle && keys(%{$self->{create}}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->runner->is_oracle and keys %{$$self{"create"};} == 0) 0 708 708
Condition33%1/3
$self->runner->is_oracle and keys %{$$self{"create"};} == 0
!ll&&!rl&&r
70800
MC/DC0%0/2
$self->runner->is_oracle and keys %{$$self{"create"};} == 0
$self->runner->is_oracle keys %{$$self{"create"};} == 0
Truth table33%1/3
A and B
A= $self->runner->is_oracleB= keys %{$$self{"create"};} == 0
ABresult
0X0
100
111
967 my @pk_columns = grep {
968 0 $_->is_in_pk
969 } $self->source->columns;
970  
971 0 ▶ die "Must specify something about some column or have a PK in Oracle"
Branch: 1 branch
BranchTrueFalseTotal
unless @pk_columns 0 0 0
972 unless @pk_columns;
973  
974 # This will work even if there are multiple columns in the PK.
975 0 $self->set_value($pk_columns[0]->name, undef);
976 }
977 }
978  
979 54 1;
980 __END__
« Sims.pm ↑ Summary Sims/Runner.pm »