File Coverage

blib/lib/Test/DatabaseRow.pm
Criterion Covered Total %
statement 55 55 100.0
branch 18 18 100.0
condition 9 15 60.0
subroutine 9 9 100.0
pod 3 3 100.0
total 94 100 94.0


line stmt bran cond sub pod time code
1             package Test::DatabaseRow;
2              
3             # require at least a version of Perl that is merely ancient, but not
4             # prehistoric
5 7     7   270688 use 5.006;
  7         32  
  7         327  
6              
7 7     7   47 use strict;
  7         13  
  7         382  
8 7     7   40 use warnings;
  7         21  
  7         453  
9              
10             # set row_ok to be exported
11 7     7   43 use base qw(Exporter);
  7         10  
  7         1007  
12             our @EXPORT;
13              
14 7     7   55 use Carp qw(croak);
  7         13  
  7         1125  
15             our @CARP_OK = qw(Test::DatabaseRow TestDatabaseRow::Object);
16              
17             # set the version number
18             our $VERSION = "2.04";
19              
20 7     7   5052 use Test::DatabaseRow::Object;
  7         22  
  7         4240  
21             our $object_class = "Test::DatabaseRow::Object";
22              
23             sub row_ok {
24              
25             # horrible, horrible package vars
26             # In 2003 Mark Fowler chose to make a procedual interface
27             # to this module and keep state in package vars to make the
28             # interface easy. In 2011 Mark Fowler isn't sure this is
29             # a great idea
30 51     51 1 90966 our $dbh;
31 51         79 our $force_utf8;
32 51         78 our $verbose;
33 51         66 our $verbose_data;
34              
35             # defaults
36 51   33     870 my %args = (
      33        
37             dbh => $dbh,
38             force_utf8 => $force_utf8,
39             verbose => $verbose || $ENV{TEST_DBROW_VERBOSE},
40             verbose_data => $verbose_data || $ENV{TEST_DBROW_VERBOSE_DATA},
41             check_all_rows => 0,
42             @_ );
43              
44             # rename "sql" to "sql_and_bind"
45             # (it's called just sql for legacy reasons)
46 51 100 66     246 $args{sql_and_bind} = $args{sql}
47             if exists $args{sql} && !exists $args{sql_and_bind};
48              
49             # remove description, provide default fallback from label
50 51         121 my $label = delete $args{label};
51 51         86 my $description = delete $args{description};
52 51 100       136 $description = $label unless defined $description;
53 51 100       138 $description = "simple db test" unless defined $description;
54              
55             # do the test
56 51         391 my $tbr = $object_class->new(%args);
57 46         205 my $tbr_result = $tbr->test_ok();
58              
59             # store the results of the database operation in a var passed
60             # into this function.
61             #
62             # This is another example of functionality that is difficult
63             # to add to a procedural interface and would have been easier
64             # if I'd used an OO interface. That's the problem with
65             # published APIs though, isn't it? It's hard to change them
66 39 100       132 if ($args{store_rows}) {
67 2 100       29 croak "Must pass an arrayref in 'store_rows'"
68             unless ref $args{store_rows} eq "ARRAY";
69 1         2 @{ $args{store_rows} } = @{ $tbr->db_results };
  1         3  
  1         3  
70             }
71 38 100       112 if ($args{store_row}) {
72 5 100 100     41 if (ref $args{store_row} eq "HASH") {
  2 100 66     14  
    100          
73 1         2 %{ $args{store_row} } = %{ $tbr->db_results->[0] };
  1         4  
  1         4  
74 2         9 } elsif (ref $args{store_row} eq "SCALAR" && !defined ${ $args{store_row} }) {
75 1         4 ${ $args{store_row} } = $tbr->db_results->[0];
  1         4  
76             } elsif (ref $args{store_row} eq "REF" && ref ${ $args{store_row} } eq "HASH" ) {
77 2         3 %{${ $args{store_row} }} = %{ $tbr->db_results->[0] };
  2         3  
  2         8  
  2         6  
78             } else {
79 1         13 croak "Invalid argument passed in 'store_row'";
80             }
81             }
82              
83             # render the result with Test::Builder
84 37         85 local $Test::Builder::Level = $Test::Builder::Level + 1;
85 37         149 return $tbr_result->pass_to_test_builder( $description );
86             }
87             push @EXPORT, qw(row_ok);
88              
89             sub not_row_ok {
90 1     1 1 974 local $Test::Builder::Level = $Test::Builder::Level + 1;
91 1         5 return row_ok(@_, results => 0);
92             }
93             push @EXPORT, qw(not_row_ok);
94              
95             sub all_row_ok {
96 3     3 1 1666 local $Test::Builder::Level = $Test::Builder::Level + 1;
97 3         13 return row_ok(@_, check_all_rows => 1);
98             }
99             push @EXPORT, qw(all_row_ok);
100              
101              
102             # truth at end of the module
103             1;
104              
105             __END__