Move simple IN/BETWEEN tests to t/05in_between.t
[dbsrgits/SQL-Abstract.git] / t / 05in_between.t
CommitLineData
cf02fc47 1use strict;
2use warnings;
3use Test::More;
97084113 4use Test::Warn;
cf02fc47 5use Test::Exception;
2fadf08e 6use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where)];
cf02fc47 7
cf02fc47 8use SQL::Abstract;
9
cf02fc47 10my @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 {
4d8b3dc4 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 {
cf02fc47 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 {
4d8b3dc4 60 where => { x => { -between => \"'this' AND 'that'" } },
cf02fc47 61 stmt => "WHERE (x BETWEEN 'this' AND 'that')",
62 bind => [],
4d8b3dc4 63 test => '-between with literal sql with a literal (\"\'this\' AND \'that\'")',
cf02fc47 64 },
7f54040f 65
66 # generate a set of invalid -between tests
67 ( map { {
68 where => { x => { -between => $_ } },
69 test => 'invalid -between args',
97084113 70 throws => qr|Operator 'BETWEEN' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref|,
7f54040f 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 )),
e41c3bdd 85 {
86 where => {
0336eddb 87 start0 => { -between => [ 1, { -upper => 2 } ] },
e41c3bdd 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 (
b3b79607 96 ( start0 BETWEEN ? AND UPPER ? )
e41c3bdd 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 },
5e5cbf51 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 },
904c3621 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 },
4a1f01a3 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,
e41c3bdd 152 where => { x => { -in => [] } },
153 stmt => "WHERE ( 0=1 )",
154 bind => [],
155 test => '-in with an empty array',
156 },
157 {
158 parenthesis_significant => 1,
4a1f01a3 159 where => { x => { -in => \'( 1,2,lower(y) )' } },
b9a4fdae 160 stmt => "WHERE ( x IN ( 1,2,lower(y) ) )",
4a1f01a3 161 bind => [],
162 test => '-in with a literal scalarref',
163 },
46dc2f3e 164
165 # note that outer parens are opened even though literal was requested below
4a1f01a3 166 {
167 parenthesis_significant => 1,
168 where => { x => { -in => \['( ( ?,?,lower(y) ) )', 1, 2] } },
46dc2f3e 169 stmt => "WHERE ( x IN ( ?,?,lower(y) ) )",
4a1f01a3 170 bind => [1, 2],
171 test => '-in with a literal arrayrefref',
172 },
e41c3bdd 173 {
174 parenthesis_significant => 1,
175 where => {
171a709f 176 status => { -in => \"(SELECT status_codes\nFROM states)" },
177 },
171a709f 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 => {
e41c3bdd 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 },
46dc2f3e 200
0336eddb 201 {
202 where => { x => { -in => [ \['LOWER(?)', 'A' ], \'LOWER(b)', { -lower => 'c' } ] } },
203 stmt => " WHERE ( x IN ( LOWER(?), LOWER(b), LOWER ? ) )",
204 bind => [qw/A c/],
205 test => '-in with an array of function array refs with args',
206 },
279eb282 207 {
97084113 208 throws => qr/
032dfe20 209 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
210 \Qwhen the -IN operator was given an undef-containing list: \E
211 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
212 \Qversion of SQL::Abstract will emit the logically correct SQL \E
213 \Qinstead of raising this exception)\E
214 /x,
279eb282 215 where => { x => { -in => [ 1, undef ] } },
032dfe20 216 stmt => " WHERE ( x IN ( ? ) OR x IS NULL )",
279eb282 217 bind => [ 1 ],
428975b0 218 test => '-in with undef as an element',
279eb282 219 },
220 {
97084113 221 throws => qr/
032dfe20 222 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
223 \Qwhen the -IN operator was given an undef-containing list: \E
224 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
225 \Qversion of SQL::Abstract will emit the logically correct SQL \E
226 \Qinstead of raising this exception)\E
227 /x,
279eb282 228 where => { x => { -in => [ 1, undef, 2, 3, undef ] } },
032dfe20 229 stmt => " WHERE ( x IN ( ?, ?, ? ) OR x IS NULL )",
279eb282 230 bind => [ 1, 2, 3 ],
032dfe20 231 test => '-in with multiple undef elements',
279eb282 232 },
904c3621 233 {
234 where => { a => { -in => 42 }, b => { -not_in => 42 } },
235 stmt => ' WHERE a IN ( ? ) AND b NOT IN ( ? )',
236 bind => [ 42, 42 ],
237 test => '-in, -not_in with scalar',
238 },
239 {
240 where => { a => { -in => [] }, b => { -not_in => [] } },
241 stmt => ' WHERE ( 0=1 AND 1=1 )',
242 bind => [],
243 test => '-in, -not_in with empty arrays',
244 },
245 {
246 throws => qr/
247 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
248 \Qwhen the -IN operator was given an undef-containing list: \E
249 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
250 \Qversion of SQL::Abstract will emit the logically correct SQL \E
251 \Qinstead of raising this exception)\E
252 /x,
253 where => { a => { -in => [42, undef] }, b => { -not_in => [42, undef] } },
254 stmt => ' WHERE ( ( a IN ( ? ) OR a IS NULL ) AND b NOT IN ( ? ) AND b IS NOT NULL )',
255 bind => [ 42, 42 ],
256 test => '-in, -not_in with undef among elements',
257 },
258 {
259 throws => qr/
260 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
261 \Qwhen the -IN operator was given an undef-containing list: \E
262 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
263 \Qversion of SQL::Abstract will emit the logically correct SQL \E
264 \Qinstead of raising this exception)\E
265 /x,
266 where => { a => { -in => [undef] }, b => { -not_in => [undef] } },
267 stmt => ' WHERE ( a IS NULL AND b IS NOT NULL )',
268 bind => [],
269 test => '-in, -not_in with just undef element',
270 },
271 {
272 where => { a => { -in => undef } },
273 throws => qr/Argument passed to the 'IN' operator can not be undefined/,
274 test => '-in with undef argument',
275 },
cf02fc47 276);
277
cf02fc47 278for my $case (@in_between_tests) {
279 TODO: {
280 local $TODO = $case->{todo} if $case->{todo};
4a1f01a3 281 local $SQL::Abstract::Test::parenthesis_significant = $case->{parenthesis_significant};
df7b1db3 282 my $label = $case->{test} || 'in-between test';
cf02fc47 283
032dfe20 284 my $sql = SQL::Abstract->new ($case->{args} || {});
4d8b3dc4 285
97084113 286 if (my $e = $case->{throws}) {
df7b1db3 287 throws_ok { $sql->where($case->{where}) } $e, "$label throws correctly";
032dfe20 288 }
289 else {
97084113 290 my ($stmt, @bind);
291 warnings_are {
292 ($stmt, @bind) = $sql->where($case->{where});
df7b1db3 293 } [], "$label gives no warnings";
97084113 294
2fadf08e 295 is_same_sql_bind(
296 $stmt,
297 \@bind,
298 $case->{stmt},
299 $case->{bind},
df7b1db3 300 "$label generates correct SQL and bind",
2fadf08e 301 ) || diag_where ( $case->{where} );
032dfe20 302 }
cf02fc47 303 }
304}
10e6c946 305
306done_testing;