Sims/Runner.pm

filter: Statementstmt 94.9% 263 / 277 Branchbran 83.6% 87 / 104 Conditioncond 84.6% 55 / 65 MC/DCmcdc 72.7% 24 / 33 Subroutinesub 100% 31 / 31 Podpod 0.0% 0 / 15 totaltotal 87.6% 460 / 525 CC 92 SCAR 28.1
CC
92
Cov
91%
fix_columns
38.4
fix_fk_dependencies
21.0
find_by_unique_constraints
8.1
CRAP 16.6
↑ Summary Sims.pm »
1 package DBIx::Class::Sims::Runner;
2  
3 25 ▶use 5.010_002;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 25 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 ▶use DDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 25 ▶use Hash::Merge qw( merge );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 ▶use String::Random qw( random_regex );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 25 ▶use DBIx::Class::Sims::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 ###### FROM HERE ######
16 # These are utility methods to help navigate the rel_info hash.
17 379 ▶my $is_fk = sub { return exists $_[0]{attrs}{is_foreign_key_constraint} };
Subroutine __ANON__: called CC=1 SCAR=0.0
18 my $short_source = sub {
19 84 ▶ (my $x = $_[0]{source}) =~ s/.*:://;
Subroutine __ANON__: called CC=1 SCAR=0.0
20 84 return $x;
21 25 };
22  
23 # ribasushi says: at least make sure the cond is a hashref (not guaranteed)
24 144 ▶my $self_fk_cols = sub { map {/^self\.(.*)/; $1} values %{$_[0]{cond}} };
Subroutine __ANON__: called CC=1 SCAR=0.0
25 84 ▶my $self_fk_col = sub { ($self_fk_cols->(@_))[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
26 84 ▶my $foreign_fk_cols = sub { map {/^foreign\.(.*)/; $1} keys %{$_[0]{cond}} };
Subroutine __ANON__: called CC=1 SCAR=0.0
27 84 ▶my $foreign_fk_col = sub { ($foreign_fk_cols->(@_))[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
28 ###### TO HERE ######
29  
30 sub new {
31 56 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
32 56 my $self = bless {@_}, $class;
33 56 $self->initialize;
34 56 return $self;
35 }
36  
37 sub initialize {
38 56 ▶ my $self = shift;
Subroutine initialize: called CC=6 SCAR=17.9
Pod undocumented
39  
40 56 $self->{is_fk} = {};
41 56 foreach my $name ( $self->schema->sources ) {
42 113 my $source = $self->schema->source($name);
43  
44 113 ▶ $self->{reqs}{$name} //= {};
Condition100%2/2
$self->{"reqs"}{$name} //= {}
l!l
5108
MC/DC100%1/1
$self->{"reqs"}{$name} //= {}
$self->{"reqs"}{$name}
Truth table100%2/2
A
A= $self->{"reqs"}{$name}
Aresult
11
00
45 113 foreach my $rel_name ( $source->relationships ) {
46 113 my $rel_info = $source->relationship_info($rel_name);
47  
48 113 ▶ if ($is_fk->($rel_info)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$is_fk($rel_info)) 60 53 113
49 60 $self->{reqs}{$name}{$rel_name} = 1;
50 60 $self->{is_fk}{$name}{$_} = 1 for $self_fk_cols->($rel_info);
51 }
52 }
53 }
54  
55 56 $self->{created} = {};
56 56 $self->{duplicates} = {};
57  
58 56 return;
59 }
60  
61 1273 ▶sub schema { shift->{schema} }
Subroutine schema: called CC=1 SCAR=0.0
Pod undocumented
62  
63 sub create_search {
64 45 ▶ my $self = shift;
Subroutine create_search: called CC=4 SCAR=13.9
Pod undocumented
65 45 my ($rs, $name, $cond) = @_;
66  
67 45 my $source = $self->schema->source($name);
68 45 my %cols = map { $_ => 1 } $source->columns;
69 my $search = {
70 (map {
71 38 $_ => $cond->{$_}
72 } grep {
73 45 exists $cols{$_}
74 } keys %$cond)
75 };
76 45 $rs = $rs->search($search);
77  
78 45 foreach my $rel_name ($source->relationships) {
79 59 ▶ next unless exists $cond->{$rel_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cond->{$rel_name} 52 7 59
80 7 ▶ next unless reftype($cond->{$rel_name}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($cond->{$rel_name}) eq "HASH" 2 5 7
81  
82 my %search = map {
83 3 ;"$rel_name.$_" => $cond->{$rel_name}{$_}
84 } grep {
85 # Nested relationships are automagically handled. q.v. t/t5.t
86 5 !ref $cond->{$rel_name}{$_}
87 5 } keys %{$cond->{$rel_name}};
88  
89  
90 5 $rs = $rs->search(\%search, { join => $rel_name });
91 }
92  
93 45 return $rs;
94 }
95  
96 sub fix_fk_dependencies {
97 134 ▶ my $self = shift;
Subroutine fix_fk_dependencies: called CC=20 SCAR=30.4
Pod undocumented
98 134 my ($name, $item) = @_;
99  
100 # 1. If we have something, then:
101 # a. If it's a scalar, then, COND = { $fk => scalar }
102 # b. Look up the row by COND
103 # c. If the row is not there, then $create_item->($fksrc, COND)
104 # 2. If we don't have something and the column is non-nullable, then:
105 # a. If rows exists, pick a random one.
106 # b. If rows don't exist, $create_item->($fksrc, {})
107 134 my %child_deps;
108 134 my $source = $self->schema->source($name);
109 134 foreach my $rel_name ( $source->relationships ) {
110 133 my $rel_info = $source->relationship_info($rel_name);
111 133 ▶ unless ( $is_fk->($rel_info) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$is_fk($rel_info)) 67 66 133
112 67 ▶ if ($item->{$rel_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{$rel_name}) 13 54 67
113 13 $child_deps{$rel_name} = delete $item->{$rel_name};
114 }
115 67 next;
116 }
117  
118 66 ▶ next unless $self->{reqs}{$name}{$rel_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"reqs"}{$name}{$rel_name} 0 66 66
119  
120 66 my $col = $self_fk_col->($rel_info);
121 66 my $fkcol = $foreign_fk_col->($rel_info);
122  
123 66 my $fk_name = $short_source->($rel_info);
124 66 my $rs = $self->schema->resultset($fk_name);
125  
126 66 my $cond;
127 66 ▶ my $proto = delete($item->{$rel_name}) // delete($item->{$col});
Condition100%3/3
delete $item->{$rel_name} // delete $item->{$col}
l!l&&r!l&&!r
212421
MC/DC100%2/2
delete $item->{$rel_name} // delete $item->{$col}
delete $item->{$rel_name} delete $item->{$col}
Truth table100%3/3
A // B
A= delete $item->{$rel_name}B= delete $item->{$col}
ABresult
1X1
011
000
128 66 ▶ if ($proto) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto) 45 21 66
129 # Assume anything blessed is blessed into DBIC.
130 45 ▶ if (blessed($proto)) {
Branch: 4 branches
BranchTrueFalseTotal
if (builtin::blessed($proto)) { } 1 44 45
elsif (ref $proto eq 'HASH') { } 16 28 44
elsif (not ref $proto) { } 24 4 28
elsif (ref $proto eq 'SCALAR') { } 4 0 4
131 1 $cond = { $fkcol => $proto->$fkcol };
132 }
133 # Assume any hashref is a Sims specification
134 elsif (ref($proto) eq 'HASH') {
135 16 $cond = $proto
136 }
137 # Assume any unblessed scalar is a column value.
138 elsif (!ref($proto)) {
139 24 $cond = { $fkcol => $proto };
140 }
141 # Use a referenced row
142 elsif (ref($proto) eq 'SCALAR') {
143 4 my ($table, $idx) = ($$proto =~ /(.+)\[(\d+)\]$/);
144 4 ▶ unless ($table && defined $idx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table and defined $idx) 0 4 4
Condition33%1/3
$table and defined $idx
!ll&&!rl&&r
004
MC/DC0%0/2
$table and defined $idx
$table defined $idx
Truth table33%1/3
A and B
A= $tableB= defined $idx
ABresult
0X0
100
111
145 0 die "Unsure what to do about $name->$rel_name():" . np($proto);
146 }
147 4 ▶ unless (exists $self->{rows}{$table}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"rows"}{$table}) 0 4 4
148 0 die "No rows in $table to reference\n";
149 }
150 4 ▶ unless (exists $self->{rows}{$table}[$idx]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"rows"}{$table}[$idx]) 0 4 4
151 0 die "Not enough ($idx) rows in $table to reference\n";
152 }
153  
154 4 $cond = { $fkcol => $self->{rows}{$table}[$idx]->$fkcol };
155 }
156 else {
157 0 die "Unsure what to do about $name->$rel_name():" . np($proto);
158 }
159 }
160  
161 66 my $col_info = $source->column_info($col);
162 66 ▶ if ( $cond ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cond) { } 45 21 66
elsif ($col_info->{'is_nullable'}) { } 3 18 21
163 45 $rs = $self->create_search($rs, $fk_name, $cond);
164 }
165 elsif ( $col_info->{is_nullable} ) {
166 3 next;
167 }
168 else {
169 18 $cond = {};
170 }
171  
172 63 ▶ my $meta = delete $cond->{__META__} // {};
Condition100%2/2
delete $cond->{"__META__"} // {}
l!l
162
MC/DC100%1/1
delete $cond->{"__META__"} // {}
delete $cond->{"__META__"}
Truth table100%2/2
A
A= delete $cond->{"__META__"}
Aresult
11
00
173  
174 #warn "Looking for $name->$rel_name(".np($cond).")\n";
175  
176 63 my $parent;
177 63 ▶ unless ($meta->{create}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta->{"create"}) 62 1 63
178 62 $parent = $rs->search(undef, { rows => 1 })->first;
179 }
180 63 ▶ unless ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent) 26 37 63
181 26 $parent = $self->create_item($fk_name, $cond);
182 }
183 63 $item->{$col} = $parent->get_column($fkcol);
184 }
185  
186 134 return \%child_deps;
187 }
188  
189 {
190 25 my %pending;
191 25 my %added_by;
192 sub are_columns_equal {
193 2 ▶ my $self = shift;
Subroutine are_columns_equal: called CC=6 SCAR=18.0
Pod undocumented
194 2 my ($src, $row, $compare) = @_;
195 2 foreach my $col ($self->schema->source($src)->columns) {
196 7 ▶ next if $self->{is_fk}{$src}{$col};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_fk"}{$src}{$col} 3 4 7
197  
198 4 ▶ next unless exists $row->{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $row->{$col} 2 2 4
199 2 ▶ return unless exists $compare->{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $compare->{$col} 0 2 2
200 2 ▶ return if $compare->{$col} ne $row->{$col};
Branch: 1 branch
BranchTrueFalseTotal
if $compare->{$col} ne $row->{$col} 0 2 2
201 }
202 2 return 1;
203 };
204  
205 sub add_child {
206 23 ▶ my $self = shift;
Subroutine add_child: called CC=6 SCAR=17.9
Pod undocumented
207 23 my ($src, $fkcol, $row, $adder) = @_;
208 # If $row has the same keys (other than parent columns) as another row
209 # added by a different parent table, then set the foreign key for this
210 # parent in the existing row.
211 23 foreach my $compare (@{$self->{spec}{$src}}) {
212 12 ▶ next if exists $added_by{$adder} && exists $added_by{$adder}{$compare};
Branch: 1 branch
BranchTrueFalseTotal
if exists $added_by{$adder} and exists $added_by{$adder}{$compare} 10 2 12
Condition100%3/3
exists $added_by{$adder} and exists $added_by{$adder}{$compare}
!ll&&!rl&&r
1110
MC/DC100%2/2
exists $added_by{$adder} and exists $added_by{$adder}{$compare}
exists $added_by{$adder} exists $added_by{$adder}{$compare}
Truth table100%3/3
A and B
A= exists $added_by{$adder}B= exists $added_by{$adder}{$compare}
ABresult
0X0
100
111
213 2 ▶ if ($self->are_columns_equal($src, $row, $compare)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->are_columns_equal($src, $row, $compare)) 2 0 2
214 2 $compare->{$fkcol} = $row->{$fkcol};
215 2 return;
216 }
217 }
218  
219 21 push @{$self->{spec}{$src}}, $row;
220 21 ▶ $added_by{$adder} //= {};
Condition100%2/2
$added_by{$adder} //= {}
l!l
174
MC/DC100%1/1
$added_by{$adder} //= {}
$added_by{$adder}
Truth table100%2/2
A
A= $added_by{$adder}
Aresult
11
00
221 21 $added_by{$adder}{$row} = !!1;
222 21 $pending{$src} = 1;
223 }
224  
225 56 ▶ sub has_pending { keys %pending != 0; }
Subroutine has_pending: called CC=1 SCAR=0.0
Pod undocumented
226 80 ▶ sub delete_pending { delete $pending{$_[1]}; }
Subroutine delete_pending: called CC=1 SCAR=0.0
Pod undocumented
227 1 ▶ sub clear_pending { %pending = (); }
Subroutine clear_pending: called CC=1 SCAR=0.0
Pod undocumented
228 }
229  
230 25 sub find_by_unique_constraints {
231 134 ▶ my $self = shift;
Subroutine find_by_unique_constraints: called CC=8 SCAR=21.0
Pod undocumented
232 134 my ($name, $item) = @_;
233  
234 134 my $source = $self->schema->source($name);
235 my @uniques = map {
236 134 [ $source->unique_constraint_columns($_) ]
237 } $source->unique_constraint_names();
238  
239 134 my $rs = $self->schema->resultset($name);
240 134 my $searched = {};
241 134 foreach my $unique (@uniques) {
242 # If there are specified values for all the columns in a specific unqiue constraint ...
243 146 ▶ next if grep { ! exists $item->{$_} } @$unique;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not exists $item->{$_};} @$unique 127 19 146
244  
245 # ... then add that to the list of potential values to search.
246 19 my %criteria;
247 19 foreach my $colname (@{$unique}) {
248 24 my $value = $item->{$colname};
249 24 my $classname = blessed($value);
250 24 ▶ if ( $classname && $classname->isa('DateTime') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($classname and $classname->isa("DateTime")) 0 24 24
Condition33%1/3
$classname and $classname->isa("DateTime")
!ll&&!rl&&r
2400
MC/DC0%0/2
$classname and $classname->isa("DateTime")
$classname $classname->isa("DateTime")
Truth table33%1/3
A and B
A= $classnameB= $classname->isa("DateTime")
ABresult
0X0
100
111
251 0 my $dtf = $self->schema->storage->datetime_parser;
252 0 $value = $dtf->format_datetime($value);
253 }
254  
255 24 $criteria{$colname} = $value;
256 }
257
258 19 $rs = $rs->search(\%criteria);
259 19 $searched = merge($searched, \%criteria);
260 }
261  
262 134 ▶ return unless keys %$searched;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$searched 120 14 134
263 14 my $row = $rs->search(undef, { rows => 1 })->first;
264 14 ▶ if ($row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row) 2 12 14
265 2 push @{$self->{duplicates}{$name}}, {
266 criteria => $searched,
267 found => { $row->get_columns },
268 };
269 2 return $row;
270 }
271 12 return;
272 }
273  
274 sub fix_child_dependencies {
275 133 ▶ my $self = shift;
Subroutine fix_child_dependencies: called CC=8 SCAR=20.8
Pod undocumented
276 133 my ($name, $row, $child_deps) = @_;
277  
278 # 1. If we have something, then:
279 # a. If it's not an array, then make it an array
280 # 2. If we don't have something,
281 # a. Make an array with an empty item
282 # XXX This is more than one item would be supported
283 # In all cases, make sure to add { $fkcol => $row->get_column($col) } to the
284 # child's $item
285 133 my $source = $self->schema->source($name);
286 133 foreach my $rel_name ( $source->relationships ) {
287 133 my $rel_info = $source->relationship_info($rel_name);
288 133 ▶ next if $is_fk->($rel_info);
Branch: 1 branch
BranchTrueFalseTotal
if &$is_fk($rel_info) 66 67 133
289 67 ▶ next unless $child_deps->{$rel_name} // $self->{reqs}{$name}{$rel_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $child_deps->{$rel_name} // $self->{"reqs"}{$name}{$rel_name} 49 18 67
Condition100%3/3
$child_deps->{$rel_name} // $self->{"reqs"}{$name}{$rel_name}
l!l&&r!l&&!r
13549
MC/DC100%2/2
$child_deps->{$rel_name} // $self->{"reqs"}{$name}{$rel_name}
$child_deps->{$rel_name} $self->{"reqs"}{$name}{$rel_name}
Truth table100%3/3
A // B
A= $child_deps->{$rel_name}B= $self->{"reqs"}{$name}{$rel_name}
ABresult
1X1
011
000
290  
291 18 my $col = $self_fk_col->($rel_info);
292 18 my $fkcol = $foreign_fk_col->($rel_info);
293  
294 18 my $fk_name = $short_source->($rel_info);
295  
296 18 my @children;
297 18 ▶ if ($child_deps->{$rel_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_deps->{$rel_name}) { } 13 5 18
298 13 my $n = DBIx::Class::Sims::Util->normalize_aoh($child_deps->{$rel_name});
299 13 ▶ unless ($n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 0 13 13
300 0 die "Don't know what to do with $name->{$rel_name}\n\t".np($row);
301 }
302 13 @children = @{$n};
303 }
304 else {
305 5 @children = ( ({}) x $self->{reqs}{$name}{$rel_name} );
306 }
307  
308 # Need to ensure that $child_deps >= $self->{reqs}
309  
310 18 foreach my $child (@children) {
311 23 $child->{$fkcol} = $row->get_column($col);
312 23 $self->add_child($fk_name, $fkcol, $child, $name);
313 }
314 }
315 }
316  
317 sub fix_columns {
318 134 ▶ my $self = shift;
Subroutine fix_columns: called CC=33 SCAR=36.5
Pod undocumented
319 134 my ($name, $item) = @_;
320  
321 134 my $source = $self->schema->source($name);
322  
323 my %is = (
324 in_pk => sub {
325 213 ▶ my $n = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
326 grep {
327 213 $_ eq $n
328 } $source->primary_columns;
329 },
330 in_uk => sub {
331 88 ▶ my $n = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
332 grep {
333 94 $_ eq $n
334 } map {
335 88 $source->unique_constraint_columns($_)
336 } $source->unique_constraint_names;
337 },
338 134 );
339  
340 134 foreach my $col_name ( $source->columns ) {
341 358 my $sim_spec;
342 358 ▶ if ( exists $item->{$col_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{$col_name}) 109 249 358
343 109 ▶ if ((reftype($item->{$col_name}) // '') eq 'REF' &&
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($item->{$col_name}) // '') eq 'REF' and (builtin::reftype(${$$item{$col_name};}) // '') eq 'HASH') { } 2 107 109
Condition71%5/7
(builtin::reftype($item->{$col_name}) // "") eq "REF" and (builtin::reftype(${$$item{$col_name};}) // "") eq "HASH"
!ll&&!rl&&r
10702
builtin::reftype($item->{$col_name}) // ""
l!l
2107
builtin::reftype(${$$item{$col_name};}) // ""
l!l
20
MC/DC50%1/2
(builtin::reftype($item->{$col_name}) // "") eq "REF" and (builtin::reftype(${$$item{$col_name};}) // "") eq "HASH"
builtin::reftype($item->{$col_name}) builtin::reftype(${$$item{$col_name};})
Truth table66%2/3
A and B
A= builtin::reftype($item->{$col_name})B= builtin::reftype(${$$item{$col_name};})
ABresult
0X0
100
111
344 2 (reftype(${$item->{$col_name}}) // '') eq 'HASH' ) {
345 2 $sim_spec = ${ delete $item->{$col_name} };
346 }
347 # Pass the value along to DBIC - we don't know how to deal with it.
348 else {
349 107 next;
350 }
351 }
352  
353 251 my $info = $source->column_info($col_name);
354  
355 251 ▶ $sim_spec //= $info->{sim};
Condition100%3/3
$sim_spec //= $info->{"sim"}
l!l&&r!l&&!r
232217
MC/DC100%2/2
$sim_spec //= $info->{"sim"}
$sim_spec $info->{"sim"}
Truth table100%3/3
A //= B
A= $sim_specB= $info->{"sim"}
ABresult
1X1
011
000
356 251 ▶ if ( ref($sim_spec // '') eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref($sim_spec // '') eq 'HASH') { } 34 217 251
elsif (not $info->{'is_nullable'} || exists $info->{'default_value'} || $is{'in_pk'}->($col_name) || $is{'in_uk'}->($col_name) || $self->{'is_fk'}{$name}{$col_name}) { } 48 169 217
Condition100%2/2
$sim_spec // ""
l!l
34217
MC/DC100%1/1
$sim_spec // ""
$sim_spec
Truth table100%2/2
A
A= $sim_spec
Aresult
11
00
357 34 ▶ if ( exists $sim_spec->{null_chance} && !$info->{is_nullable} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sim_spec->{"null_chance"} and not $info->{"is_nullable"}) 0 34 34
Condition33%1/3
exists $sim_spec->{"null_chance"} and not $info->{"is_nullable"}
!ll&&!rl&&r
3400
MC/DC0%0/2
exists $sim_spec->{"null_chance"} and not $info->{"is_nullable"}
exists $sim_spec->{"null_chance"} not $info->{"is_nullable"}
Truth table33%1/3
A and not(B)
A= exists $sim_spec->{"null_chance"}B= not $info->{"is_nullable"}
ABresult
0X0
100
111
358 # Add check for not a number
359 0 ▶ if ( rand() < $sim_spec->{null_chance} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < $sim_spec->{"null_chance"}) 0 0 0
360 0 $item->{$col_name} = undef;
361 0 next;
362 }
363 }
364  
365 34 ▶ if ( ref($sim_spec->{func} // '') eq 'CODE' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref($sim_spec->{'func'} // '') eq 'CODE') { } 2 32 34
elsif (exists $sim_spec->{'value'}) { } 18 14 32
elsif ($sim_spec->{'type'}) { } 5 9 14
Condition100%2/2
$sim_spec->{"func"} // ""
l!l
232
MC/DC100%1/1
$sim_spec->{"func"} // ""
$sim_spec->{"func"}
Truth table100%2/2
A
A= $sim_spec->{"func"}
Aresult
11
00
366 2 $item->{$col_name} = $sim_spec->{func}->($info);
367 }
368 elsif ( exists $sim_spec->{value} ) {
369 18 $item->{$col_name} = $sim_spec->{value};
370 }
371 elsif ( $sim_spec->{type} ) {
372 5 my $meth = $self->{parent}->sim_type($sim_spec->{type});
373 5 ▶ if ( $meth ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meth) { } 5 0 5
374 5 $item->{$col_name} = $meth->($info);
375 }
376 else {
377 0 warn "Type '$sim_spec->{type}' is not loaded";
378 }
379 }
380 else {
381 9 ▶ if ( $info->{data_type} eq 'int' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($info->{'data_type'} eq 'int') { } 4 5 9
elsif ($info->{'data_type'} eq 'varchar') { } 5 0 5
382 4 ▶ my $min = $sim_spec->{min} // 0;
Condition100%2/2
$sim_spec->{"min"} // 0
l!l
22
MC/DC100%1/1
$sim_spec->{"min"} // 0
$sim_spec->{"min"}
Truth table100%2/2
A
A= $sim_spec->{"min"}
Aresult
11
00
383 4 ▶ my $max = $sim_spec->{max} // 100;
Condition100%2/2
$sim_spec->{"max"} // 100
l!l
22
MC/DC100%1/1
$sim_spec->{"max"} // 100
$sim_spec->{"max"}
Truth table100%2/2
A
A= $sim_spec->{"max"}
Aresult
11
00
384 4 $item->{$col_name} = int(rand($max-$min))+$min;
385 }
386 elsif ( $info->{data_type} eq 'varchar' ) {
387 5 ▶ my $min = $sim_spec->{min} // 1;
Condition100%2/2
$sim_spec->{"min"} // 1
l!l
32
MC/DC100%1/1
$sim_spec->{"min"} // 1
$sim_spec->{"min"}
Truth table100%2/2
A
A= $sim_spec->{"min"}
Aresult
11
00
388 5 ▶ my $max = $sim_spec->{max} // $info->{data_length} // 255;
Condition100%5/5
($sim_spec->{"max"} // $info->{"data_length"}) // 255
l!l
32
$sim_spec->{"max"} // $info->{"data_length"}
l!l&&r!l&&!r
212
MC/DC100%2/2
($sim_spec->{"max"} // $info->{"data_length"}) // 255
$sim_spec->{"max"} $info->{"data_length"}
Truth table100%3/3
A // B
A= $sim_spec->{"max"}B= $info->{"data_length"}
ABresult
1X1
011
000
389 5 $item->{$col_name} = random_regex(
390 '\w' . "{$min,$max}"
391 );
392 }
393 }
394 }
395 # If it's not nullable, doesn't have a default value and isn't part of a
396 # primary key (could be auto-increment) or part of a unique key or part of a
397 # foreign key, then generate a value for it.
398 ▶ elsif (
Condition91%11/12
$info->{'is_nullable'} || exists $info->{'default_value'} || $is{'in_pk'}->($col_name) || $is{'in_uk'}->($col_name) || $self->{'is_fk'}{$name}{$col_name}
l!l&&r!l&&!r
1313848
$info->{"is_nullable"} or exists $info->{"default_value"} or $is{"in_pk"}->($col_name) or $is{"in_uk"}->($col_name)
l!l&&r!l&&!r
129286
$info->{"is_nullable"} or exists $info->{"default_value"} or $is{"in_pk"}->($col_name)
l!l&&r!l&&!r
412588
$info->{"is_nullable"} or exists $info->{"default_value"}
l!l&&r!l&&!r
40213
MC/DC80%4/5
$info->{'is_nullable'} || exists $info->{'default_value'} || $is{'in_pk'}->($col_name) || $is{'in_uk'}->($col_name) || $self->{'is_fk'}{$name}{$col_name}
$info->{"is_nullable"} exists $info->{"default_value"} $is{"in_pk"}->($col_name) $is{"in_uk"}->($col_name) $self->{'is_fk'}{$name}{$col_name}
Truth table83%5/6
A or B or C or D || E
A= $info->{"is_nullable"}B= exists $info->{"default_value"}C= $is{"in_pk"}->($col_name)D= $is{"in_uk"}->($col_name)E= $self->{'is_fk'}{$name}{$col_name}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
399 !$info->{is_nullable} &&
400 !exists $info->{default_value} &&
401 !$is{in_pk}->($col_name) &&
402 !$is{in_uk}->($col_name) &&
403 !$self->{is_fk}{$name}{$col_name}
404 ) {
405 48 ▶ if ( $info->{data_type} eq 'int' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($info->{'data_type'} eq 'int') { } 0 48 48
elsif ($info->{'data_type'} eq 'varchar') { } 48 0 48
406 0 my $min = 0;
407 0 my $max = 100;
408 0 $item->{$col_name} = int(rand($max-$min))+$min;
409 }
410 elsif ( $info->{data_type} eq 'varchar' ) {
411 48 my $min = 1;
412 48 ▶ my $max = $info->{data_length} // 255;
Condition50%1/2
$info->{"data_length"} // 255
l!l
048
MC/DC0%0/1
$info->{"data_length"} // 255
$info->{"data_length"}
Truth table50%1/2
A
A= $info->{"data_length"}
Aresult
11
00
413 48 $item->{$col_name} = random_regex(
414 '\w' . "{$min,$max}"
415 );
416 }
417 }
418 }
419 }
420  
421 sub create_item {
422 134 ▶ my $self = shift;
Subroutine create_item: called CC=3 SCAR=11.0
Pod undocumented
423 134 my ($name, $item) = @_;
424  
425 #warn "Starting with $name (".np($item).")\n";
426 134 $self->fix_columns($name, $item);
427  
428 134 my $source = $self->schema->source($name);
429 134 $self->{hooks}{preprocess}->($name, $source, $item);
430  
431 134 my $child_deps = $self->fix_fk_dependencies($name, $item);
432  
433 #warn "Creating $name (".np($item).")\n";
434 134 my $row = $self->find_by_unique_constraints($name, $item);
435 134 ▶ unless ($row) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row) 132 2 134
436 132 $row = eval {
437 132 $self->schema->resultset($name)->create($item);
438 132 ▶ }; if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 131 132
439 1 warn "ERROR Creating $name (".np($item).")\n";
440 1 die $@;
441 }
442 # This tracks everything that was created, not just what was requested.
443 131 $self->{created}{$name}++;
444 }
445  
446 133 $self->fix_child_dependencies($name, $row, $child_deps);
447  
448 133 $self->{hooks}{postprocess}->($name, $source, $row);
449  
450 133 return $row;
451 }
452  
453 sub run {
454 56 ▶ my $self = shift;
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
455  
456 return $self->schema->txn_do(sub {
457 56 ▶ $self->{rows} = {};
Subroutine __ANON__: called CC=7 SCAR=19.5
458 56 while (1) {
459 57 foreach my $name ( @{$self->{toposort}} ) {
460 116 ▶ next unless $self->{spec}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"spec"}{$name} 35 81 116
461  
462 81 ▶ while ( my $item = shift @{$self->{spec}{$name}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @{$$self{"spec"}{$name};}) 108 80 188
463 108 my $row = $self->create_item($name, $item);
464  
465 107 ▶ if ($self->{initial_spec}{$name}{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"initial_spec"}{$name}{$item}) 86 21 107
466 86 ▶ push @{$self->{rows}{$name} //= []}, $row;
Condition100%2/2
$self->{"rows"}{$name} //= []
l!l
2066
MC/DC100%1/1
$self->{"rows"}{$name} //= []
$self->{"rows"}{$name}
Truth table100%2/2
A
A= $self->{"rows"}{$name}
Aresult
11
00
467 }
468 }
469  
470 80 $self->delete_pending($name);
471 }
472  
473 56 ▶ last unless $self->has_pending();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_pending 55 1 56
474 1 $self->clear_pending();
475 }
476  
477 55 return $self->{rows};
478 56 });
479 }
480  
481 25 1;
482 __END__
↑ Summary Sims.pm »