Actually weaken Converter->sqla_instance ref
[dbsrgits/SQL-Abstract.git] / t / 04modifiers.t
CommitLineData
e5360be4 1use strict;
2use warnings;
3use Test::More;
4use Test::Exception;
97084113 5use Test::Warn;
2fadf08e 6use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where)];
e5360be4 7
e5360be4 8use SQL::Abstract;
771ea2ec 9use Storable 'dclone';
8fec3fd9 10
48d9f5f8 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
e5360be4 19=begin
01a01e57 20Test -and -or and -nest modifiers, assuming the following:
e5360be4 21
6c8ac65d 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.
f2a0d52b 26 * When in column multi-condition context i.e.
6c8ac65d 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
e5360be4 29
30=cut
31
05a05fd1 32# no warnings (the -or/-and => { } warning is silly, there is nothing wrong with such usage)
33my $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
a8b90cb7 41my @and_or_tests = (
05a05fd1 42 # basic tests
a8b90cb7 43 {
44 where => { -and => [a => 1, b => 2] },
05a05fd1 45 %{$and_or_args->{and}},
a8b90cb7 46 },
47 {
48 where => [ -and => [a => 1, b => 2] ],
05a05fd1 49 %{$and_or_args->{and}},
a8b90cb7 50 },
51 {
52 where => { -or => [a => 1, b => 2] },
05a05fd1 53 %{$and_or_args->{or}},
a8b90cb7 54 },
55 {
56 where => [ -or => [a => 1, b => 2] ],
05a05fd1 57 %{$and_or_args->{or}},
58 },
59
6c8ac65d 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
f2a0d52b 77 # test modifiers within hashrefs
05a05fd1 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
f2a0d52b 93 # test modifiers within arrayrefs
05a05fd1 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
6c8ac65d 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)
64385831 154 {
155 where => { x => { '!=' => [ -and => (1 .. 3) ] } },
156 stmt => 'WHERE x != ? AND x != ? AND x != ?',
157 bind => [1..3],
158 },
6c8ac65d 159
160 # the -or should affect only the inner hashref, as we are not in an outer arrayref
05a05fd1 161 {
162 where => { x => {
163 -or => { '!=', 1, '>=', 2 }, -like => 'x%'
164 }},
25e4c693 165 stmt => 'WHERE x LIKE ? AND ( x != ? OR x >= ? )',
eb49170d 166 bind => [qw/x% 1 2/],
05a05fd1 167 },
6c8ac65d 168
169 # the -and should affect the OUTER arrayref, while the internal structures remain intact
05a05fd1 170 {
f2a0d52b 171 where => { x => [
172 -and => [ 1, 2 ], { -like => 'x%' }
05a05fd1 173 ]},
6c8ac65d 174 stmt => 'WHERE (x = ? OR x = ?) AND x LIKE ?',
05a05fd1 175 bind => [qw/1 2 x%/],
a8b90cb7 176 },
6c8ac65d 177
a8b90cb7 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 },
6c8ac65d 183
a8b90cb7 184 {
185 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
106c861e 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/],
a8b90cb7 188 },
e5360be4 189
a8b90cb7 190 {
a8b90cb7 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 },
e5360be4 195
a8b90cb7 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 },
6c8ac65d 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] } } },
106c861e 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/],
6c8ac65d 208 },
209
a8b90cb7 210 {
6c8ac65d 211 # flip logic in arrays except where excplicitly requested otherwise
a8b90cb7 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 },
05a05fd1 217
f2a0d52b 218 # 1st -and is in column mode, thus flips the entire array, whereas the
6c8ac65d 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
05a05fd1 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'} ],
6c8ac65d 249 [ {'!=' => 'toto', '=' => 'koko'} ],
05a05fd1 250 ] },
6c8ac65d 251 stmt => 'WHERE (foo LIKE ? OR foo > ?) AND (foo LIKE ? AND foo < ?) AND (foo LIKE ? OR foo LIKE ?) AND (foo != ? AND foo = ?)',
05a05fd1 252 bind => [qw/foo% moo %bar baz %alpha %beta toto koko/],
253 },
6c8ac65d 254
05a05fd1 255 {
6c8ac65d 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}] ],
05a05fd1 268 stmt => 'WHERE foo = ? AND bar = ? AND baz = ?',
6c8ac65d 269 bind => [1 .. 3],
05a05fd1 270 },
271 {
6c8ac65d 272 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, woz => 4} ] ],
273 stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR woz = ?)',
05a05fd1 274 bind => [1 .. 4],
275 },
276
6c8ac65d 277 # -and has only 1 following element, thus all still ORed
05a05fd1 278 {
6c8ac65d 279 where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] },
05a05fd1 280 stmt => 'WHERE col < ? OR col > ? OR col != ?',
281 bind => [qw/123 456 789/],
282 },
283
6c8ac65d 284 # flipping array logic affects both column value and condition arrays
05a05fd1 285 {
6c8ac65d 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/],
05a05fd1 305 },
a8b90cb7 306);
e5360be4 307
c771453a 308# modN and mod_N were a bad design decision - they went away
309my @invalid_numbered_mods = (
189bb68b 310 {
01a01e57 311 -and => [a => 10, b => 11],
312 -and2 => [ c => 20, d => 21 ],
403e50d1 313 -nest => [ x => 1 ],
314 -nest2 => [ y => 2 ],
01a01e57 315 -or => { m => 7, n => 8 },
316 -or2 => { m => 17, n => 18 },
403e50d1 317 },
189bb68b 318 {
01a01e57 319 -and2 => [a => 10, b => 11],
320 -and_3 => [ c => 20, d => 21 ],
403e50d1 321 -nest2 => [ x => 1 ],
322 -nest_3 => [ y => 2 ],
323 -or2 => { m => 7, n => 8 },
324 -or_3 => { m => 17, n => 18 },
325 },
403e50d1 326);
a8b90cb7 327
01a01e57 328my @nest_tests = (
2f641e1f 329 {
01a01e57 330 where => {a => 1, -nest => [b => 2, c => 3]},
2f641e1f 331 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
332 bind => [qw/2 3 1/],
333 },
334 {
01a01e57 335 where => {a => 1, -nest => {b => 2, c => 3}},
37490959 336 stmt => 'WHERE ( ( b = ? AND c = ? AND a = ? ) )',
2f641e1f 337 bind => [qw/2 3 1/],
338 },
339 {
01a01e57 340 where => {a => 1, -or => {-nest => {b => 2, c => 3}}},
37490959 341 stmt => 'WHERE ( ( b = ? AND c = ? AND a = ? ) )',
2f641e1f 342 bind => [qw/2 3 1/],
343 },
344 {
01a01e57 345 where => {a => 1, -or => {-nest => [b => 2, c => 3]}},
2f641e1f 346 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
347 bind => [qw/2 3 1/],
348 },
349 {
01a01e57 350 where => {a => 1, -nest => {-or => {b => 2, c => 3}}},
106c861e 351 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
352 bind => [qw/2 3 1/],
2f641e1f 353 },
3a8b7db0 354 {
01a01e57 355 where => [a => 1, -nest => {b => 2, c => 3}, -nest => [d => 4, e => 5]],
37490959 356 stmt => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR d = ? OR e = ? ) )',
3a8b7db0 357 bind => [qw/1 2 3 4 5/],
358 },
c771453a 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 },
2f641e1f 383);
384
a8b90cb7 385for my $case (@and_or_tests) {
ef8c0c94 386 TODO: {
387 local $TODO = $case->{todo} if $case->{todo};
388
a8b90cb7 389 my $sql = SQL::Abstract->new ($case->{args} || {});
8fec3fd9 390
771ea2ec 391 my $where_copy = dclone($case->{where});
ce261791 392
97084113 393 warnings_are {
a8b90cb7 394 my ($stmt, @bind) = $sql->where($case->{where});
403e50d1 395 is_same_sql_bind(
396 $stmt,
397 \@bind,
398 $case->{stmt},
399 $case->{bind},
2fadf08e 400 ) || diag_where( $case->{where} );
97084113 401 } [], 'No warnings within and-or tests';
ce261791 402
771ea2ec 403 is_deeply ($case->{where}, $where_copy, 'Where conditions unchanged');
ef8c0c94 404 }
e5360be4 405}
403e50d1 406
01a01e57 407for my $case (@nest_tests) {
2f641e1f 408 TODO: {
409 local $TODO = $case->{todo} if $case->{todo};
410
411 local $SQL::Abstract::Test::parenthesis_significant = 1;
2f641e1f 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},
2fadf08e 421 ) || diag_where ( $case->{where} );
2f641e1f 422 });
423 }
424}
425
c771453a 426for my $case (@invalid_numbered_mods) {
403e50d1 427 my $sql = SQL::Abstract->new ($case->{args} || {});
189bb68b 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');
403e50d1 431}
432
8fec3fd9 433done_testing;