Cheater/Eval.pm

filter: Statementstmt 63.4% 255 / 402 Branchbran 52.8% 92 / 174 Conditioncond 43.8% 25 / 57 MC/DCmcdc 6.0% 2 / 33 Subroutinesub 73.0% 19 / 26 Podpod 0.0% 0 / 16 totaltotal 55.5% 393 / 708 CC 126 SCAR 43.4
CC
126
Cov
59%
gen_txt_col
181.2
gen_num_col
82.8
gen_table_schema
56.0
CRAP 76.6
« Cheater/Parser.pm ↑ Summary Cheater/Util.pm »
1 package Cheater::Eval;
2  
3 7 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #use Smart::Comments;
7 7 ▶use Date::Calc qw( Localtime );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Data::Random qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 rand_chars
10 rand_date rand_time rand_datetime
11 7 );
12 7 ▶use Parse::RandGen::Regexp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Cheater::Util qw( quote_sql_str );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub get_today ();
17  
18 7 our $NowDate = 'now';
19 7 our $NowDatetime = 'now';
20  
21 7 has 'ast' => (is => 'ro', isa => 'Cheater::AST');
22 7 has '_samples' => (is => 'ro', isa => 'HashRef');
23 7 has '_cols_visited' => (is => 'ro', isa => 'HashRef');
24  
25 sub pick_elems ($$$);
26  
27 sub gen_txt_col ($$$$$$$);
28 sub gen_num_col ($$$$$$);
29 sub gen_domain_val ($);
30  
31 sub gen_int ($);
32 sub gen_real ($);
33  
34 30 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
35 30 my $self = shift;
36 30 $self->{_samples} = {};
37 30 $self->{_cols_visited} = {};
38 }
39  
40 30 ▶sub go {
Subroutine go: called CC=2 SCAR=6.9
Pod undocumented
41 30 my $self = shift;
42 30 my $ast = $self->ast;
43 30 my $goals = $ast->goals;
44 ### $goals
45  
46 30 my %computed;
47 30 for my $goal (sort keys %$goals) {
48 32 $computed{$goal} = $self->gen_goal($goal);
49 }
50  
51 25 return \%computed;
52 }
53  
54 0 ▶sub rand_regex ($) {
Subroutine rand_regex: not called CC=1 SCAR=6.9
Pod undocumented
55 0 my $regex = shift;
56 0 Parse::RandGen::Regexp->new(qr/$regex/)->pick();
57 }
58  
59 32 ▶sub gen_goal {
Subroutine gen_goal: called CC=3 SCAR=11.0
Pod undocumented
60 32 my ($self, $table) = @_;
61 32 my $ast = $self->ast;
62  
63 32 my $goals = $ast->goals;
64 32 my $tables = $ast->tables;
65  
66 32 my $rows = $goals->{$table};
67  
68 32 my @cols_data;
69 32 ▶ my $tb_spec = $tables->{$table} or
Branch: 1 branch
BranchTrueFalseTotal
my $tb_spec = $tables->{$table} or die "Table $table not defined.\n" 32 0 32
70 die "Table $table not defined.\n";
71  
72 32 for my $col (@$tb_spec) {
73 #say "col: ", $col->[0];
74 35 my $name = $col->[0];
75 35 push @cols_data, $self->gen_column($table, $name);
76 }
77  
78 27 return \@cols_data;
79 }
80  
81 36 ▶sub gen_column {
Subroutine gen_column: called CC=10 SCAR=25.8
Pod undocumented
82 36 my ($self, $table, $col_name) = @_;
83  
84 #warn "gen column $table.$col_name...\n";
85  
86 36 my $samples = $self->_samples;
87 36 my $cols_visited = $self->_cols_visited;
88  
89 36 my $ast = $self->ast;
90 36 my $deps = $ast->deps;
91 36 my $goals = $ast->goals;
92 36 my $cols = $ast->cols;
93 36 my $types = $ast->types;
94 36 my $col_clones = $ast->col_clones;
95  
96 36 my $qcol = "$table.$col_name";
97  
98 36 ▶ if (defined $samples->{$qcol}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $samples->{$qcol}) 1 35 36
99 1 return $samples->{$qcol};
100 }
101  
102 35 ▶ if (my $dep = $cols_visited->{$qcol}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dep = $cols_visited->{$qcol}) 0 35 35
103 0 die "ERROR: Found circular column references: $qcol references $dep but $dep somehow depends on $qcol.\n";
104 }
105  
106 35 my $rows = $goals->{$table};
107  
108 35 ▶ my $spec = $cols->{$qcol} or
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $cols->{$qcol} or die "Column spec not found for $qcol\n" 35 0 35
109 die "Column spec not found for $qcol\n";
110  
111 my $type = $spec->{type} or
112 35 ▶ die "Type not found for $qcol";
Branch: 1 branch
BranchTrueFalseTotal
my $type = $spec->{"type"} or die "Type not found for $qcol" 35 0 35
113  
114 35 my $attrs = $spec->{attrs};
115 35 my $domain = $spec->{domain};
116  
117 35 ▶ if (my $dep = $deps->{$qcol}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dep = $deps->{$qcol}) 1 34 35
118 #warn "setting $qcol rely on $dep...\n";
119  
120 1 $cols_visited->{$qcol} = $dep;
121 1 my ($dep_table, $dep_col_name) = split /\./, $dep, 2;
122 1 ▶ if (! $cols->{$dep}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cols->{$dep}) 0 1 1
123 0 die "ERROR: Column $qcol references non-existent column $dep.\n";
124 }
125 1 my $refs_data = $self->gen_column($dep_table, $dep_col_name);
126  
127 1 my $data;
128 1 ▶ if ($col_clones->{$qcol} && $col_clones->{$qcol} eq $dep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_clones->{$qcol} and $col_clones->{$qcol} eq $dep) { } 1 0 1
Condition33%1/3
$col_clones->{$qcol} and $col_clones->{$qcol} eq $dep
!ll&&!rl&&r
001
MC/DC0%0/2
$col_clones->{$qcol} and $col_clones->{$qcol} eq $dep
$col_clones->{$qcol} $col_clones->{$qcol} eq $dep
Truth table33%1/3
A and B
A= $col_clones->{$qcol}B= $col_clones->{$qcol} eq $dep
ABresult
0X0
100
111
129 1 $data = $refs_data;
130 } else {
131 0 $data = pick_elems($refs_data, $attrs, $rows);
132 }
133  
134 1 $samples->{$qcol} = $data;
135 1 return $data;
136 }
137  
138 34 given ($type) {
139 34 when ('text') {
140 my $data = gen_txt_col($table, $col_name, $domain, $attrs, $rows,
141 0 ▶ sub { join '', rand_chars( set => 'all', min => 5, max => 16 );
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 },
143 undef,
144 0 );
145 0 $samples->{$qcol} = $data;
146 0 return $data;
147 }
148 34 when ('integer') {
149 13 my $data = gen_num_col($table, $col_name, $domain, $attrs, $rows, 'i');
150 10 $samples->{$qcol} = $data;
151 10 return $data;
152 }
153 21 when ('serial') {
154 3 push @$attrs, 'serial';
155 3 my $data = gen_num_col($table, $col_name, $domain, $attrs, $rows, 'i');
156 3 $samples->{$qcol} = $data;
157 3 return $data;
158 }
159 18 when ('real') {
160 #warn "HERE";
161 0 my $data = gen_num_col($table, $col_name, $domain, $attrs, $rows, 'r');
162 0 $samples->{$qcol} = $data;
163 0 return $data;
164 }
165 18 when ('date') {
166 my $data = gen_txt_col($table, $col_name, $domain, $attrs, $rows,
167 29 ▶ sub { rand_date(min => $NowDate) },
Subroutine __ANON__: called CC=1 SCAR=0.0
168 10 qr/^\d{4}-\d{2}-\d{2}$/,
169 );
170 10 $samples->{$qcol} = $data;
171 10 return $data;
172 }
173 8 when ('time') {
174 2 my $data = gen_txt_col($table, $col_name, $domain, $attrs, $rows,
175 \&rand_time,
176 qr/^\d{2}:\d{2}:\d{2}$/,
177 );
178 0 $samples->{$qcol} = $data;
179 0 return $data;
180 }
181 6 when ('datetime') {
182 my $data = gen_txt_col($table, $col_name, $domain, $attrs, $rows,
183 19 ▶ sub { rand_datetime(min => $NowDatetime); },
Subroutine __ANON__: called CC=1 SCAR=0.0
184 6 qr/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/,
185 );
186 6 $samples->{$qcol} = $data;
187 6 return $data;
188 }
189 0 default {
190 0 die "Type $type not defined.\n";
191 # TODO
192 0 my $type_def = $types->{$type};
193 0 ▶ if (! $type_def) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type_def) 0 0 0
194 0 die "Type $type not defined for table $table column $col_name.\n";
195 }
196  
197 0 my $data = gen_custom_type_col($table, $col_name, $attrs, $type_def, $domain, $rows);
198 0 $samples->{$qcol} = $data;
199 0 return $data;
200 }
201 }
202  
203 # impossible to reach here...
204 }
205  
206 0 ▶sub pick_elems ($$$) {
Subroutine pick_elems: not called CC=6 SCAR=37.4
Pod undocumented
207 0 my ($set, $attrs, $m) = @_;
208  
209 0 my $not_null;
210 0 for (@$attrs) {
211 0 ▶ if ($_ eq 'not null') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "not null") 0 0 0
212 0 $not_null = 1;
213 0 last;
214 }
215 }
216  
217 0 my $n = @$set;
218 0 my @res;
219 0 for (1..$m) {
220 0 ▶ if (! $not_null) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($not_null) 0 0 0
221 0 ▶ if ((int rand 10) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (int rand 10 == 0) 0 0 0
222 0 push @res, undef;
223 0 next;
224 }
225 }
226  
227 0 my $i = int rand $n;
228 0 push @res, $set->[$i];
229 }
230  
231 0 return \@res;
232 }
233  
234 36 ▶sub gen_int ($) {
Subroutine gen_int: called CC=2 SCAR=6.9
Pod undocumented
235 36 my $unsigned = shift;
236 36 ▶ return $unsigned ? (int rand 1_000_000) :
Branch: 1 branch
BranchTrueFalseTotal
$unsigned ? : 5 31 36
237 (int rand 1_000_000) - 500_000;
238 }
239  
240 0 ▶sub gen_real ($) {
Subroutine gen_real: not called CC=2 SCAR=17.9
Pod undocumented
241 0 my $unsigned = shift;
242 0 ▶ return $unsigned ? (rand 1_000_000) :
Branch: 1 branch
BranchTrueFalseTotal
$unsigned ? : 0 0 0
243 (rand 1_000_000) - 500_000;
244 }
245  
246 16 ▶sub gen_num_col ($$$$$$) {
Subroutine gen_num_col: called CC=42 SCAR=44.2
Pod undocumented
247 16 my ($table, $col_name, $domain, $attrs, $n, $type) = @_;
248  
249 16 my ($unique, $asc, $desc, $not_null, $unsigned, $empty_domain);
250  
251 16 ▶ if ($domain && @$domain == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain and @$domain == 0) 0 16 16
Condition66%2/3
$domain and @$domain == 0
!ll&&!rl&&r
970
MC/DC0%0/2
$domain and @$domain == 0
$domain @$domain == 0
Truth table66%2/3
A and B
A= $domainB= @$domain == 0
ABresult
0X0
100
111
252 0 $empty_domain = 1;
253 }
254  
255 ### $domain
256 ### $attrs
257 #warn "type: $type\n";
258  
259 16 for (@$attrs) {
260 18 ▶ if ($_ eq 'serial') {
Branch: 7 branches
BranchTrueFalseTotal
if ($_ eq 'serial') { } 3 15 18
elsif ($_ eq 'not null') { } 10 5 15
elsif ($_ eq 'unique') { } 3 2 5
elsif ($_ eq 'unsigned') { } 0 2 2
elsif ($_ eq 'asc') { } 1 1 2
elsif ($_ eq 'desc') { } 1 0 1
elsif (ref $_) { } 0 0 0
261 3 $unique = 1;
262 3 $asc = 1;
263 3 $not_null = 1;
264 3 $unsigned = 1;
265 } elsif ($_ eq 'not null') {
266 10 $not_null = 1;
267 } elsif ($_ eq 'unique') {
268 3 $unique = 1;
269 } elsif ($_ eq 'unsigned') {
270 0 $unsigned = 1;
271 } elsif ($_ eq 'asc') {
272 1 $asc = 1;
273 } elsif ($_ eq 'desc') {
274 1 $desc = 1;
275 } elsif (ref $_) {
276 # do nothing
277 } else {
278 0 die "Unknown attribute: $_\n";
279 }
280 }
281  
282 16 ▶ if ($asc && $desc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($asc and $desc) 0 16 16
Condition66%2/3
$asc and $desc
!ll&&!rl&&r
1240
MC/DC0%0/2
$asc and $desc
$asc $desc
Truth table66%2/3
A and B
A= $ascB= $desc
ABresult
0X0
100
111
283 0 die "table $table, column $col_name: asc hates desc.\n";
284 }
285  
286 16 ▶ if ($empty_domain && $not_null) {
Branch: 1 branch
BranchTrueFalseTotal
if ($empty_domain and $not_null) 0 16 16
Condition33%1/3
$empty_domain and $not_null
!ll&&!rl&&r
1600
MC/DC0%0/2
$empty_domain and $not_null
$empty_domain $not_null
Truth table33%1/3
A and B
A= $empty_domainB= $not_null
ABresult
0X0
100
111
287 0 die "table $table, column $col_name: empty domain {} hates \"not null\".\n";
288 }
289  
290 16 my @nums;
291 my %hist;
292 16 for (1..$n) {
293 65 ▶ if ($unique) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique) 24 41 65
294 24 my $i = 0;
295 24 while (1) {
296 24 ▶ if (++$i > 10_000) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i > 10000) 0 24 24
297 0 die "ERROR: Too many attempts failed for table $table, column $col_name.\n";
298 }
299  
300 24 my $gen_null;
301 24 ▶ if (!$not_null) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($not_null) 0 24 24
302 0 $gen_null = (int rand 10) == 0;
303 0 ▶ if ($gen_null) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gen_null) 0 0 0
304 0 push @nums, undef;
305 0 last;
306 }
307 }
308  
309 24 ▶ if (! $gen_null) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gen_null) 24 0 24
310 24 my $num;
311 24 ▶ if (defined $domain) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain) { } 9 15 24
312 9 $num = gen_domain_val($domain);
313 8 ▶ if (!defined $num) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $num) 0 8 8
314 0 ▶ if ($not_null) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_null) 0 0 0
315 0 die "table $table, column $col_name: not null hates {} domain.\n";
316 }
317  
318 0 push @nums, $num;
319 0 last;
320 }
321  
322 8 ▶ if (! looks_like_number($num)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($num)) 0 8 8
323 0 die "table $table, column $col_name: \"$num\" does not look like a number.\n";
324 }
325  
326 8 ▶ if ($type eq 'i') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "i") 8 0 8
327 8 $num = int $num;
328 }
329 } else {
330 #warn "Type: $type";
331 15 ▶ $num = $type eq 'i' ? gen_int($unsigned) : gen_real($unsigned);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'i' ? : 15 0 15
332 }
333  
334 23 ▶ if (! $hist{$num}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hist{$num}) 23 0 23
335 23 push @nums, $num;
336 23 $hist{$num} = 1;
337 23 last;
338 }
339 }
340 }
341 23 next;
342 }
343  
344 41 my $gen_null;
345 41 ▶ if (!$not_null) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($not_null) 9 32 41
346 9 $gen_null = (int rand 10) == 0;
347 9 ▶ if ($gen_null) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gen_null) 1 8 9
348 1 push @nums, undef;
349 }
350 }
351  
352 41 ▶ if (! $gen_null) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gen_null) 40 1 41
353 40 my $num;
354 40 ▶ if (defined $domain) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain) { } 19 21 40
355 19 $num = gen_domain_val($domain);
356 18 ▶ if (defined $num) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $num) 18 0 18
357 18 ▶ if (! looks_like_number($num)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($num)) 1 17 18
358  
359 1 die "table $table, column $col_name: \"$num\" does not look like a number.\n";
360 }
361  
362 17 ▶ $num = int $num if $type eq 'i';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "i" 17 0 17
363 }
364 } else {
365 21 ▶ $num = $type eq 'i' ? gen_int($unsigned) : gen_real($unsigned);
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'i' ? : 21 0 21
366 }
367  
368 38 push @nums, $num;
369 }
370 }
371  
372 13 ▶ if ($asc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($asc) { } 4 9 13
elsif ($desc) { } 1 8 9
373 @nums = sort {
374 4 ▶ my $aa = $a // 0;
Condition50%1/2
$a // 0
l!l
260
MC/DC0%0/1
$a // 0
$a
Truth table50%1/2
A
A= $a
Aresult
11
00
375 26 ▶ my $bb = $b // 0;
Condition50%1/2
$b // 0
l!l
260
MC/DC0%0/1
$b // 0
$b
Truth table50%1/2
A
A= $b
Aresult
11
00
376 26 $aa <=> $bb
377 } @nums;
378  
379 } elsif ($desc) {
380 @nums = sort {
381 1 ▶ my $aa = $a // 0;
Condition50%1/2
$a // 0
l!l
80
MC/DC0%0/1
$a // 0
$a
Truth table50%1/2
A
A= $a
Aresult
11
00
382 8 ▶ my $bb = $b // 0;
Condition50%1/2
$b // 0
l!l
80
MC/DC0%0/1
$b // 0
$b
Truth table50%1/2
A
A= $b
Aresult
11
00
383 8 $bb <=> $aa
384 } @nums;
385 }
386  
387 13 return \@nums;
388 }
389  
390 18 ▶sub gen_txt_col ($$$$$$$) {
Subroutine gen_txt_col: called CC=37 SCAR=52.0
Pod undocumented
391 18 my ($table, $col_name, $domain, $attrs, $n, $gen, $check) = @_;
392  
393 18 my ($unique, $asc, $desc, $not_null, $empty_domain);
394  
395 18 ▶ if ($domain && @$domain == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain and @$domain == 0) 0 18 18
Condition66%2/3
$domain and @$domain == 0
!ll&&!rl&&r
1260
MC/DC0%0/2
$domain and @$domain == 0
$domain @$domain == 0
Truth table66%2/3
A and B
A= $domainB= @$domain == 0
ABresult
0X0
100
111
396 0 $empty_domain = 1;
397 }
398  
399 18 for (@$attrs) {
400 13 ▶ if ($_ eq 'not null') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "not null") 8 5 13
401 8 $not_null = 1;
402 }
403  
404 13 ▶ if ($_ eq 'unique') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "unique") 0 13 13
405 0 $unique = 1;
406 }
407  
408 13 ▶ if ($_ eq 'asc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "asc") 2 11 13
409 2 $asc = 1;
410 }
411  
412 13 ▶ if ($_ eq 'desc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "desc") 2 11 13
413 2 $desc = 1;
414 }
415 }
416  
417 18 ▶ if ($asc && $desc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($asc and $desc) 0 18 18
Condition66%2/3
$asc and $desc
!ll&&!rl&&r
1620
MC/DC0%0/2
$asc and $desc
$asc $desc
Truth table66%2/3
A and B
A= $ascB= $desc
ABresult
0X0
100
111
418 0 die "table $table, column $col_name: asc hates desc.\n";
419 }
420  
421 18 ▶ if ($empty_domain && $not_null) {
Branch: 1 branch
BranchTrueFalseTotal
if ($empty_domain and $not_null) 0 18 18
Condition33%1/3
$empty_domain and $not_null
!ll&&!rl&&r
1800
MC/DC0%0/2
$empty_domain and $not_null
$empty_domain $not_null
Truth table33%1/3
A and B
A= $empty_domainB= $not_null
ABresult
0X0
100
111
422 0 die "table $table, column $col_name: empty domain {} hates \"not null\".\n";
423 }
424  
425 18 my @txts;
426 my %hist;
427 18 for (1..$n) {
428 76 ▶ if ($unique) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique) 0 76 76
429 0 my $i = 0;
430 0 while (1) {
431 0 ▶ if (++$i > 10_000) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i > 10000) 0 0 0
432 0 die "ERROR: Too many attempts failed for table $table, column $col_name.\n";
433 }
434 #warn "unique looping";
435 0 my $gen_null;
436 0 ▶ if (! $not_null) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($not_null) 0 0 0
437 0 $gen_null = (int rand 10) == 0;
438 0 ▶ if ($gen_null) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gen_null) 0 0 0
439 0 push @txts, undef;
440 0 last;
441 }
442 }
443 0 ▶ if (! $gen_null) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gen_null) 0 0 0
444 0 my $txt;
445 0 ▶ if (defined $domain) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain) { } 0 0 0
446 0 $txt = gen_domain_val($domain);
447 0 ▶ if (!defined $txt) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $txt) 0 0 0
448 0 ▶ if ($not_null) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_null) 0 0 0
449 0 die "table $table, column $col_name: not null hates {} domain.\n";
450 }
451  
452 0 push @txts, $txt;
453 0 last;
454 }
455 0 ▶ if (defined $check and $txt !~ $check) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $check and not $txt =~ /$check/) 0 0 0
Condition0%0/3
defined $check and not $txt =~ /$check/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $check and not $txt =~ /$check/
defined $check not $txt =~ /$check/
Truth table0%0/3
A and not(B)
A= defined $checkB= not $txt =~ /$check/
ABresult
0X0
100
111
456 0 die "table $table, column $col_name: Bad domain value \"$txt\" for the column type.\n";
457 }
458 #warn "txt: $txt";
459 } else {
460 0 $txt = $gen->();
461 }
462  
463 0 ▶ if (! $hist{$txt}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hist{$txt}) 0 0 0
464 0 push @txts, $txt;
465 0 $hist{$txt} = 1;
466 0 last;
467 }
468 }
469 }
470 0 next;
471 }
472  
473 76 my $gen_null;
474 76 ▶ if (!$not_null) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($not_null) 36 40 76
475 36 $gen_null = (int rand 10) == 0;
476 36 ▶ if ($gen_null) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gen_null) 6 30 36
477 6 push @txts, undef;
478 }
479 }
480  
481 76 ▶ if (! $gen_null) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gen_null) 70 6 76
482 70 my $txt;
483 70 ▶ if (defined $domain) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain) { } 22 48 70
484 22 $txt = gen_domain_val($domain);
485 21 ▶ if (defined $txt and defined $check and $txt !~ $check) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $txt and defined $check and not $txt =~ /$check/) 1 20 21
Condition50%3/6
defined $txt and defined $check and not $txt =~ /$check/
!ll&&!rl&&r
0201
defined $txt and defined $check
!ll&&!rl&&r
0021
MC/DC33%1/3
defined $txt and defined $check and not $txt =~ /$check/
defined $txt defined $check not $txt =~ /$check/
Truth table50%2/4
A and B and not(C)
A= defined $txtB= defined $checkC= not $txt =~ /$check/
ABCresult
0XX0
10X0
1100
1111
486 1 die "table $table, column $col_name: Bad domain value \"$txt\" for the column type.\n";
487 }
488 } else {
489 48 $txt = $gen->();
490 }
491 68 push @txts, $txt;
492 }
493 } # for loop
494  
495 16 ▶ if ($asc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($asc) { } 2 14 16
elsif ($desc) { } 2 12 14
496 @txts = sort {
497 2 ▶ my $aa = $a // 'NULL';
Condition50%1/2
$a // "NULL"
l!l
160
MC/DC0%0/1
$a // "NULL"
$a
Truth table50%1/2
A
A= $a
Aresult
11
00
498 16 ▶ my $bb = $b // 'NULL';
Condition50%1/2
$b // "NULL"
l!l
160
MC/DC0%0/1
$b // "NULL"
$b
Truth table50%1/2
A
A= $b
Aresult
11
00
499 16 $aa cmp $bb
500 } @txts;
501 } elsif ($desc) {
502 @txts = sort {
503 2 ▶ my $aa = $a // 'NULL';
Condition50%1/2
$a // "NULL"
l!l
160
MC/DC0%0/1
$a // "NULL"
$a
Truth table50%1/2
A
A= $a
Aresult
11
00
504 16 ▶ my $bb = $b // 'NULL';
Condition50%1/2
$b // "NULL"
l!l
160
MC/DC0%0/1
$b // "NULL"
$b
Truth table50%1/2
A
A= $b
Aresult
11
00
505 16 $bb cmp $aa
506 } @txts;
507 }
508  
509 16 return \@txts;
510 }
511  
512 0 ▶sub gen_custom_type_col ($$$) {
Subroutine gen_custom_type_col: not called CC=1 SCAR=0.0
Pod undocumented
513 }
514  
515 25 ▶sub to_string {
Subroutine to_string: called CC=2 SCAR=6.9
Pod undocumented
516 25 my ($self, $computed) = @_;
517  
518 25 my $s = '';
519  
520 25 for my $table (sort keys %$computed) {
521 27 $s .= "$table\n";
522 27 $s .= $self->stringify_table($table, $computed->{$table});
523 }
524  
525 25 return $s;
526 }
527  
528 27 ▶sub stringify_table {
Subroutine stringify_table: called CC=7 SCAR=19.5
Pod undocumented
529 27 my ($self, $table, $cols) = @_;
530  
531 27 my $ast = $self->ast;
532 27 my $tables = $ast->tables;
533  
534 ### $ast
535  
536 27 ▶ my $tb_spec = $tables->{$table} or
Branch: 1 branch
BranchTrueFalseTotal
my $tb_spec = $tables->{$table} or die "Table $table not defined.\n" 27 0 27
537 die "Table $table not defined.\n";
538  
539 27 my $s = '';
540  
541 27 for my $col (@$tb_spec) {
542 #say "col: ", $col->[0];
543 30 my $name = $col->[0];
544 30 $s .= "\t$name";
545 }
546  
547 27 $s .= "\n";
548  
549 27 ▶ if (!@$cols) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$cols) 0 27 27
550 0 return $s;
551 }
552  
553 27 my $col = $cols->[0];
554 27 my $nrows = @$col;
555  
556 27 my @rows;
557 27 for my $i (0 .. $nrows - 1) {
558 127 my @row;
559 127 for my $col (@$cols) {
560 140 ▶ my $val = $col->[$i] // 'NULL';
Condition100%2/2
$col->[$i] // "NULL"
l!l
1328
MC/DC100%1/1
$col->[$i] // "NULL"
$col->[$i]
Truth table100%2/2
A
A= $col->[$i]
Aresult
11
00
561 140 $s .= "\t$val";
562 }
563 127 $s .= "\n";
564 127 push @rows, \@row;
565 }
566  
567 27 return $s;
568 }
569  
570 50 ▶sub gen_domain_val ($) {
Subroutine gen_domain_val: called CC=12 SCAR=30.8
Pod undocumented
571 50 my $domain = shift;
572  
573 ### $domain
574  
575 50 ▶ if (@$domain == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$domain == 0) 0 50 50
576 0 return undef;
577 }
578  
579 50 my $i = int rand @$domain;
580 50 my $atom = $domain->[$i];
581 #warn "domain size: ", scalar(@$domain);
582 #warn "ATOM: $i $atom\n";
583 50 ▶ if (my $ref = ref $atom) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $atom) 32 18 50
584 32 ▶ if ($ref eq 'Parse::RandGen::Regexp') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'Parse::RandGen::Regexp') { } 0 32 32
elsif ($ref eq 'Regexp') { } 3 29 32
elsif ($ref eq 'ARRAY') { } 29 0 29
585 0 return $atom->pick;
586 } elsif ($ref eq 'Regexp') {
587 3 $atom = Parse::RandGen::Regexp->new($atom);
588 0 return $atom->pick;
589 } elsif ($ref eq 'ARRAY') {
590 29 given ($atom->[0]) {
591 29 when ('nrange') {
592 9 my $a = $atom->[1];
593 9 my $b = $atom->[2];
594  
595 9 ▶ if ($b < $a) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b < $a) 0 9 9
596 0 die "Bad range: $a .. $b: $b < $a\n";
597 }
598 9 ▶ if ($a =~ /^-?\d+$/ && $b =~ /^-?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a =~ /^-?\d+$/ and $b =~ /^-?\d+$/) 9 0 9
Condition33%1/3
$a =~ /^-?\d+$/ and $b =~ /^-?\d+$/
!ll&&!rl&&r
009
MC/DC0%0/2
$a =~ /^-?\d+$/ and $b =~ /^-?\d+$/
$a =~ /^-?\d+$/ $b =~ /^-?\d+$/
Truth table33%1/3
A and B
A= $a =~ /^-?\d+$/B= $b =~ /^-?\d+$/
ABresult
0X0
100
111
599 # pure integer
600 9 return int(rand($b - $a + 1)) + $a;
601 }
602  
603 0 return sprintf "%.5lf", rand($b - $a) + $a;
604 }
605 20 when ('drange') {
606 10 my $a = $atom->[1];
607 10 my $b = $atom->[2];
608  
609 10 ▶ if ($b lt $a) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b lt $a) 0 10 10
610 0 die "Bad date range: $a .. $b: $b is earlier than $a\n";
611 }
612  
613 10 return rand_date( min => $a, max => $b );
614 }
615 10 when ('trange') {
616 0 my $a = $atom->[1];
617 0 my $b = $atom->[2];
618  
619 0 ▶ if ($b lt $a) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b lt $a) 0 0 0
620 0 die "Bad time range: $a .. $b: $b is earlier than $a\n";
621 }
622  
623 0 return rand_time( min => $a, max => $b);
624 }
625 10 when ('dtrange') {
626 10 my $a = $atom->[1];
627 10 my $b = $atom->[2];
628  
629 10 ▶ if ($b lt $a) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b lt $a) 0 10 10
630 0 die "Bad datetime range: $a .. $b: $b is earlier than $a\n";
631 }
632  
633 10 return rand_datetime( min => $a, max => $b);
634 }
635 0 default {
636 0 die "Unknown domain atom type: $atom->[0]";
637 }
638 }
639 } else {
640 0 die "Unkown domain atom ref: $ref";
641 }
642 }
643  
644 18 return $atom;
645 }
646  
647 0 ▶sub canonicalize_table {
Subroutine canonicalize_table: not called CC=6 SCAR=37.4
Pod undocumented
648 0 my ($self, $table, $cols) = @_;
649  
650 0 my $ast = $self->ast;
651 0 my $tables = $ast->tables;
652  
653 0 ▶ my $tb_spec = $tables->{$table} or
Branch: 1 branch
BranchTrueFalseTotal
my $tb_spec = $tables->{$table} or die "Table $table not defined.\n" 0 0 0
654 die "Table $table not defined.\n";
655  
656 0 my @col_names;
657  
658 0 for my $col (@$tb_spec) {
659 #say "col: ", $col->[0];
660 0 push @col_names, $col->[0];
661 }
662  
663 0 my @rows = (\@col_names);
664  
665 0 ▶ if (!@$cols) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$cols) 0 0 0
666 0 return \@rows;
667 }
668  
669 0 my $col = $cols->[0];
670 0 my $nrows = @$col;
671  
672 0 for my $i (0 .. $nrows - 1) {
673 0 my @row;
674 0 for my $col (@$cols) {
675 0 push @row, $col->[$i];
676 }
677 0 push @rows, \@row;
678 }
679  
680 0 return \@rows;
681 }
682  
683 0 ▶sub gen_table_schema {
Subroutine gen_table_schema: not called CC=7 SCAR=40.3
Pod undocumented
684 0 my ($self, $table) = @_;
685  
686 0 my $ast = $self->ast;
687 0 my $deps = $ast->deps;
688 0 my $tables = $ast->tables;
689 0 my $cols = $ast->cols;
690  
691 0 ▶ my $tb_spec = $tables->{$table} or
Branch: 1 branch
BranchTrueFalseTotal
my $tb_spec = $tables->{$table} or die "Table $table not defined.\n" 0 0 0
692 die "Table $table not defined.\n";
693  
694 0 my @col_defs;
695  
696 0 for my $col (@$tb_spec) {
697 #use Data::Dumper;
698  
699 0 my $name = $col->[0];
700 0 my $qcol = "$table.$name";
701 0 my $spec = $cols->{$qcol};
702 0 my $type = $spec->{type};
703  
704 #say "trying type: $type";
705 0 ▶ while ($type eq 'refs') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "refs") 0 0 0
706 #use Data::Dumper;
707 0 my $dep = $deps->{$qcol};
708  
709 0 my $col = $cols->{$dep};
710 #warn Dumper($col);
711 0 $type = $col->{type};
712 #say "trying type for $dep $col: $type";
713 0 $qcol = $dep;
714 }
715  
716 #say "type: $type";
717 #say Dumper($spec);
718 0 my @attrs = grep { $_ =~ /^(?:not null|unique)$/ } @{ $spec->{attrs} };
719  
720 my ($cmt) = grep {
721 0 ▶ ref $_ && ref $_ eq 'ARRAY' && $_->[0] eq 'for'
Condition0%0/6
ref $_ && ref $_ eq 'ARRAY' && $_->[0] eq 'for'
!ll&&!rl&&r
000
ref $_ and ref $_ eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $_ && ref $_ eq 'ARRAY' && $_->[0] eq 'for'
ref $_ ref $_ eq "ARRAY" $_->[0] eq 'for'
Truth table0%0/4
A and B && C
A= ref $_B= ref $_ eq "ARRAY"C= $_->[0] eq 'for'
ABCresult
0XX0
10X0
1100
1111
722 0 } @{ $spec->{attrs} };
723  
724 0 ▶ if ($cmt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmt) 0 0 0
725 0 push @attrs, "comment " . quote_sql_str($cmt->[1]);
726 }
727  
728 0 push @col_defs, {
729 name => $name,
730 type => $type,
731 attrs => \@attrs,
732 }
733 }
734  
735 0 return \@col_defs,
736 }
737  
738 7 1;
« Cheater/Parser.pm ↑ Summary Cheater/Util.pm »