Fixed the problem with values() not behaving the same as the rest of the code.
[scpubgit/Q-Branch.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
32eab2da 9use SQL::Abstract;
10
11# Make sure to test the examples, since having them break is somewhat
12# embarrassing. :-(
13
5ebfe5f2 14my $not_stringifiable = bless {}, 'SQLA::NotStringifiable';
ba566dc3 15
bab725ce 16my@x=(
32eab2da 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 {
73a67e2c 30 where => [
31 status => 'completed',
32 user => 'nwiger',
33 ],
34 stmt => " WHERE ( status = ? OR user = ? )",
35 bind => [qw/completed nwiger/],
36 },
37
38 {
32eab2da 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',
96449e8e 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",
32eab2da 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',
96449e8e 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",
32eab2da 136 bind => [qw/1 3/],
137 },
138
139
140 {
141 where => {
142 id => 1,
143 num => {
144 '<=' => 20,
145 '>' => 10,
146 },
147 },
96449e8e 148# LDNOTE : modified test below, just parentheses differ
149# stmt => " WHERE ( id = ? AND num <= ? AND num > ? )",
150 stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )",
32eab2da 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 },
96449e8e 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 ( ?, ? ) )",
32eab2da 166 bind => [7,8,9,'a','b',100,200,150,160,'zz','yy','30','40'],
167 },
168
8a68b5be 169 {
170 where => {
171 id => [],
172 bar => {'!=' => []},
173 },
174 stmt => " WHERE ( 1=1 AND 0=1 )",
175 bind => [],
176 },
177
96449e8e 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 },
4b7b6026 187 {
188 where => { -and => [{}, { 'me.id' => '1'}] },
189 stmt => " WHERE ( ( me.id = ? ) )",
190 bind => [ 1 ],
191 },
192
fffe6900 193 {
ba566dc3 194 where => { foo => $not_stringifiable, },
195 stmt => " WHERE ( foo = ? )",
196 bind => [ $not_stringifiable ],
197 },
198
bab725ce 199);my @handle_tests = (
73a67e2c 200 {
201 where => \[ 'foo ?','bar' ],
202 stmt => " WHERE (foo = ?)",
203 bind => [ "bar" ],
204 },
bab725ce 205);my@x2=(
73a67e2c 206
207 {
208 where => [ \[ 'foo ?','bar' ] ],
209 stmt => " WHERE (foo = ?)",
210 bind => [ "bar" ],
211 },
32eab2da 212);
213
6dcf723c 214
73a67e2c 215plan tests => ( @handle_tests * 2 ) + 1;
6dcf723c 216
8a68b5be 217for my $case (@handle_tests) {
32eab2da 218 my $sql = SQL::Abstract->new;
73a67e2c 219 my($stmt, @bind);
220 lives_ok (sub {
221 ($stmt, @bind) = $sql->where($case->{where}, $case->{order});
222 is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind});
223 });
32eab2da 224}
225
8a68b5be 226dies_ok {
227 my $sql = SQL::Abstract->new;
228 $sql->where({ foo => { '>=' => [] }},);
fffe6900 229};