Debian Codegraph @

t/022_DBD_Mock_Session_bound_params.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 {
14 my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
15 isa_ok($dbh, 'DBI::db');
16
17 my $session = DBD::Mock::Session->new((
18 {
19 statement => 'SELECT foo FROM bar WHERE baz = ?',
20 bound_params => [ 100 ],
21 results => [[ 'foo' ], [ 10 ]]
22 },
23 {
24 statement => 'SELECT bar FROM foo WHERE baz = ?',
25 bound_params => [ 125 ],
26 results => [[ 'bar' ], [ 15 ]]
27 },
28 ));
29 isa_ok($session, 'DBD::Mock::Session');
30
31 $dbh->{mock_session} = $session;
32
33 eval {
34 my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
35 $sth->execute(100);
36 my ($result) = $sth->fetchrow_array();
37 is($result, 10, '... got the right value');
38 };
39 ok(!$@, '... everything worked as planned');
40
41 eval {
42 my $sth = $dbh->prepare('SELECT bar FROM foo WHERE baz = ?');
43 $sth->execute(125);
44 my ($result) = $sth->fetchrow_array();
45 is($result, 15, '... got the right value');
46 };
47 ok(!$@, '... everything worked as planned');
48
49 # Shuts up warning when object is destroyed
50 undef $dbh->{mock_session};
51 }
52
53 {
54 my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
55 isa_ok($dbh, 'DBI::db');
56
57 my $session = DBD::Mock::Session->new((
58 {
59 statement => 'SELECT foo FROM bar WHERE baz = ?',
60 bound_params => [ 100 ],
61 results => [[ 'foo' ], [ 10 ]]
62 },
63 {
64 statement => 'SELECT bar FROM foo WHERE baz = 125',
65 results => [[ 'bar' ], [ 15 ]]
66 },
67 {
68 statement => 'DELETE FROM bar WHERE baz = ?',
69 results => [[], [], []],
70 bound_params => [ 100 ]
71 }
72 ));
73 isa_ok($session, 'DBD::Mock::Session');
74
75 $dbh->{mock_session} = $session;
76
77 eval {
78 my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
79 $sth->execute(100);
80 my ($result) = $sth->fetchrow_array();
81 is($result, 10, '... got the right value');
82 };
83 ok(!$@, '... first state worked as planned');
84
85 eval {
86 my $sth = $dbh->prepare('SELECT bar FROM foo WHERE baz = 125');
87 $sth->execute();
88 my ($result) = $sth->fetchrow_array();
89 is($result, 15, '... got the right value');
90 };
91 ok(!$@, '... second state worked as planned');
92
93 eval {
94 my $sth = $dbh->prepare('DELETE FROM bar WHERE baz = ?');
95 $sth->execute(100);
96 is($sth->rows(), 2, '... got the right number of affected rows');
97 };
98 ok(!$@, '... third state worked as planned');
99
100 # Shuts up warning when object is destroyed
101 undef $dbh->{mock_session};
102 }
103
104 # check some errors
105
106 {
107 my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
108 isa_ok($dbh, 'DBI::db');
109
110 my $session = DBD::Mock::Session->new((
111 {
112 statement => 'SELECT foo FROM bar WHERE baz = ?',
113 bound_params => [ 100 ],
114 results => [[ 'foo' ], [ 10 ]]
115 }
116 ));
117 isa_ok($session, 'DBD::Mock::Session');
118
119 $dbh->{mock_session} = $session;
120
121 eval {
122 my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
123 $sth->execute(100, 200);
124 my ($result) = $sth->fetchrow_array();
125 };
126 ok($@, '... everything failed as planned');
127 like($@,
128 qr/Session Error\: Not the same number of bound params in current state in DBD\:\:Mock\:\:Session/,
129 '... everything failed as planned');
130
131 # Shuts up warning when object is destroyed
132 undef $dbh->{mock_session};
133 }
134
135 {
136 my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
137 isa_ok($dbh, 'DBI::db');
138
139 my $session = DBD::Mock::Session->new((
140 {
141 statement => 'SELECT foo FROM bar WHERE baz = ?',
142 bound_params => [ 100 ],
143 results => [[ 'foo' ], [ 10 ]]
144 }
145 ));
146 isa_ok($session, 'DBD::Mock::Session');
147
148 $dbh->{mock_session} = $session;
149
150 eval {
151 my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
152 $sth->execute(200);
153 my ($result) = $sth->fetchrow_array();
154 };
155 ok($@, '... everything failed as planned');
156 like($@,
157 qr/Session Error\: Bound param 0 do not match in current state in DBD\:\:Mock\:\:Session/,
158 '... everything failed as planned');
159
160 # Shuts up warning when object is destroyed
161 undef $dbh->{mock_session};
162 }
163
164 {
165 my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
166 isa_ok($dbh, 'DBI::db');
167
168 my $session = DBD::Mock::Session->new((
169 {
170 statement => 'SELECT foo FROM bar WHERE baz = ?',
171 bound_params => [ 100 ],
172 results => [[ 'foo' ], [ 10 ]]
173 },
174 {
175 statement => 'SELECT foo FROM bar WHERE baz = ?',
176 bound_params => [ 125 ],
177 results => [[ 'foo' ], [ 15 ]]
178 },
179 ));
180 isa_ok($session, 'DBD::Mock::Session');
181
182 $dbh->{mock_session} = $session;
183
184 eval {
185 my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
186 $sth->execute(100);
187 my ($result) = $sth->fetchrow_array();
188 is($result, 10, '... first execute got the right value');
189 $sth->execute(125);
190 ($result) = $sth->fetchrow_array();
191 is($result, 15, '... second execute got the right value');
192 };
193 ok(!$@, '... everything worked as planned');
194
195 # Shuts up warning when object is destroyed
196 undef $dbh->{mock_session};
197 }
198
199
200 {
201 my $dbh = DBI->connect('dbi:Mock:PostgreSQL', '', '', { RaiseError => 1, PrintError => 0 });
202 isa_ok($dbh, 'DBI::db');
203
204 my $session = DBD::Mock::Session->new((
205 {
206 statement => 'SELECT foo FROM bar WHERE baz = ANY(?)',
207 bound_params => [ [ 100, 101 ] ],
208 results => [[ 'foo' ], [ 10 ]]
209 },
210 {
211 statement => 'SELECT foo FROM bar WHERE baz = ANY(?)',
212 bound_params => [ [ 125, undef ] ],
213 results => [[ 'foo' ], [ 15 ]]
214 },
215 {
216 statement => 'SELECT foo FROM bar WHERE baz = ANY(?)',
217 bound_params => [ [ 150, qr/^abc/ ] ],
218 results => [[ 'foo' ], [ 20 ]]
219 },
220 ));
221 isa_ok($session, 'DBD::Mock::Session');
222
223 $dbh->{mock_session} = $session;
224
225 eval {
226 my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ANY(?)');
227 $sth->execute([ 100, 101 ]);
228 my ($result) = $sth->fetchrow_array();
229 is($result, 10, '... first execute got the right value');
230 $sth->execute([125, undef]);
231 ($result) = $sth->fetchrow_array();
232 is($result, 15, '... second execute got the right value');
233 $sth->execute([150, 'abcdef']);
234 ($result) = $sth->fetchrow_array();
235 is($result, 20, '... third execute got the right value');
236 };
237 ok(!$@, '... everything worked as planned')
238 or diag $@;
239
240 # Shuts up warning when object is destroyed
241 undef $dbh->{mock_session};
242 }
243
244 # Expect array, find scalar
245 {
246 my $dbh = DBI->connect('dbi:Mock:PostgreSQL', '', '', { RaiseError => 1, PrintError => 0 });
247 isa_ok($dbh, 'DBI::db');
248
249 my $session = DBD::Mock::Session->new((
250 {
251 statement => 'SELECT foo FROM bar WHERE baz = ANY(?)',
252 bound_params => [ [ ] ],
253 results => [[ 'foo' ], [ 10 ]]
254 }
255 ));
256 isa_ok($session, 'DBD::Mock::Session');
257
258 $dbh->{mock_session} = $session;
259
260 eval {
261 my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ANY(?)');
262 $sth->execute(200);
263 my ($result) = $sth->fetchrow_array();
264 };
265 ok($@, '... everything failed as planned');
266 like($@,
267 qr/got: 200\s+expected: ARRAY/m,
268 '... everything failed as planned');
269
270 # Shuts up warning when object is destroyed
271 undef $dbh->{mock_session};
272 }
273
274 # Expect array with undef, find array with scalar
275 {
276 my $dbh = DBI->connect('dbi:Mock:PostgreSQL', '', '', { RaiseError => 1, PrintError => 0 });
277 isa_ok($dbh, 'DBI::db');
278
279 my $session = DBD::Mock::Session->new((
280 {
281 statement => 'SELECT foo FROM bar WHERE baz = ANY(?)',
282 bound_params => [ [ undef ] ],
283 results => [[ 'foo' ], [ 10 ]]
284 }
285 ));
286 isa_ok($session, 'DBD::Mock::Session');
287
288 $dbh->{mock_session} = $session;
289
290 eval {
291 my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ANY(?)');
292 $sth->execute( [200] );
293 my ($result) = $sth->fetchrow_array();
294 };
295 ok($@, '... everything failed as planned');
296 like($@,
297 qr/got: 200\s+expected: \<undef\>/m,
298 '... everything failed as planned');
299
300 # Shuts up warning when object is destroyed
301 undef $dbh->{mock_session};
302 }
303
304 # Expect array with scalar, find array with undef
305 {
306 my $dbh = DBI->connect('dbi:Mock:PostgreSQL', '', '', { RaiseError => 1, PrintError => 0 });
307 isa_ok($dbh, 'DBI::db');
308
309 my $session = DBD::Mock::Session->new((
310 {
311 statement => 'SELECT foo FROM bar WHERE baz = ANY(?)',
312 bound_params => [ [ 200 ] ],
313 results => [[ 'foo' ], [ 10 ]]
314 }
315 ));
316 isa_ok($session, 'DBD::Mock::Session');
317
318 $dbh->{mock_session} = $session;
319
320 eval {
321 my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ANY(?)');
322 $sth->execute( [undef] );
323 my ($result) = $sth->fetchrow_array();
324 };
325 ok($@, '... everything failed as planned');
326 like($@,
327 qr/got: \<undef\>\s+expected: 200/m,
328 '... everything failed as planned');
329
330 # Shuts up warning when object is destroyed
331 undef $dbh->{mock_session};
332 }
333
334 # Expect array with scalar, find array with different scalar
335 {
336 my $dbh = DBI->connect('dbi:Mock:PostgreSQL', '', '', { RaiseError => 1, PrintError => 0 });
337 isa_ok($dbh, 'DBI::db');
338
339 my $session = DBD::Mock::Session->new((
340 {
341 statement => 'SELECT foo FROM bar WHERE baz = ANY(?)',
342 bound_params => [ [ 200 ] ],
343 results => [[ 'foo' ], [ 10 ]]
344 }
345 ));
346 isa_ok($session, 'DBD::Mock::Session');
347
348 $dbh->{mock_session} = $session;
349
350 eval {
351 my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ANY(?)');
352 $sth->execute( [201] );
353 my ($result) = $sth->fetchrow_array();
354 };
355 ok($@, '... everything failed as planned');
356 like($@,
357 qr/got: 201\s+expected: 200/m,
358 '... everything failed as planned');
359
360 # Shuts up warning when object is destroyed
361 undef $dbh->{mock_session};
362 }
363
364 # Expect array with scalar matching regex, find array with mismatching scalar
365 {
366 my $dbh = DBI->connect('dbi:Mock:PostgreSQL', '', '', { RaiseError => 1, PrintError => 0 });
367 isa_ok($dbh, 'DBI::db');
368
369 my $session = DBD::Mock::Session->new((
370 {
371 statement => 'SELECT foo FROM bar WHERE baz = ANY(?)',
372 bound_params => [ [ qr/abc/ ] ],
373 results => [[ 'foo' ], [ 10 ]]
374 }
375 ));
376 isa_ok($session, 'DBD::Mock::Session');
377
378 $dbh->{mock_session} = $session;
379
380 eval {
381 my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ANY(?)');
382 $sth->execute( ['def'] );
383 my ($result) = $sth->fetchrow_array();
384 };
385 ok($@, '... everything failed as planned');
386 like($@,
387 qr/got: def\s+expected: \(\?[^:]*:abc\)/m,
388 '... everything failed as planned');
389
390 # Shuts up warning when object is destroyed
391 undef $dbh->{mock_session};
392 }
393
394 # Expect array with 2 elements, find array with 1 element
395 {
396 my $dbh = DBI->connect('dbi:Mock:PostgreSQL', '', '', { RaiseError => 1, PrintError => 0 });
397 isa_ok($dbh, 'DBI::db');
398
399 my $session = DBD::Mock::Session->new((
400 {
401 statement => 'SELECT foo FROM bar WHERE baz = ANY(?)',
402 bound_params => [ [ 1, 2 ] ],
403 results => [[ 'foo' ], [ 10 ]]
404 }
405 ));
406 isa_ok($session, 'DBD::Mock::Session');
407
408 $dbh->{mock_session} = $session;
409
410 eval {
411 my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ANY(?)');
412 $sth->execute( ['def'] );
413 my ($result) = $sth->fetchrow_array();
414 };
415 ok($@, '... everything failed as planned');
416 like($@,
417 qr/got: array of length 1\s+expected: array of length 2/m,
418 '... everything failed as planned');
419
420 # Shuts up warning when object is destroyed
421 undef $dbh->{mock_session};
422 }
423
424 # Non-PostgreSQL driver doesn't support bound arrays
425 {
426 my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
427 isa_ok($dbh, 'DBI::db');
428
429 my $session = DBD::Mock::Session->new((
430 {
431 statement => 'SELECT foo FROM bar WHERE baz = ANY(?)',
432 bound_params => [ [ 1 ] ],
433 results => [[ 'foo' ], [ 10 ]]
434 }
435 ));
436 isa_ok($session, 'DBD::Mock::Session');
437
438 $dbh->{mock_session} = $session;
439
440 eval {
441 my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ANY(?)');
442 $sth->execute( [1] );
443 my ($result) = $sth->fetchrow_array();
444 };
445 ok($@, '... everything failed as planned');
446 like($@,
447 qr/got: ARRAY\(0x[0-9a-f]+\)\s+expected: ARRAY\(0x[0-9a-f]+\)/m,
448 '... everything failed as planned');
449
450 # Shuts up warning when object is destroyed
451 undef $dbh->{mock_session};
452 }
453
454 done_testing();