Debian Codegraph @

t/51_commit.t

raw
1 #!/usr/bin/perl
2
3 # Check commit, rollback and "AutoCommit" attribute
4
5 use strict;
6 use warnings;
7 use Test::More;
8
9 BEGIN { use_ok ("DBI") }
10 do "./t/lib.pl";
11
12 my $nano = $ENV{DBI_SQL_NANO};
13 my @tbl_def = (
14 [ "id", "INTEGER", 4, 0 ],
15 [ "name", "CHAR", 64, 0 ],
16 );
17
18 my $nano_msg = "SQL::Nano does not support count (*)";
19 sub RowCount {
20 my ($dbh, $tbl) = @_;
21
22 if ($nano) {
23 $nano_msg and diag ($nano_msg);
24 $nano_msg = "";
25 return 0;
26 }
27
28 local $dbh->{PrintError} = 1;
29 my $sth = $dbh->prepare ("SELECT count (*) FROM $tbl") or return;
30 $sth->execute or return;
31 my $row = $sth->fetch or return;
32 $row->[0];
33 } # RowCount
34
35 ok (my $dbh = Connect (), "connect");
36
37 ok (my $tbl = FindNewTable ($dbh), "find new test table");
38
39 like (my $def = TableDefinition ($tbl, @tbl_def),
40 qr{^create table $tbl}i, "table definition");
41 ok ($dbh->do ($def), "create table");
42
43 is ($dbh->{AutoCommit}, 1, "AutoCommit on");
44
45 eval { $dbh->{AutoCommit} = 0; };
46 like ($@, qr{^Can't disable AutoCommit}, "disable");
47 is ($dbh->{AutoCommit}, 1, "AutoCommit still on");
48
49 # Check whether AutoCommit mode works.
50 ok ($dbh->do ("insert into $tbl values (1, 'Jochen')"), "insert 1");
51 is (RowCount ($dbh, $tbl), $nano ? 0 : 1, "1 row");
52
53 ok ($dbh->disconnect, "disconnect");
54
55 ok ($dbh = Connect (), "connect");
56 is (RowCount ($dbh, $tbl), $nano ? 0 : 1, "still 1 row");
57
58 # Check whether commit issues a warning in AutoCommit mode
59 ok ($dbh->do ("insert into $tbl values (2, 'Tim')"), "insert 2");
60 is ($dbh->{AutoCommit}, 1, "AutoCommit on");
61 { my $got_warn = 0;
62 local $SIG{__WARN__} = sub { $got_warn++; };
63 eval { ok ($dbh->commit, "commit"); };
64 is ($got_warn, 1, "warning");
65 }
66
67 # Check whether rollback issues a warning in AutoCommit mode
68 # We accept error messages as being legal, because the DBI
69 # requirement of just issuing a warning seems scary.
70 ok ($dbh->do ("insert into $tbl values (3, 'Alligator')"), "insert 3");
71 is ($dbh->{AutoCommit}, 1, "AutoCommit on");
72 { my $got_warn = 0;
73 local $SIG{__WARN__} = sub { $got_warn++; };
74 eval { is ($dbh->rollback, 0, "rollback"); };
75 is ($got_warn, 1, "warning");
76 is ($dbh->err, undef, "err");
77 }
78
79 ok ($dbh->do ("drop table $tbl"), "drop table");
80 ok ($dbh->disconnect, "disconnect");
81
82 done_testing ();