d3ede2f44335e189c855889c80d354f9a9499a9d
[scpubgit/Q-Branch.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)];
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     parenthesis_significant => 1,
145     where => { x => { -in => [ 1 .. 3] } },
146     stmt => "WHERE ( x IN (?, ?, ?) )",
147     bind => [ 1 .. 3],
148     test => '-in with an array of scalars',
149   },
150   {
151     parenthesis_significant => 1,
152     where => { x => { -in => [] } },
153     stmt => "WHERE ( 0=1 )",
154     bind => [],
155     test => '-in with an empty array',
156   },
157   {
158     parenthesis_significant => 1,
159     where => { x => { -in => \'( 1,2,lower(y) )' } },
160     stmt => "WHERE ( x IN ( 1,2,lower(y) ) )",
161     bind => [],
162     test => '-in with a literal scalarref',
163   },
164
165   # note that outer parens are opened even though literal was requested below
166   {
167     parenthesis_significant => 1,
168     where => { x => { -in => \['( ( ?,?,lower(y) ) )', 1, 2] } },
169     stmt => "WHERE ( x IN ( ?,?,lower(y) ) )",
170     bind => [1, 2],
171     test => '-in with a literal arrayrefref',
172   },
173   {
174     parenthesis_significant => 1,
175     where => {
176       status => { -in => \"(SELECT status_codes\nFROM states)" },
177     },
178     stmt => " WHERE ( status IN ( SELECT status_codes FROM states )) ",
179     bind => [],
180     test => '-in multi-line subquery test',
181   },
182   {
183     parenthesis_significant => 1,
184     where => {
185       customer => { -in => \[
186         'SELECT cust_id FROM cust WHERE balance > ?',
187         2000,
188       ]},
189       status => { -in => \'SELECT status_codes FROM states' },
190     },
191     stmt => "
192       WHERE ((
193             customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
194         AND status IN ( SELECT status_codes FROM states )
195       ))
196     ",
197     bind => [2000],
198     test => '-in POD test',
199   },
200
201   {
202     parenthesis_significant => 1,
203     where => { x => { -in => [ \['LOWER(?)', 'A' ], \'LOWER(b)', { -lower => 'c' } ] } },
204     stmt => " WHERE ( x IN ( LOWER(?), LOWER(b), LOWER ? ) )",
205     bind => [qw/A c/],
206     test => '-in with an array of function array refs with args',
207   },
208   {
209     throws => qr/
210       \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
211       \Qwhen the -IN operator was given an undef-containing list: \E
212       \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
213       \Qversion of SQL::Abstract will emit the logically correct SQL \E
214       \Qinstead of raising this exception)\E
215     /x,
216     parenthesis_significant => 1,
217     where => { x => { -in => [ 1, undef ] } },
218     stmt => " WHERE ( x IN ( ? ) OR x IS NULL )",
219     bind => [ 1 ],
220     test => '-in with undef as an element',
221   },
222   {
223     throws => qr/
224       \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
225       \Qwhen the -IN operator was given an undef-containing list: \E
226       \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
227       \Qversion of SQL::Abstract will emit the logically correct SQL \E
228       \Qinstead of raising this exception)\E
229     /x,
230     parenthesis_significant => 1,
231     where => { x => { -in => [ 1, undef, 2, 3, undef ] } },
232     stmt => " WHERE ( x IN ( ?, ?, ? ) OR x IS NULL )",
233     bind => [ 1, 2, 3 ],
234     test => '-in with multiple undef elements',
235   },
236   {
237     parenthesis_significant => 1,
238     where => { a => { -in => 42 }, b => { -not_in => 42 } },
239     stmt => ' WHERE ( ( a IN ( ? ) AND b NOT IN ( ? ) ) )',
240     bind => [ 42, 42 ],
241     test => '-in, -not_in with scalar',
242   },
243   {
244     where => { a => { -in => [] }, b => { -not_in => [] } },
245     stmt => ' WHERE ( 0=1 AND 1=1 )',
246     bind => [],
247     test => '-in, -not_in with empty arrays',
248   },
249   {
250     throws => qr/
251       \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
252       \Qwhen the -IN operator was given an undef-containing list: \E
253       \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
254       \Qversion of SQL::Abstract will emit the logically correct SQL \E
255       \Qinstead of raising this exception)\E
256     /x,
257     parenthesis_significant => 1,
258     where => { a => { -in => [42, undef] }, b => { -not_in => [42, undef] } },
259     stmt => ' WHERE ( ( a IN ( ? ) OR a IS NULL ) AND b NOT IN ( ? ) AND b IS NOT NULL )',
260     bind => [ 42, 42 ],
261     test => '-in, -not_in with undef among elements',
262   },
263   {
264     throws => qr/
265       \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
266       \Qwhen the -IN operator was given an undef-containing list: \E
267       \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
268       \Qversion of SQL::Abstract will emit the logically correct SQL \E
269       \Qinstead of raising this exception)\E
270     /x,
271     where => { a => { -in => [undef] }, b => { -not_in => [undef] } },
272     stmt => ' WHERE ( a IS NULL AND b IS NOT NULL )',
273     bind => [],
274     test => '-in, -not_in with just undef element',
275   },
276   {
277     where => { a => { -in => undef } },
278     throws => qr/Argument passed to the 'IN' operator can not be undefined/,
279     test => '-in with undef argument',
280   },
281 );
282
283 for my $case (@in_between_tests) {
284   TODO: {
285     local $TODO = $case->{todo} if $case->{todo};
286     local $SQL::Abstract::Test::parenthesis_significant = $case->{parenthesis_significant};
287     my $label = $case->{test} || 'in-between test';
288
289     my $sql = SQL::Abstract->new ($case->{args} || {});
290
291     if (my $e = $case->{throws}) {
292       throws_ok { $sql->where($case->{where}) } $e, "$label throws correctly";
293     }
294     else {
295       my ($stmt, @bind);
296       warnings_are {
297         ($stmt, @bind) = $sql->where($case->{where});
298       } [], "$label gives no warnings";
299
300       is_same_sql_bind(
301         $stmt,
302         \@bind,
303         $case->{stmt},
304         $case->{bind},
305         "$label generates correct SQL and bind",
306       ) || diag_where ( $case->{where} );
307     }
308   }
309 }
310
311 done_testing;