File Coverage

blib/lib/DBomb/Test/Util.pm
Criterion Covered Total %
statement 9 45 20.0
branch 0 30 0.0
condition 0 9 0.0
subroutine 3 8 37.5
pod 0 5 0.0
total 12 97 12.3


line stmt bran cond sub pod time code
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     10   18510 use strict;
  10         23  
  10         610  
15 10     10   62 use warnings;
  10         22  
  10         493  
16             our $VERSION = '$Revision: 1.3 $';
17              
18 10     10   50 use base qw(Exporter);
  10         20  
  10         8214  
19              
20             our %EXPORT_TAGS = ( all => [qw{same_results same_list count_table truncate_table drop_table}] );
21             Exporter::export_ok_tags('all');
22              
23             ## Be sure to set the DBH from outside this package.
24             our $dbh;
25              
26             ## Compares two sql statemtns, or two query objects or a query object and sql statement
27             sub same_results {
28 0     0 0   my $dbh = shift;
29 0 0         my @sql = map { UNIVERSAL::isa($_,'DBomb::Query')? scalar($_->sql) : $_->[0] } @_;
  0            
30 0           my $msg = "SQL: " . $sql[0]
31             . "\nSAME: " . $sql[1];
32              
33 0           eval {
34 0           my @results;
35 0           for (@_){
36 0 0         if (UNIVERSAL::isa($_,'DBomb::Query')){
37 0           push @results, $_->selectall_arrayref;
38             }
39             else{
40 0           my($sql,@bind_values) = @$_;
41 0 0         my $sth = $dbh->prepare($sql) or die $DBI::errstr;
42 0 0         $sth->execute(@bind_values) or die $DBI::errstr;
43 0           push @results, $sth->fetchall_arrayref;
44             }
45             }
46 0 0         die "Results are not the same." unless same_list(@results);
47             };
48 0 0         if ($@){
49 0 0         print STDERR "$msg\n$@" if $@;
50 0           return 0;
51             }
52              
53 0           return 1;
54             }
55              
56             ## compares two lists for identical values.
57             sub same_list {
58 0     0 0   my($a,$b) = @_;
59 0 0 0       return 0 unless defined($a) && defined($b);
60 0 0         return 0 unless ref($a) eq 'ARRAY';
61 0 0         return 0 unless ref($b) eq 'ARRAY';
62 0 0         return 0 unless @$a == @$b;
63              
64 0           for my $i (0..$#$a){
65 0           my($x,$y) = ($a->[$i], $b->[$i]);
66 0 0 0       next if ((not defined $x) && (not defined $y));
67 0 0 0       if(ref($x) eq 'ARRAY' && ref($y) eq 'ARRAY'){
68 0 0         return 0 unless same_list($x,$y);
69 0           next;
70             }
71 0 0         return 0 unless $a->[$i] eq $b->[$i];
72             }
73 0           return 1;
74             }
75              
76             sub count_table
77             {
78 0     0 0   $dbh->selectcol_arrayref("SELECT COUNT(*) FROM $_[0]")->[0]
79             }
80              
81              
82             sub truncate_table
83             {
84 0     0 0   $dbh->do("DELETE FROM $_[0]");
85             }
86              
87             sub drop_table
88             {
89 0     0 0   local $dbh->{RaiseError};
90 0           local $dbh->{PrintError};
91 0           $dbh->do("DROP TABLE $_[0]")
92             }
93              
94             1;
95             __END__