| 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 misc. attributes of $dbh |
| 14 |
|
| 15 |
my $dbh = DBI->connect('DBI:Mock:', '', ''); |
| 16 |
isa_ok($dbh, 'DBI::db'); |
| 17 |
|
| 18 |
$dbh->{AutoCommit} = 1; |
| 19 |
ok($dbh->{AutoCommit}, '... it handles AutoCommit as well'); |
| 20 |
|
| 21 |
$dbh->{AutoCommit} = 0; |
| 22 |
ok(! $dbh->{AutoCommit}, '... and turns off AutoCommit as well'); |
| 23 |
|
| 24 |
for (0 .. 5) { |
| 25 |
my $sth = $dbh->prepare('SELECT * FROM foo'); |
| 26 |
$sth->execute(); |
| 27 |
} |
| 28 |
|
| 29 |
is(scalar(@{$dbh->{mock_all_history}}), 6, '... we have 6 statements'); |
| 30 |
|
| 31 |
$dbh->{mock_clear_history} = 1; |
| 32 |
|
| 33 |
is(scalar(@{$dbh->{mock_all_history}}), 0, '... we have 0 statements'); |
| 34 |
|
| 35 |
# test the misc. attributes of $sth |
| 36 |
|
| 37 |
$dbh->{mock_add_resultset} = [['foo'], [1], [2], [3]]; |
| 38 |
|
| 39 |
my $SQL = 'SELECT foo FROM bar WHERE baz = ?'; |
| 40 |
|
| 41 |
# prepare a statement |
| 42 |
my $sth = $dbh->prepare($SQL); |
| 43 |
|
| 44 |
# mock_is_executed |
| 45 |
is($sth->{mock_is_executed}, 'no', '... not executed yet'); |
| 46 |
|
| 47 |
# execute and bind the param |
| 48 |
$sth->execute('test'); |
| 49 |
|
| 50 |
is($sth->{mock_is_executed}, 'yes', '... has been executed now'); |
| 51 |
|
| 52 |
# mock_my_history |
| 53 |
my $history = $sth->{mock_my_history}; |
| 54 |
ok($history, '... got something back for our history'); |
| 55 |
isa_ok($history, 'DBD::Mock::StatementTrack'); |
| 56 |
|
| 57 |
# mock_statement |
| 58 |
is($sth->{mock_statement}, $SQL, '... our statement is as expected'); |
| 59 |
|
| 60 |
# mock_fields |
| 61 |
is_deeply( |
| 62 |
$sth->{mock_fields}, |
| 63 |
[ 'foo' ], |
| 64 |
'... our fields is as expected'); |
| 65 |
|
| 66 |
# mock_records |
| 67 |
is_deeply( |
| 68 |
$sth->{mock_records}, |
| 69 |
[[1], [2], [3]], |
| 70 |
'... we have 3 records'); |
| 71 |
|
| 72 |
# mock_num_records |
| 73 |
is($sth->{mock_num_records}, 3, '... we have 3 records'); |
| 74 |
|
| 75 |
# mock_current_record_num |
| 76 |
is($sth->{mock_current_record_num}, 0, '... we are at record number 0'); |
| 77 |
|
| 78 |
# mock_is_finished |
| 79 |
is($sth->{mock_is_finished}, 'no', '... we are not yet finished'); |
| 80 |
|
| 81 |
# mock_is_depleted |
| 82 |
ok(!$sth->{mock_is_depleted}, '... nor are we depleted'); |
| 83 |
|
| 84 |
for (1 .. 3) { |
| 85 |
is(($sth->fetchrow_array())[0], $_, '... got the expected row'); |
| 86 |
is($sth->{mock_current_record_num}, $_, '... we are at record number ' . $_); |
| 87 |
} |
| 88 |
|
| 89 |
# mock_is_depleted |
| 90 |
ok($sth->{mock_is_depleted}, '... now we are depleted'); |
| 91 |
|
| 92 |
# mock_is_finished |
| 93 |
is($sth->{mock_is_finished}, 'no', '... we are not yet finished'); |
| 94 |
|
| 95 |
$sth->finish(); |
| 96 |
|
| 97 |
# mock_is_finished |
| 98 |
is($sth->{mock_is_finished}, 'yes', '... and we are now finished'); |
| 99 |
|
| 100 |
done_testing(); |