Actually use the descriptions in the test cases
[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 },
4a1f01a3 130
131 {
132 parenthesis_significant => 1,
133 where => { x => { -in => [ 1 .. 3] } },
134 stmt => "WHERE ( x IN (?, ?, ?) )",
135 bind => [ 1 .. 3],
136 test => '-in with an array of scalars',
137 },
138 {
139 parenthesis_significant => 1,
e41c3bdd 140 where => { x => { -in => [] } },
141 stmt => "WHERE ( 0=1 )",
142 bind => [],
143 test => '-in with an empty array',
144 },
145 {
146 parenthesis_significant => 1,
4a1f01a3 147 where => { x => { -in => \'( 1,2,lower(y) )' } },
b9a4fdae 148 stmt => "WHERE ( x IN ( 1,2,lower(y) ) )",
4a1f01a3 149 bind => [],
150 test => '-in with a literal scalarref',
151 },
46dc2f3e 152
153 # note that outer parens are opened even though literal was requested below
4a1f01a3 154 {
155 parenthesis_significant => 1,
156 where => { x => { -in => \['( ( ?,?,lower(y) ) )', 1, 2] } },
46dc2f3e 157 stmt => "WHERE ( x IN ( ?,?,lower(y) ) )",
4a1f01a3 158 bind => [1, 2],
159 test => '-in with a literal arrayrefref',
160 },
e41c3bdd 161 {
162 parenthesis_significant => 1,
163 where => {
171a709f 164 status => { -in => \"(SELECT status_codes\nFROM states)" },
165 },
171a709f 166 stmt => " WHERE ( status IN ( SELECT status_codes FROM states )) ",
167 bind => [],
168 test => '-in multi-line subquery test',
169 },
170 {
171 parenthesis_significant => 1,
172 where => {
e41c3bdd 173 customer => { -in => \[
174 'SELECT cust_id FROM cust WHERE balance > ?',
175 2000,
176 ]},
177 status => { -in => \'SELECT status_codes FROM states' },
178 },
179 stmt => "
180 WHERE ((
181 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
182 AND status IN ( SELECT status_codes FROM states )
183 ))
184 ",
185 bind => [2000],
186 test => '-in POD test',
187 },
46dc2f3e 188
0336eddb 189 {
190 where => { x => { -in => [ \['LOWER(?)', 'A' ], \'LOWER(b)', { -lower => 'c' } ] } },
191 stmt => " WHERE ( x IN ( LOWER(?), LOWER(b), LOWER ? ) )",
192 bind => [qw/A c/],
193 test => '-in with an array of function array refs with args',
194 },
279eb282 195 {
97084113 196 throws => qr/
032dfe20 197 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
198 \Qwhen the -IN operator was given an undef-containing list: \E
199 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
200 \Qversion of SQL::Abstract will emit the logically correct SQL \E
201 \Qinstead of raising this exception)\E
202 /x,
279eb282 203 where => { x => { -in => [ 1, undef ] } },
032dfe20 204 stmt => " WHERE ( x IN ( ? ) OR x IS NULL )",
279eb282 205 bind => [ 1 ],
428975b0 206 test => '-in with undef as an element',
279eb282 207 },
208 {
97084113 209 throws => qr/
032dfe20 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,
279eb282 216 where => { x => { -in => [ 1, undef, 2, 3, undef ] } },
032dfe20 217 stmt => " WHERE ( x IN ( ?, ?, ? ) OR x IS NULL )",
279eb282 218 bind => [ 1, 2, 3 ],
032dfe20 219 test => '-in with multiple undef elements',
279eb282 220 },
cf02fc47 221);
222
cf02fc47 223for my $case (@in_between_tests) {
224 TODO: {
225 local $TODO = $case->{todo} if $case->{todo};
4a1f01a3 226 local $SQL::Abstract::Test::parenthesis_significant = $case->{parenthesis_significant};
df7b1db3 227 my $label = $case->{test} || 'in-between test';
cf02fc47 228
032dfe20 229 my $sql = SQL::Abstract->new ($case->{args} || {});
4d8b3dc4 230
97084113 231 if (my $e = $case->{throws}) {
df7b1db3 232 throws_ok { $sql->where($case->{where}) } $e, "$label throws correctly";
032dfe20 233 }
234 else {
97084113 235 my ($stmt, @bind);
236 warnings_are {
237 ($stmt, @bind) = $sql->where($case->{where});
df7b1db3 238 } [], "$label gives no warnings";
97084113 239
2fadf08e 240 is_same_sql_bind(
241 $stmt,
242 \@bind,
243 $case->{stmt},
244 $case->{bind},
df7b1db3 245 "$label generates correct SQL and bind",
2fadf08e 246 ) || diag_where ( $case->{where} );
032dfe20 247 }
cf02fc47 248 }
249}
10e6c946 250
251done_testing;