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