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