Debian Codegraph @

t/023_statement_failure.t

raw
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
14
15 {
16 my $dbh = DBI->connect('DBI:Mock:', '', '', {RaiseError => 1, PrintError => 0});
17 isa_ok($dbh, 'DBI::db');
18
19 $dbh->{mock_add_resultset} = {
20 results => DBD::Mock->NULL_RESULTSET,
21 failure => [ 3, 'Ohlala!' ],
22 };
23
24 my $sth = eval { $dbh->prepare('INSERT INTO bar (foo) VALUES (?)') };
25 ok(!$@, '$sth handle prepared correctly');
26 isa_ok($sth, 'DBI::st');
27
28 eval { $sth->execute('baz') };
29 ok( $@, '$sth handled executed and died' );
30
31 $dbh->{mock_add_resultset} = {
32 sql => qr/SELECT/,
33 results => DBD::Mock->NULL_RESULTSET,
34 failure => [ 5, 'Ooops!' ],
35 };
36
37 $sth = eval { $dbh->prepare('SELECT foo FROM bar') };
38 ok(!$@, '$sth handle prepared correctly');
39 isa_ok($sth, 'DBI::st');
40
41 eval { $sth->execute() };
42 ok( $@, '$sth handled executed and died' );
43
44 $dbh->{mock_add_resultset} = {
45 sql => 'SELECT foo FROM bar',
46 results => DBD::Mock->NULL_RESULTSET,
47 failure => [ 5, 'Ooops!' ],
48 };
49
50 $sth = eval { $dbh->prepare('SELECT foo FROM bar') };
51 ok(!$@, '$sth handle prepared correctly');
52 isa_ok($sth, 'DBI::st');
53
54 eval { $sth->execute() };
55 ok( $@, '$sth handled executed and died' );
56
57 $dbh->{mock_add_resultset} = {
58 sql => 'SELECT bar FROM foo',
59 results => [
60 [ 'bar' ],
61 [1], [2], [3], [4], [5], [6], [7], [8], [9], [10]
62 ]
63 };
64 #test new error generators
65 $dbh->{mock_can_prepare} = 0;
66 $dbh->{mock_can_execute} = 1;
67 $dbh->{mock_can_fetch} = 1;
68 eval {
69 my $sth = $dbh->prepare("SELECT bar FROM foo");
70 $sth->execute;
71 while (my $row = $sth->fetchrow_arrayref) {
72 1;
73 }
74 };
75 ok($@ =~ /Cannot prepare/, '$sth handle failed to prepare');
76
77 $dbh->{mock_can_prepare} = -3;
78 $dbh->{mock_can_execute} = 1;
79 $dbh->{mock_can_fetch} = 1;
80 my $i = 0;
81 for (1 .. 10) {
82 $i++;
83 eval {
84 my $sth = $dbh->prepare("SELECT bar FROM foo");
85 $sth->execute;
86 while (my $row = $sth->fetchrow_arrayref) {
87 1;
88 }
89 };
90 last if $@;
91 }
92 ok($@ =~ /Cannot prepare/, "$@ should contain 'Cannot prepare'");
93 ok($i == 4, "$i should be 4");
94
95 $dbh->{mock_can_prepare} = 1;
96 $dbh->{mock_can_execute} = 0;
97 $dbh->{mock_can_fetch} = 1;
98 eval {
99 my $sth = $dbh->prepare("SELECT bar FROM foo");
100 $sth->execute;
101 while (my $row = $sth->fetchrow_arrayref) {
102 1;
103 }
104 };
105 ok($@ =~ /Cannot execute/, '$sth handle failed to execute');
106
107 $dbh->{mock_can_prepare} = 1;
108 $dbh->{mock_can_execute} = -3;
109 $dbh->{mock_can_fetch} = 1;
110 $i = 0;
111 for (1 .. 10) {
112 $i++;
113 eval {
114 my $sth = $dbh->prepare("SELECT bar FROM foo");
115 $sth->execute;
116 while (my $row = $sth->fetchrow_arrayref) {
117 1;
118 }
119 };
120 last if $@;
121 }
122 ok($@ =~ /Cannot execute/, "$@ should contain 'Cannot execute'");
123 ok($i == 4, "$i should be 4");
124
125 $dbh->{mock_can_prepare} = 1;
126 $dbh->{mock_can_execute} = 1;
127 $dbh->{mock_can_fetch} = 0;
128 eval {
129 my $sth = $dbh->prepare("SELECT bar FROM foo");
130 $sth->execute;
131 while (my $row = $sth->fetchrow_arrayref) {
132 1;
133 }
134 };
135 ok($@ =~ /Cannot fetch/, '$sth handle failed to fetch');
136
137 $dbh->{mock_can_prepare} = 1;
138 $dbh->{mock_can_execute} = 1;
139 $dbh->{mock_can_fetch} = 0;
140 eval {
141 my $sth = $dbh->prepare("SELECT bar FROM foo");
142 $sth->execute;
143 while (my @row = $sth->fetchrow_array) {
144 1;
145 }
146 };
147 ok($@ =~ /Cannot fetch/, '$sth handle failed to fetch');
148
149 $dbh->{mock_can_prepare} = 1;
150 $dbh->{mock_can_execute} = 1;
151 $dbh->{mock_can_fetch} = 0;
152 eval {
153 my $sth = $dbh->prepare("SELECT bar FROM foo");
154 $sth->execute;
155 while (my $row = $sth->fetchrow_hashref) {
156 1;
157 }
158 };
159 ok($@ =~ /Cannot fetch/, '$sth handle failed to fetch');
160
161 $dbh->{mock_can_prepare} = 1;
162 $dbh->{mock_can_execute} = 1;
163 $dbh->{mock_can_fetch} = 0;
164 eval {
165 my $sth = $dbh->prepare("SELECT bar FROM foo");
166 $sth->execute;
167 my @row = $sth->fetchall_arrayref;
168 };
169 ok($@ =~ /Cannot fetch/, '$sth handle failed to fetch');
170
171 $dbh->{mock_can_prepare} = 1;
172 $dbh->{mock_can_execute} = 1;
173 $dbh->{mock_can_fetch} = -100;
174 {
175 my $sth;
176 eval {
177 $sth = $dbh->prepare("select bar from foo");
178 $sth->execute;
179 };
180 ok(!$@, "prepare and execute should work");
181 isa_ok($sth, 'DBI::st');
182
183 eval { my $row = $sth->fetch };
184 ok(!$@, "fetch should work");
185 ok($dbh->{mock_can_fetch}==-99, "$dbh->{mock_can_fetch} should be -99");
186
187 eval { my $row = $sth->fetchrow_arrayref };
188 ok(!$@, "fetch should work");
189 ok($dbh->{mock_can_fetch}==-98, "$dbh->{mock_can_fetch} should be -98");
190
191 eval { my @row = $sth->fetchrow_array };
192 ok(!$@, "fetch should work");
193 ok($dbh->{mock_can_fetch}==-97, "$dbh->{mock_can_fetch} should be -97");
194
195 eval { my $row = $sth->fetchrow_hashref };
196 ok(!$@, "fetch should work");
197 ok($dbh->{mock_can_fetch}==-96, "$dbh->{mock_can_fetch} should be -96");
198
199 eval { my @rows = $sth->fetchall_arrayref };
200 ok(!$@, "fetch should work");
201 ok($dbh->{mock_can_fetch}==-95, "$dbh->{mock_can_fetch} should be -95");
202 }
203 }
204
205 done_testing();