| 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 |
my $dbh = DBI->connect('DBI:Mock:', '', '', { RaiseError => 1, PrintError => 0 }); |
| 14 |
isa_ok($dbh, "DBI::db"); |
| 15 |
ok($dbh->{RaiseError}, '... RaiseError is set correctly'); |
| 16 |
ok(! $dbh->{PrintError}, '... PrintError is set correctly'); |
| 17 |
|
| 18 |
my $sth_exec = $dbh->prepare('SELECT foo FROM bar'); |
| 19 |
isa_ok($sth_exec, "DBI::st"); |
| 20 |
|
| 21 |
# turn off the handle between the prepare and execute... |
| 22 |
$dbh->{mock_can_connect} = 0; |
| 23 |
|
| 24 |
# check our value is correctly set |
| 25 |
is($dbh->{mock_can_connect}, 0, '... can connect is set to 0'); |
| 26 |
|
| 27 |
# and check the side effects of that |
| 28 |
ok(!$dbh->{Active}, '... the handle is not Active'); |
| 29 |
ok(!$dbh->ping(), '... and ping returns false'); |
| 30 |
|
| 31 |
# now try to execute it |
| 32 |
|
| 33 |
eval { $sth_exec->execute() }; |
| 34 |
ok($@, '... we got an exception'); |
| 35 |
like($@, qr/No connection present/, '... we got the expected execption'); |
| 36 |
|
| 37 |
# turn off the database between execute and fetch |
| 38 |
|
| 39 |
$dbh->{mock_can_connect} = 1; |
| 40 |
|
| 41 |
# check our value is correctly set |
| 42 |
is($dbh->{mock_can_connect}, 1, '... can connect is set to 1'); |
| 43 |
|
| 44 |
# and check the side effects of that |
| 45 |
ok($dbh->{Active}, '... the handle is Active'); |
| 46 |
ok($dbh->ping(), '... and ping returns true'); |
| 47 |
|
| 48 |
$dbh->{mock_add_resultset} = [[ qw(foo bar ) ], # column headers |
| 49 |
[ qw(this that ) ], # first row values |
| 50 |
[ qw(never seen) ]]; # second row values |
| 51 |
|
| 52 |
my $sth_fetch = $dbh->prepare('SELECT foo, bar FROM baz'); |
| 53 |
isa_ok($sth_fetch, "DBI::st"); |
| 54 |
|
| 55 |
eval { $sth_fetch->execute() }; |
| 56 |
ok(!$@, '... executed without exception'); |
| 57 |
|
| 58 |
my $row = eval { $sth_fetch->fetchrow_arrayref() }; |
| 59 |
ok(!$@, '... the first row was returned without execption'); |
| 60 |
is_deeply($row, [ qw(this that) ], '... we got back the expected data in the first row'); |
| 61 |
|
| 62 |
# now turn off the database |
| 63 |
$dbh->{mock_can_connect} = 0; |
| 64 |
|
| 65 |
# check our value is correctly set |
| 66 |
is($dbh->{mock_can_connect}, 0, '... can connect is set to 0'); |
| 67 |
|
| 68 |
# and check the side effects of that |
| 69 |
ok(!$dbh->{Active}, '... the handle is not Active'); |
| 70 |
ok(!$dbh->ping(), '... and ping returns false'); |
| 71 |
|
| 72 |
$row = eval { $sth_fetch->fetchrow_arrayref() }; |
| 73 |
ok($@, '... we got the exception'); |
| 74 |
like($sth_fetch->errstr, |
| 75 |
qr/^No connection present/, |
| 76 |
'... fetching row against inactive db throws expected exception' ); |
| 77 |
|
| 78 |
done_testing(); |