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