Actually weaken Converter->sqla_instance ref
[dbsrgits/SQL-Abstract.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 went away
309 my @invalid_numbered_mods = (
310     {
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     {
319       -and2 => [a => 10, b => 11],
320       -and_3 => [ c => 20, d => 21 ],
321       -nest2 => [ x => 1 ],
322       -nest_3 => [ y => 2 ],
323       -or2 => { m => 7, n => 8 },
324       -or_3 => { m => 17, n => 18 },
325     },
326 );
327
328 my @nest_tests = (
329  {
330    where => {a => 1, -nest => [b => 2, c => 3]},
331    stmt  => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
332    bind  => [qw/2 3 1/],
333  },
334  {
335    where => {a => 1, -nest => {b => 2, c => 3}},
336    stmt  => 'WHERE ( ( b = ? AND c = ? AND a = ? ) )',
337    bind  => [qw/2 3 1/],
338  },
339  {
340    where => {a => 1, -or => {-nest => {b => 2, c => 3}}},
341    stmt  => 'WHERE ( ( b = ? AND c = ? AND a = ? ) )',
342    bind  => [qw/2 3 1/],
343  },
344  {
345    where => {a => 1, -or => {-nest => [b => 2, c => 3]}},
346    stmt  => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
347    bind  => [qw/2 3 1/],
348  },
349  {
350    where => {a => 1, -nest => {-or => {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}, -nest => [d => 4, e => 5]],
356    stmt  => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR d = ? OR e = ? ) )',
357    bind  => [qw/1 2 3 4 5/],
358  },
359  {
360    where => { -and => [
361     -and => [a => 10, b => 11],
362     -and => [ c => 20, d => 21 ],
363     -nest => [ x => 1 ],
364     -nest => [ y => 2 ],
365     -or => { m => 7, n => 8 },
366     -or => { m => 17, n => 18 },
367    ] },
368    stmt => 'WHERE ( ( a = ? AND b = ? AND c = ? AND d = ? AND ( x = ? ) AND ( y = ? ) AND ( m = ? OR n = ? ) AND ( m = ? OR n = ? ) ) )',
369    bind => [ 10, 11, 20, 21, 1, 2, 7, 8, 17, 18 ],
370  },
371  {
372    where => [ -and => [
373     -and => [a => 10, b => 11],
374     -and => [ c => 20, d => 21 ],
375     -nest => [ x => 1 ],
376     -nest => [ y => 2 ],
377     -or => { m => 7, n => 8 },
378     -or => { m => 17, n => 18 },
379    ] ],
380    stmt => 'WHERE ( ( ( a = ? AND b = ? AND c = ? AND d = ? AND ( x = ? ) AND ( y = ? ) AND ( m = ? OR n = ? ) AND ( m = ? OR n = ? ) ) ) )',
381    bind => [ 10, 11, 20, 21, 1, 2, 7, 8, 17, 18 ],
382  },
383 );
384
385 for my $case (@and_or_tests) {
386   TODO: {
387     local $TODO = $case->{todo} if $case->{todo};
388
389     my $sql = SQL::Abstract->new ($case->{args} || {});
390
391     my $where_copy = dclone($case->{where});
392
393     warnings_are {
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     } [], 'No warnings within and-or tests';
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 for my $case (@invalid_numbered_mods) {
427     my $sql = SQL::Abstract->new ($case->{args} || {});
428     throws_ok (sub {
429       $sql->where($case);
430     }, qr/\QUse of [and|or|nest]_N modifiers is no longer supported/, 'Exception thrown on bogus syntax');
431 }
432
433 done_testing;