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