Commit | Line | Data |
41751122 |
1 | #!/usr/bin/perl |
32eab2da |
2 | |
3 | use strict; |
41751122 |
4 | use warnings; |
5 | use Test::More; |
8a68b5be |
6 | use Test::Exception; |
5aad8cf3 |
7 | use SQL::Abstract::Test import => ['is_same_sql_bind']; |
fffe6900 |
8 | |
f7c0b413 |
9 | use Data::Dumper; |
32eab2da |
10 | use SQL::Abstract; |
11 | |
12 | # Make sure to test the examples, since having them break is somewhat |
13 | # embarrassing. :-( |
14 | |
5ebfe5f2 |
15 | my $not_stringifiable = bless {}, 'SQLA::NotStringifiable'; |
ba566dc3 |
16 | |
c59982bb |
17 | my @handle_tests = ( |
32eab2da |
18 | { |
19 | where => { |
20 | requestor => 'inna', |
21 | worker => ['nwiger', 'rcwe', 'sfz'], |
22 | status => { '!=', 'completed' } |
23 | }, |
24 | order => [], |
25 | stmt => " WHERE ( requestor = ? AND status != ? AND ( ( worker = ? ) OR" |
26 | . " ( worker = ? ) OR ( worker = ? ) ) )", |
27 | bind => [qw/inna completed nwiger rcwe sfz/], |
28 | }, |
29 | |
30 | { |
73a67e2c |
31 | where => [ |
32 | status => 'completed', |
33 | user => 'nwiger', |
34 | ], |
35 | stmt => " WHERE ( status = ? OR user = ? )", |
36 | bind => [qw/completed nwiger/], |
37 | }, |
38 | |
39 | { |
32eab2da |
40 | where => { |
41 | user => 'nwiger', |
42 | status => 'completed' |
43 | }, |
44 | order => [qw/ticket/], |
45 | stmt => " WHERE ( status = ? AND user = ? ) ORDER BY ticket", |
46 | bind => [qw/completed nwiger/], |
47 | }, |
48 | |
49 | { |
50 | where => { |
51 | user => 'nwiger', |
52 | status => { '!=', 'completed' } |
53 | }, |
54 | order => [qw/ticket/], |
55 | stmt => " WHERE ( status != ? AND user = ? ) ORDER BY ticket", |
56 | bind => [qw/completed nwiger/], |
57 | }, |
58 | |
59 | { |
60 | where => { |
61 | status => 'completed', |
62 | reportid => { 'in', [567, 2335, 2] } |
63 | }, |
64 | order => [], |
65 | stmt => " WHERE ( reportid IN ( ?, ?, ? ) AND status = ? )", |
66 | bind => [qw/567 2335 2 completed/], |
67 | }, |
68 | |
69 | { |
70 | where => { |
71 | status => 'completed', |
72 | reportid => { 'not in', [567, 2335, 2] } |
73 | }, |
74 | order => [], |
75 | stmt => " WHERE ( reportid NOT IN ( ?, ?, ? ) AND status = ? )", |
76 | bind => [qw/567 2335 2 completed/], |
77 | }, |
78 | |
79 | { |
80 | where => { |
81 | status => 'completed', |
82 | completion_date => { 'between', ['2002-10-01', '2003-02-06'] }, |
83 | }, |
84 | order => \'ticket, requestor', |
96449e8e |
85 | #LDNOTE: modified parentheses |
86 | # stmt => " WHERE ( completion_date BETWEEN ? AND ? AND status = ? ) ORDER BY ticket, requestor", |
87 | stmt => " WHERE ( ( completion_date BETWEEN ? AND ? ) AND status = ? ) ORDER BY ticket, requestor", |
32eab2da |
88 | bind => [qw/2002-10-01 2003-02-06 completed/], |
89 | }, |
90 | |
91 | { |
92 | where => [ |
93 | { |
94 | user => 'nwiger', |
95 | status => { 'in', ['pending', 'dispatched'] }, |
96 | }, |
97 | { |
98 | user => 'robot', |
99 | status => 'unassigned', |
100 | }, |
101 | ], |
102 | order => [], |
103 | stmt => " WHERE ( ( status IN ( ?, ? ) AND user = ? ) OR ( status = ? AND user = ? ) )", |
104 | bind => [qw/pending dispatched nwiger unassigned robot/], |
105 | }, |
106 | |
107 | { |
108 | where => { |
109 | priority => [ {'>', 3}, {'<', 1} ], |
110 | requestor => \'is not null', |
111 | }, |
112 | order => 'priority', |
113 | stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor is not null ) ORDER BY priority", |
114 | bind => [qw/3 1/], |
115 | }, |
116 | |
117 | { |
118 | where => { |
119 | priority => [ {'>', 3}, {'<', 1} ], |
120 | requestor => { '!=', undef }, |
121 | }, |
122 | order => [qw/a b c d e f g/], |
123 | stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor IS NOT NULL )" |
124 | . " ORDER BY a, b, c, d, e, f, g", |
125 | bind => [qw/3 1/], |
126 | }, |
127 | |
128 | { |
129 | where => { |
130 | priority => { 'between', [1, 3] }, |
131 | requestor => { 'like', undef }, |
132 | }, |
133 | order => \'requestor, ticket', |
96449e8e |
134 | #LDNOTE: modified parentheses |
135 | # stmt => " WHERE ( priority BETWEEN ? AND ? AND requestor IS NULL ) ORDER BY requestor, ticket", |
136 | stmt => " WHERE ( ( priority BETWEEN ? AND ? ) AND requestor IS NULL ) ORDER BY requestor, ticket", |
32eab2da |
137 | bind => [qw/1 3/], |
138 | }, |
139 | |
140 | |
141 | { |
142 | where => { |
143 | id => 1, |
144 | num => { |
145 | '<=' => 20, |
146 | '>' => 10, |
147 | }, |
148 | }, |
96449e8e |
149 | # LDNOTE : modified test below, just parentheses differ |
150 | # stmt => " WHERE ( id = ? AND num <= ? AND num > ? )", |
151 | stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )", |
32eab2da |
152 | bind => [qw/1 20 10/], |
153 | }, |
154 | |
155 | { |
156 | where => { foo => {-not_like => [7,8,9]}, |
157 | fum => {'like' => [qw/a b/]}, |
158 | nix => {'between' => [100,200] }, |
159 | nox => {'not between' => [150,160] }, |
160 | wix => {'in' => [qw/zz yy/]}, |
161 | wux => {'not_in' => [qw/30 40/]} |
162 | }, |
96449e8e |
163 | # LDNOTE: modified parentheses for BETWEEN (trivial). |
164 | # Also modified the logic of "not_like" (severe, same reasons as #14 in 00where.t) |
165 | # stmt => " WHERE ( ( ( foo NOT LIKE ? ) OR ( foo NOT LIKE ? ) OR ( foo NOT LIKE ? ) ) AND ( ( fum LIKE ? ) OR ( fum LIKE ? ) ) AND nix BETWEEN ? AND ? AND nox NOT BETWEEN ? AND ? AND wix IN ( ?, ? ) AND wux NOT IN ( ?, ? ) )", |
166 | stmt => " WHERE ( ( foo NOT LIKE ? AND foo NOT LIKE ? AND foo NOT LIKE ? ) AND ( ( fum LIKE ? ) OR ( fum LIKE ? ) ) AND ( nix BETWEEN ? AND ? ) AND ( nox NOT BETWEEN ? AND ? ) AND wix IN ( ?, ? ) AND wux NOT IN ( ?, ? ) )", |
32eab2da |
167 | bind => [7,8,9,'a','b',100,200,150,160,'zz','yy','30','40'], |
168 | }, |
169 | |
8a68b5be |
170 | { |
171 | where => { |
172 | id => [], |
173 | bar => {'!=' => []}, |
174 | }, |
175 | stmt => " WHERE ( 1=1 AND 0=1 )", |
176 | bind => [], |
177 | }, |
178 | |
96449e8e |
179 | |
180 | { |
181 | where => { |
182 | foo => \["IN (?, ?)", 22, 33], |
183 | bar => [-and => \["> ?", 44], \["< ?", 55] ], |
184 | }, |
185 | stmt => " WHERE ( (bar > ? AND bar < ?) AND foo IN (?, ?) )", |
186 | bind => [44, 55, 22, 33], |
187 | }, |
4b7b6026 |
188 | { |
189 | where => { -and => [{}, { 'me.id' => '1'}] }, |
190 | stmt => " WHERE ( ( me.id = ? ) )", |
191 | bind => [ 1 ], |
192 | }, |
193 | |
fffe6900 |
194 | { |
ba566dc3 |
195 | where => { foo => $not_stringifiable, }, |
196 | stmt => " WHERE ( foo = ? )", |
197 | bind => [ $not_stringifiable ], |
198 | }, |
199 | |
73a67e2c |
200 | { |
474e3335 |
201 | where => \[ 'foo = ?','bar' ], |
c59982bb |
202 | stmt => " WHERE (foo = ?)", |
73a67e2c |
203 | bind => [ "bar" ], |
204 | }, |
205 | |
206 | { |
474e3335 |
207 | where => [ \[ 'foo = ?','bar' ] ], |
c59982bb |
208 | stmt => " WHERE (foo = ?)", |
73a67e2c |
209 | bind => [ "bar" ], |
210 | }, |
32eab2da |
211 | ); |
212 | |
f7c0b413 |
213 | # add extra modifier tests, based on 2 outcomes |
1610db0d |
214 | my $mod_or_and = { |
f7c0b413 |
215 | stmt => 'WHERE ( foo = ? OR bar = ? ) AND baz = ? ', |
216 | bind => [qw/1 2 3/], |
217 | }; |
1610db0d |
218 | my $mod_or_or = { |
f7c0b413 |
219 | stmt => 'WHERE ( foo = ? OR bar = ? ) OR baz = ?', |
220 | bind => [qw/1 2 3/], |
221 | }; |
1610db0d |
222 | my $mod_and_or = { |
223 | stmt => 'WHERE ( foo = ? AND bar = ? ) OR baz = ?', |
224 | bind => [qw/1 2 3/], |
225 | }; |
f7c0b413 |
226 | |
227 | push @handle_tests, ( |
228 | # test modifiers within hashrefs |
229 | { |
230 | where => { -or => [ |
231 | [ foo => 1, bar => 2 ], |
232 | baz => 3, |
233 | ]}, |
1610db0d |
234 | %$mod_or_or, |
f7c0b413 |
235 | }, |
236 | { |
237 | where => { -and => [ |
238 | [ foo => 1, bar => 2 ], |
239 | baz => 3, |
240 | ]}, |
1610db0d |
241 | %$mod_or_and, |
f7c0b413 |
242 | }, |
243 | |
244 | # test modifiers within arrayrefs |
245 | { |
246 | where => [ -or => [ |
247 | [ foo => 1, bar => 2 ], |
248 | baz => 3, |
249 | ]], |
1610db0d |
250 | %$mod_or_or, |
f7c0b413 |
251 | }, |
252 | { |
253 | where => [ -and => [ |
254 | [ foo => 1, bar => 2 ], |
255 | baz => 3, |
256 | ]], |
1610db0d |
257 | %$mod_or_and, |
f7c0b413 |
258 | }, |
259 | |
1610db0d |
260 | # test ambiguous modifiers within hashrefs (op extends to to immediate RHS only) |
f7c0b413 |
261 | { |
262 | where => { -and => [ -or => |
263 | [ foo => 1, bar => 2 ], |
264 | baz => 3, |
265 | ]}, |
1610db0d |
266 | %$mod_or_and, |
f7c0b413 |
267 | }, |
268 | { |
269 | where => { -or => [ -and => |
270 | [ foo => 1, bar => 2 ], |
271 | baz => 3, |
272 | ]}, |
1610db0d |
273 | %$mod_and_or, |
f7c0b413 |
274 | }, |
275 | |
1610db0d |
276 | # test ambiguous modifiers within arrayrefs (op extends to to immediate RHS only) |
f7c0b413 |
277 | { |
278 | where => [ -and => [ -or => |
279 | [ foo => 1, bar => 2 ], |
280 | baz => 3, |
281 | ]], |
1610db0d |
282 | %$mod_or_and, |
f7c0b413 |
283 | }, |
284 | { |
285 | where => [ -or => [ -and => |
286 | [ foo => 1, bar => 2 ], |
287 | baz => 3, |
288 | ]], |
1610db0d |
289 | %$mod_and_or, |
f7c0b413 |
290 | }, |
291 | ); |
292 | |
73a67e2c |
293 | plan tests => ( @handle_tests * 2 ) + 1; |
6dcf723c |
294 | |
8a68b5be |
295 | for my $case (@handle_tests) { |
f7c0b413 |
296 | local $Data::Dumper::Terse = 1; |
32eab2da |
297 | my $sql = SQL::Abstract->new; |
73a67e2c |
298 | my($stmt, @bind); |
299 | lives_ok (sub { |
300 | ($stmt, @bind) = $sql->where($case->{where}, $case->{order}); |
f7c0b413 |
301 | is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind}) |
302 | || diag "Search term:\n" . Dumper $case->{where}; |
73a67e2c |
303 | }); |
32eab2da |
304 | } |
305 | |
8a68b5be |
306 | dies_ok { |
307 | my $sql = SQL::Abstract->new; |
308 | $sql->where({ foo => { '>=' => [] }},); |
fffe6900 |
309 | }; |