blib/lib/Test/SQL/Translator.pm

filter: Statementstmt 94.9% 186 / 196 Branchbran 66.1% 41 / 62 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 19 / 19 Podpod 63.6% 7 / 11 totaltotal 85.6% 257 / 300 CC 41 SCAR 17.2
CC
41
Cov
87%
maybe_plan
12.1
field_ok
9.1
table_ok
6.2
CRAP 5.6
« blib/lib/SQL/Translator/Schema/Trigger.pm ↑ Summary blib/lib/SQL/Translator/Parser/DBI/MySQL.pm »
1 package Test::SQL::Translator;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Test::SQL::Translator - Test::More test functions for the Schema objects.
8  
9 =cut
10  
11 106 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 62 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 59 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 59 ▶use SQL::Translator::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 59 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 59 our @EXPORT_OK;
18 59 our $VERSION = '1.66';
19 59 our @EXPORT = qw(
20 schema_ok
21 table_ok
22 field_ok
23 constraint_ok
24 index_ok
25 view_ok
26 trigger_ok
27 procedure_ok
28 maybe_plan
29 );
30  
31 # $ATTRIBUTES{ <schema_object_name> } = { <attribname> => <default>, ... }
32 59 my %ATTRIBUTES = (
33 field => {
34 name => undef,
35 data_type => '',
36 default_value => undef,
37 size => '0',
38 is_primary_key => 0,
39 is_unique => 0,
40 is_nullable => 1,
41 is_foreign_key => 0,
42 is_auto_increment => 0,
43 comments => '',
44 extra => {},
45  
46 # foreign_key_reference,
47 is_valid => 1,
48  
49 # order
50 },
51 constraint => {
52 name => '',
53 type => '',
54 deferrable => 1,
55 expression => '',
56 is_valid => 1,
57 fields => [],
58 match_type => '',
59 options => [],
60 on_delete => '',
61 on_update => '',
62 reference_fields => [],
63 reference_table => '',
64 extra => {},
65 },
66 index => {
67 fields => [],
68 is_valid => 1,
69 name => "",
70 options => [],
71 type => NORMAL,
72 extra => {},
73 },
74 view => {
75 name => "",
76 sql => "",
77 fields => [],
78 is_valid => 1,
79 extra => {},
80 },
81 trigger => {
82 name => '',
83 perform_action_when => undef,
84 database_events => undef,
85 on_table => undef,
86 action => undef,
87 is_valid => 1,
88 extra => {},
89 },
90 procedure => {
91 name => '',
92 sql => '',
93 parameters => [],
94 owner => '',
95 comments => '',
96 extra => {},
97 },
98 table => {
99 comments => undef,
100 name => '',
101  
102 #primary_key => undef, # pkey constraint
103 options => [],
104  
105 #order => 0,
106 fields => undef,
107 constraints => undef,
108 indices => undef,
109 is_valid => 1,
110 extra => {},
111 },
112 schema => {
113 name => '',
114 database => '',
115 procedures => undef, # [] when set
116 tables => undef, # [] when set
117 triggers => undef, # [] when set
118 views => undef, # [] when set
119 is_valid => 1,
120 extra => {},
121 }
122 );
123  
124 # Given a test hash and schema object name set any attribute keys not present in
125 # the test hash to their default value for that schema object type.
126 # e.g. default_attribs( $test, "field" );
127 35 ▶sub default_attribs {
Subroutine default_attribs: called CC=3 SCAR=11.2
Pod undocumented
128 35 my ($hashref, $object_type) = @_;
129  
130 35 ▶ if (!exists $ATTRIBUTES{$object_type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ATTRIBUTES{$object_type}) 0 35 35
131 0 die "Can't add default attribs for unknown Schema " . "object type '$object_type'.";
132 }
133  
134 35 for my $attr (
135 374 grep { !exists $hashref->{$_} }
136 35 keys %{ $ATTRIBUTES{$object_type} }
137 ) {
138 238 $hashref->{$attr} = $ATTRIBUTES{$object_type}{$attr};
139 }
140  
141 35 return $hashref;
142 }
143  
144 # Format test name so it will prepend the test names used below.
145 40 ▶sub t_name {
Subroutine t_name: called CC=3 SCAR=11.4
Pod undocumented
146 40 my $name = shift;
147 40 ▶ $name ||= "";
Condition50%1/2
$name ||= ""
l!l
040
MC/DC0%0/1
$name ||= ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
148 40 ▶ $name = "$name - " if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 40 40
149 40 return $name;
150 }
151  
152 21 ▶sub field_ok {
Subroutine field_ok: called CC=9 SCAR=22.1
Pod documented
153 21 my ($f1, $test, $name) = @_;
154 21 my $t_name = t_name($name);
155 21 default_attribs($test, "field");
156  
157 21 ▶ unless ($f1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($f1) 0 21 21
158 0 fail " Field '$test->{name}' doesn't exist!";
159  
160 # TODO Do a skip on the following tests. Currently the test counts wont
161 # match at the end. So at least it fails.
162 0 return;
163 }
164  
165 21 my $full_name = $f1->table->name . "." . $test->{name};
166  
167 21 is($f1->name, $test->{name}, "${t_name}Field '$full_name'");
168  
169 21 ▶ is($f1->is_valid, $test->{is_valid}, "$t_name is " . ($test->{is_valid} ? '' : 'not ') . 'valid');
Branch: 1 branch
BranchTrueFalseTotal
$test->{'is_valid'} ? : 21 0 21
170  
171 21 is($f1->data_type, $test->{data_type}, "$t_name type is '$test->{data_type}'");
172  
173 21 is($f1->size, $test->{size}, "$t_name size is '$test->{size}'");
174  
175 is(
176 $f1->default_value,
177 $test->{default_value},
178 "$t_name default value is "
179 . (
180 defined($test->{default_value})
181 21 ▶ ? "'$test->{default_value}'"
Branch: 1 branch
BranchTrueFalseTotal
defined $test->{'default_value'} ? : 5 16 21
182 : "UNDEF"
183 )
184 );
185  
186 21 ▶ is($f1->is_nullable, $test->{is_nullable}, "$t_name " . ($test->{is_nullable} ? 'can' : 'cannot') . ' be null');
Branch: 1 branch
BranchTrueFalseTotal
$test->{'is_nullable'} ? : 18 3 21
187  
188 21 ▶ is($f1->is_unique, $test->{is_unique}, "$t_name " . ($test->{is_unique} ? 'can' : 'cannot') . ' be unique');
Branch: 1 branch
BranchTrueFalseTotal
$test->{'is_unique'} ? : 2 19 21
189  
190 is(
191 $f1->is_primary_key,
192 $test->{is_primary_key},
193 21 ▶ "$t_name is " . ($test->{is_primary_key} ? '' : 'not ') . 'a primary_key'
Branch: 1 branch
BranchTrueFalseTotal
$test->{'is_primary_key'} ? : 2 19 21
194 );
195  
196 is(
197 $f1->is_foreign_key,
198 $test->{is_foreign_key},
199 21 ▶ "$t_name is " . ($test->{is_foreign_key} ? '' : 'not') . ' a foreign_key'
Branch: 1 branch
BranchTrueFalseTotal
$test->{'is_foreign_key'} ? : 1 20 21
200 );
201  
202 is(
203 $f1->is_auto_increment,
204 $test->{is_auto_increment},
205 21 ▶ "$t_name is " . ($test->{is_auto_increment} ? '' : 'not ') . 'an auto_increment'
Branch: 1 branch
BranchTrueFalseTotal
$test->{'is_auto_increment'} ? : 2 19 21
206 );
207  
208 21 is($f1->comments, $test->{comments}, "$t_name comments");
209  
210 21 is_deeply({ $f1->extra }, $test->{extra}, "$t_name extra");
211 }
212  
213 4 ▶sub constraint_ok {
Subroutine constraint_ok: called CC=3 SCAR=11.0
Pod documented
214 4 my ($obj, $test, $name) = @_;
215 4 my $t_name = t_name($name);
216 4 default_attribs($test, "constraint");
217  
218 4 is($obj->name, $test->{name}, "${t_name}Constraint '$test->{name}'");
219  
220 4 is($obj->type, $test->{type}, "$t_name type is '$test->{type}'");
221  
222 4 ▶ is($obj->deferrable, $test->{deferrable}, "$t_name " . ($test->{deferrable} ? 'can' : 'cannot') . ' be deferred');
Branch: 1 branch
BranchTrueFalseTotal
$test->{'deferrable'} ? : 4 0 4
223  
224 4 ▶ is($obj->is_valid, $test->{is_valid}, "$t_name is " . ($test->{is_valid} ? '' : 'not ') . 'valid');
Branch: 1 branch
BranchTrueFalseTotal
$test->{'is_valid'} ? : 4 0 4
225  
226 4 is($obj->table->name, $test->{table}, "$t_name table is '$test->{table}'");
227  
228 4 is($obj->expression, $test->{expression}, "$t_name expression is '$test->{expression}'");
229  
230 4 is_deeply([ $obj->fields ], $test->{fields}, "$t_name fields are '" . join(",", @{ $test->{fields} }) . "'");
231  
232 4 is($obj->reference_table, $test->{reference_table}, "$t_name reference_table is '$test->{reference_table}'");
233  
234 is_deeply(
235 [ $obj->reference_fields ],
236 $test->{reference_fields},
237 4 "$t_name reference_fields are '" . join(",", @{ $test->{reference_fields} }) . "'"
238 );
239  
240 4 is($obj->match_type, $test->{match_type}, "$t_name match_type is '$test->{match_type}'");
241  
242 4 is($obj->on_delete, $test->{on_delete}, "$t_name on_delete is '$test->{on_delete}'");
243  
244 4 is($obj->on_update, $test->{on_update}, "$t_name on_update is '$test->{on_update}'");
245  
246 4 is_deeply([ $obj->options ], $test->{options}, "$t_name options are '" . join(",", @{ $test->{options} }) . "'");
247  
248 4 is_deeply({ $obj->extra }, $test->{extra}, "$t_name extra");
249 }
250  
251 1 ▶sub index_ok {
Subroutine index_ok: called CC=2 SCAR=6.9
Pod documented
252 1 my ($obj, $test, $name) = @_;
253 1 my $t_name = t_name($name);
254 1 default_attribs($test, "index");
255  
256 1 is($obj->name, $test->{name}, "${t_name}Index '$test->{name}'");
257  
258 1 ▶ is($obj->is_valid, $test->{is_valid}, "$t_name is " . ($test->{is_valid} ? '' : 'not ') . 'valid');
Branch: 1 branch
BranchTrueFalseTotal
$test->{'is_valid'} ? : 1 0 1
259  
260 1 is($obj->type, $test->{type}, "$t_name type is '$test->{type}'");
261  
262 1 is_deeply([ $obj->fields ], $test->{fields}, "$t_name fields are '" . join(",", @{ $test->{fields} }) . "'");
263  
264 1 is_deeply([ $obj->options ], $test->{options}, "$t_name options are '" . join(",", @{ $test->{options} }) . "'");
265  
266 1 is_deeply({ $obj->extra }, $test->{extra}, "$t_name extra");
267 }
268  
269 2 ▶sub trigger_ok {
Subroutine trigger_ok: called CC=3 SCAR=11.1
Pod documented
270 2 my ($obj, $test, $name) = @_;
271 2 my $t_name = t_name($name);
272 2 default_attribs($test, "index");
273  
274 2 is($obj->name, $test->{name}, "${t_name}Trigger '$test->{name}'");
275  
276 2 ▶ is($obj->is_valid, $test->{is_valid}, "$t_name is " . ($test->{is_valid} ? '' : 'not ') . 'valid');
Branch: 1 branch
BranchTrueFalseTotal
$test->{'is_valid'} ? : 2 0 2
277  
278 is(
279 $obj->perform_action_when,
280 $test->{perform_action_when},
281 2 "$t_name perform_action_when is '$test->{perform_action_when}'"
282 );
283  
284 is(
285 join(',', $obj->database_events),
286 $test->{database_events},
287 2 sprintf("%s database_events is '%s'", $t_name, $test->{'database_events'},)
288 );
289  
290 2 is($obj->on_table, $test->{on_table}, "$t_name on_table is '$test->{on_table}'");
291  
292 is($obj->scope, $test->{scope}, "$t_name scope is '$test->{scope}'")
293 2 ▶ if exists $test->{scope};
Branch: 1 branch
BranchTrueFalseTotal
if exists $test->{"scope"} 2 0 2
294  
295 2 is($obj->action, $test->{action}, "$t_name action is '$test->{action}'");
296  
297 2 is_deeply({ $obj->extra }, $test->{extra}, "$t_name extra");
298 }
299  
300 1 ▶sub view_ok {
Subroutine view_ok: called CC=2 SCAR=6.9
Pod documented
301 1 my ($obj, $test, $name) = @_;
302 1 my $t_name = t_name($name);
303 1 default_attribs($test, "index");
304  
305 #isa_ok( $v, 'SQL::Translator::Schema::View', 'View' );
306  
307 1 is($obj->name, $test->{name}, "${t_name}View '$test->{name}'");
308  
309 1 ▶ is($obj->is_valid, $test->{is_valid}, "$t_name is " . ($test->{is_valid} ? '' : 'not ') . 'valid');
Branch: 1 branch
BranchTrueFalseTotal
$test->{'is_valid'} ? : 1 0 1
310  
311 1 is($obj->sql, $test->{sql}, "$t_name sql is '$test->{sql}'");
312  
313 1 is_deeply([ $obj->fields ], $test->{fields}, "$t_name fields are '" . join(",", @{ $test->{fields} }) . "'");
314  
315 1 is_deeply({ $obj->extra }, $test->{extra}, "$t_name extra");
316 }
317  
318 1 ▶sub procedure_ok {
Subroutine procedure_ok: called CC=1 SCAR=0.0
Pod documented
319 1 my ($obj, $test, $name) = @_;
320 1 my $t_name = t_name($name);
321 1 default_attribs($test, "index");
322  
323 #isa_ok( $v, 'SQL::Translator::Schema::View', 'View' );
324  
325 1 is($obj->name, $test->{name}, "${t_name}Procedure '$test->{name}'");
326  
327 1 is($obj->sql, $test->{sql}, "$t_name sql is '$test->{sql}'");
328  
329 is_deeply([ $obj->parameters ],
330 1 $test->{parameters}, "$t_name parameters are '" . join(",", @{ $test->{parameters} }) . "'");
331  
332 1 is($obj->comments, $test->{comments}, "$t_name comments is '$test->{comments}'");
333  
334 1 is($obj->owner, $test->{owner}, "$t_name owner is '$test->{owner}'");
335  
336 1 is_deeply({ $obj->extra }, $test->{extra}, "$t_name extra");
337 }
338  
339 4 ▶sub table_ok {
Subroutine table_ok: called CC=6 SCAR=18.2
Pod documented
340 4 my ($obj, $test, $name) = @_;
341 4 my $t_name = t_name($name);
342 4 default_attribs($test, "table");
343 4 my %arg = %$test;
344  
345 4 ▶ my $tbl_name = $arg{name} || die "Need a table name to test.";
Condition50%1/2
$arg{'name'} || die('Need a table name to test.')
l!l
40
MC/DC0%0/1
$arg{'name'} || die('Need a table name to test.')
$arg{'name'}
Truth table50%1/2
A
A= $arg{'name'}
Aresult
11
00
346 4 is($obj->{name}, $arg{name}, "${t_name}Table '$arg{name}'");
347  
348 4 is_deeply([ $obj->options ], $test->{options}, "$t_name options are '" . join(",", @{ $test->{options} }) . "'");
349  
350 4 is_deeply({ $obj->extra }, $test->{extra}, "$t_name extra");
351  
352 # Fields
353 4 ▶ if ($arg{fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{'fields'}) { } 4 0 4
354 4 my @fldnames = map { $_->{name} } @{ $arg{fields} };
355 4 is_deeply([ map { $_->name } $obj->get_fields ],
356 [@fldnames], "${t_name} field names are " . join(", ", @fldnames));
357 4 foreach (@{ $arg{fields} }) {
358 21 ▶ my $f_name = $_->{name} || die "Need a field name to test.";
Condition50%1/2
$_->{'name'} || die('Need a field name to test.')
l!l
210
MC/DC0%0/1
$_->{'name'} || die('Need a field name to test.')
$_->{'name'}
Truth table50%1/2
A
A= $_->{'name'}
Aresult
11
00
359 21 ▶ next unless my $fld = $obj->get_field($f_name);
Branch: 1 branch
BranchTrueFalseTotal
unless my $fld = $obj->get_field($f_name) 0 21 21
360 21 field_ok($fld, $_, $name);
361 }
362 } else {
363 0 is(scalar($obj->get_fields), undef, "${t_name} has no fields.");
364 }
365  
366 # Constraints and Indices
367 4 _test_kids(
368 $obj, $test, $name,
369 {
370 constraint => 'constraints',
371 index => 'indices',
372 }
373 );
374 }
375  
376 5 ▶sub _test_kids {
Subroutine _test_kids: called CC=5 SCAR=16.1
377 5 my ($obj, $test, $name, $kids) = @_;
378 5 my $t_name = t_name($name);
379 5 my $obj_name = ref $obj;
380 5 ($obj_name) = $obj_name =~ m/^.*::(.*)$/;
381  
382 5 ▶ while (my ($object_type, $plural) = each %$kids) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$kids) 11 5 16
383 11 ▶ next unless defined $test->{$plural};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $test->{$plural} 6 5 11
384  
385 5 ▶ if (my @tests = @{ $test->{$plural} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@tests) = @{$test->{$plural};}) 5 0 5
386 5 my $meth = "get_$plural";
387 5 my @objects = $obj->$meth;
388 5 is(scalar(@objects), scalar(@tests), "${t_name}$obj_name has " . scalar(@tests) . " $plural");
389  
390 5 for my $object (@objects) {
391 9 my $ans = { lc($obj_name) => $obj->name, %{ shift @tests } };
392  
393 9 my $meth = "${object_type}_ok";
394 {
395 59 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
396 9 $meth->($object, $ans, $name);
397 }
398 }
399 }
400 }
401 }
402  
403 1 ▶sub schema_ok {
Subroutine schema_ok: called CC=5 SCAR=16.3
Pod undocumented
404 1 my ($obj, $test, $name) = @_;
405 1 my $t_name = t_name($name);
406 1 default_attribs($test, "schema");
407  
408 1 is($obj->name, $test->{name}, "${t_name}Schema name is '$test->{name}'");
409  
410 1 is($obj->database, $test->{database}, "$t_name database is '$test->{database}'");
411  
412 1 is_deeply({ $obj->extra }, $test->{extra}, "$t_name extra");
413  
414 1 ▶ is($obj->is_valid, $test->{is_valid}, "$t_name is " . ($test->{is_valid} ? '' : 'not ') . 'valid');
Branch: 1 branch
BranchTrueFalseTotal
$test->{'is_valid'} ? : 1 0 1
415  
416 # Tables
417 1 ▶ if ($test->{tables}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->{'tables'}) { } 1 0 1
418 is_deeply(
419 2 [ map { $_->name } $obj->get_tables ],
420 1 [ map { $_->{name} } @{ $test->{tables} } ],
421 "${t_name} table names match"
422 );
423 1 foreach (@{ $test->{tables} }) {
424 2 ▶ my $t_name = $_->{name} || die "Need a table name to test.";
Condition50%1/2
$_->{'name'} || die('Need a table name to test.')
l!l
20
MC/DC0%0/1
$_->{'name'} || die('Need a table name to test.')
$_->{'name'}
Truth table50%1/2
A
A= $_->{'name'}
Aresult
11
00
425 2 table_ok($obj->get_table($t_name), $_, $name);
426 }
427 } else {
428 0 is(scalar($obj->get_tables), undef, "${t_name} has no tables.");
429 }
430  
431 # Procedures, Triggers, Views
432 1 _test_kids(
433 $obj, $test, $name,
434 {
435 procedure => 'procedures',
436 trigger => 'triggers',
437 view => 'views',
438 }
439 );
440 }
441  
442 # maybe_plan($ntests, @modules)
443 #
444 # Calls plan $ntests if @modules can all be loaded; otherwise,
445 # calls skip_all with an explanation of why the tests were skipped.
446 52 ▶sub maybe_plan {
Subroutine maybe_plan: called CC=10 SCAR=24.9
Pod undocumented
447 52 my ($ntests, @modules) = @_;
448 52 my @errors;
449  
450 52 for my $module (@modules) {
451 52 ▶ eval "use $module;";
Subroutine BEGIN: called CC=1 SCAR=0.0
452 102 ▶ next if !$@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 98 4 102
453  
454 4 ▶ if ($@ =~ /Can't locate (\S+)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($@ =~ /Can't locate (\S+)/) { } 4 0 4
elsif ($@ =~ /([\w\:]+ version [\d\.]+) required.+?this is only version/) { } 0 0 0
elsif ($@ =~ /Can't load .+? for module .+?DynaLoader\.pm/i) { } 0 0 0
455 4 my $mod = $1;
456 4 $mod =~ s/\.pm$//;
457 4 $mod =~ s#/#::#g;
458 4 push @errors, $mod;
459 } elsif ($@ =~ /([\w\:]+ version [\d\.]+) required.+?this is only version/) {
460 0 push @errors, $1;
461 } elsif ($@ =~ /Can't load .+? for module .+?DynaLoader\.pm/i) {
462 0 push @errors, $module;
463 } else {
464 0 (my $err = $@) =~ s/\n+/\\n/g; # Can't have newlines in the skip message
465 0 push @errors, "$module: $err";
466 }
467 }
468  
469 52 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 3 49 52
470 3 ▶ my $msg = sprintf "Missing dependenc%s: %s", @errors == 1 ? 'y' : 'ies', join ", ", @errors;
Branch: 1 branch
BranchTrueFalseTotal
@errors == 1 ? : 2 1 3
471 3 plan skip_all => $msg;
472 }
473 49 ▶ return unless defined $ntests;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ntests 6 43 49
474  
475 43 ▶ if ($ntests ne 'no_plan') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ntests ne 'no_plan') { } 43 0 43
476 43 plan tests => $ntests;
477 } else {
478 0 plan 'no_plan';
479 }
480 }
481  
482 59 1; # compile please ===========================================================
483 __END__
« blib/lib/SQL/Translator/Schema/Trigger.pm ↑ Summary blib/lib/SQL/Translator/Parser/DBI/MySQL.pm »