Merge branch 'master' into dq
[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{
71       \QOperator 'BETWEEN' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref\E
72         |
73       \QArgument passed to the 'BETWEEN' operator can not be undefined\E
74     }x,,
75   } } (
76     [ 1, 2, 3 ],
77     [ 1, undef, 3 ],
78     [ undef, 2, 3 ],
79     [ 1, 2, undef ],
80     [ 1, undef ],
81     [ undef, 2 ],
82     [ undef, undef ],
83     [ 1 ],
84     [ undef ],
85     [],
86     1,
87     undef,
88   )),
89   {
90     where => {
91       start0 => { -between => [ 1, { -upper => 2 } ] },
92       start1 => { -between => \["? AND ?", 1, 2] },
93       start2 => { -between => \"lower(x) AND upper(y)" },
94       start3 => { -between => [
95         \"lower(x)",
96         \["upper(?)", 'stuff' ],
97       ] },
98     },
99     stmt => "WHERE (
100           ( start0 BETWEEN ? AND UPPER(?)         )
101       AND ( start1 BETWEEN ? AND ?                )
102       AND ( start2 BETWEEN lower(x) AND upper(y)  )
103       AND ( start3 BETWEEN lower(x) AND upper(?)  )
104     )",
105     bind => [1, 2, 1, 2, 'stuff'],
106     test => '-between POD test',
107   },
108   {
109     args => { bindtype => 'columns' },
110     where => {
111       start0 => { -between => [ 1, { -upper => 2 } ] },
112       start1 => { -between => \["? AND ?", [ start1 => 1], [start1 => 2] ] },
113       start2 => { -between => \"lower(x) AND upper(y)" },
114       start3 => { -between => [
115         \"lower(x)",
116         \["upper(?)", [ start3 => 'stuff'] ],
117       ] },
118     },
119     stmt => "WHERE (
120           ( start0 BETWEEN ? AND UPPER(?)         )
121       AND ( start1 BETWEEN ? AND ?                )
122       AND ( start2 BETWEEN lower(x) AND upper(y)  )
123       AND ( start3 BETWEEN lower(x) AND upper(?)  )
124     )",
125     bind => [
126       [ start0 => 1 ],
127       [ start0 => 2 ],
128       [ start1 => 1 ],
129       [ start1 => 2 ],
130       [ start3 => 'stuff' ],
131     ],
132     test => '-between POD test',
133   },
134   {
135     where => { 'test1.a' => { 'In', ['boom', 'bang'] } },
136     stmt => ' WHERE ( test1.a IN ( ?, ? ) )',
137     bind => ['boom', 'bang'],
138     test => 'In (no dash, initial cap) with qualified column',
139   },
140   {
141     where => { a => { 'between', ['boom', 'bang'] } },
142     stmt => ' WHERE ( a BETWEEN ? AND ? )',
143     bind => ['boom', 'bang'],
144     test => 'between (no dash) with two placeholders',
145   },
146
147   {
148     where => { x => { -in => [ 1 .. 3] } },
149     stmt => "WHERE x IN (?, ?, ?)",
150     bind => [ 1 .. 3 ],
151     test => '-in with an array of scalars',
152   },
153   {
154     where => { x => { -in => [] } },
155     stmt => "WHERE 0=1",
156     bind => [],
157     test => '-in with an empty array',
158   },
159   {
160     where => { x => { -in => \'( 1,2,lower(y) )' } },
161     stmt => "WHERE x IN ( 1,2,lower(y) )",
162     bind => [],
163     test => '-in with a literal scalarref',
164   },
165
166   # note that outer parens are opened even though literal was requested below
167   {
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     where => {
175       status => { -in => \"(SELECT status_codes\nFROM states)" },
176     },
177     stmt => " WHERE status IN ( SELECT status_codes FROM states )",
178     bind => [],
179     test => '-in multi-line subquery test',
180   },
181   {
182     where => {
183       customer => { -in => \[
184         'SELECT cust_id FROM cust WHERE balance > ?',
185         2000,
186       ]},
187       status => { -in => \'SELECT status_codes FROM states' },
188     },
189     stmt => "
190       WHERE
191             customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
192         AND status IN ( SELECT status_codes FROM states )
193     ",
194     bind => [2000],
195     test => '-in POD test',
196   },
197
198   {
199     where => { x => { -in => [ \['LOWER(?)', 'A' ], \'LOWER(b)', { -lower => 'c' } ] } },
200     stmt => " WHERE ( x IN ( LOWER(?), LOWER(b), LOWER(?) ) )",
201     bind => [qw/A c/],
202     test => '-in with an array of function array refs with args',
203   },
204   {
205     where => { x => { -in => [ 1, undef ] } },
206     stmt => " WHERE ( x IN ( ? ) OR x IS NULL )",
207     bind => [ 1 ],
208     test => '-in with undef as an element',
209   },
210   {
211     where => { x => { -in => [ 1, undef, 2, 3, undef ] } },
212     stmt => " WHERE ( x IN ( ?, ?, ? ) OR x IS NULL )",
213     bind => [ 1, 2, 3 ],
214     test => '-in with multiple undef elements',
215   },
216   {
217     where => { a => { -in => 42 }, b => { -not_in => 42 } },
218     stmt => ' WHERE a IN ( ? ) AND b NOT IN ( ? )',
219     bind => [ 42, 42 ],
220     test => '-in, -not_in with scalar',
221   },
222   {
223     where => { a => { -in => [] }, b => { -not_in => [] } },
224     stmt => ' WHERE ( 0=1 AND 1=1 )',
225     bind => [],
226     test => '-in, -not_in with empty arrays',
227   },
228   {
229     where => { a => { -in => [42, undef] }, b => { -not_in => [42, undef] } },
230     stmt => ' WHERE ( ( a IN ( ? ) OR a IS NULL ) AND b NOT IN ( ? ) AND b IS NOT NULL )',
231     bind => [ 42, 42 ],
232     test => '-in, -not_in with undef among elements',
233   },
234   {
235     where => { a => { -in => [undef] }, b => { -not_in => [undef] } },
236     stmt => ' WHERE ( a IS NULL AND b IS NOT NULL )',
237     bind => [],
238     test => '-in, -not_in with just undef element',
239   },
240   {
241     where => { a => { -in => undef } },
242     throws => qr/Argument passed to the 'IN' operator can not be undefined/,
243     test => '-in with undef argument',
244   },
245 );
246
247 for my $case (@in_between_tests) {
248   TODO: {
249     local $TODO = $case->{todo} if $case->{todo};
250     local $SQL::Abstract::Test::parenthesis_significant = $case->{parenthesis_significant};
251     my $label = $case->{test} || 'in-between test';
252
253     my $sql = SQL::Abstract->new ($case->{args} || {});
254
255     if (my $e = $case->{throws}) {
256       my $stmt;
257       throws_ok { ($stmt) = $sql->where($case->{where}) } $e, "$label throws correctly"
258         or diag dumper ({ where => $case->{where}, result => $stmt });
259     }
260     else {
261       my ($stmt, @bind);
262       warnings_are {
263         ($stmt, @bind) = $sql->where($case->{where});
264       } [], "$label gives no warnings";
265
266       is_same_sql_bind(
267         $stmt,
268         \@bind,
269         $case->{stmt},
270         $case->{bind},
271         "$label generates correct SQL and bind",
272       ) || diag_where ( $case->{where} );
273     }
274   }
275 }
276
277 done_testing;