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