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