Centralize handling of in-test dumpering
[scpubgit/Q-Branch.git] / t / 04modifiers.t
CommitLineData
e5360be4 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Test::More;
6use Test::Exception;
2fadf08e 7use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where)];
e5360be4 8
e5360be4 9use SQL::Abstract;
771ea2ec 10use Storable 'dclone';
8fec3fd9 11
48d9f5f8 12#### WARNING ####
13#
14# -nest has been undocumented on purpose, but is still supported for the
15# foreseable future. Do not rip out the -nest tests before speaking to
16# someone on the DBIC mailing list or in irc.perl.org#dbix-class
17#
18#################
19
e5360be4 20=begin
01a01e57 21Test -and -or and -nest modifiers, assuming the following:
e5360be4 22
6c8ac65d 23 * Modifiers are respected in both hashrefs and arrayrefs (with the obvious
24 limitation of one modifier type per hahsref)
25 * When in condition context i.e. where => { -or { a = 1 } }, each modifier
26 affects only the immediate element following it.
428975b0 27 * When in column multi-condition context i.e.
6c8ac65d 28 where => { x => { '!=', [-and, [qw/1 2 3/]] } }, a modifier affects the
29 OUTER ARRAYREF if and only if it is the first element of said ARRAYREF
e5360be4 30
31=cut
32
05a05fd1 33# no warnings (the -or/-and => { } warning is silly, there is nothing wrong with such usage)
34my $and_or_args = {
35 and => { stmt => 'WHERE a = ? AND b = ?', bind => [qw/1 2/] },
36 or => { stmt => 'WHERE a = ? OR b = ?', bind => [qw/1 2/] },
37 or_and => { stmt => 'WHERE ( foo = ? OR bar = ? ) AND baz = ? ', bind => [qw/1 2 3/] },
38 or_or => { stmt => 'WHERE foo = ? OR bar = ? OR baz = ?', bind => [qw/1 2 3/] },
39 and_or => { stmt => 'WHERE ( foo = ? AND bar = ? ) OR baz = ?', bind => [qw/1 2 3/] },
40};
41
a8b90cb7 42my @and_or_tests = (
05a05fd1 43 # basic tests
a8b90cb7 44 {
45 where => { -and => [a => 1, b => 2] },
05a05fd1 46 %{$and_or_args->{and}},
a8b90cb7 47 },
48 {
49 where => [ -and => [a => 1, b => 2] ],
05a05fd1 50 %{$and_or_args->{and}},
a8b90cb7 51 },
52 {
53 where => { -or => [a => 1, b => 2] },
05a05fd1 54 %{$and_or_args->{or}},
a8b90cb7 55 },
56 {
57 where => [ -or => [a => 1, b => 2] ],
05a05fd1 58 %{$and_or_args->{or}},
59 },
60
6c8ac65d 61 {
62 where => { -and => {a => 1, b => 2} },
63 %{$and_or_args->{and}},
64 },
65 {
66 where => [ -and => {a => 1, b => 2} ],
67 %{$and_or_args->{and}},
68 },
69 {
70 where => { -or => {a => 1, b => 2} },
71 %{$and_or_args->{or}},
72 },
73 {
74 where => [ -or => {a => 1, b => 2} ],
75 %{$and_or_args->{or}},
76 },
77
428975b0 78 # test modifiers within hashrefs
05a05fd1 79 {
80 where => { -or => [
81 [ foo => 1, bar => 2 ],
82 baz => 3,
83 ]},
84 %{$and_or_args->{or_or}},
85 },
86 {
87 where => { -and => [
88 [ foo => 1, bar => 2 ],
89 baz => 3,
90 ]},
91 %{$and_or_args->{or_and}},
92 },
93
428975b0 94 # test modifiers within arrayrefs
05a05fd1 95 {
96 where => [ -or => [
97 [ foo => 1, bar => 2 ],
98 baz => 3,
99 ]],
100 %{$and_or_args->{or_or}},
101 },
102 {
103 where => [ -and => [
104 [ foo => 1, bar => 2 ],
105 baz => 3,
106 ]],
107 %{$and_or_args->{or_and}},
108 },
109
110 # test ambiguous modifiers within hashrefs (op extends to to immediate RHS only)
111 {
112 where => { -and => [ -or =>
113 [ foo => 1, bar => 2 ],
114 baz => 3,
115 ]},
116 %{$and_or_args->{or_and}},
117 },
118 {
119 where => { -or => [ -and =>
120 [ foo => 1, bar => 2 ],
121 baz => 3,
122 ]},
123 %{$and_or_args->{and_or}},
124 },
125
126 # test ambiguous modifiers within arrayrefs (op extends to to immediate RHS only)
127 {
128 where => [ -and => [ -or =>
129 [ foo => 1, bar => 2 ],
130 baz => 3,
131 ]],
132 %{$and_or_args->{or_and}},
133 },
134 {
135 where => [ -or => [ -and =>
136 [ foo => 1, bar => 2 ],
137 baz => 3
138 ]],
139 %{$and_or_args->{and_or}},
140 },
141
6c8ac65d 142 # test column multi-cond in arrayref (useless example)
143 {
144 where => { x => [ -and => (1 .. 3) ] },
145 stmt => 'WHERE x = ? AND x = ? AND x = ?',
146 bind => [1..3],
147 },
148 # test column multi-cond in arrayref (more useful)
149 {
150 where => { x => [ -and => {'!=' => 1}, {'!=' => 2}, {'!=' => 3} ] },
151 stmt => 'WHERE x != ? AND x != ? AND x != ?',
152 bind => [1..3],
153 },
154 # test column multi-cond in arrayref (even more useful)
64385831 155 {
156 where => { x => { '!=' => [ -and => (1 .. 3) ] } },
157 stmt => 'WHERE x != ? AND x != ? AND x != ?',
158 bind => [1..3],
159 },
6c8ac65d 160
161 # the -or should affect only the inner hashref, as we are not in an outer arrayref
05a05fd1 162 {
163 where => { x => {
164 -or => { '!=', 1, '>=', 2 }, -like => 'x%'
165 }},
25e4c693 166 stmt => 'WHERE x LIKE ? AND ( x != ? OR x >= ? )',
eb49170d 167 bind => [qw/x% 1 2/],
05a05fd1 168 },
6c8ac65d 169
170 # the -and should affect the OUTER arrayref, while the internal structures remain intact
05a05fd1 171 {
428975b0 172 where => { x => [
173 -and => [ 1, 2 ], { -like => 'x%' }
05a05fd1 174 ]},
6c8ac65d 175 stmt => 'WHERE (x = ? OR x = ?) AND x LIKE ?',
05a05fd1 176 bind => [qw/1 2 x%/],
a8b90cb7 177 },
6c8ac65d 178
a8b90cb7 179 {
180 where => { -and => [a => 1, b => 2], x => 9, -or => { c => 3, d => 4 } },
181 stmt => 'WHERE a = ? AND b = ? AND ( c = ? OR d = ? ) AND x = ?',
182 bind => [qw/1 2 3 4 9/],
183 },
6c8ac65d 184
a8b90cb7 185 {
186 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
106c861e 187 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND (c = ? OR d = ? OR (l = ? OR l = ?) ) AND x = ?',
188 bind => [qw/1 2 11 12 3 4 21 22 9/],
a8b90cb7 189 },
e5360be4 190
a8b90cb7 191 {
a8b90cb7 192 where => { -or => [a => 1, b => 2, k => [11, 12] ], x => 9, -and => { c => 3, d => 4, l => { '=' => [21, 22] } } },
193 stmt => 'WHERE c = ? AND d = ? AND ( l = ? OR l = ?) AND (a = ? OR b = ? OR k = ? OR k = ?) AND x = ?',
194 bind => [qw/3 4 21 22 1 2 11 12 9/],
195 },
e5360be4 196
a8b90cb7 197 {
198 where => [ -or => [a => 1, b => 2], -or => { c => 3, d => 4}, e => 5, -and => [ f => 6, g => 7], [ h => 8, i => 9, -and => [ k => 10, l => 11] ], { m => 12, n => 13 }],
199 stmt => 'WHERE a = ? OR b = ? OR c = ? OR d = ? OR e = ? OR ( f = ? AND g = ?) OR h = ? OR i = ? OR ( k = ? AND l = ? ) OR (m = ? AND n = ?)',
200 bind => [1 .. 13],
201 },
6c8ac65d 202
203 {
204 # explicit OR logic in arrays should leave everything intact
205 args => { logic => 'or' },
206 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
106c861e 207 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ( c = ? OR d = ? OR l = ? OR l = ? ) AND x = ? ',
208 bind => [qw/1 2 11 12 3 4 21 22 9/],
6c8ac65d 209 },
210
a8b90cb7 211 {
6c8ac65d 212 # flip logic in arrays except where excplicitly requested otherwise
a8b90cb7 213 args => { logic => 'and' },
214 where => [ -or => [a => 1, b => 2], -or => { c => 3, d => 4}, e => 5, -and => [ f => 6, g => 7], [ h => 8, i => 9, -and => [ k => 10, l => 11] ], { m => 12, n => 13 }],
215 stmt => 'WHERE (a = ? OR b = ?) AND (c = ? OR d = ?) AND e = ? AND f = ? AND g = ? AND h = ? AND i = ? AND k = ? AND l = ? AND m = ? AND n = ?',
216 bind => [1 .. 13],
217 },
05a05fd1 218
428975b0 219 # 1st -and is in column mode, thus flips the entire array, whereas the
6c8ac65d 220 # 2nd one is just a condition modifier
221 {
222 where => [
223 col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ],
224 -and => [
225 col2 => [ -or => { -like => 'crap' }, { -like => 'crop' } ],
226 col3 => [ -and => { -like => 'chap' }, { -like => 'chop' } ],
227 ],
228 ],
229 stmt => 'WHERE
230 (col < ? AND col > ? AND col != ?)
231 OR
232 (
233 ( col2 LIKE ? OR col2 LIKE ? )
234 AND
235 ( col3 LIKE ? AND col3 LIKE ? )
236 )
237 ',
238 bind => [qw/123 456 789 crap crop chap chop/],
239 },
240
05a05fd1 241 ##########
242 # some corner cases by ldami (some produce useless SQL, just for clarification on 1.5 direction)
243 #
244
245 {
246 where => { foo => [
247 -and => [ { -like => 'foo%'}, {'>' => 'moo'} ],
248 { -like => '%bar', '<' => 'baz'},
249 [ {-like => '%alpha'}, {-like => '%beta'} ],
6c8ac65d 250 [ {'!=' => 'toto', '=' => 'koko'} ],
05a05fd1 251 ] },
6c8ac65d 252 stmt => 'WHERE (foo LIKE ? OR foo > ?) AND (foo LIKE ? AND foo < ?) AND (foo LIKE ? OR foo LIKE ?) AND (foo != ? AND foo = ?)',
05a05fd1 253 bind => [qw/foo% moo %bar baz %alpha %beta toto koko/],
254 },
6c8ac65d 255
05a05fd1 256 {
6c8ac65d 257 where => [
258 -and => [a => 1, b => 2],
259 -or => [c => 3, d => 4],
260 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ],
261 ],
262 stmt => 'WHERE (a = ? AND b = ?) OR c = ? OR d = ? OR (e LIKE ? AND e LIKE ?)',
263 bind => [qw/1 2 3 4 foo% %bar/],
264 },
265
266 # -or has nothing to flip
267 {
268 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3}] ],
05a05fd1 269 stmt => 'WHERE foo = ? AND bar = ? AND baz = ?',
6c8ac65d 270 bind => [1 .. 3],
05a05fd1 271 },
272 {
6c8ac65d 273 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, woz => 4} ] ],
274 stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR woz = ?)',
05a05fd1 275 bind => [1 .. 4],
276 },
277
6c8ac65d 278 # -and has only 1 following element, thus all still ORed
05a05fd1 279 {
6c8ac65d 280 where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] },
05a05fd1 281 stmt => 'WHERE col < ? OR col > ? OR col != ?',
282 bind => [qw/123 456 789/],
283 },
284
6c8ac65d 285 # flipping array logic affects both column value and condition arrays
05a05fd1 286 {
6c8ac65d 287 args => { logic => 'and' },
288 where => [ col => [ {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
289 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
290 bind => [qw/123 456 789 0/],
291 },
292
293 # flipping array logic with explicit -and works
294 {
295 args => { logic => 'and' },
296 where => [ col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
297 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
298 bind => [qw/123 456 789 0/],
299 },
300 # flipping array logic with explicit -or flipping it back
301 {
302 args => { logic => 'and' },
303 where => [ col => [ -or => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
304 stmt => 'WHERE (col < ? OR col > ? OR col != ?) AND col2 = ?',
305 bind => [qw/123 456 789 0/],
05a05fd1 306 },
a8b90cb7 307);
e5360be4 308
403e50d1 309# modN and mod_N were a bad design decision - they go away in SQLA2, warn now
01a01e57 310my @numbered_mods = (
403e50d1 311 {
312 backcompat => {
01a01e57 313 -and => [a => 10, b => 11],
314 -and2 => [ c => 20, d => 21 ],
403e50d1 315 -nest => [ x => 1 ],
316 -nest2 => [ y => 2 ],
01a01e57 317 -or => { m => 7, n => 8 },
318 -or2 => { m => 17, n => 18 },
403e50d1 319 },
320 correct => { -and => [
321 -and => [a => 10, b => 11],
322 -and => [ c => 20, d => 21 ],
01a01e57 323 -nest => [ x => 1 ],
324 -nest => [ y => 2 ],
403e50d1 325 -or => { m => 7, n => 8 },
326 -or => { m => 17, n => 18 },
327 ] },
328 },
329 {
330 backcompat => {
01a01e57 331 -and2 => [a => 10, b => 11],
332 -and_3 => [ c => 20, d => 21 ],
403e50d1 333 -nest2 => [ x => 1 ],
334 -nest_3 => [ y => 2 ],
335 -or2 => { m => 7, n => 8 },
336 -or_3 => { m => 17, n => 18 },
337 },
338 correct => [ -and => [
339 -and => [a => 10, b => 11],
340 -and => [ c => 20, d => 21 ],
01a01e57 341 -nest => [ x => 1 ],
342 -nest => [ y => 2 ],
403e50d1 343 -or => { m => 7, n => 8 },
344 -or => { m => 17, n => 18 },
345 ] ],
346 },
347);
a8b90cb7 348
01a01e57 349my @nest_tests = (
2f641e1f 350 {
01a01e57 351 where => {a => 1, -nest => [b => 2, c => 3]},
2f641e1f 352 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
353 bind => [qw/2 3 1/],
354 },
355 {
01a01e57 356 where => {a => 1, -nest => {b => 2, c => 3}},
2f641e1f 357 stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
358 bind => [qw/2 3 1/],
359 },
360 {
01a01e57 361 where => {a => 1, -or => {-nest => {b => 2, c => 3}}},
2f641e1f 362 stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
363 bind => [qw/2 3 1/],
364 },
365 {
01a01e57 366 where => {a => 1, -or => {-nest => [b => 2, c => 3]}},
2f641e1f 367 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
368 bind => [qw/2 3 1/],
369 },
370 {
01a01e57 371 where => {a => 1, -nest => {-or => {b => 2, c => 3}}},
106c861e 372 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
373 bind => [qw/2 3 1/],
2f641e1f 374 },
3a8b7db0 375 {
01a01e57 376 where => [a => 1, -nest => {b => 2, c => 3}, -nest => [d => 4, e => 5]],
c592e251 377 stmt => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR ( d = ? OR e = ? ) ) )',
3a8b7db0 378 bind => [qw/1 2 3 4 5/],
379 },
2f641e1f 380);
381
a8b90cb7 382for my $case (@and_or_tests) {
ef8c0c94 383 TODO: {
384 local $TODO = $case->{todo} if $case->{todo};
385
a8b90cb7 386 my @w;
387 local $SIG{__WARN__} = sub { push @w, @_ };
ce261791 388
a8b90cb7 389 my $sql = SQL::Abstract->new ($case->{args} || {});
8fec3fd9 390
771ea2ec 391 my $where_copy = dclone($case->{where});
ce261791 392
428975b0 393 lives_ok (sub {
a8b90cb7 394 my ($stmt, @bind) = $sql->where($case->{where});
403e50d1 395 is_same_sql_bind(
396 $stmt,
397 \@bind,
398 $case->{stmt},
399 $case->{bind},
2fadf08e 400 ) || diag_where( $case->{where} );
e5360be4 401 });
a8b90cb7 402 is (@w, 0, 'No warnings within and-or tests')
403 || diag join "\n", 'Emitted warnings:', @w;
ce261791 404
771ea2ec 405 is_deeply ($case->{where}, $where_copy, 'Where conditions unchanged');
ef8c0c94 406 }
e5360be4 407}
403e50d1 408
01a01e57 409for my $case (@nest_tests) {
2f641e1f 410 TODO: {
411 local $TODO = $case->{todo} if $case->{todo};
412
413 local $SQL::Abstract::Test::parenthesis_significant = 1;
2f641e1f 414
415 my $sql = SQL::Abstract->new ($case->{args} || {});
416 lives_ok (sub {
417 my ($stmt, @bind) = $sql->where($case->{where});
418 is_same_sql_bind(
419 $stmt,
420 \@bind,
421 $case->{stmt},
422 $case->{bind},
2fadf08e 423 ) || diag_where ( $case->{where} );
2f641e1f 424 });
425 }
426}
427
01a01e57 428
429
430my $w_str = "\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0\E";
431for my $case (@numbered_mods) {
2f641e1f 432 TODO: {
433 local $TODO = $case->{todo} if $case->{todo};
434
01a01e57 435 my @w;
436 local $SIG{__WARN__} = sub { push @w, @_ };
403e50d1 437 my $sql = SQL::Abstract->new ($case->{args} || {});
438 lives_ok (sub {
439 my ($old_s, @old_b) = $sql->where($case->{backcompat});
440 my ($new_s, @new_b) = $sql->where($case->{correct});
441 is_same_sql_bind(
442 $old_s, \@old_b,
443 $new_s, \@new_b,
444 'Backcompat and the correct(tm) syntax result in identical statements',
2fadf08e 445 ) || diag_where ( {
446 backcompat => $case->{backcompat},
447 correct => $case->{correct},
448 });
403e50d1 449 });
450
01a01e57 451 ok (@w, 'Warnings were emitted about a mod_N construct');
452
453 my @non_match;
454 for (@w) {
455 push @non_match, $_ if ($_ !~ /$w_str/);
456 }
457
458 is (@non_match, 0, 'All warnings match the deprecation message')
459 || diag join "\n", 'Rogue warnings:', @non_match;
2f641e1f 460 }
403e50d1 461}
462
8fec3fd9 463done_testing;