Debian Codegraph @

t/016_mock_add_resultset_test.t

raw
1 use 5.008;
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9 BEGIN {
10 use_ok('DBD::Mock');
11 use_ok('DBI');
12 }
13
14 # test the ability to overwrite a
15 # hash based 'mock_add_resultset'
16 # and have it work as expected
17
18 # tests for the return value of execute below as well
19
20 my $dbh = DBI->connect('dbi:Mock:', '', '');
21 isa_ok($dbh, 'DBI::db');
22
23 $dbh->{mock_add_resultset} = {
24 sql => 'SELECT foo FROM bar',
25 results => [[ 'foo' ], [ 10 ]]
26 };
27
28 {
29 my $sth = $dbh->prepare('SELECT foo FROM bar');
30 isa_ok($sth, 'DBI::st');
31
32 my $rows = $sth->execute();
33 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
34
35 my ($result) = $sth->fetchrow_array();
36
37
38 is($result, 10, '... got the result we expected');
39
40 $sth->finish();
41 }
42
43 $dbh->{mock_add_resultset} = {
44 sql => 'SELECT foo FROM bar',
45 results => [[ 'foo' ], [ 50 ]]
46 };
47
48 {
49 my $sth = $dbh->prepare('SELECT foo FROM bar');
50 isa_ok($sth, 'DBI::st');
51
52 my $rows = $sth->execute();
53 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
54
55 my ($result) = $sth->fetchrow_array();
56
57 is($result, 50, '... got the result we expected');
58
59 $sth->finish();
60 }
61
62 # get it again
63 {
64 my $sth = $dbh->prepare('SELECT foo FROM bar');
65 isa_ok($sth, 'DBI::st');
66
67 my $rows = $sth->execute();
68 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
69
70 my ($result) = $sth->fetchrow_array();
71
72 is($result, 50, '... got the result we expected');
73
74 $sth->finish();
75 }
76
77 # and one more time for good measure
78 {
79 my $sth = $dbh->prepare('SELECT foo FROM bar');
80 isa_ok($sth, 'DBI::st');
81
82 my $rows = $sth->execute();
83 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
84
85 my ($result) = $sth->fetchrow_array();
86
87 is($result, 50, '... got the result we expected');
88
89 $sth->finish();
90 }
91
92 ## test regular expression for query matching
93 $dbh->{mock_add_resultset} = {
94 sql => qr/^SELECT foo/,
95 results => [ [ 'foo' ], [ 200 ] ],
96 };
97
98 ## This one should never be used as the above one will have precedence
99 $dbh->{mock_add_resultset} = {
100 sql => qr/^SELECT foo FROM/,
101 results => [ [ 'foo' ], [ 300 ] ],
102 };
103
104 {
105 my $sth = $dbh->prepare('SELECT foo FROM oof');
106 isa_ok($sth, 'DBI::st');
107
108 my $rows = $sth->execute();
109 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
110
111 my ($result) = $sth->fetchrow_array();
112
113 is($result, 200, '... got the result we expected');
114
115 $sth->finish();
116 }
117
118 ## overwrite regular expression matching
119 $dbh->{mock_add_resultset} = {
120 sql => qr/^SELECT foo/,
121 results => [ [ 'foo' ], [ 400 ] ],
122 };
123
124 {
125 my $sth = $dbh->prepare('SELECT foo FROM oof');
126 isa_ok($sth, 'DBI::st');
127
128 my $rows = $sth->execute();
129 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
130
131 my ($result) = $sth->fetchrow_array();
132
133 is($result, 400, '... got the result we expected');
134
135 $sth->finish();
136 }
137
138 # check that statically assigned queries take precedence over regex matched ones
139 {
140 my $sth = $dbh->prepare('SELECT foo FROM bar');
141 isa_ok($sth, 'DBI::st');
142
143 my $rows = $sth->execute();
144 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
145
146 my ($result) = $sth->fetchrow_array();
147
148 is($result, 50, '... got the result we expected');
149
150 $sth->finish();
151 }
152
153 ## test the return value of execute
154
155 $dbh->{mock_add_resultset} = {
156 sql => 'INSERT INTO foo VALUES(bar)',
157 results => [[], []]
158 };
159
160 # check no SELECT statements
161 {
162 my $sth = $dbh->prepare('INSERT INTO foo VALUES(bar)');
163 isa_ok($sth, 'DBI::st');
164
165 my $rows = $sth->execute();
166 is($rows, 1, '... got back 1 for rows with our INSERT statement');
167
168 $sth->finish();
169 }
170
171 $dbh->{mock_add_resultset} = {
172 sql => 'UPDATE foo SET(bar = "baz")',
173 results => [[], [], [], [], []]
174 };
175
176 # check no SELECT statements
177 {
178 my $sth = $dbh->prepare('UPDATE foo SET(bar = "baz")');
179 isa_ok($sth, 'DBI::st');
180
181 my $rows = $sth->execute();
182 is($rows, 4, '... got back 4 for rows with our UPDATE statement');
183
184 $sth->finish();
185 }
186
187
188 $dbh->{mock_add_resultset} = {
189 sql => 'SELECT x FROM y WHERE z = ?',
190 results => [ ["x"] ],
191 callback => sub {
192 my @bound_params = @_;
193
194 my %result = ( rows => [[ 1] ] );
195
196 if ($bound_params[0] == 1) {
197 $result{rows} = [ [32] ];
198 } elsif ($bound_params[0] == 2) {
199 $result{rows} = [ [43] ];
200 }
201
202 return %result;
203 },
204 };
205
206 {
207 my $sth = $dbh->prepare('SELECT x FROM y WHERE z = ?');
208 isa_ok($sth, 'DBI::st');
209
210 is($sth->{NUM_OF_FIELDS}, 1, "... When we specify the fields in the results parameter then we expect an answer from NUM_OF_FIELDS before we execute the statement");
211
212 my $rows = $sth->execute(1);
213 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
214
215 is($sth->{NUM_OF_FIELDS}, 1, "... When we specify the fields in the results parameter then we expect an answer from NUM_OF_FIELDS after we execute the statement");
216
217 my ($result) = $sth->fetchrow_array();
218
219 is($result, 32, '... got the result we expected');
220
221 $rows = $sth->execute(2);
222 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
223
224 ($result) = $sth->fetchrow_array();
225
226 is($result, 43, '... got the result we expected');
227
228 $rows = $sth->execute(33);
229 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
230
231 ($result) = $sth->fetchrow_array();
232
233 is($result, 1, '... got the result we expected');
234 $sth->finish();
235 }
236
237 $dbh->{mock_add_resultset} = {
238 sql => 'SELECT a FROM b WHERE c = ?',
239 callback => sub {
240 my @bound_params = @_;
241
242 my %result = (
243 fields => [ "a" ],
244 rows => [[ 1] ]
245 );
246
247 if ($bound_params[0] == 1) {
248 $result{rows} = [ [32] ];
249 } elsif ($bound_params[0] == 2) {
250 $result{rows} = [ [43] ];
251 }
252
253 return %result;
254 },
255 };
256
257 {
258 my $sth = $dbh->prepare('SELECT a FROM b WHERE c = ?');
259 isa_ok($sth, 'DBI::st');
260
261 is($sth->{NUM_OF_FIELDS}, 0 , "... When we don't specify the fields in the results parameter and we haven't activated the DefaultFieldsToUndef feature, then we expect the NUM_OF_FIELDS to be 0 before we execute the statement");
262
263 my $rows = $sth->execute(1);
264 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
265
266 is($sth->{NUM_OF_FIELDS}, 1, "... When we don't specify the fields in the results parameter then we still expect an answer from NUM_OF_FIELDS after we've execute the statement");
267
268 my ($result) = $sth->fetchrow_array();
269
270 is($result, 32, '... got the result we expected');
271
272 $rows = $sth->execute(2);
273 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
274
275 ($result) = $sth->fetchrow_array();
276
277 is($result, 43, '... got the result we expected');
278
279 $rows = $sth->execute(33);
280 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
281
282 ($result) = $sth->fetchrow_array();
283
284 is($result, 1, '... got the result we expected');
285 $sth->finish();
286 }
287
288 {
289 # Activate the DefaultFieldsToUndef feature
290 $DBD::Mock::DefaultFieldsToUndef = 1;
291 my $sth = $dbh->prepare('SELECT a FROM b WHERE c = ?');
292 isa_ok($sth, 'DBI::st');
293
294 is($sth->{NUM_OF_FIELDS}, undef , "... When we don't specify the fields in the results parameter then we expect the NUM_OF_FIELDS to be undef before we execute the statement");
295
296 my $rows = $sth->execute(1);
297 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
298
299 is($sth->{NUM_OF_FIELDS}, 1, "... When we don't specify the fields in the results parameter then we still expect an answer from NUM_OF_FIELDS after we've execute the statement");
300
301 my ($result) = $sth->fetchrow_array();
302
303 is($result, 32, '... got the result we expected');
304
305 $rows = $sth->execute(2);
306 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
307
308 ($result) = $sth->fetchrow_array();
309
310 is($result, 43, '... got the result we expected');
311
312 $rows = $sth->execute(33);
313 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
314
315 ($result) = $sth->fetchrow_array();
316
317 is($result, 1, '... got the result we expected');
318 $sth->finish();
319 }
320
321 $dbh->{mock_start_insert_id} = [ 'y', 4 ];
322
323 $dbh->{mock_add_resultset} = {
324 sql => 'INSERT INTO y ( x ) VALUES ( ? )',
325 callback => sub {
326 my @bound_params = @_;
327
328 my %result = (
329 fields => [],
330 rows => []
331 );
332
333 return %result;
334 },
335 };
336
337 {
338 my $sth = $dbh->prepare( 'INSERT INTO y ( x ) VALUES ( ? )' );
339
340 $sth->execute( 'test' );
341
342 is( $dbh->last_insert_id( (undef) x 4 ), 4, "last_insert_id should return the next Id value after an insert as our callback doesn't override it")
343 }
344
345
346 $dbh->{mock_add_resultset} = {
347 sql => 'INSERT INTO y ( x ) VALUES ( ? )',
348 callback => sub {
349 my @bound_params = @_;
350
351 my %result = (
352 fields => [],
353 rows => [],
354 last_insert_id => 99,
355 );
356
357 return %result;
358 },
359 };
360
361 {
362 my $sth = $dbh->prepare( 'INSERT INTO y ( x ) VALUES ( ? )' );
363
364 $sth->execute( 'test' );
365
366 is( $dbh->last_insert_id( (undef) x 4 ), 99, "last_insert_id should return the id the callback has provided");
367
368 is( $dbh->{mock_last_insert_ids}{y}, 5, "If we provide a last_insert_id value then the one stored against the table shouldn't be updated");
369 }
370
371
372 $dbh->{mock_add_resultset} = {
373 sql => 'SELECT x FROM y WHERE z = ?',
374 callback => sub {
375 return (
376 fields => [ 'a' ],
377 rows => [ 1 ],
378 );
379 },
380 };
381
382 {
383 my $sth = $dbh->prepare('SELECT x FROM y WHERE z = ?');
384 isa_ok($sth, 'DBI::st');
385
386 throws_ok {
387 my $rows = $sth->execute(1);
388 } qr/DBD::Mock error - a resultset's callback should return rows as an arrayref of arrayrefs/, "If we don't provided an arrayref or arrayrefs for the rows then that's an error";
389 }
390
391 done_testing();