Sims.pm

filter: Statementstmt 96.3% 185 / 192 Branchbran 88.7% 55 / 62 Conditioncond 63.4% 33 / 52 MC/DCmcdc 48.1% 13 / 27 Subroutinesub 100% 31 / 31 Podpod 37.5% 3 / 8 totaltotal 86.0% 320 / 372 CC 63 SCAR 28.3
CC
63
Cov
89%
load_sims
40.2
__ANON__
9.0
set_sim_type
8.3
CRAP 16.9
↑ Summary Sims/Item.pm »
1 # vim: set sw=2 ft=perl:
2 package DBIx::Class::Sims;
3  
4 54 ▶use 5.010_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 54 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 54 our $VERSION = '0.500012';
9  
10 {
11 # Do **NOT** import a clone() function into the DBIx::Class::Schema namespace
12 # because that will override DBIC's clone() method and break all the things.
13 package MyCloner;
14  
15 20531 ▶ sub clone {
Subroutine clone: called CC=3 SCAR=11.0
16 20531 my ($data) = @_;
17  
18 20531 ▶ if (ref($data) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 9496 11035 20531
elsif (ref $data eq 'ARRAY') { } 2333 8702 11035
19 return {
20 9496 map { $_ => clone($data->{$_}) }
21 keys %$data
22 };
23 }
24 elsif (ref($data) eq 'ARRAY') {
25 return [
26 2333 map { clone($_) }
27 @$data
28 ];
29 }
30  
31 8702 return $data;
32 }
33 }
34  
35 54 ▶use DDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 54 ▶use Data::Walk qw( walk );
Subroutine BEGIN: called CC=1 SCAR=0.0
38 54 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 54 ▶use DBIx::Class::TopoSort ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 54 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 54 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 54 ▶use List::MoreUtils qw( natatime );
Subroutine BEGIN: called CC=1 SCAR=0.0
43 54 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 {
46 # The aliases in this block are done at BEGIN time so that the ::Types class
47 # can use them when it is loaded through `use`.
48  
49 54 my @sim_names;
50 my %sim_types;
51 54 my @sim_matchers;
52  
53 55 ▶ sub set_sim_type {
Subroutine set_sim_type: called CC=8 SCAR=21.2
Pod documented
54 55 shift;
55 55 ▶ my $types = shift // '';
Condition50%1/2
shift() // ""
l!l
550
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
56  
57 55 ▶ if (ref($types) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $types eq 'HASH') { } 54 1 55
elsif (ref $types eq 'ARRAY') { } 1 0 1
58 54 ▶ while ( my ($name, $meth) = each(%$types) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$types) 648 54 702
59 648 ▶ next unless ref($meth) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $meth eq "CODE" 0 648 648
60  
61 648 $sim_types{$name} = $meth;
62 648 push @sim_names, $name;
63 }
64 }
65 elsif (ref($types) eq 'ARRAY') {
66 1 foreach my $item (@$types) {
67 1 ▶ next unless ref($item->[2]) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item->[2] eq "CODE" 0 1 1
68  
69 1 push @sim_names, $item->[0];
70 1 push @sim_matchers, [ qr/^$item->[1]$/, $item->[2] ];
71 }
72 }
73  
74 55 return;
75 }
76 54 ▶ BEGIN { *set_sim_types = \&set_sim_type; }
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 8 ▶ sub __find_sim_type {
Subroutine __find_sim_type: called CC=3 SCAR=11.0
79 8 my ($str) = @_;
80  
81 8 ▶ unless (exists $sim_types{$str}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $sim_types{$str}) 2 6 8
82 2 ▶ my $item = first { $str =~ $_->[0] } @sim_matchers;
Subroutine __ANON__: called CC=1 SCAR=0.0
83 2 ▶ if ($item) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item) 2 0 2
84 2 $sim_types{$str} = $item->[1];
85 }
86 }
87  
88 8 return $sim_types{$str};
89 }
90  
91 9 ▶ sub sim_type {
Subroutine sim_type: called CC=3 SCAR=12.0
Pod undocumented
92 9 shift;
93  
94 # If no specific type requested, then return the complete list of all
95 # registered types.
96 9 ▶ return sort @sim_names if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 1 8 9
97  
98 8 ▶ return __find_sim_type($_[0]) if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 8 0 8
99 0 return map { __find_sim_type($_) } @_;
100 }
101 54 ▶ BEGIN { *sim_types = \&sim_type; }
Subroutine BEGIN: called CC=1 SCAR=0.0
102 }
103 54 ▶use DBIx::Class::Sims::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 54 ▶use DBIx::Class::Sims::Runner;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 54 ▶use DBIx::Class::Sims::Util qw( normalize_aoh reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108 1 ▶sub add_sims {
Subroutine add_sims: called CC=5 SCAR=16.4
Pod documented
109 1 my $class = shift;
110 1 my ($schema, $source, @remainder) = @_;
111  
112 1 my $rsrc = $schema->source($source);
113 1 my $it = natatime(2, @remainder);
114 1 my $merger = Hash::Merge->new('RIGHT_PRECEDENT');
115 1 ▶ while (my ($column, $sim_info) = $it->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$it()) 1 1 2
116 1 ▶ my $col_info = $schema->source($source)->column_info($column) // next;
Condition50%1/2
$schema->source($source)->column_info($column) // (next)
l!l
10
MC/DC0%0/1
$schema->source($source)->column_info($column) // (next)
$schema->source($source)->column_info($column)
Truth table50%1/2
A
A= $schema->source($source)->column_info($column)
Aresult
11
00
117 $col_info->{sim} = $merger->merge(
118 $col_info->{sim} // {},
119 1 ▶ $sim_info // {},
Condition50%2/4
$col_info->{"sim"} // {}
l!l
01
$sim_info // {}
l!l
10
MC/DC0%0/1
$col_info->{"sim"} // {}
$col_info->{"sim"}
MC/DC0%0/1
$sim_info // {}
$sim_info
Truth table50%1/2
A
A= $col_info->{"sim"}
Aresult
11
00
Truth table50%1/2
A
A= $sim_info
Aresult
11
00
120 );
121 }
122  
123 1 return;
124 }
125 54 *add_sim = \&add_sims;
126  
127 669 ▶sub load_sims {
Subroutine load_sims: called CC=33 SCAR=36.9
Pod documented
128 669 my $self = shift;
129 669 my $schema;
130 669 ▶ if (ref($self) && $self->isa('DBIx::Class::Schema')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and $self->isa('DBIx::Class::Schema')) { } 668 1 669
Condition66%2/3
ref $self and $self->isa("DBIx::Class::Schema")
!ll&&!rl&&r
10668
MC/DC50%1/2
ref $self and $self->isa("DBIx::Class::Schema")
ref $self $self->isa("DBIx::Class::Schema")
Truth table66%2/3
A and B
A= ref $selfB= $self->isa("DBIx::Class::Schema")
ABresult
0X0
100
111
131 668 $schema = $self;
132 }
133 else {
134 1 $schema = shift(@_);
135 }
136 669 my ($spec_proto, $opts_proto) = @_;
137 669 ▶ $spec_proto = MyCloner::clone($spec_proto // {});
Condition50%1/2
$spec_proto // {}
l!l
6690
MC/DC0%0/1
$spec_proto // {}
$spec_proto
Truth table50%1/2
A
A= $spec_proto
Aresult
11
00
138 669 ▶ $opts_proto = MyCloner::clone($opts_proto // {});
Condition100%2/2
$opts_proto // {}
l!l
48621
MC/DC100%1/1
$opts_proto // {}
$opts_proto
Truth table100%2/2
A
A= $opts_proto
Aresult
11
00
139  
140 669 my $spec = massage_input($schema, normalize_input($spec_proto));
141 669 my $opts = normalize_input($opts_proto);
142  
143 # Create a lookup of the items passed in so we can return them back.
144 669 my $initial_spec = {};
145 669 foreach my $name (keys %$spec) {
146 698 my $normalized = normalize_aoh($spec->{$name});
147 698 ▶ unless ($normalized) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($normalized) 1 697 698
148 1 warn "Skipping $name - I don't know what to do!\n";
149 1 delete $spec->{$name};
150 1 next;
151 }
152 697 $spec->{$name} = $normalized;
153  
154 697 foreach my $item (@{$spec->{$name}}) {
155 742 $initial_spec->{$name}{$item} = 1;
156 }
157 }
158  
159 669 my ($rows, $additional) = ({}, {});
160 669 ▶ if (keys %{$spec}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$spec;}) 668 1 669
161 # Yes, this invokes srand() twice, once in implicitly in rand() and once
162 # again right after. But, that's okay. We don't care what the seed is and
163 # this allows DBIC to be called multiple times in the same process in the
164 # same second without problems.
165 668 ▶ $additional->{seed} = $opts->{seed} //= rand(time & $$);
Condition100%2/2
$opts->{"seed"} //= rand(time & $$)
l!l
1667
MC/DC100%1/1
$opts->{"seed"} //= rand(time & $$)
$opts->{"seed"}
Truth table100%2/2
A
A= $opts->{"seed"}
Aresult
11
00
166 668 srand($opts->{seed});
167  
168 668 my @toposort;
169 668 ▶ if ( $opts->{topograph_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'topograph_file'}) { } 1 667 668
170 1 @toposort = load_topograph($opts->{topograph_file});
171 }
172 else {
173 @toposort = DBIx::Class::TopoSort->toposort(
174 $schema,
175 667 ▶ %{$opts->{toposort} // {}},
Condition100%2/2
$opts->{"toposort"} // {}
l!l
10657
MC/DC100%1/1
$opts->{"toposort"} // {}
$opts->{"toposort"}
Truth table100%2/2
A
A= $opts->{"toposort"}
Aresult
11
00
176 );
177 }
178  
179 666 ▶ if ( $opts->{topograph_trace} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"topograph_trace"}) 1 665 666
180 1 save_topograph(\@toposort, $opts->{topograph_trace});
181 }
182  
183 666 ▶ my $strict_mode = $opts->{strict_mode} // 0;
Condition50%1/2
$opts->{"strict_mode"} // 0
l!l
0666
MC/DC0%0/1
$opts->{"strict_mode"} // 0
$opts->{"strict_mode"}
Truth table50%1/2
A
A= $opts->{"strict_mode"}
Aresult
11
00
184 666 ▶ if ($strict_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strict_mode) 0 666 666
185 0 ▶ $opts->{ignore_unknown_tables} //= 0;
Condition0%0/2
$opts->{"ignore_unknown_tables"} //= 0
l!l
00
MC/DC0%0/1
$opts->{"ignore_unknown_tables"} //= 0
$opts->{"ignore_unknown_tables"}
Truth table0%0/2
A
A= $opts->{"ignore_unknown_tables"}
Aresult
11
00
186 0 ▶ $opts->{ignore_unknown_columns} //= 0;
Condition0%0/2
$opts->{"ignore_unknown_columns"} //= 0
l!l
00
MC/DC0%0/1
$opts->{"ignore_unknown_columns"} //= 0
$opts->{"ignore_unknown_columns"}
Truth table0%0/2
A
A= $opts->{"ignore_unknown_columns"}
Aresult
11
00
187 0 ▶ $opts->{allow_relationship_column_names} //= 0;
Condition0%0/2
$opts->{"allow_relationship_column_names"} //= 0
l!l
00
MC/DC0%0/1
$opts->{"allow_relationship_column_names"} //= 0
$opts->{"allow_relationship_column_names"}
Truth table0%0/2
A
A= $opts->{"allow_relationship_column_names"}
Aresult
11
00
188 0 ▶ $opts->{die_on_failure} //= 1;
Condition0%0/2
$opts->{"die_on_failure"} //= 1
l!l
00
MC/DC0%0/1
$opts->{"die_on_failure"} //= 1
$opts->{"die_on_failure"}
Truth table0%0/2
A
A= $opts->{"die_on_failure"}
Aresult
11
00
189 0 ▶ $opts->{die_on_unique_mismatch} //= 1;
Condition0%0/2
$opts->{"die_on_unique_mismatch"} //= 1
l!l
00
MC/DC0%0/1
$opts->{"die_on_unique_mismatch"} //= 1
$opts->{"die_on_unique_mismatch"}
Truth table0%0/2
A
A= $opts->{"die_on_unique_mismatch"}
Aresult
11
00
190 }
191  
192 my $runner = DBIx::Class::Sims::Runner->new(
193 parent => $self,
194 schema => $schema,
195 toposort => \@toposort,
196 initial_spec => $initial_spec,
197 spec => $spec,
198 constraints => normalize_input($opts->{constraints} // {}),
199  
200 hooks => $opts->{hooks} // {},
201  
202 # Set this to false to throw a warning if a PK auto-increment column
203 # has a value set. It defaults to false. Set to true to disable.
204 allow_pk_set_value => $opts->{allow_pk_set_value} // 0,
205 ignore_unknown_tables => $opts->{ignore_unknown_tables} // 0,
206 ignore_unknown_columns => $opts->{ignore_unknown_columns} // 0,
207 allow_relationship_column_names => $opts->{allow_relationship_column_names} // 1,
208 die_on_unique_mismatch => $opts->{die_on_unique_mismatch} // 1,
209  
210 object_trace => $opts->{object_trace},
211 predictable_values => $opts->{predictable_values},
212 666 ▶ );
Condition100%14/14
$opts->{"constraints"} // {}
l!l
8658
$opts->{"hooks"} // {}
l!l
8658
$opts->{"allow_pk_set_value"} // 0
l!l
13653
$opts->{"ignore_unknown_tables"} // 0
l!l
1665
$opts->{"ignore_unknown_columns"} // 0
l!l
1665
$opts->{"allow_relationship_column_names"} // 1
l!l
1665
$opts->{"die_on_unique_mismatch"} // 1
l!l
1665
MC/DC100%1/1
$opts->{"constraints"} // {}
$opts->{"constraints"}
MC/DC100%1/1
$opts->{"hooks"} // {}
$opts->{"hooks"}
MC/DC100%1/1
$opts->{"allow_pk_set_value"} // 0
$opts->{"allow_pk_set_value"}
MC/DC100%1/1
$opts->{"ignore_unknown_tables"} // 0
$opts->{"ignore_unknown_tables"}
MC/DC100%1/1
$opts->{"ignore_unknown_columns"} // 0
$opts->{"ignore_unknown_columns"}
MC/DC100%1/1
$opts->{"allow_relationship_column_names"} // 1
$opts->{"allow_relationship_column_names"}
MC/DC100%1/1
$opts->{"die_on_unique_mismatch"} // 1
$opts->{"die_on_unique_mismatch"}
Truth table100%2/2
A
A= $opts->{"constraints"}
Aresult
11
00
Truth table100%2/2
A
A= $opts->{"hooks"}
Aresult
11
00
Truth table100%2/2
A
A= $opts->{"allow_pk_set_value"}
Aresult
11
00
Truth table100%2/2
A
A= $opts->{"ignore_unknown_tables"}
Aresult
11
00
Truth table100%2/2
A
A= $opts->{"ignore_unknown_columns"}
Aresult
11
00
Truth table100%2/2
A
A= $opts->{"allow_relationship_column_names"}
Aresult
11
00
Truth table100%2/2
A
A= $opts->{"die_on_unique_mismatch"}
Aresult
11
00
213  
214 666 $rows = eval {
215 666 $runner->run();
216 666 ▶ }; if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 112 554 666
217 112 $additional->{error} = $@;
218  
219 112 ▶ if ($opts->{die_on_failure} // 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"die_on_failure"} // 1) 112 0 112
Condition50%1/2
$opts->{"die_on_failure"} // 1
l!l
0112
MC/DC0%0/1
$opts->{"die_on_failure"} // 1
$opts->{"die_on_failure"}
Truth table50%1/2
A
A= $opts->{"die_on_failure"}
Aresult
11
00
220 112 warn "SEED: $opts->{seed}\n";
221 112 die $@;
222 }
223 }
224  
225 554 $additional->{created} = $runner->{created};
226 554 $additional->{duplicates} = $runner->{duplicates};
227  
228 # Force a reload from the database of every row we're returning.
229 554 foreach my $item (values %$rows) {
230 579 $_->discard_changes for @$item;
231 }
232 }
233  
234 555 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 543 12 555
235 543 return ($rows, $additional);
236 }
237 else {
238 12 return $rows;
239 }
240 }
241  
242 54 ▶use JSON::MaybeXS qw( decode_json encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
243 54 ▶use YAML::Any qw( LoadFile Load );
Subroutine BEGIN: called CC=1 SCAR=0.0
244 2009 ▶sub normalize_input {
Subroutine normalize_input: called CC=4 SCAR=13.9
Pod undocumented
245 2009 my ($proto) = @_;
246  
247 2009 ▶ if ( ref($proto) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) 2005 4 2009
248 2005 return $proto;
249 }
250  
251 # Doing a stat on a filename with a newline throws an error.
252 4 my $x = eval {
253 54 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
254 4 ▶ if ( -e $proto ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $proto) 2 2 4
255 2 return LoadFile($proto);
256 }
257 };
258 4 ▶ return $x if $x;
Branch: 1 branch
BranchTrueFalseTotal
if $x 2 2 4
259  
260 2 return Load($proto);
261 }
262  
263 675 ▶sub massage_input {
Subroutine massage_input: called CC=1 SCAR=0.0
Pod undocumented
264 675 my ($schema, $struct) = @_;
265  
266 675 my $dtp = $schema->storage->datetime_parser;
267 675 my $merger = Hash::Merge->new('RIGHT_PRECEDENT');
268 walk({
269 2785 ▶ preprocess => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
270 # Don't descend into the weeds. Only do the things we care about.
271 2785 ▶ return if grep { blessed($_) } @_;
Branch: 1 branch
BranchTrueFalseTotal
if grep {builtin::blessed($_);} @_ 8 2777 2785
272 2777 ▶ return unless grep { reftype($_) } @_;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {reftype($_);} @_ 617 2160 2777
273 2160 return @_;
274 },
275 4598 ▶ wanted => sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
276 4598 ▶ return unless reftype($_) eq 'HASH' && !blessed($_);
Branch: 1 branch
BranchTrueFalseTotal
unless reftype($_) eq "HASH" and not builtin::blessed($_) 2673 1925 4598
Condition66%2/3
reftype($_) eq "HASH" and not builtin::blessed($_)
!ll&&!rl&&r
267301925
MC/DC50%1/2
reftype($_) eq "HASH" and not builtin::blessed($_)
reftype($_) eq "HASH" not builtin::blessed($_)
Truth table66%2/3
A and not(B)
A= reftype($_) eq "HASH"B= not builtin::blessed($_)
ABresult
0X0
100
111
277 1925 foreach my $k ( keys %$_ ) {
278 2032 my $t = $_;
279  
280 # Expand the dot-naming convention.
281 2032 ▶ while ( $k =~ /([^.]*)\.(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /([^.]*)\.(.*)/) 23 2032 2055
282 $t->{$1} = $merger->merge(
283 ($t->{$1} // {}),
284 23 ▶ { $2 => delete($t->{$k}) },
Condition100%2/2
$t->{$1} // {}
l!l
122
MC/DC100%1/1
$t->{$1} // {}
$t->{$1}
Truth table100%2/2
A
A= $t->{$1}
Aresult
11
00
285 );
286 23 $t = $t->{$1}; $k = $2;
287 }
288  
289 # Handle DateTime values passed to us.
290 2032 ▶ if (defined $t->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t->{$k}) 2029 3 2032
291 2029 ▶ if ( $t->{$k} =~ /^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{$k} =~ /^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)$/) 2 2027 2029
292 # format_datetime() requires a DateTime object. This may be a
293 # string, therefore hoist it if need-be.
294 2 ▶ unless (blessed($t->{$k})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($t->{$k})) 1 1 2
295 1 $t->{$k} = DateTime->new(
296 year => $1, month => $2, day => $3,
297 hour => $4, minute => $5, second => $6,
298 );
299 }
300 2 $t->{$k} = $dtp->format_datetime($t->{$k});
301 }
302 }
303 }
304 },
305 675 }, $struct);
306  
307 675 return $struct;
308 }
309  
310 1 ▶sub load_topograph {
Subroutine load_topograph: called CC=1 SCAR=0.0
Pod undocumented
311 1 my ($filename) = @_;
312 1 my $data = do {
313 1 local $/;
314 1 open my $fh, '<', $filename;
315 1 <$fh>;
316 };
317 1 return @{decode_json($data)};
318 }
319  
320 1 ▶sub save_topograph {
Subroutine save_topograph: called CC=1 SCAR=0.0
Pod undocumented
321 1 my ($array, $filename) = @_;
322 1 open my $fh, '>', $filename;
323 1 print $fh encode_json($array);
324 1 close $fh;
325 }
326  
327 54 1;
328 __END__
↑ Summary Sims/Item.pm »