Croak on invalid top-level special ops
[dbsrgits/SQL-Abstract.git] / t / 05in_between.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Warn;
5 use Test::Exception;
6 use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where dumper)];
7
8 use SQL::Abstract;
9
10 my @in_between_tests = (
11   {
12     where => { x => { -between => [1, 2] } },
13     stmt => 'WHERE (x BETWEEN ? AND ?)',
14     bind => [qw/1 2/],
15     test => '-between with two placeholders',
16   },
17   {
18     where => { x => { -between => [\"1", 2] } },
19     stmt => 'WHERE (x BETWEEN 1 AND ?)',
20     bind => [qw/2/],
21     test => '-between with one literal sql arg and one placeholder',
22   },
23   {
24     where => { x => { -between => [1, \"2"] } },
25     stmt => 'WHERE (x BETWEEN ? AND 2)',
26     bind => [qw/1/],
27     test => '-between with one placeholder and one literal sql arg',
28   },
29   {
30     where => { x => { -between => [\'current_date - 1', \'current_date - 0'] } },
31     stmt => 'WHERE (x BETWEEN current_date - 1 AND current_date - 0)',
32     bind => [],
33     test => '-between with two literal sql arguments',
34   },
35   {
36     where => { x => { -between => [ \['current_date - ?', 1], \['current_date - ?', 0] ] } },
37     stmt => 'WHERE (x BETWEEN current_date - ? AND current_date - ?)',
38     bind => [1, 0],
39     test => '-between with two literal sql arguments with bind',
40   },
41   {
42     where => { x => { -between => \['? AND ?', 1, 2] } },
43     stmt => 'WHERE (x BETWEEN ? AND ?)',
44     bind => [1,2],
45     test => '-between with literal sql with placeholders (\["? AND ?", scalar, scalar])',
46   },
47   {
48     where => { x => { -between => \["'something' AND ?", 2] } },
49     stmt => "WHERE (x BETWEEN 'something' AND ?)",
50     bind => [2],
51     test => '-between with literal sql with one literal arg and one placeholder (\["\'something\' AND ?", scalar])',
52   },
53   {
54     where => { x => { -between => \["? AND 'something'", 1] } },
55     stmt => "WHERE (x BETWEEN ? AND 'something')",
56     bind => [1],
57     test => '-between with literal sql with one placeholder and one literal arg (\["? AND \'something\'", scalar])',
58   },
59   {
60     where => { x => { -between => \"'this' AND 'that'" } },
61     stmt => "WHERE (x BETWEEN 'this' AND 'that')",
62     bind => [],
63     test => '-between with literal sql with a literal (\"\'this\' AND \'that\'")',
64   },
65
66   # generate a set of invalid -between tests
67   ( map { {
68     where => { x => { -between => $_ } },
69     test => 'invalid -between args',
70     throws => qr|Operator 'BETWEEN' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref|,
71   } } (
72     [ 1, 2, 3 ],
73     [ 1, undef, 3 ],
74     [ undef, 2, 3 ],
75     [ 1, 2, undef ],
76     [ 1, undef ],
77     [ undef, 2 ],
78     [ undef, undef ],
79     [ 1 ],
80     [ undef ],
81     [],
82     1,
83     undef,
84   )),
85   {
86     where => {
87       start0 => { -between => [ 1, { -upper => 2 } ] },
88       start1 => { -between => \["? AND ?", 1, 2] },
89       start2 => { -between => \"lower(x) AND upper(y)" },
90       start3 => { -between => [
91         \"lower(x)",
92         \["upper(?)", 'stuff' ],
93       ] },
94     },
95     stmt => "WHERE (
96           ( start0 BETWEEN ? AND UPPER ?          )
97       AND ( start1 BETWEEN ? AND ?                )
98       AND ( start2 BETWEEN lower(x) AND upper(y)  )
99       AND ( start3 BETWEEN lower(x) AND upper(?)  )
100     )",
101     bind => [1, 2, 1, 2, 'stuff'],
102     test => '-between POD test',
103   },
104   {
105     args => { bindtype => 'columns' },
106     where => {
107       start0 => { -between => [ 1, { -upper => 2 } ] },
108       start1 => { -between => \["? AND ?", [ start1 => 1], [start1 => 2] ] },
109       start2 => { -between => \"lower(x) AND upper(y)" },
110       start3 => { -between => [
111         \"lower(x)",
112         \["upper(?)", [ start3 => 'stuff'] ],
113       ] },
114     },
115     stmt => "WHERE (
116           ( start0 BETWEEN ? AND UPPER ?          )
117       AND ( start1 BETWEEN ? AND ?                )
118       AND ( start2 BETWEEN lower(x) AND upper(y)  )
119       AND ( start3 BETWEEN lower(x) AND upper(?)  )
120     )",
121     bind => [
122       [ start0 => 1 ],
123       [ start0 => 2 ],
124       [ start1 => 1 ],
125       [ start1 => 2 ],
126       [ start3 => 'stuff' ],
127     ],
128     test => '-between POD test',
129   },
130   {
131     where => { 'test1.a' => { 'In', ['boom', 'bang'] } },
132     stmt => ' WHERE ( test1.a IN ( ?, ? ) )',
133     bind => ['boom', 'bang'],
134     test => 'In (no dash, initial cap) with qualified column',
135   },
136   {
137     where => { a => { 'between', ['boom', 'bang'] } },
138     stmt => ' WHERE ( a BETWEEN ? AND ? )',
139     bind => ['boom', 'bang'],
140     test => 'between (no dash) with two placeholders',
141   },
142
143   {
144     where => { x => { -in => [ 1 .. 3] } },
145     stmt => "WHERE x IN (?, ?, ?)",
146     bind => [ 1 .. 3 ],
147     test => '-in with an array of scalars',
148   },
149   {
150     where => { x => { -in => [] } },
151     stmt => "WHERE 0=1",
152     bind => [],
153     test => '-in with an empty array',
154   },
155   {
156     where => { x => { -in => \'( 1,2,lower(y) )' } },
157     stmt => "WHERE x IN ( 1,2,lower(y) )",
158     bind => [],
159     test => '-in with a literal scalarref',
160   },
161
162   # note that outer parens are opened even though literal was requested below
163   {
164     where => { x => { -in => \['( ( ?,?,lower(y) ) )', 1, 2] } },
165     stmt => "WHERE x IN ( ?,?,lower(y) )",
166     bind => [1, 2],
167     test => '-in with a literal arrayrefref',
168   },
169   {
170     where => {
171       status => { -in => \"(SELECT status_codes\nFROM states)" },
172     },
173     stmt => " WHERE status IN ( SELECT status_codes FROM states )",
174     bind => [],
175     test => '-in multi-line subquery test',
176   },
177   {
178     where => {
179       customer => { -in => \[
180         'SELECT cust_id FROM cust WHERE balance > ?',
181         2000,
182       ]},
183       status => { -in => \'SELECT status_codes FROM states' },
184     },
185     stmt => "
186       WHERE
187             customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
188         AND status IN ( SELECT status_codes FROM states )
189     ",
190     bind => [2000],
191     test => '-in POD test',
192   },
193
194   {
195     where => { x => { -in => [ \['LOWER(?)', 'A' ], \'LOWER(b)', { -lower => 'c' } ] } },
196     stmt => " WHERE ( x IN ( LOWER(?), LOWER(b), LOWER ? ) )",
197     bind => [qw/A c/],
198     test => '-in with an array of function array refs with args',
199   },
200   {
201     throws => qr/
202       \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
203       \Qwhen the -IN operator was given an undef-containing list: \E
204       \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
205       \Qversion of SQL::Abstract will emit the logically correct SQL \E
206       \Qinstead of raising this exception)\E
207     /x,
208     where => { x => { -in => [ 1, undef ] } },
209     stmt => " WHERE ( x IN ( ? ) OR x IS NULL )",
210     bind => [ 1 ],
211     test => '-in with undef as an element',
212   },
213   {
214     throws => qr/
215       \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
216       \Qwhen the -IN operator was given an undef-containing list: \E
217       \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
218       \Qversion of SQL::Abstract will emit the logically correct SQL \E
219       \Qinstead of raising this exception)\E
220     /x,
221     where => { x => { -in => [ 1, undef, 2, 3, undef ] } },
222     stmt => " WHERE ( x IN ( ?, ?, ? ) OR x IS NULL )",
223     bind => [ 1, 2, 3 ],
224     test => '-in with multiple undef elements',
225   },
226   {
227     where => { a => { -in => 42 }, b => { -not_in => 42 } },
228     stmt => ' WHERE a IN ( ? ) AND b NOT IN ( ? )',
229     bind => [ 42, 42 ],
230     test => '-in, -not_in with scalar',
231   },
232   {
233     where => { a => { -in => [] }, b => { -not_in => [] } },
234     stmt => ' WHERE ( 0=1 AND 1=1 )',
235     bind => [],
236     test => '-in, -not_in with empty arrays',
237   },
238   {
239     throws => qr/
240       \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
241       \Qwhen the -IN operator was given an undef-containing list: \E
242       \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
243       \Qversion of SQL::Abstract will emit the logically correct SQL \E
244       \Qinstead of raising this exception)\E
245     /x,
246     where => { a => { -in => [42, undef] }, b => { -not_in => [42, undef] } },
247     stmt => ' WHERE ( ( a IN ( ? ) OR a IS NULL ) AND b NOT IN ( ? ) AND b IS NOT NULL )',
248     bind => [ 42, 42 ],
249     test => '-in, -not_in with undef among elements',
250   },
251   {
252     throws => qr/
253       \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
254       \Qwhen the -IN operator was given an undef-containing list: \E
255       \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
256       \Qversion of SQL::Abstract will emit the logically correct SQL \E
257       \Qinstead of raising this exception)\E
258     /x,
259     where => { a => { -in => [undef] }, b => { -not_in => [undef] } },
260     stmt => ' WHERE ( a IS NULL AND b IS NOT NULL )',
261     bind => [],
262     test => '-in, -not_in with just undef element',
263   },
264   {
265     where => { a => { -in => undef } },
266     throws => qr/Argument passed to the 'IN' operator can not be undefined/,
267     test => '-in with undef argument',
268   },
269
270   {
271     where => { -in => [42] },
272     throws => qr/Illegal use of top-level '-in'/,
273     test => 'Top level -in',
274   },
275   {
276     where => { -between => [42, 69] },
277     throws => qr/Illegal use of top-level '-between'/,
278     test => 'Top level -between',
279   },
280 );
281
282 for my $case (@in_between_tests) {
283   TODO: {
284     local $TODO = $case->{todo} if $case->{todo};
285     local $SQL::Abstract::Test::parenthesis_significant = $case->{parenthesis_significant};
286     my $label = $case->{test} || 'in-between test';
287
288     my $sql = SQL::Abstract->new ($case->{args} || {});
289
290     if (my $e = $case->{throws}) {
291       my $stmt;
292       throws_ok { ($stmt) = $sql->where($case->{where}) } $e, "$label throws correctly"
293         or diag dumper ({ where => $case->{where}, result => $stmt });
294     }
295     else {
296       my ($stmt, @bind);
297       warnings_are {
298         ($stmt, @bind) = $sql->where($case->{where});
299       } [], "$label gives no warnings";
300
301       is_same_sql_bind(
302         $stmt,
303         \@bind,
304         $case->{stmt},
305         $case->{bind},
306         "$label generates correct SQL and bind",
307       ) || diag_where ( $case->{where} );
308     }
309   }
310 }
311
312 done_testing;