Removed debugging code accidently added
[scpubgit/Q-Branch.git] / t / 02where.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use Test::Exception;
7 use SQL::Abstract::Test import => ['is_same_sql_bind'];
8
9 use Data::Dumper;
10 use SQL::Abstract;
11
12 # Make sure to test the examples, since having them break is somewhat
13 # embarrassing. :-(
14
15 my $not_stringifiable = bless {}, 'SQLA::NotStringifiable';
16
17 my @handle_tests = (
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     {
31         where  => [
32             status => 'completed',
33             user   => 'nwiger',
34         ],
35         stmt => " WHERE ( status = ? OR user = ? )",
36         bind => [qw/completed nwiger/],
37     },
38
39     {
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',
85 #LDNOTE: modified parentheses
86 #
87 # acked by RIBASUSHI
88         stmt => "WHERE ( ( completion_date BETWEEN ? AND ? ) AND status = ? ) ORDER BY ticket, requestor",
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',
135 #LDNOTE: modified parentheses
136 #
137 # acked by RIBASUSHI
138         stmt => " WHERE ( ( priority BETWEEN ? AND ? ) AND requestor IS NULL ) ORDER BY requestor, ticket",
139         bind => [qw/1 3/],
140     },
141
142
143     {
144         where => {  
145             id  => 1,
146             num => {
147              '<=' => 20,
148              '>'  => 10,
149             },
150         },
151 # LDNOTE : modified test below, just parentheses differ
152 #
153 # acked by RIBASUSHI
154         stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )",
155         bind => [qw/1 20 10/],
156     },
157
158     {
159 # LDNOTE 23.03.09 : modified test below, just parentheses differ
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                  },
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 ( ?, ? ) )",
168         bind => [7,8,9,'a','b',100,200,150,160,'zz','yy','30','40'],
169     },
170
171     {
172         where => {
173             bar => {'!=' => []},
174         },
175         stmt => " WHERE ( 1=1 )",
176         bind => [],
177     },
178
179     {
180         where => {
181             id  => [],
182         },
183         stmt => " WHERE ( 0=1 )",
184         bind => [],
185     },
186
187
188     {
189         where => {
190             foo => \["IN (?, ?)", 22, 33],
191             bar => [-and =>  \["> ?", 44], \["< ?", 55] ],
192         },
193         stmt => " WHERE ( (bar > ? AND bar < ?) AND foo IN (?, ?) )",
194         bind => [44, 55, 22, 33],
195     },
196    {
197        where => { -and => [{}, { 'me.id' => '1'}] },
198        stmt => " WHERE ( ( me.id = ? ) )",
199        bind => [ 1 ],
200    },
201
202    {
203        where => { foo => $not_stringifiable, },
204        stmt => " WHERE ( foo = ? )",
205        bind => [ $not_stringifiable ],
206    },
207
208    {
209        where => \[ 'foo = ?','bar' ],
210        stmt => " WHERE (foo = ?)",
211        bind => [ "bar" ],
212    },
213
214    {
215        where => [ \[ 'foo = ?','bar' ] ],
216        stmt => " WHERE (foo = ?)",
217        bind => [ "bar" ],
218    },
219
220    {
221        where => { -bool => \'function(x)' },
222        stmt => " WHERE function(x)",
223        bind => [],
224    },
225
226    {
227        where => { -bool => 'foo' },
228        stmt => " WHERE foo",
229        bind => [],
230    },
231
232    {
233        where => { -and => [-bool => 'foo', -bool => 'bar'] },
234        stmt => " WHERE foo AND bar",
235        bind => [],
236    },
237
238    {
239        where => { -or => [-bool => 'foo', -bool => 'bar'] },
240        stmt => " WHERE foo OR bar",
241        bind => [],
242    },
243
244    {
245        where => { -not_bool => \'function(x)' },
246        stmt => " WHERE NOT function(x)",
247        bind => [],
248    },
249
250    {
251        where => { -not_bool => 'foo' },
252        stmt => " WHERE NOT foo",
253        bind => [],
254    },
255
256    {
257        where => { -and => [-not_bool => 'foo', -not_bool => 'bar'] },
258        stmt => " WHERE (NOT foo) AND (NOT bar)",
259        bind => [],
260    },
261
262    {
263        where => { -or => [-not_bool => 'foo', -not_bool => 'bar'] },
264        stmt => " WHERE (NOT foo) OR (NOT bar)",
265        bind => [],
266    },
267
268    {
269        where => { -bool => \['function(?)', 20]  },
270        stmt => " WHERE function(?)",
271        bind => [20],
272    },
273
274    {
275        where => { -not_bool => \['function(?)', 20]  },
276        stmt => " WHERE NOT function(?)",
277        bind => [20],
278    },
279
280    {
281        where => { -bool => { a => 1, b => 2}  },
282        stmt => " WHERE a = ? AND b = ?",
283        bind => [1, 2],
284    },
285
286    {
287        where => { -bool => [ a => 1, b => 2] },
288        stmt => " WHERE a = ? OR b = ?",
289        bind => [1, 2],
290    },
291
292    {
293        where => { -not_bool => { a => 1, b => 2}  },
294        stmt => " WHERE NOT (a = ? AND b = ?)",
295        bind => [1, 2],
296    },
297
298    {
299        where => { -not_bool => [ a => 1, b => 2] },
300        stmt => " WHERE NOT ( a = ? OR b = ? )",
301        bind => [1, 2],
302    },
303
304 );
305
306 plan tests => ( @handle_tests * 2 ) + 1;
307
308 for my $case (@handle_tests) {
309     local $Data::Dumper::Terse = 1;
310     my $sql = SQL::Abstract->new;
311     my($stmt, @bind);
312     lives_ok (sub { 
313       ($stmt, @bind) = $sql->where($case->{where}, $case->{order});
314       is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind})
315         || diag "Search term:\n" . Dumper $case->{where};
316     });
317 }
318
319 dies_ok {
320     my $sql = SQL::Abstract->new;
321     $sql->where({ foo => { '>=' => [] }},);
322 };