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