| 1 |
use 5.008; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More; |
| 7 |
|
| 8 |
BEGIN { |
| 9 |
use_ok('DBI'); |
| 10 |
} |
| 11 |
|
| 12 |
my $dbh = DBI->connect( 'dbi:Mock:', '', '' ); |
| 13 |
isa_ok($dbh, 'DBI::db'); |
| 14 |
|
| 15 |
$dbh->{PrintError} = 0; |
| 16 |
|
| 17 |
################################################################################ |
| 18 |
|
| 19 |
$dbh->{AutoCommit} = 0; |
| 20 |
ok( $dbh->{AutoCommit} == 0, "AutoCommit is off" ); |
| 21 |
|
| 22 |
ok( $dbh->commit, 'commit() returns true' ); |
| 23 |
ok( $dbh->rollback, 'rollback() returns true' ); |
| 24 |
|
| 25 |
ok( !defined $dbh->begin_work, "begin_work() fails if AutoCommit is off" ); |
| 26 |
is( $DBI::errstr, 'AutoCommit is off, you are already within a transaction'); |
| 27 |
|
| 28 |
my $history = $dbh->{mock_all_history}; |
| 29 |
ok( @$history == 2, "Correct number of statements" ); |
| 30 |
|
| 31 |
is( $history->[0]->statement, 'COMMIT' ); |
| 32 |
ok( @{$history->[0]->bound_params} == 0, 'No parameters' ); |
| 33 |
|
| 34 |
is( $history->[1]->statement, 'ROLLBACK' ); |
| 35 |
ok( @{$history->[1]->bound_params} == 0, 'No parameters' ); |
| 36 |
|
| 37 |
ok( $dbh->{AutoCommit} == 0, "AutoCommit is still off" ); |
| 38 |
|
| 39 |
$dbh->{mock_clear_history} = 1; |
| 40 |
|
| 41 |
################################################################################ |
| 42 |
|
| 43 |
$dbh->{AutoCommit} = 1; |
| 44 |
ok( $dbh->{AutoCommit} == 1, "AutoCommit is on" ); |
| 45 |
|
| 46 |
ok( !defined $dbh->commit, "Commit returns false" ); |
| 47 |
is( $DBI::errstr, "commit ineffective with AutoCommit" ); |
| 48 |
ok( !defined $dbh->rollback, "Rollback returns false" ); |
| 49 |
is( $DBI::errstr, "rollback ineffective with AutoCommit" ); |
| 50 |
|
| 51 |
ok( $dbh->{AutoCommit} == 1, "AutoCommit is still on" ); |
| 52 |
|
| 53 |
$history = $dbh->{mock_all_history}; |
| 54 |
ok( @$history == 0, "Correct number of statements" ); |
| 55 |
|
| 56 |
$dbh->{mock_clear_history} = 1; |
| 57 |
|
| 58 |
################################################################################ |
| 59 |
|
| 60 |
$dbh->{AutoCommit} = 1; |
| 61 |
ok( $dbh->{AutoCommit} == 1, "AutoCommit is on" ); |
| 62 |
|
| 63 |
ok( $dbh->begin_work, 'begin_work() returns true' ); |
| 64 |
ok( $dbh->{AutoCommit} == 0, "AutoCommit is now off" ); |
| 65 |
|
| 66 |
ok( $dbh->rollback, 'rollback() returns true' ); |
| 67 |
ok( $dbh->{AutoCommit} == 1, "AutoCommit is back on" ); |
| 68 |
|
| 69 |
ok( $dbh->begin_work, 'begin_work() returns true' ); |
| 70 |
ok( $dbh->{AutoCommit} == 0, "AutoCommit is now off" ); |
| 71 |
|
| 72 |
ok( $dbh->commit, 'rollback() returns true' ); |
| 73 |
ok( $dbh->{AutoCommit} == 1, "AutoCommit is back on" ); |
| 74 |
|
| 75 |
$history = $dbh->{mock_all_history}; |
| 76 |
ok( @$history == 4, "Correct number of statements" ); |
| 77 |
|
| 78 |
is( $history->[0]->statement, 'BEGIN WORK' ); |
| 79 |
ok( @{$history->[0]->bound_params} == 0, 'No parameters' ); |
| 80 |
|
| 81 |
is( $history->[1]->statement, 'ROLLBACK' ); |
| 82 |
ok( @{$history->[1]->bound_params} == 0, 'No parameters' ); |
| 83 |
|
| 84 |
is( $history->[2]->statement, 'BEGIN WORK' ); |
| 85 |
ok( @{$history->[2]->bound_params} == 0, 'No parameters' ); |
| 86 |
|
| 87 |
is( $history->[3]->statement, 'COMMIT' ); |
| 88 |
ok( @{$history->[3]->bound_params} == 0, 'No parameters' ); |
| 89 |
|
| 90 |
done_testing(); |