| 1 |
use 5.008; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More; |
| 7 |
|
| 8 |
BEGIN { |
| 9 |
use_ok( 'DBD::Mock' => qw(Pool) ); |
| 10 |
use_ok('DBI'); |
| 11 |
} |
| 12 |
|
| 13 |
# check that the pool works |
| 14 |
{ |
| 15 |
|
| 16 |
my $dbh = DBI->connect("DBI:Mock:", '', '', {RaiseError => 1 }); |
| 17 |
isa_ok($dbh, 'DBD::Mock::Pool::db'); |
| 18 |
|
| 19 |
my $dbh2 = DBI->connect("DBI:Mock:", '', '', {RaiseError => 1 }); |
| 20 |
isa_ok($dbh2, 'DBD::Mock::Pool::db'); |
| 21 |
|
| 22 |
is($dbh, $dbh2, '... these handles should be the same'); |
| 23 |
|
| 24 |
ok($dbh->disconnect(), '... this will not actually do anything just return true'); |
| 25 |
ok($dbh2->disconnect(), '... this will not actually do anything just return true'); |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
# check that the pool holds result sets |
| 30 |
# in an scope indepenent manner |
| 31 |
|
| 32 |
{ |
| 33 |
# set up handle from pool |
| 34 |
my $dbh = DBI->connect("DBI:Mock:", '', '', {RaiseError => 1 }); |
| 35 |
isa_ok($dbh, 'DBD::Mock::Pool::db'); |
| 36 |
|
| 37 |
$dbh->{mock_add_resultset} = [[ 'foo', 'bar', 'baz' ], [ 1, 2, 3 ]]; |
| 38 |
|
| 39 |
ok($dbh->disconnect(), '... not really disconnecting, just returning true'); |
| 40 |
} |
| 41 |
|
| 42 |
{ |
| 43 |
# execute a statement, and expect the results |
| 44 |
my $dbh = DBI->connect("DBI:Mock:", '', '', {RaiseError => 1 }); |
| 45 |
isa_ok($dbh, 'DBD::Mock::Pool::db'); |
| 46 |
|
| 47 |
my $sth = $dbh->prepare("SELECT foo, bar, baz FROM whatever"); |
| 48 |
$sth->execute(); |
| 49 |
is_deeply( |
| 50 |
$sth->fetchrow_arrayref(), |
| 51 |
[ 1, 2, 3 ], |
| 52 |
'... got our row correctly' |
| 53 |
); |
| 54 |
$sth->finish(); |
| 55 |
|
| 56 |
ok($dbh->disconnect(), '... not really disconnecting, just returning true'); |
| 57 |
} |
| 58 |
|
| 59 |
{ |
| 60 |
# check our statement history |
| 61 |
my $dbh = DBI->connect("DBI:Mock:", '', ''); |
| 62 |
isa_ok($dbh, 'DBD::Mock::Pool::db'); |
| 63 |
|
| 64 |
my $history = $dbh->{mock_all_history}; |
| 65 |
|
| 66 |
is(scalar @{$history}, 1, '... we executed 1 statement'); |
| 67 |
|
| 68 |
is( $history->[0]->statement(), |
| 69 |
"SELECT foo, bar, baz FROM whatever", |
| 70 |
'... this the statement we executed'); |
| 71 |
|
| 72 |
ok($dbh->disconnect(), '... not really disconnecting, just returning true'); |
| 73 |
} |
| 74 |
|
| 75 |
done_testing(); |