Data/Plugin/SQLTypes.pm

filter: Statementstmt 84.5% 82 / 97 Branchbran 70.5% 48 / 68 Conditioncond 40.0% 20 / 50 MC/DCmcdc 19.2% 5 / 26 Subroutinesub 93.7% 30 / 32 Podpod 92.8% 13 / 14 totaltotal 68.9% 198 / 287 CC 54 SCAR 20.3
CC
54
Cov
70%
_json_encoder
30.0
generator_for_type
20.0
varchar
14.1
CRAP 7.6
« Data/Template.pm ↑ Summary Data/GeneratorSub.pm »
1 package Mock::Data::Plugin::SQLTypes;
2 1 ▶use Mock::Data::Plugin -exporter_setup => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Mock::Data::Plugin::Net qw( cidr macaddr ), 'ipv4', { -as => 'inet' };
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mock::Data::Plugin::Number qw( integer decimal float sequence uuid byte );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mock::Data::Plugin::Text 'join' => { -as => 'text_join' }, 'words';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our %type_generators= map +($_ => 1), qw(
7 integer tinyint smallint bigint
8 sequence serial smallserial bigserial
9 numeric decimal
10 float float4 real float8 double double_precision
11 bit bool boolean
12 varchar char nvarchar
13 text tinytext mediumtext longtext ntext
14 blob tinyblob mediumblob longblob bytea
15 varbinary binary
16 date datetime datetime2 datetimeoffset timestamp
17 datetime_with_time_zone datetime_without_time_zone
18 json jsonb
19 uuid inet cidr macaddr
20 );
21 1 export(keys %type_generators);
22  
23 # ABSTRACT: Collection of generators that produce data matching a SQL column type
24 1 our $VERSION = '0.04'; # VERSION
25  
26  
27 1 ▶sub apply_mockdata_plugin {
Subroutine apply_mockdata_plugin: called CC=1 SCAR=0.0
Pod undocumented
28 1 my ($class, $mock)= @_;
29 1 $mock->load_plugin('Text')->add_generators(
30 map +("SQL::$_" => $class->can($_)), keys %type_generators
31 );
32 }
33  
34  
35 0 ▶sub generator_for_type {
Subroutine generator_for_type: not called CC=4 SCAR=30.0
Pod documented
36 0 my ($mock, $type)= @_;
37 0 $type =~ s/\s+/_/g;
38 my $gen= $mock->generators->{$type} // $mock->generators->{"SQL::$type"}
39 0 ▶ // $type_generators{$type} && Mock::Data::GeneratorSub->new(__PACKAGE__->can($type));
Condition0%0/9
($mock->generators->{$type} // $mock->generators->{"SQL::$type"}) // $Mock::Data::Plugin::SQLTypes::type_generators{$type} && "Mock::Data::GeneratorSub"->new(__PACKAGE__->can($type))
l!l&&r!l&&!r
000
$mock->generators->{$type} // $mock->generators->{"SQL::$type"}
l!l&&r!l&&!r
000
$Mock::Data::Plugin::SQLTypes::type_generators{$type} and "Mock::Data::GeneratorSub"->new(__PACKAGE__->can($type))
!ll&&!rl&&r
000
MC/DC0%0/4
($mock->generators->{$type} // $mock->generators->{"SQL::$type"}) // $Mock::Data::Plugin::SQLTypes::type_generators{$type} && "Mock::Data::GeneratorSub"->new(__PACKAGE__->can($type))
$mock->generators->{$type} $mock->generators->{"SQL::$type"} $Mock::Data::Plugin::SQLTypes::type_generators{$type} "Mock::Data::GeneratorSub"->new(__PACKAGE__->can($type))
Truth table0%0/5
A // B // C and D
A= $mock->generators->{$type}B= $mock->generators->{"SQL::$type"}C= $Mock::Data::Plugin::SQLTypes::type_generators{$type}D= "Mock::Data::GeneratorSub"->new(__PACKAGE__->can($type))
ABCDresult
1XXX1
01XX1
00111
000X0
00100
40 # TODO: check for complex things like postgres arrays
41 0 return $gen;
42 }
43  
44  
45 10 ▶sub tinyint {
Subroutine tinyint: called CC=3 SCAR=11.0
Pod documented
46 10 my $mock= shift;
47 10 ▶ my $params= ref $_[0] eq 'HASH'? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 5 5 10
48 10 ▶ integer($mock, { $params? %$params : (), bits => 8 }, @_);
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 5 5 10
49 }
50  
51 10 ▶sub smallint {
Subroutine smallint: called CC=3 SCAR=11.0
Pod documented
52 10 my $mock= shift;
53 10 ▶ my $params= ref $_[0] eq 'HASH'? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 5 5 10
54 10 ▶ integer($mock, { $params? %$params : (), bits => 16 }, @_);
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 5 5 10
55 }
56  
57 10 ▶sub bigint {
Subroutine bigint: called CC=3 SCAR=11.0
Pod documented
58 10 my $mock= shift;
59 10 ▶ my $params= ref $_[0] eq 'HASH'? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 5 5 10
60 10 ▶ integer($mock, { $params? %$params : (), bits => 64 }, @_);
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 5 5 10
61 }
62  
63  
64 1 ▶BEGIN { *bigserial= *smallserial= *serial= *sequence; }
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66  
67 1 ▶BEGIN { *numeric= *decimal; }
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69  
70 1 ▶BEGIN { *real= *float4= *float; }
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 15 ▶sub double {
Subroutine double: called CC=3 SCAR=11.7
Pod documented
73 15 my $mock= shift;
74 15 ▶ my $params= ref $_[0] eq 'HASH'? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 15 15
75 15 ▶ float($mock, { bits => 53, $params? %$params : () }, @_);
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 0 15 15
76 }
77  
78 1 ▶BEGIN { *float8= *double_precision= *double; }
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80  
81 15 ▶sub bit {
Subroutine bit: called CC=1 SCAR=0.0
Pod documented
82 15 int rand 2;
83 }
84 1 ▶BEGIN { *bool= *boolean= *bit; }
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86  
87 30 ▶sub varchar {
Subroutine varchar: called CC=12 SCAR=26.4
Pod documented
88 30 my $mock= shift;
89 30 ▶ my $params= ref $_[0] eq 'HASH'? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 10 20 30
90 30 ▶ my $size= shift // ($params? $params->{size} : undef) // 16;
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 10 5 15
Condition100%5/5
(shift() // ($params ? $params->{"size"} : undef)) // 16
l!l
255
shift() // ($params ? $params->{"size"} : undef)
l!l&&r!l&&!r
15105
MC/DC100%2/2
(shift() // ($params ? $params->{"size"} : undef)) // 16
shift() ($params ? $params->{"size"} : undef)
Truth table100%3/3
A // B
A= shift()B= ($params ? $params->{"size"} : undef)
ABresult
1X1
011
000
91 30 ▶ my $size_weight= ($params? $params->{size_weight} : undef) // \&_default_size_weight;
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 10 20 30
Condition50%1/2
($params ? $params->{"size_weight"} : undef) // \&Mock::Data::Plugin::SQLTypes::_default_size_weight
l!l
030
MC/DC0%0/1
($params ? $params->{"size_weight"} : undef) // \&Mock::Data::Plugin::SQLTypes::_default_size_weight
($params ? $params->{"size_weight"} : undef)
Truth table50%1/2
A
A= ($params ? $params->{"size_weight"} : undef)
Aresult
11
00
92 30 ▶ my $source= ($params? $params->{source} : undef);
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 10 20 30
93 30 ▶ if (defined $source && !ref $source) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source and not ref $source) { } 0 30 30
Condition33%1/3
defined $source and not ref $source
!ll&&!rl&&r
3000
MC/DC0%0/2
defined $source and not ref $source
defined $source not ref $source
Truth table33%1/3
A and not(B)
A= defined $sourceB= not ref $source
ABresult
0X0
100
111
94 Carp::croak("No generator '$source' available")
95 0 ▶ unless $mock->generators->{$source};
Branch: 1 branch
BranchTrueFalseTotal
unless $mock->generators->{$source} 0 0 0
96 } else {
97 30 ▶ $source= $mock->generators->{word}? 'word' : \&word;
Branch: 1 branch
BranchTrueFalseTotal
$mock->generators->{'word'} ? : 30 0 30
98 }
99 30 return text_join($mock, {
100 source => $source,
101 max_len => $size,
102 len => $size_weight->($size),
103 });
104 }
105 30 ▶sub _default_size_weight {
Subroutine _default_size_weight: called CC=3 SCAR=11.1
106 30 my $size= shift;
107 30 ▶ $size <= 32? int rand($size+1)
Branch: 2 branches
BranchTrueFalseTotal
$size <= 32 ? : 25 5 30
int rand 100 ? : 5 0 5
108 : int rand(100)? int rand(33)
109 : 33+int rand($size-31)
110 }
111  
112  
113  
114 1 ▶BEGIN { *nvarchar= *varchar; }
Subroutine BEGIN: called CC=1 SCAR=0.0
115  
116 5 ▶sub text {
Subroutine text: called CC=3 SCAR=11.7
Pod documented
117 5 my $mock= shift;
118 5 ▶ my $params= ref $_[0] eq 'HASH'? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 5 5
119 5 ▶ varchar($mock, { size => 256, ($params? %$params : ()) }, @_);
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 0 5 5
120 }
121  
122 1 ▶BEGIN { *ntext= *tinytext= *mediumtext= *longtext= *text; }
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124  
125 10 ▶sub char {
Subroutine char: called CC=7 SCAR=20.2
Pod documented
126 10 my $mock= shift;
127 10 ▶ my $params= ref $_[0] eq 'HASH'? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 10 10
128 10 ▶ my $size= @_? shift : ($params? $params->{size} : undef) // 1;
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 5 5 10
$params ? : 0 5 5
Condition50%1/2
($params ? $params->{"size"} : undef) // 1
l!l
05
MC/DC0%0/1
($params ? $params->{"size"} : undef) // 1
($params ? $params->{"size"} : undef)
Truth table50%1/2
A
A= ($params ? $params->{"size"} : undef)
Aresult
11
00
129 10 ▶ my $str= varchar($mock, ($params? $params : ()), $size);
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 0 10 10
130 10 ▶ $str .= ' 'x($size - length $str) if length $str < $size;
Branch: 1 branch
BranchTrueFalseTotal
if length $str < $size 5 5 10
131 10 return $str;
132 }
133  
134  
135 20 ▶sub _epoch_to_iso8601 {
Subroutine _epoch_to_iso8601: called CC=1 SCAR=0.0
136 20 my @t= localtime(shift);
137 20 return sprintf "%04d-%02d-%02d %02d:%02d:%02d", $t[5]+1900, $t[4]+1, @t[3,2,1,0];
138 }
139 20 ▶sub _iso8601_to_epoch {
Subroutine _iso8601_to_epoch: called CC=5 SCAR=17.8
140 20 my $str= shift;
141 20 ▶ $str =~ /^
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /^ (\d{4}) - (\d{2}) - (\d{2}) (?: [T ] (\d{2}) : (\d{2}) # maybe time (?: :(\d{2}) # maybe seconds (?: \. \d+ )? # ignore milliseconds )? (?: Z | [-+ ][:\d]+ )? # ignore timezone or Z )? /x or Carp::croak("Invalid date '${str}'. Expecting format YYYY-MM-DD[ HH:MM:SS[.SSS][TZ]]") 20 0 20
142 (\d{4}) - (\d{2}) - (\d{2})
143 (?: [T ] (\d{2}) : (\d{2}) # maybe time
144 (?: :(\d{2}) # maybe seconds
145 (?: \. \d+ )? # ignore milliseconds
146 )?
147 (?: Z | [-+ ][:\d]+ )? # ignore timezone or Z
148 )?
149 /x or Carp::croak("Invalid date '$str'. Expecting format YYYY-MM-DD[ HH:MM:SS[.SSS][TZ]]");
150 20 require POSIX;
151 20 ▶ return POSIX::mktime($6||0, $5||0, $4||0, $3, $2-1, $1-1900);
Condition50%3/6
$6 || 0
l!l
020
$5 || 0
l!l
020
$4 || 0
l!l
020
MC/DC0%0/1
$6 || 0
$6
MC/DC0%0/1
$5 || 0
$5
MC/DC0%0/1
$4 || 0
$4
Truth table50%1/2
A
A= $6
Aresult
11
00
Truth table50%1/2
A
A= $5
Aresult
11
00
Truth table50%1/2
A
A= $4
Aresult
11
00
152 }
153  
154 20 ▶sub datetime {
Subroutine datetime: called CC=6 SCAR=18.0
Pod documented
155 20 my $mock= shift;
156 20 ▶ my $params= ref $_[0] eq 'HASH'? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 10 10 20
157 20 ▶ my $before= $params && $params->{before}? _iso8601_to_epoch($params->{before}) : (time - 86400);
Branch: 1 branch
BranchTrueFalseTotal
$params && $params->{'before'} ? : 10 10 20
Condition66%2/3
$params and $params->{"before"}
!ll&&!rl&&r
10010
MC/DC50%1/2
$params and $params->{"before"}
$params $params->{"before"}
Truth table66%2/3
A and B
A= $paramsB= $params->{"before"}
ABresult
0X0
100
111
158 20 ▶ my $after= $params && $params->{after}? _iso8601_to_epoch($params->{after}) : (time - int(10*365.25*86400));
Branch: 1 branch
BranchTrueFalseTotal
$params && $params->{'after'} ? : 10 10 20
Condition66%2/3
$params and $params->{"after"}
!ll&&!rl&&r
10010
MC/DC50%1/2
$params and $params->{"after"}
$params $params->{"after"}
Truth table66%2/3
A and B
A= $paramsB= $params->{"after"}
ABresult
0X0
100
111
159 20 _epoch_to_iso8601($after + int rand($before-$after));
160 }
161  
162 10 ▶sub date {
Subroutine date: called CC=1 SCAR=0.0
Pod documented
163 10 substr(datetime(@_), 0, 10)
164 }
165  
166 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
167 1 *timestamp= *datetime2= *datetime_without_time_zone= *datetime;
168 1 *datetimeoffset= *datetime_with_time_zone= *datetime;
169 }
170  
171  
172 10 ▶sub blob {
Subroutine blob: called CC=5 SCAR=16.7
Pod documented
173 10 my $mock= shift;
174 10 ▶ my $params= ref $_[0] eq 'HASH'? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 10 10
175 10 ▶ my $size= shift // ($params? $params->{size} : undef) // 256;
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 0 5 5
Condition80%4/5
(shift() // ($params ? $params->{"size"} : undef)) // 256
l!l
55
shift() // ($params ? $params->{"size"} : undef)
l!l&&r!l&&!r
505
MC/DC50%1/2
(shift() // ($params ? $params->{"size"} : undef)) // 256
shift() ($params ? $params->{"size"} : undef)
Truth table66%2/3
A // B
A= shift()B= ($params ? $params->{"size"} : undef)
ABresult
1X1
011
000
176 10 byte($mock, $size);
177 }
178  
179 1 ▶BEGIN { *tinyblob= *mediumblob= *longblob= *bytea= *binary= *varbinary= *blob; }
Subroutine BEGIN: called CC=1 SCAR=0.0
180  
181  
182 1 our $json;
183 0 ▶sub _json_encoder {
Subroutine _json_encoder: not called CC=5 SCAR=34.0
184 0 ▶ $json //= do {
Condition0%0/3
$Mock::Data::Plugin::SQLTypes::json //= do { local $@; my $mod = eval { do { require JSON::MaybeXS; "JSON::MaybeXS" } } || eval { do { require JSON; "JSON" } } || eval { do { require JSON::PP; "JSON::PP" } } or Carp::croak("No JSON module found. This must be installed for the SQL::json generator."); $mod->new->canonical->ascii }
l!l&&r!l&&!r
000
MC/DC0%0/2
$Mock::Data::Plugin::SQLTypes::json //= do { local $@; my $mod = eval { do { require JSON::MaybeXS; "JSON::MaybeXS" } } || eval { do { require JSON; "JSON" } } || eval { do { require JSON::PP; "JSON::PP" } } or Carp::croak("No JSON module found. This must be installed for the SQL::json generator."); $mod->new->canonical->ascii }
$Mock::Data::Plugin::SQLTypes::json do { local $@; my $mod = eval { do { require JSON::MaybeXS; "JSON::MaybeXS" } } || eval { do { require JSON; "JSON" } } || eval { do { require JSON::PP; "JSON::PP" } } or Carp::croak("No JSON module found. This must be installed for the SQL::json generator."); $mod->new->canonical->ascii }
Truth table0%0/3
A //= B
A= $Mock::Data::Plugin::SQLTypes::jsonB= do { local $@; my $mod = eval { do { require JSON::MaybeXS; "JSON::MaybeXS" } } || eval { do { require JSON; "JSON" } } || eval { do { require JSON::PP; "JSON::PP" } } or Carp::croak("No JSON module found. This must be installed for the SQL::json generator."); $mod->new->canonical->ascii }
ABresult
1X1
011
000
185 0 local $@;
186 0 my $mod= eval { require JSON::MaybeXS; 'JSON::MaybeXS' }
187 0 || eval { require JSON; 'JSON' }
188 0 ▶ || eval { require JSON::PP; 'JSON::PP' }
Branch: 1 branch
BranchTrueFalseTotal
my $mod = eval { do { require JSON::MaybeXS; "JSON::MaybeXS" } } || eval { do { require JSON; "JSON" } } || eval { do { require JSON::PP; "JSON::PP" } } or Carp::croak("No JSON module found. This must be installed for the SQL::json generator.") 0 0 0
Condition0%0/6
eval { do { require JSON::MaybeXS; 'JSON::MaybeXS' } } || eval { do { require JSON; 'JSON' } } || eval { do { require JSON::PP; 'JSON::PP' } }
l!l&&r!l&&!r
000
eval { do { require JSON::MaybeXS; "JSON::MaybeXS" } } or eval { do { require JSON; "JSON" } }
l!l&&r!l&&!r
000
MC/DC0%0/3
eval { do { require JSON::MaybeXS; 'JSON::MaybeXS' } } || eval { do { require JSON; 'JSON' } } || eval { do { require JSON::PP; 'JSON::PP' } }
eval { do { require JSON::MaybeXS; "JSON::MaybeXS" } } eval { do { require JSON; "JSON" } } eval { do { require JSON::PP; 'JSON::PP' } }
Truth table0%0/4
A or B || C
A= eval { do { require JSON::MaybeXS; "JSON::MaybeXS" } }B= eval { do { require JSON; "JSON" } }C= eval { do { require JSON::PP; 'JSON::PP' } }
ABCresult
1XX1
01X1
0011
0000
189 or Carp::croak("No JSON module found. This must be installed for the SQL::json generator.");
190 0 $mod->new->canonical->ascii
191 };
192 }
193  
194 10 ▶sub json {
Subroutine json: called CC=5 SCAR=18.6
Pod documented
195 10 my $mock= shift;
196 10 ▶ my $params= ref $_[0] eq 'HASH'? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 10 10
197 10 ▶ my $data= shift // ($params? $params->{data} : undef);
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 0 10 10
Condition33%1/3
shift() // ($params ? $params->{"data"} : undef)
l!l&&r!l&&!r
0010
MC/DC0%0/2
shift() // ($params ? $params->{"data"} : undef)
shift() ($params ? $params->{"data"} : undef)
Truth table33%1/3
A // B
A= shift()B= ($params ? $params->{"data"} : undef)
ABresult
1X1
011
000
198 10 ▶ return defined $data? _json_encoder->encode($data) : '{}';
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 0 10 10
199 }
200  
201 1 ▶BEGIN { *jsonb= *json; }
Subroutine BEGIN: called CC=1 SCAR=0.0
202  
203  
204 1 1;
205  
206 __END__
« Data/Template.pm ↑ Summary Data/GeneratorSub.pm »