| 1 | package DBomb::Test::Util; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | DBomb::Test::Util - THIS PACKAGE SHOULD NOT BE INSTALLED. | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | This is a helper package for the "make test" part of the DBomb::* distribution. | ||||||||||||||||||||||||||||
| 10 | It should not be installed. If it is installed, then report it as a bug, please. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =cut | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 10 | our $VERSION = '$Revision: 1.3 $'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 10 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 10 | our %EXPORT_TAGS = ( all => [qw{same_results same_list count_table truncate_table drop_table}] ); | |||||||||||||||||||||||||||
| 21 | 10 | Exporter::export_ok_tags('all'); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ## Be sure to set the DBH from outside this package. | ||||||||||||||||||||||||||||
| 24 | 10 | our $dbh; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | ## Compares two sql statemtns, or two query objects or a query object and sql statement | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub same_results { | ||||||||||||||||||||||||||
|
Subroutine
same_results: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 0 | my $dbh = shift; | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | my @sql = map { UNIVERSAL::isa($_,'DBomb::Query')? scalar($_->sql) : $_->[0] } @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | my $msg = "SQL: " . $sql[0] | |||||||||||||||||||||||||||
| 31 | . "\nSAME: " . $sql[1]; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | eval { | |||||||||||||||||||||||||||
| 34 | 0 | my @results; | |||||||||||||||||||||||||||
| 35 | 0 | for (@_){ | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | if (UNIVERSAL::isa($_,'DBomb::Query')){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | push @results, $_->selectall_arrayref; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | else{ | ||||||||||||||||||||||||||||
| 40 | 0 | my($sql,@bind_values) = @$_; | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | my $sth = $dbh->prepare($sql) or die $DBI::errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $sth->execute(@bind_values) or die $DBI::errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | push @results, $sth->fetchall_arrayref; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | die "Results are not the same." unless same_list(@results); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | }; | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ($@){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | print STDERR "$msg\n$@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | return 0; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | return 1; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | ## compares two lists for identical values. | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub same_list { | ||||||||||||||||||||||||||
|
Subroutine
same_list: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 0 | my($a,$b) = @_; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | return 0 unless defined($a) && defined($b); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $a and defined $b
MC/DC0%0/2
defined $a and defined $b
defined $a
defined $b
Truth table0%0/3
A and B
A= defined $aB= defined $b
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | return 0 unless ref($a) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | return 0 unless ref($b) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | return 0 unless @$a == @$b; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | for my $i (0..$#$a){ | |||||||||||||||||||||||||||
| 65 | 0 | my($x,$y) = ($a->[$i], $b->[$i]); | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | next if ((not defined $x) && (not defined $y)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $x or defined $y
MC/DC0%0/2
defined $x or defined $y
defined $x
defined $y
Truth table0%0/3
A or B
A= defined $xB= defined $y
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if(ref($x) eq 'ARRAY' && ref($y) eq 'ARRAY'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $x eq "ARRAY" and ref $y eq "ARRAY"
MC/DC0%0/2
ref $x eq "ARRAY" and ref $y eq "ARRAY"
ref $x eq "ARRAY"
ref $y eq "ARRAY"
Truth table0%0/3
A and B
A= ref $x eq "ARRAY"B= ref $y eq "ARRAY"
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | return 0 unless same_list($x,$y); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | next; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | return 0 unless $a->[$i] eq $b->[$i]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 0 | return 1; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub count_table | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
count_table: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 0 | $dbh->selectcol_arrayref("SELECT COUNT(*) FROM $_[0]")->[0] | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub truncate_table | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
truncate_table: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 0 | $dbh->do("DELETE FROM $_[0]"); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | sub drop_table | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
drop_table: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | 0 | local $dbh->{RaiseError}; | |||||||||||||||||||||||||||
| 90 | 0 | local $dbh->{PrintError}; | |||||||||||||||||||||||||||
| 91 | 0 | $dbh->do("DROP TABLE $_[0]") | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 10 | 1; | |||||||||||||||||||||||||||
| 95 | __END__ | ||||||||||||||||||||||||||||