cp -r from branches/1.50_RC where svk push won't work
[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 SQL::Abstract;
10
11 # Make sure to test the examples, since having them break is somewhat
12 # embarrassing. :-(
13
14 my $not_stringifiable = bless {}, 'SQLA::NotStringifiable';
15
16 my @handle_tests = (
17     {
18         where => {
19             requestor => 'inna',
20             worker => ['nwiger', 'rcwe', 'sfz'],
21             status => { '!=', 'completed' }
22         },
23         order => [],
24         stmt => " WHERE ( requestor = ? AND status != ? AND ( ( worker = ? ) OR"
25               . " ( worker = ? ) OR ( worker = ? ) ) )",
26         bind => [qw/inna completed nwiger rcwe sfz/],
27     },
28
29     {
30         where  => [
31             status => 'completed',
32             user   => 'nwiger',
33         ],
34         stmt => " WHERE ( status = ? OR user = ? )",
35         bind => [qw/completed nwiger/],
36     },
37
38     {
39         where  => {
40             user   => 'nwiger',
41             status => 'completed'
42         },
43         order => [qw/ticket/],
44         stmt => " WHERE ( status = ? AND user = ? ) ORDER BY ticket",
45         bind => [qw/completed nwiger/],
46     },
47
48     {
49         where  => {
50             user   => 'nwiger',
51             status => { '!=', 'completed' }
52         },
53         order => [qw/ticket/],
54         stmt => " WHERE ( status != ? AND user = ? ) ORDER BY ticket",
55         bind => [qw/completed nwiger/],
56     },
57
58     {
59         where  => {
60             status   => 'completed',
61             reportid => { 'in', [567, 2335, 2] }
62         },
63         order => [],
64         stmt => " WHERE ( reportid IN ( ?, ?, ? ) AND status = ? )",
65         bind => [qw/567 2335 2 completed/],
66     },
67
68     {
69         where  => {
70             status   => 'completed',
71             reportid => { 'not in', [567, 2335, 2] }
72         },
73         order => [],
74         stmt => " WHERE ( reportid NOT IN ( ?, ?, ? ) AND status = ? )",
75         bind => [qw/567 2335 2 completed/],
76     },
77
78     {
79         where  => {
80             status   => 'completed',
81             completion_date => { 'between', ['2002-10-01', '2003-02-06'] },
82         },
83         order => \'ticket, requestor',
84 #LDNOTE: modified parentheses
85 #        stmt => " WHERE ( completion_date BETWEEN ? AND ? AND status = ? ) ORDER BY ticket, requestor",
86         stmt => " WHERE ( ( completion_date BETWEEN ? AND ? ) AND status = ? ) ORDER BY ticket, requestor",
87         bind => [qw/2002-10-01 2003-02-06 completed/],
88     },
89
90     {
91         where => [
92             {
93                 user   => 'nwiger',
94                 status => { 'in', ['pending', 'dispatched'] },
95             },
96             {
97                 user   => 'robot',
98                 status => 'unassigned',
99             },
100         ],
101         order => [],
102         stmt => " WHERE ( ( status IN ( ?, ? ) AND user = ? ) OR ( status = ? AND user = ? ) )",
103         bind => [qw/pending dispatched nwiger unassigned robot/],
104     },
105
106     {
107         where => {  
108             priority  => [ {'>', 3}, {'<', 1} ],
109             requestor => \'is not null',
110         },
111         order => 'priority',
112         stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor is not null ) ORDER BY priority",
113         bind => [qw/3 1/],
114     },
115
116     {
117         where => {  
118             priority  => [ {'>', 3}, {'<', 1} ],
119             requestor => { '!=', undef }, 
120         },
121         order => [qw/a b c d e f g/],
122         stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor IS NOT NULL )"
123               . " ORDER BY a, b, c, d, e, f, g",
124         bind => [qw/3 1/],
125     },
126
127     {
128         where => {  
129             priority  => { 'between', [1, 3] },
130             requestor => { 'like', undef }, 
131         },
132         order => \'requestor, ticket',
133 #LDNOTE: modified parentheses
134 #        stmt => " WHERE ( priority BETWEEN ? AND ? AND requestor IS NULL ) ORDER BY requestor, ticket",
135         stmt => " WHERE ( ( priority BETWEEN ? AND ? ) AND requestor IS NULL ) ORDER BY requestor, ticket",
136         bind => [qw/1 3/],
137     },
138
139
140     {
141         where => {  
142             id  => 1,
143             num => {
144              '<=' => 20,
145              '>'  => 10,
146             },
147         },
148 # LDNOTE : modified test below, just parentheses differ
149 #        stmt => " WHERE ( id = ? AND num <= ? AND num > ? )",
150         stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )",
151         bind => [qw/1 20 10/],
152     },
153
154     {
155         where => { foo => {-not_like => [7,8,9]},
156                    fum => {'like' => [qw/a b/]},
157                    nix => {'between' => [100,200] },
158                    nox => {'not between' => [150,160] },
159                    wix => {'in' => [qw/zz yy/]},
160                    wux => {'not_in'  => [qw/30 40/]}
161                  },
162 # LDNOTE: modified parentheses for BETWEEN (trivial).
163 # Also modified the logic of "not_like" (severe, same reasons as #14 in 00where.t)
164 #        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 ( ?, ? ) )",
165         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 ( ?, ? ) )",
166         bind => [7,8,9,'a','b',100,200,150,160,'zz','yy','30','40'],
167     },
168
169     {
170         where => {
171             id  => [],
172             bar => {'!=' => []},
173         },
174         stmt => " WHERE ( 1=1 AND 0=1 )",
175         bind => [],
176     },
177
178
179     {
180         where => {
181             foo => \["IN (?, ?)", 22, 33],
182             bar => [-and =>  \["> ?", 44], \["< ?", 55] ],
183         },
184         stmt => " WHERE ( (bar > ? AND bar < ?) AND foo IN (?, ?) )",
185         bind => [44, 55, 22, 33],
186     },
187    {
188        where => { -and => [{}, { 'me.id' => '1'}] },
189        stmt => " WHERE ( ( me.id = ? ) )",
190        bind => [ 1 ],
191    },
192
193    {
194        where => { foo => $not_stringifiable, },
195        stmt => " WHERE ( foo = ? )",
196        bind => [ $not_stringifiable ],
197    },
198
199    {
200        where => \[ 'foo = ?','bar' ],
201        stmt => " WHERE (foo = ?)",
202        bind => [ "bar" ],
203    },
204
205    {
206        where => [ \[ 'foo = ?','bar' ] ],
207        stmt => " WHERE (foo = ?)",
208        bind => [ "bar" ],
209    },
210 );
211
212 plan tests => ( @handle_tests * 2 ) + 1;
213
214 for my $case (@handle_tests) {
215     my $sql = SQL::Abstract->new;
216     my($stmt, @bind);
217     lives_ok (sub { 
218       ($stmt, @bind) = $sql->where($case->{where}, $case->{order});
219       is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind});
220     });
221 }
222
223 dies_ok {
224     my $sql = SQL::Abstract->new;
225     $sql->where({ foo => { '>=' => [] }},);
226 };