Sims/Column.pm

filter: Statementstmt 91.2% 83 / 91 Branchbran 75.0% 18 / 24 Conditioncond 86.9% 20 / 23 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 96.3% 26 / 27 Podpod 0.0% 0 / 22 totaltotal 76.7% 152 / 198 CC 23 SCAR 19.9
CC
23
Cov
88%
generate_value
18.7
initialize
6.1
sim_spec
4.0
CRAP 7.3
« Sims/Types.pm ↑ Summary Sims/Util.pm »
1 # This class exists to encapsulate the a DBIx::Class column hashref and provide
2 # Sims-specific functionality to navigate columns and what goes into a column.
3  
4 package DBIx::Class::Sims::Column;
5  
6 54 ▶use 5.010_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 54 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 54 ▶use DDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 54 ▶use DateTime::Event::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # The type functions are given a column object, so have all the random functions
15 # be available as methods on the column object.
16 54 ▶use DBIx::Class::Sims::Random qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
17 random_integer random_decimal
18 random_string random_regex
19 random_item random_choice
20 54 );
21  
22 # Requires the following attributes:
23 # * source
24 # * name
25 # * info
26 1876 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
27 1876 my $class = shift;
28 1876 my $self = bless {@_}, $class;
29 1876 $self->initialize;
30 1876 return $self;
31 }
32  
33 my %types = (
34 756 numeric => {( map { $_ => 1 } qw(
35 tinyint smallint mediumint bigint
36 int integer int1 int2 int3 int4 int8 middleint
37 bool boolean
38 ))},
39 486 decimal => {( map { $_ => 1 } qw(
40 float float4 float8
41 real
42 double
43 decimal dec
44 numeric
45 fixed
46 ))},
47 756 string => {( map { $_ => 1 } qw(
48 char varchar varchar2
49 binary varbinary
50 text tinytext mediumtext longtext long
51 blob tinyblob mediumblob longblob
52 ))},
53 # These will be unhandled
54 54 datetime => {( map { $_ => 1 } qw(
55 date
56 datetime
57 timestamp
58 year
59 ))},
60 #unknown => {( map { $_ => 1 } qw(
61 # enum set bit json
62 # geometry point linestring polygon
63 # multipoint multilinestring multipolygon geometrycollection
64 #))},
65 );
66  
67 1876 ▶sub initialize {
Subroutine initialize: called CC=6 SCAR=18.0
Pod undocumented
68 1876 my $self = shift;
69  
70 1876 $self->{is_in_pk} = 0;
71 1876 $self->{uks} = [];
72 1876 $self->{fks} = [];
73  
74 1876 ▶ $self->info->{data_type} //= 'unknown';
Condition50%1/2
$self->info->{"data_type"} //= "unknown"
l!l
18760
MC/DC0%0/1
$self->info->{"data_type"} //= "unknown"
$self->info->{"data_type"}
Truth table50%1/2
A
A= $self->info->{"data_type"}
Aresult
11
00
75 1876 ▶ if ( exists $types{numeric}{$self->info->{data_type}} ) {
Branch: 4 branches
BranchTrueFalseTotal
if (exists $types{'numeric'}{$self->info->{'data_type'}}) { } 967 909 1876
elsif (exists $types{'decimal'}{$self->info->{'data_type'}}) { } 14 895 909
elsif (exists $types{'string'}{$self->info->{'data_type'}}) { } 893 2 895
elsif (exists $types{'datetime'}{$self->info->{'data_type'}}) { } 2 0 2
76 967 $self->{type} = 'numeric';
77 }
78 elsif ( exists $types{decimal}{$self->info->{data_type}} ) {
79 14 $self->{type} = 'decimal';
80 }
81 elsif ( exists $types{string}{$self->info->{data_type}} ) {
82 893 $self->{type} = 'string';
83 }
84 elsif ( exists $types{datetime}{$self->info->{data_type}} ) {
85 2 $self->{type} = 'datetime';
86 }
87 else {
88 0 $self->{type} = 'unknown';
89 }
90  
91 # This is to conform to the interface expected by the types tests.
92 1876 $self->{predictable_values} = $self->source->runner->{predictable_values};
93  
94 1876 return;
95 }
96  
97 9410 ▶sub info { shift->{info} }
Subroutine info: called CC=1 SCAR=0.0
Pod undocumented
98 14798 ▶sub name { shift->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
99 1976 ▶sub source { shift->{source} }
Subroutine source: called CC=1 SCAR=0.0
Pod undocumented
100 1588 ▶sub sim_spec {
Subroutine sim_spec: called CC=4 SCAR=13.9
Pod undocumented
101 1588 my $self = shift;
102  
103 1588 ▶ unless (exists $self->{sim_spec}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"sim_spec"}) 1356 232 1588
104 1356 ▶ if ($self->info->{sim}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->info->{'sim'}) { } 568 788 1356
105 568 $self->{sim_spec} = MyCloner::clone($self->info->{sim});
106  
107 # Handle the optional plural of "value"
108 568 ▶ if (exists $self->{sim_spec}{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"sim_spec"}{"values"}) 502 66 568
109 502 $self->{sim_spec}{value} = delete $self->{sim_spec}{values};
110 }
111 }
112 else {
113 788 $self->{sim_spec} = undef;
114 }
115 }
116  
117 1588 return $self->{sim_spec};
118 }
119  
120 1170 ▶sub is_nullable { shift->info->{is_nullable} }
Subroutine is_nullable: called CC=1 SCAR=0.0
Pod undocumented
121 16 ▶sub is_auto_increment { shift->info->{is_auto_increment} }
Subroutine is_auto_increment: called CC=1 SCAR=0.0
Pod undocumented
122 504 ▶sub is_inflated { defined shift->info->{_inflate_info} }
Subroutine is_inflated: called CC=1 SCAR=0.0
Pod undocumented
123  
124 136 ▶sub has_default_value { exists shift->{info}{default_value} }
Subroutine has_default_value: called CC=1 SCAR=0.0
Pod undocumented
125 #sub default_value {}
126  
127 1533 ▶sub is_in_pk { shift->{is_in_pk} }
Subroutine is_in_pk: called CC=1 SCAR=0.0
Pod undocumented
128 815 ▶sub in_pk { shift->{is_in_pk} = 1; return }
Subroutine in_pk: called CC=1 SCAR=0.0
Pod undocumented
129  
130 3 ▶sub is_in_uk { @{shift->{uks}} != 0 }
Subroutine is_in_uk: called CC=1 SCAR=0.0
Pod undocumented
131 893 ▶sub in_uk { push @{shift->{uks}}, $_[0]; return }
Subroutine in_uk: called CC=1 SCAR=0.0
Pod undocumented
132  
133 807 ▶sub is_in_fk { @{shift->{fks}} != 0 }
Subroutine is_in_fk: called CC=1 SCAR=0.0
Pod undocumented
134 152 ▶sub in_fk { push @{shift->{fks}}, $_[0]; return }
Subroutine in_fk: called CC=1 SCAR=0.0
Pod undocumented
135 94 ▶sub fks { @{shift->{fks}} }
Subroutine fks: called CC=1 SCAR=0.0
Pod undocumented
136  
137 0 ▶sub is_datetime { shift->{type} eq 'datetime' }
Subroutine is_datetime: not called CC=1 SCAR=6.9
Pod undocumented
138 129 ▶sub is_decimal { shift->{type} eq 'decimal' }
Subroutine is_decimal: called CC=1 SCAR=0.0
Pod undocumented
139 147 ▶sub is_numeric { shift->{type} eq 'numeric' }
Subroutine is_numeric: called CC=1 SCAR=0.0
Pod undocumented
140 116 ▶sub is_string { shift->{type} eq 'string' }
Subroutine is_string: called CC=1 SCAR=0.0
Pod undocumented
141 #sub is_unknown { shift->{type} eq 'unknown' }
142  
143 147 ▶sub generate_value {
Subroutine generate_value: called CC=15 SCAR=29.3
Pod undocumented
144 147 my $self = shift;
145 147 my %opts = @_;
146 147 ▶ $opts{die_on_unknown} //= 1;
Condition50%1/2
$opts{"die_on_unknown"} //= 1
l!l
1470
MC/DC0%0/1
$opts{"die_on_unknown"} //= 1
$opts{"die_on_unknown"}
Truth table50%1/2
A
A= $opts{"die_on_unknown"}
Aresult
11
00
147  
148 147 my $spec = $self->sim_spec;
149 147 ▶ if ( $self->is_numeric ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($self->is_numeric) { } 18 129 147
elsif ($self->is_decimal) { } 13 116 129
elsif ($self->is_string) { } 116 0 116
elsif ($self->is_datetime) { } 0 0 0
elsif ($opts{'die_on_unknown'}) { } 0 0 0
150 18 ▶ my $min = $spec->{min} // 0;
Condition100%2/2
$spec->{"min"} // 0
l!l
216
MC/DC100%1/1
$spec->{"min"} // 0
$spec->{"min"}
Truth table100%2/2
A
A= $spec->{"min"}
Aresult
11
00
151 18 ▶ my $max = $spec->{max} // 100;
Condition100%2/2
$spec->{"max"} // 100
l!l
216
MC/DC100%1/1
$spec->{"max"} // 100
$spec->{"max"}
Truth table100%2/2
A
A= $spec->{"max"}
Aresult
11
00
152 18 return int(rand($max-$min))+$min;
153 }
154 elsif ( $self->is_decimal ) {
155 13 ▶ my $min = $spec->{min} // 0;
Condition100%2/2
$spec->{"min"} // 0
l!l
211
MC/DC100%1/1
$spec->{"min"} // 0
$spec->{"min"}
Truth table100%2/2
A
A= $spec->{"min"}
Aresult
11
00
156 13 ▶ my $max = $spec->{max} // 100;
Condition100%2/2
$spec->{"max"} // 100
l!l
211
MC/DC100%1/1
$spec->{"max"} // 100
$spec->{"max"}
Truth table100%2/2
A
A= $spec->{"max"}
Aresult
11
00
157 13 return rand($max-$min)+$min;
158 }
159 elsif ( $self->is_string ) {
160 116 ▶ my $min = $spec->{min} // 1;
Condition100%2/2
$spec->{"min"} // 1
l!l
4112
MC/DC100%1/1
$spec->{"min"} // 1
$spec->{"min"}
Truth table100%2/2
A
A= $spec->{"min"}
Aresult
11
00
161 116 ▶ my $max = $spec->{max} // $self->info->{data_length} // $self->info->{size} // $min;
Condition88%8/9
(($spec->{"max"} // $self->info->{"data_length"}) // $self->info->{"size"}) // $min
l!l&&r!l&&!r
100160
($spec->{"max"} // $self->info->{"data_length"}) // $self->info->{"size"}
l!l&&r!l&&!r
39716
$spec->{"max"} // $self->info->{"data_length"}
l!l&&r!l&&!r
21113
MC/DC0%0/4
(($spec->{"max"} // $self->info->{"data_length"}) // $self->info->{"size"}) // $min
$spec->{"max"} $self->info->{"data_length"} $self->info->{"size"} $min
Truth table80%4/5
A // B // C // D
A= $spec->{"max"}B= $self->info->{"data_length"}C= $self->info->{"size"}D= $min
ABCDresult
1XXX1
01XX1
001X1
00011
00000
162 116 return $self->random_regex(
163 '\w' . "{$min,$max}"
164 );
165 }
166 elsif ( $self->is_datetime ) {
167 0 my $dt_set = DateTime::Event::Random->new(
168 start => DateTime->new( year => 1900 ),
169 end => DateTime->new( year => 2100 ),
170 );
171 0 return $dt_set->next( DateTime->today )->datetime;
172 }
173 elsif ( $opts{die_on_unknown} ) {
174 0 die "ERROR: @{[$self->source->name]}\.@{[$self->name]} is not nullable, but I don't know how to handle @{[$self->info->{data_type}]}\n";
175 }
176 }
177  
178 54 1;
179 __END__
« Sims/Types.pm ↑ Summary Sims/Util.pm »