| 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 |
# test this as an exception |
| 14 |
{ |
| 15 |
my $dbh = DBI->connect('DBI:Mock:', '', '', { RaiseError => 1, PrintError => 0 }); |
| 16 |
isa_ok($dbh, "DBI::db"); |
| 17 |
# check to be sure this is set, otherwise |
| 18 |
# the test wont be set up right |
| 19 |
is($dbh->{RaiseError}, 1, '... make sure RaiseError is set correctly'); |
| 20 |
|
| 21 |
# check to see it is active in the first place |
| 22 |
ok($dbh->{Active}, '...our handle with the default settting is Active' ); |
| 23 |
ok($dbh->ping(), '...and successfuly pinged handle' ); |
| 24 |
|
| 25 |
$dbh->{mock_can_connect} = 0; |
| 26 |
|
| 27 |
# check our value is correctly set |
| 28 |
is($dbh->{mock_can_connect}, 0, '... can connect is set to 0'); |
| 29 |
|
| 30 |
# and check the side effects of that |
| 31 |
ok(!$dbh->{Active}, '...our handle is no longer Active after setting mock_can_connect'); |
| 32 |
ok(!$dbh->ping(), '...and unsuccessfuly pinged handle (good)'); |
| 33 |
|
| 34 |
my $sth = eval { $dbh->prepare( "SELECT foo FROM bar" ) }; |
| 35 |
ok($@, '... we should have an exception'); |
| 36 |
|
| 37 |
like($@, |
| 38 |
qr/No connection present/, |
| 39 |
'Preparing statement against inactive handle throws expected exception' ); |
| 40 |
|
| 41 |
like($dbh->errstr, |
| 42 |
qr/^No connection present/, |
| 43 |
'Preparing statement against inactive handle sets expected DBI error' ); |
| 44 |
|
| 45 |
$dbh->disconnect(); |
| 46 |
} |
| 47 |
|
| 48 |
# and now test this as a warning |
| 49 |
{ |
| 50 |
|
| 51 |
my $dbh = DBI->connect('DBI:Mock:', '', '', { PrintError => 1 }); |
| 52 |
isa_ok($dbh, "DBI::db"); |
| 53 |
# check to be sure this is set, otherwise |
| 54 |
# the test wont be set up right |
| 55 |
is($dbh->{PrintError}, 1, '... make sure PrintError is set correctly'); |
| 56 |
|
| 57 |
# check to see it is active in the first place |
| 58 |
ok($dbh->{Active}, '...our handle with the default settting is Active' ); |
| 59 |
ok($dbh->ping(), '...and successfuly pinged handle' ); |
| 60 |
|
| 61 |
$dbh->{mock_can_connect} = 0; |
| 62 |
|
| 63 |
# check our value is correctly set |
| 64 |
is($dbh->{mock_can_connect}, 0, '... can connect is set to 0'); |
| 65 |
|
| 66 |
# and check the side effects of that |
| 67 |
ok(!$dbh->{Active}, '...our handle is no longer Active after setting mock_can_connect'); |
| 68 |
ok(!$dbh->ping(), '...and unsuccessfuly pinged handle (good)'); |
| 69 |
|
| 70 |
{ # isolate the warn handler |
| 71 |
local $SIG{__WARN__} = sub { |
| 72 |
my $msg = shift; |
| 73 |
like($msg, qr/No connection present/, '...got the expected warning'); |
| 74 |
}; |
| 75 |
|
| 76 |
my $sth = eval { $dbh->prepare( "SELECT foo FROM bar" ) }; |
| 77 |
ok(!$@, '... we should not have an exception'); |
| 78 |
ok(!defined($sth), '... and our statement should be undefined'); |
| 79 |
} |
| 80 |
|
| 81 |
$dbh->disconnect(); |
| 82 |
} |
| 83 |
|
| 84 |
done_testing(); |