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