27db12cd09b9240f66c11626eea529b0bbc6e2c7
[dbsrgits/SQL-Abstract.git] / t / 04modifiers.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Exception;
5 use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where)];
6
7 use SQL::Abstract;
8 use Storable 'dclone';
9
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
18 =begin
19 Test -and -or and -nest modifiers, assuming the following:
20
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.
25   * When in column multi-condition context i.e.
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
28
29 =cut
30
31 # no warnings (the -or/-and => { } warning is silly, there is nothing wrong with such usage)
32 my $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
40 my @and_or_tests = (
41   # basic tests
42   {
43     where => { -and => [a => 1, b => 2] },
44     %{$and_or_args->{and}},
45   },
46   {
47     where => [ -and => [a => 1, b => 2] ],
48     %{$and_or_args->{and}},
49   },
50   {
51     where => { -or => [a => 1, b => 2] },
52     %{$and_or_args->{or}},
53   },
54   {
55     where => [ -or => [a => 1, b => 2] ],
56     %{$and_or_args->{or}},
57   },
58
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
76   # test modifiers within hashrefs
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
92   # test modifiers within arrayrefs
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
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)
153   {
154     where => { x => { '!=' => [ -and => (1 .. 3) ] } },
155     stmt => 'WHERE x != ? AND x != ? AND x != ?',
156     bind => [1..3],
157   },
158
159   # the -or should affect only the inner hashref, as we are not in an outer arrayref
160   {
161     where => { x => {
162       -or => { '!=', 1, '>=', 2 }, -like => 'x%'
163     }},
164     stmt => 'WHERE x LIKE ? AND ( x != ? OR x >= ? )',
165     bind => [qw/x% 1 2/],
166   },
167
168   # the -and should affect the OUTER arrayref, while the internal structures remain intact
169   {
170     where => { x => [
171       -and => [ 1, 2 ], { -like => 'x%' }
172     ]},
173     stmt => 'WHERE (x = ? OR x = ?) AND x LIKE ?',
174     bind => [qw/1 2 x%/],
175   },
176
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   },
182
183   {
184     where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
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/],
187   },
188
189   {
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   },
194
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   },
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] } }  },
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/],
207   },
208
209   {
210     # flip logic in arrays except where excplicitly requested otherwise
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   },
216
217   # 1st -and is in column mode, thus flips the entire array, whereas the
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
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'} ],
248       [ {'!=' => 'toto', '=' => 'koko'} ],
249     ] },
250     stmt => 'WHERE (foo LIKE ? OR foo > ?) AND (foo LIKE ? AND foo < ?) AND (foo LIKE ? OR foo LIKE ?) AND (foo != ? AND foo = ?)',
251     bind => [qw/foo% moo %bar baz %alpha %beta toto koko/],
252   },
253
254   {
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}] ],
267     stmt => 'WHERE foo = ? AND bar = ? AND baz = ?',
268     bind => [1 .. 3],
269   },
270   {
271     where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, woz => 4} ] ],
272     stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR woz = ?)',
273     bind => [1 .. 4],
274   },
275
276   # -and has only 1 following element, thus all still ORed
277   {
278     where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] },
279     stmt => 'WHERE col < ? OR col > ? OR col != ?',
280     bind => [qw/123 456 789/],
281   },
282
283   # flipping array logic affects both column value and condition arrays
284   {
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/],
304   },
305 );
306
307 # modN and mod_N were a bad design decision - they go away in SQLA2, warn now
308 my @numbered_mods = (
309   {
310     backcompat => {
311       -and => [a => 10, b => 11],
312       -and2 => [ c => 20, d => 21 ],
313       -nest => [ x => 1 ],
314       -nest2 => [ y => 2 ],
315       -or => { m => 7, n => 8 },
316       -or2 => { m => 17, n => 18 },
317     },
318     correct => { -and => [
319       -and => [a => 10, b => 11],
320       -and => [ c => 20, d => 21 ],
321       -nest => [ x => 1 ],
322       -nest => [ y => 2 ],
323       -or => { m => 7, n => 8 },
324       -or => { m => 17, n => 18 },
325     ] },
326   },
327   {
328     backcompat => {
329       -and2 => [a => 10, b => 11],
330       -and_3 => [ c => 20, d => 21 ],
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 ],
339       -nest => [ x => 1 ],
340       -nest => [ y => 2 ],
341       -or => { m => 7, n => 8 },
342       -or => { m => 17, n => 18 },
343     ] ],
344   },
345 );
346
347 my @nest_tests = (
348  {
349    where => {a => 1, -nest => [b => 2, c => 3]},
350    stmt  => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
351    bind  => [qw/2 3 1/],
352  },
353  {
354    where => {a => 1, -nest => {b => 2, c => 3}},
355    stmt  => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
356    bind  => [qw/2 3 1/],
357  },
358  {
359    where => {a => 1, -or => {-nest => {b => 2, c => 3}}},
360    stmt  => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
361    bind  => [qw/2 3 1/],
362  },
363  {
364    where => {a => 1, -or => {-nest => [b => 2, c => 3]}},
365    stmt  => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
366    bind  => [qw/2 3 1/],
367  },
368  {
369    where => {a => 1, -nest => {-or => {b => 2, c => 3}}},
370    stmt  => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
371    bind  => [qw/2 3 1/],
372  },
373  {
374    where => [a => 1, -nest => {b => 2, c => 3}, -nest => [d => 4, e => 5]],
375    stmt  => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR ( d = ? OR e = ? ) ) )',
376    bind  => [qw/1 2 3 4 5/],
377  },
378 );
379
380 for my $case (@and_or_tests) {
381   TODO: {
382     local $TODO = $case->{todo} if $case->{todo};
383
384     my @w;
385     local $SIG{__WARN__} = sub { push @w, @_ };
386
387     my $sql = SQL::Abstract->new ($case->{args} || {});
388
389     my $where_copy = dclone($case->{where});
390
391     lives_ok (sub {
392       my ($stmt, @bind) = $sql->where($case->{where});
393       is_same_sql_bind(
394         $stmt,
395         \@bind,
396         $case->{stmt},
397         $case->{bind},
398       ) || diag_where( $case->{where} );
399     });
400     is (@w, 0, 'No warnings within and-or tests')
401       || diag join "\n", 'Emitted warnings:', @w;
402
403     is_deeply ($case->{where}, $where_copy, 'Where conditions unchanged');
404   }
405 }
406
407 for my $case (@nest_tests) {
408   TODO: {
409     local $TODO = $case->{todo} if $case->{todo};
410
411     local $SQL::Abstract::Test::parenthesis_significant = 1;
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},
421       ) || diag_where ( $case->{where} );
422     });
423   }
424 }
425
426
427
428 my $w_str = "\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0\E";
429 for my $case (@numbered_mods) {
430   TODO: {
431     local $TODO = $case->{todo} if $case->{todo};
432
433     my @w;
434     local $SIG{__WARN__} = sub { push @w, @_ };
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',
443       ) || diag_where ( {
444         backcompat => $case->{backcompat},
445         correct => $case->{correct},
446       });
447     });
448
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;
458   }
459 }
460
461 done_testing;