Debian Codegraph @

t/bug_066815.t

raw
1 use 5.008;
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8 use DBI;
9 use DBD::Mock;
10
11 # This test is designed to expose the bug found in the DBD::Mock
12 # methods begin_work, commit and rollback (RT #66815), where a failing
13 # ->prepare invocation (returning nothing) is not detected and the
14 # undefined value resulting is used anyway. In this test, as in the
15 # example found in the wild, the failure is triggered by exhaustion of
16 # the session states.
17 #
18 # This is a list of sessions designed to engineer the right condition
19 # to trigger the bug. They all start with a dummy statement (so that
20 # there are at least two states) then the final statement is removed
21 # before it is passed to DBD::Mock::Session->new (which requires at
22 # least one state). The final statements are 'BEGIN WORK', 'COMMIT'
23 # and 'ROLLBACK', respectively.
24 #
25 # Hence, when the test tries to invoke the final state, the session
26 # will have run out and DBD::Mock->verify_statement will cause the
27 # prepare method to fail.
28
29 my @cases = (
30 'begin_work' => [
31 {
32 statement => 'SELECT something FROM somewhere',
33 results => [],
34 },
35 {
36 statement => 'BEGIN WORK',
37 results => [],
38 },
39 ],
40
41 'commit' => [
42 {
43 statement => 'SELECT something FROM somewhere',
44 results => [],
45 },
46 {
47 statement => 'BEGIN WORK',
48 results => [],
49 },
50 {
51 statement => 'INSERT INTO foo (bar) VALUES (?);',
52 results => [],
53 bound_params => [1],
54 },
55 {
56 statement => 'COMMIT',
57 results => [],
58 },
59 ],
60
61 'rollback' => [
62 {
63 statement => 'SELECT something FROM somewhere',
64 results => [],
65 },
66 {
67 statement => 'BEGIN WORK',
68 results => [],
69 },
70 {
71 statement => 'INSERT INTO foo (bar) VALUES (?);',
72 results => [],
73 bound_params => [1],
74 },
75 {
76 statement => 'ROLLBACK',
77 results => [],
78 },
79 ],
80 );
81
82 while(@cases) {
83 my ($name, $states) = splice @cases, 0, 2;
84 my $case_name = "case $name";
85
86 my $dbh = DBI->connect('dbi:Mock:', '', '',
87 { PrintError => 0,
88 RaiseError => 1 });
89
90 # Add all but the last state of the expected session
91 my $missing_state = pop @$states;
92 my $num_states = @$states;
93 $dbh->{mock_session} = DBD::Mock::Session->new($name => @$states);
94
95 # Execute the initial dummy statement.
96 my $state = $states->[0];
97 my $sth = $dbh->prepare($state->{statement});
98 ok $sth,
99 "$case_name: prepare statement";
100
101 ok $sth->execute(),
102 "$case_name: execute statement";
103
104 # Now try and do the next steps in @session, but using the
105 # appropriate transaction methods directly. This should fail when
106 # the session is exhausted with a useful message. (The original
107 # bug meant that the message got clobbered by "Can't call method
108 # 'execute' on an undefined value".)
109 throws_ok {
110 # This stlibatement is always the same.
111 ok $dbh->begin_work,
112 "$case_name: start transaction";
113
114 my $state = $states->[2];
115 my $sth = $dbh->prepare($state->{statement});
116 ok $sth,
117 "$case_name: prepare statement";
118
119 ok $sth->execute(@{$state->{bound_params}}),
120 "$case_name: execute statement";
121
122 # get the final operation from the session
123 my $operation = lc $missing_state->{statement};
124
125 ok $dbh->$operation,
126 "$case_name: $operation transaction";
127 } qr/\QSession states exhausted, only '$num_states' in DBD::Mock::Session\E/;
128
129 }
130
131 done_testing();