| 1 |
use 5.008; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More; |
| 7 |
|
| 8 |
BEGIN { |
| 9 |
use_ok('DBD::Mock'); |
| 10 |
use_ok('DBI'); |
| 11 |
} |
| 12 |
|
| 13 |
{ |
| 14 |
my $dbh = DBI->connect('DBI:Mock:', '', '', { RaiseError => 1, PrintError => 0 }); |
| 15 |
isa_ok($dbh, "DBI::db"); |
| 16 |
# check to be sure this is set, otherwise |
| 17 |
# the test wont be set up right |
| 18 |
is($dbh->{RaiseError}, 1, '... make sure RaiseError is set correctly'); |
| 19 |
|
| 20 |
# check parse sub-refs |
| 21 |
|
| 22 |
my $parser = sub { |
| 23 |
my ($sql) = @_; |
| 24 |
die "incorrect use of '*'\n" if $sql =~ /^SELECT \*/; |
| 25 |
}; |
| 26 |
|
| 27 |
eval { |
| 28 |
$dbh->{mock_add_parser} = $parser; |
| 29 |
}; |
| 30 |
ok(!$@, '... parser successfully added to dbh'); |
| 31 |
|
| 32 |
is($dbh->{mock_parser}->[0], $parser, '... the same parser is stored'); |
| 33 |
|
| 34 |
my $sth1 = eval { $dbh->prepare('SELECT myfield FROM mytable') }; |
| 35 |
isa_ok($sth1, "DBI::st"); |
| 36 |
|
| 37 |
my $sth2 = eval { $dbh->prepare( 'SELECT * FROM mytable' ) }; |
| 38 |
ok(!defined($sth2), '... we should get nothing back from here'); |
| 39 |
|
| 40 |
like($@, |
| 41 |
qr/Failed to parse statement\. Error\: incorrect use of \'\*\'\. Statement\: SELECT \* FROM mytable/, |
| 42 |
'... parser failure generated correct error'); |
| 43 |
|
| 44 |
$dbh->disconnect(); |
| 45 |
} |
| 46 |
|
| 47 |
# parser class |
| 48 |
{ |
| 49 |
package MyParser; |
| 50 |
|
| 51 |
sub new { return bless {} } |
| 52 |
sub parse { |
| 53 |
my ($self, $sql) = @_; |
| 54 |
die "incorrect use of '*'\n" if $sql =~ /^SELECT \*/; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
{ |
| 59 |
my $dbh = DBI->connect('DBI:Mock:', '', '', { PrintError => 1 }); |
| 60 |
isa_ok($dbh, "DBI::db"); |
| 61 |
# check to be sure this is set, otherwise |
| 62 |
# the test wont be set up right |
| 63 |
is($dbh->{PrintError}, 1, '... make sure PrintError is set correctly'); |
| 64 |
|
| 65 |
# check parse objects |
| 66 |
|
| 67 |
my $parser = MyParser->new(); |
| 68 |
|
| 69 |
eval { |
| 70 |
$dbh->{mock_add_parser} = $parser; |
| 71 |
}; |
| 72 |
ok(!$@, '... parser successfully added to dbh'); |
| 73 |
|
| 74 |
is($dbh->{mock_parser}->[0], $parser, '... the same parser is stored'); |
| 75 |
|
| 76 |
my $sth1 = eval { $dbh->prepare('SELECT myfield FROM mytable') }; |
| 77 |
isa_ok($sth1, "DBI::st"); |
| 78 |
|
| 79 |
{ # isolate the warn handler |
| 80 |
local $SIG{__WARN__} = sub { |
| 81 |
my $msg = shift; |
| 82 |
like($msg, |
| 83 |
qr/incorrect use of \'\*\'\. Statement\: SELECT \* FROM mytable/, #' |
| 84 |
'...got the expected warning'); |
| 85 |
}; |
| 86 |
|
| 87 |
my $sth2 = eval { $dbh->prepare( 'SELECT * FROM mytable' ) }; |
| 88 |
ok(!defined($sth2), '... we should get nothing back from here'); |
| 89 |
} |
| 90 |
|
| 91 |
$dbh->disconnect(); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
{ # pass in a bad parser |
| 96 |
my $dbh = DBI->connect('DBI:Mock:', '', '', { RaiseError => 1, PrintError => 0 }); |
| 97 |
isa_ok($dbh, "DBI::db"); |
| 98 |
# check to be sure this is set, otherwise |
| 99 |
# the test wont be set up right |
| 100 |
is($dbh->{RaiseError}, 1, '... make sure RaiseError is set correctly'); |
| 101 |
|
| 102 |
eval { |
| 103 |
$dbh->{mock_add_parser} = "Fail"; |
| 104 |
}; |
| 105 |
like($@, qr/Parser must be a code reference or /, '... bad parser successfully not added to dbh'); |
| 106 |
|
| 107 |
eval { |
| 108 |
$dbh->{mock_add_parser} = []; |
| 109 |
}; |
| 110 |
like($@, qr/Parser must be a code reference or /, '... bad parser successfully not added to dbh'); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
{ |
| 115 |
# check it with PrintError too |
| 116 |
|
| 117 |
my $dbh = DBI->connect('DBI:Mock:', '', ''); |
| 118 |
isa_ok($dbh, "DBI::db"); |
| 119 |
# check to be sure this is set, otherwise |
| 120 |
# the test wont be set up right |
| 121 |
is($dbh->{PrintError}, 1, '... make sure PrintError is set correctly'); |
| 122 |
|
| 123 |
{ # isolate the warn handler |
| 124 |
local $SIG{__WARN__} = sub { |
| 125 |
my $msg = shift; |
| 126 |
like($msg, |
| 127 |
qr/Parser must be a code reference or /, |
| 128 |
'... bad parser successfully not added to dbh'); |
| 129 |
}; |
| 130 |
|
| 131 |
ok(!defined($dbh->{mock_add_parser} = {}), '... this returns undef too'); |
| 132 |
|
| 133 |
my $test = "Fail"; |
| 134 |
|
| 135 |
ok(!defined($dbh->{mock_add_parser} = \$test), '... this returns undef too'); |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
done_testing(); |