packaging one directory
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 |
|
|
5 |
BEGIN { |
|
6 |
eval { require DBD::SQLite; 1 } |
|
7 |
or plan skip_all => 'DBD::SQLite required'; |
|
8 |
eval { DBD::SQLite->VERSION >= 1.25 } |
|
9 |
or plan skip_all => 'DBD::SQLite >= 1.25 required'; |
|
10 |
|
|
11 |
plan 'no_plan'; |
|
12 |
use_ok('DBIx::Custom'); |
|
13 |
} |
|
14 |
|
|
15 |
# Function for test name |
|
16 |
my $test; |
|
17 |
sub test { |
|
18 |
$test = shift; |
|
19 |
} |
|
20 |
|
|
21 |
# Constant varialbes for test |
|
22 |
my $CREATE_TABLE = { |
|
23 |
0 => 'create table table1 (key1 char(255), key2 char(255));', |
|
24 |
1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));', |
|
25 |
2 => 'create table table2 (key1 char(255), key3 char(255));' |
|
26 |
}; |
|
27 |
|
|
many change
|
28 |
my $SELECT_TMPLS = { |
packaging one directory
|
29 |
0 => 'select * from table1;' |
30 |
}; |
|
31 |
|
|
32 |
my $DROP_TABLE = { |
|
33 |
0 => 'drop table table1' |
|
34 |
}; |
|
35 |
|
|
36 |
my $NEW_ARGS = { |
|
37 |
0 => {data_source => 'dbi:SQLite:dbname=:memory:'} |
|
38 |
}; |
|
39 |
|
|
40 |
# Variables for test |
|
41 |
my $dbi; |
|
42 |
my $sth; |
|
43 |
my $tmpl; |
|
44 |
my @tmpls; |
|
45 |
my $select_tmpl; |
|
46 |
my $insert_tmpl; |
|
47 |
my $update_tmpl; |
|
48 |
my $params; |
|
49 |
my $sql; |
|
50 |
my $result; |
|
51 |
my @rows; |
|
52 |
my $rows; |
|
53 |
my $query; |
|
54 |
my @queries; |
|
55 |
my $select_query; |
|
56 |
my $insert_query; |
|
57 |
my $update_query; |
|
58 |
my $ret_val; |
|
59 |
|
|
60 |
|
|
61 |
test 'disconnect'; |
|
62 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
63 |
$dbi->connect; |
|
64 |
$dbi->disconnect; |
|
65 |
ok(!$dbi->dbh, $test); |
|
66 |
|
|
67 |
|
|
68 |
test 'connected'; |
|
69 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
70 |
ok(!$dbi->connected, "$test : not connected"); |
|
71 |
$dbi->connect; |
|
72 |
ok($dbi->connected, "$test : connected"); |
|
73 |
|
|
74 |
|
|
75 |
test 'preapare'; |
|
76 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
77 |
$sth = $dbi->prepare($CREATE_TABLE->{0}); |
|
78 |
ok($sth, "$test : auto connect"); |
|
79 |
$sth->execute; |
|
80 |
$sth = $dbi->prepare($DROP_TABLE->{0}); |
|
81 |
ok($sth, "$test : basic"); |
|
82 |
|
|
83 |
|
|
84 |
test 'do'; |
|
85 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
86 |
$ret_val = $dbi->do($CREATE_TABLE->{0}); |
|
87 |
ok(defined $ret_val, "$test : auto connect"); |
|
88 |
$ret_val = $dbi->do($DROP_TABLE->{0}); |
|
89 |
ok(defined $ret_val, "$test : basic"); |
|
90 |
|
|
version 0.0901
|
91 |
test 'create_table'; |
92 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
93 |
$ret_val = $dbi->create_table( |
|
94 |
'table1', |
|
95 |
'key1 char(255)', |
|
96 |
'key2 char(255)' |
|
97 |
); |
|
98 |
ok(defined $ret_val, "$test : create_table"); |
|
99 |
|
|
cleanup
|
100 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
version 0.0901
|
101 |
ok(!$@, "$test : table exist"); |
102 |
|
|
103 |
$ret_val = $dbi->drop_table('table1'); |
|
104 |
ok(defined $ret_val, "$test : drop table"); |
|
105 |
|
|
106 |
eval{$dbi->select('table1')}; |
|
107 |
ok($@, "$test : table not exist"); |
|
packaging one directory
|
108 |
|
109 |
# Prepare table |
|
110 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
111 |
$dbi->connect; |
|
112 |
$dbi->do($CREATE_TABLE->{0}); |
|
113 |
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);"); |
|
114 |
$sth->execute(1, 2); |
|
115 |
$sth->execute(3, 4); |
|
116 |
|
|
117 |
|
|
118 |
test 'DBIx::Custom::Result test'; |
|
119 |
$tmpl = "select key1, key2 from table1"; |
|
120 |
$query = $dbi->create_query($tmpl); |
|
version 0.0901
|
121 |
$result = $dbi->query($query); |
packaging one directory
|
122 |
|
123 |
@rows = (); |
|
124 |
while (my $row = $result->fetch) { |
|
125 |
push @rows, [@$row]; |
|
126 |
} |
|
127 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context"); |
|
128 |
|
|
version 0.0901
|
129 |
$result = $dbi->query($query); |
packaging one directory
|
130 |
@rows = (); |
131 |
while (my @row = $result->fetch) { |
|
132 |
push @rows, [@row]; |
|
133 |
} |
|
134 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context"); |
|
135 |
|
|
version 0.0901
|
136 |
$result = $dbi->query($query); |
packaging one directory
|
137 |
@rows = (); |
138 |
while (my $row = $result->fetch_hash) { |
|
139 |
push @rows, {%$row}; |
|
140 |
} |
|
141 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context"); |
|
142 |
|
|
version 0.0901
|
143 |
$result = $dbi->query($query); |
packaging one directory
|
144 |
@rows = (); |
145 |
while (my %row = $result->fetch_hash) { |
|
146 |
push @rows, {%row}; |
|
147 |
} |
|
148 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context"); |
|
149 |
|
|
version 0.0901
|
150 |
$result = $dbi->query($query); |
packaging one directory
|
151 |
$rows = $result->fetch_all; |
152 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context"); |
|
153 |
|
|
version 0.0901
|
154 |
$result = $dbi->query($query); |
packaging one directory
|
155 |
@rows = $result->fetch_all; |
156 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context"); |
|
157 |
|
|
version 0.0901
|
158 |
$result = $dbi->query($query); |
packaging one directory
|
159 |
@rows = $result->fetch_hash_all; |
160 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_hash_all scalar context"); |
|
161 |
|
|
version 0.0901
|
162 |
$result = $dbi->query($query); |
packaging one directory
|
163 |
@rows = $result->fetch_all; |
164 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_hash_all list context"); |
|
165 |
|
|
166 |
|
|
167 |
test 'Insert query return value'; |
|
168 |
$dbi->do($DROP_TABLE->{0}); |
|
169 |
$dbi->do($CREATE_TABLE->{0}); |
|
170 |
$tmpl = "insert into table1 {insert key1 key2}"; |
|
171 |
$query = $dbi->create_query($tmpl); |
|
version 0.0901
|
172 |
$ret_val = $dbi->query($query, {key1 => 1, key2 => 2}); |
packaging one directory
|
173 |
ok($ret_val, $test); |
174 |
|
|
175 |
|
|
version 0.0901
|
176 |
test 'Direct query'; |
packaging one directory
|
177 |
$dbi->do($DROP_TABLE->{0}); |
178 |
$dbi->do($CREATE_TABLE->{0}); |
|
179 |
$insert_tmpl = "insert into table1 {insert key1 key2}"; |
|
many change
|
180 |
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2}); |
181 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
|
packaging one directory
|
182 |
$rows = $result->fetch_hash_all; |
many change
|
183 |
is_deeply($rows, [{key1 => 1, key2 => 2}], $test); |
packaging one directory
|
184 |
|
185 |
test 'Filter basic'; |
|
186 |
$dbi->do($DROP_TABLE->{0}); |
|
187 |
$dbi->do($CREATE_TABLE->{0}); |
|
many change
|
188 |
$dbi->resist_filter(twice => sub { $_[0] * 2}, |
189 |
three_times => sub { $_[0] * 3}); |
|
packaging one directory
|
190 |
|
191 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
|
192 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
many change
|
193 |
$insert_query->filter({key1 => 'twice'}); |
version 0.0901
|
194 |
$dbi->query($insert_query, {key1 => 1, key2 => 2}); |
many change
|
195 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
196 |
$rows = $result->filter({key2 => 'three_times'})->fetch_hash_all; |
|
197 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : filter fetch_filter"); |
|
packaging one directory
|
198 |
$dbi->do($DROP_TABLE->{0}); |
199 |
|
|
200 |
test 'Filter in'; |
|
many change
|
201 |
$dbi->do($CREATE_TABLE->{0}); |
packaging one directory
|
202 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
203 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
version 0.0901
|
204 |
$dbi->query($insert_query, {key1 => 2, key2 => 4}); |
packaging one directory
|
205 |
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
206 |
$select_query = $dbi->create_query($select_tmpl); |
|
many change
|
207 |
$select_query->filter({'table1.key1' => 'twice'}); |
208 |
$result = $dbi->query($select_query, {'table1.key1' => [1,5], 'table1.key2' => [2,4]}); |
|
packaging one directory
|
209 |
$rows = $result->fetch_hash_all; |
many change
|
210 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : filter"); |
packaging one directory
|
211 |
|
many many changes
|
212 |
test 'DBIx::Custom::SQLTemplate basic tag'; |
packaging one directory
|
213 |
$dbi->do($DROP_TABLE->{0}); |
214 |
$dbi->do($CREATE_TABLE->{1}); |
|
215 |
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);"); |
|
216 |
$sth->execute(1, 2, 3, 4, 5); |
|
217 |
$sth->execute(6, 7, 8, 9, 10); |
|
218 |
|
|
219 |
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
|
220 |
$query = $dbi->create_query($tmpl); |
|
version 0.0901
|
221 |
$result = $dbi->query($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
packaging one directory
|
222 |
$rows = $result->fetch_hash_all; |
223 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1"); |
|
224 |
|
|
225 |
$tmpl = "select * from table1 where {<= key1} and {like key2};"; |
|
226 |
$query = $dbi->create_query($tmpl); |
|
version 0.0901
|
227 |
$result = $dbi->query($query, {key1 => 1, key2 => '%2%'}); |
packaging one directory
|
228 |
$rows = $result->fetch_hash_all; |
229 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2"); |
|
230 |
|
|
many many changes
|
231 |
test 'DIB::Custom::SQLTemplate in tag'; |
packaging one directory
|
232 |
$dbi->do($DROP_TABLE->{0}); |
233 |
$dbi->do($CREATE_TABLE->{1}); |
|
234 |
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);"); |
|
235 |
$sth->execute(1, 2, 3, 4, 5); |
|
236 |
$sth->execute(6, 7, 8, 9, 10); |
|
237 |
|
|
238 |
$tmpl = "select * from table1 where {in key1 2};"; |
|
239 |
$query = $dbi->create_query($tmpl); |
|
version 0.0901
|
240 |
$result = $dbi->query($query, {key1 => [9, 1]}); |
packaging one directory
|
241 |
$rows = $result->fetch_hash_all; |
242 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
243 |
|
|
many many changes
|
244 |
test 'DBIx::Custom::SQLTemplate insert tag'; |
packaging one directory
|
245 |
$dbi->do("delete from table1"); |
246 |
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}'; |
|
version 0.0901
|
247 |
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
packaging one directory
|
248 |
|
many change
|
249 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
250 |
$rows = $result->fetch_hash_all; |
251 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
252 |
|
|
many many changes
|
253 |
test 'DBIx::Custom::SQLTemplate update tag'; |
packaging one directory
|
254 |
$dbi->do("delete from table1"); |
255 |
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}"; |
|
version 0.0901
|
256 |
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
257 |
$dbi->query($insert_tmpl, {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
packaging one directory
|
258 |
|
259 |
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}'; |
|
version 0.0901
|
260 |
$dbi->query($update_tmpl, {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
packaging one directory
|
261 |
|
many change
|
262 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
263 |
$rows = $result->fetch_hash_all; |
264 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
265 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic"); |
|
266 |
|
|
remove run_transaction().
|
267 |
test 'transaction'; |
packaging one directory
|
268 |
$dbi->do($DROP_TABLE->{0}); |
269 |
$dbi->do($CREATE_TABLE->{0}); |
|
remove DBIx::Custom::Transac...
|
270 |
$dbi->run_transaction(sub { |
packaging one directory
|
271 |
$insert_tmpl = 'insert into table1 {insert key1 key2}'; |
version 0.0901
|
272 |
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2}); |
273 |
$dbi->query($insert_tmpl, {key1 => 3, key2 => 4}); |
|
packaging one directory
|
274 |
}); |
many change
|
275 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
276 |
$rows = $result->fetch_hash_all; |
277 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit"); |
|
278 |
|
|
279 |
$dbi->do($DROP_TABLE->{0}); |
|
280 |
$dbi->do($CREATE_TABLE->{0}); |
|
281 |
$dbi->dbh->{RaiseError} = 0; |
|
282 |
eval{ |
|
remove DBIx::Custom::Transac...
|
283 |
$dbi->run_transaction(sub { |
packaging one directory
|
284 |
$insert_tmpl = 'insert into table1 {insert key1 key2}'; |
version 0.0901
|
285 |
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2}); |
packaging one directory
|
286 |
die "Fatal Error"; |
version 0.0901
|
287 |
$dbi->query($insert_tmpl, {key1 => 3, key2 => 4}); |
packaging one directory
|
288 |
}) |
289 |
}; |
|
290 |
like($@, qr/Fatal Error.*Rollback is success/ms, "$test : Rollback success message"); |
|
291 |
ok(!$dbi->dbh->{RaiseError}, "$test : restore RaiseError value"); |
|
many change
|
292 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
293 |
$rows = $result->fetch_hash_all; |
294 |
is_deeply($rows, [], "$test : rollback"); |
|
295 |
|
|
296 |
|
|
remove run_transaction().
|
297 |
|
packaging one directory
|
298 |
test 'Error case'; |
299 |
$dbi = DBIx::Custom->new; |
|
remove DBIx::Custom::Transac...
|
300 |
eval{$dbi->run_transaction}; |
packaging one directory
|
301 |
like($@, qr/Not yet connect to database/, "$test : Yet Connected"); |
302 |
|
|
303 |
$dbi = DBIx::Custom->new(data_source => 'dbi:SQLit'); |
|
304 |
eval{$dbi->connect;}; |
|
305 |
ok($@, "$test : connect error"); |
|
306 |
|
|
307 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
308 |
$dbi->connect; |
|
309 |
$dbi->dbh->{AutoCommit} = 0; |
|
remove DBIx::Custom::Transac...
|
310 |
eval{$dbi->run_transaction}; |
packaging one directory
|
311 |
like($@, qr/AutoCommit must be true before transaction start/, |
remove run_transaction().
|
312 |
"$test : transaction auto commit is false"); |
packaging one directory
|
313 |
|
314 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
315 |
$sql = 'laksjdf'; |
|
316 |
eval{$dbi->prepare($sql)}; |
|
317 |
like($@, qr/$sql/, "$test : prepare fail"); |
|
318 |
|
|
319 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
320 |
$sql = 'laksjdf'; |
|
321 |
eval{$dbi->do($sql, qw/1 2 3/)}; |
|
322 |
like($@, qr/$sql/, "$test : do fail"); |
|
323 |
|
|
324 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
325 |
eval{$dbi->create_query("{p }")}; |
|
326 |
ok($@, "$test : create_query invalid SQL template"); |
|
327 |
|
|
328 |
test 'insert'; |
|
329 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
330 |
$dbi->do($CREATE_TABLE->{0}); |
|
331 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
332 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
many change
|
333 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
334 |
$rows = $result->fetch_hash_all; |
335 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic"); |
|
336 |
|
|
337 |
$dbi->do('delete from table1'); |
|
many change
|
338 |
$dbi->resist_filter(three_times => sub { $_[0] * 3}); |
339 |
$dbi->insert('table1', {key1 => 1, key2 => 2}, {filter => {key1 => 'three_times'}}); |
|
many change
|
340 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
341 |
$rows = $result->fetch_hash_all; |
342 |
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : edit_query_callback"); |
|
343 |
|
|
many change
|
344 |
$dbi->do($DROP_TABLE->{0}); |
345 |
$dbi->do($CREATE_TABLE->{0}); |
|
346 |
$dbi->insert('table1', {key1 => 1, key2 => 2}, {append => ' '}); |
|
347 |
$rows = $dbi->select('table1')->fetch_hash_all; |
|
348 |
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append'); |
|
349 |
|
|
packaging one directory
|
350 |
|
351 |
test 'insert error'; |
|
352 |
eval{$dbi->insert('table1')}; |
|
353 |
like($@, qr/Key-value pairs for insert must be specified to 'insert' second argument/, "$test : insert key-value not specifed"); |
|
354 |
|
|
355 |
test 'update'; |
|
356 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
357 |
$dbi->do($CREATE_TABLE->{1}); |
|
358 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
359 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
cleanup update and update_al...
|
360 |
$dbi->update('table1', {key2 => 11}, {where => {key1 => 1}}); |
many change
|
361 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
362 |
$rows = $result->fetch_hash_all; |
363 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
364 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
365 |
"$test : basic"); |
|
366 |
|
|
many change
|
367 |
$dbi->do("delete from table1"); |
368 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
369 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
cleanup update and update_al...
|
370 |
$dbi->update('table1', {key2 => 12}, {where => {key2 => 2, key3 => 3}}); |
many change
|
371 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
372 |
$rows = $result->fetch_hash_all; |
|
373 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
374 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
375 |
"$test : update key same as search key"); |
|
packaging one directory
|
376 |
|
377 |
$dbi->do("delete from table1"); |
|
378 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
379 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
cleanup update and update_al...
|
380 |
$dbi->update('table1', {key2 => 11}, {where => {key1 => 1}, query_edit_cb => sub { |
packaging one directory
|
381 |
my $query = shift; |
many change
|
382 |
$query->filter(sub { |
many change
|
383 |
my ($value, $table, $column, $dbi) = @_; |
384 |
if ($column eq 'key2') { |
|
packaging one directory
|
385 |
return $value * 2; |
386 |
} |
|
387 |
return $value; |
|
388 |
}); |
|
cleanup update and update_al...
|
389 |
}}); |
many change
|
390 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
391 |
$rows = $result->fetch_hash_all; |
392 |
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5}, |
|
393 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
394 |
"$test : query edit callback"); |
|
395 |
|
|
cleanup update and update_al...
|
396 |
$dbi->update('table1', {key2 => 11}, {where => {key1 => 1}, append => ' ', query_edit_cb => sub { |
insert, update, delete appnd...
|
397 |
my $query = shift; |
many change
|
398 |
is($query->sql, 'update table1 set key2 = ? where table1.key1 = ? ;', |
insert, update, delete appnd...
|
399 |
"$test: append statement"); |
cleanup update and update_al...
|
400 |
}}); |
insert, update, delete appnd...
|
401 |
|
packaging one directory
|
402 |
|
403 |
test 'update error'; |
|
404 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
405 |
$dbi->do($CREATE_TABLE->{1}); |
|
406 |
eval{$dbi->update('table1')}; |
|
407 |
like($@, qr/Key-value pairs for update must be specified to 'update' second argument/, |
|
408 |
"$test : update key-value pairs not specified"); |
|
409 |
|
|
410 |
eval{$dbi->update('table1', {key2 => 1})}; |
|
411 |
like($@, qr/Key-value pairs for where clause must be specified to 'update' third argument/, |
|
412 |
"$test : where key-value pairs not specified"); |
|
413 |
|
|
cleanup update and update_al...
|
414 |
eval{$dbi->update('table1', {key2 => 1}, {where => {key2 => 3}, append => '', query_edit_cb => 'aaa'})}; |
packaging one directory
|
415 |
like($@, qr/Query edit callback must be code reference/, |
416 |
"$test : query edit callback not code reference"); |
|
417 |
|
|
418 |
|
|
419 |
test 'update_all'; |
|
420 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
421 |
$dbi->do($CREATE_TABLE->{1}); |
|
422 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
423 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
cleanup update and update_al...
|
424 |
$dbi->update_all('table1', {key2 => 10}, {query_edit_cb => sub { |
packaging one directory
|
425 |
my $query = shift; |
many change
|
426 |
$query->filter(sub { |
many change
|
427 |
my ($value, $table, $column, $dbi) = @_; |
packaging one directory
|
428 |
return $value * 2; |
429 |
}) |
|
cleanup update and update_al...
|
430 |
}}); |
many change
|
431 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
432 |
$rows = $result->fetch_hash_all; |
433 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
434 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
435 |
"$test : query edit callback"); |
|
436 |
|
|
437 |
|
|
438 |
test 'delete'; |
|
439 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
440 |
$dbi->do($CREATE_TABLE->{0}); |
|
441 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
442 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
refactoring delete and delet...
|
443 |
$dbi->delete('table1', {where => {key1 => 1}}); |
many change
|
444 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
445 |
$rows = $result->fetch_hash_all; |
446 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic"); |
|
447 |
|
|
448 |
$dbi->do("delete from table1;"); |
|
449 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
450 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
refactoring delete and delet...
|
451 |
$dbi->delete('table1', {where => {key2 => 1}, query_edit_cb => sub { |
packaging one directory
|
452 |
my $query = shift; |
many change
|
453 |
$query->filter(sub { |
many change
|
454 |
my ($value, $table, $column, $dbi) = @_; |
packaging one directory
|
455 |
return $value * 2; |
456 |
}); |
|
refactoring delete and delet...
|
457 |
}}); |
many change
|
458 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
459 |
$rows = $result->fetch_hash_all; |
460 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : query edit callback"); |
|
461 |
|
|
refactoring delete and delet...
|
462 |
$dbi->delete('table1', {where => {key1 => 1}, append => ' ', query_edit_cb => sub { |
insert, update, delete appnd...
|
463 |
my $query = shift; |
many change
|
464 |
is($query->sql, 'delete from table1 where table1.key1 = ? ;', |
insert, update, delete appnd...
|
465 |
"$test: append statement"); |
refactoring delete and delet...
|
466 |
}}); |
insert, update, delete appnd...
|
467 |
|
468 |
|
|
packaging one directory
|
469 |
$dbi->delete_all('table1'); |
470 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
471 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
refactoring delete and delet...
|
472 |
$dbi->delete('table1', {where => {key1 => 1, key2 => 2}}); |
packaging one directory
|
473 |
$rows = $dbi->select('table1')->fetch_hash_all; |
474 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key"); |
|
475 |
|
|
476 |
|
|
477 |
test 'delete error'; |
|
478 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
479 |
$dbi->do($CREATE_TABLE->{0}); |
|
480 |
eval{$dbi->delete('table1')}; |
|
481 |
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/, |
|
482 |
"$test : where key-value pairs not specified"); |
|
483 |
|
|
refactoring delete and delet...
|
484 |
eval{$dbi->delete('table1', {where => {key1 => 1}, append => '', query_edit_cb => 'aaa'})}; |
packaging one directory
|
485 |
like($@, qr/Query edit callback must be code reference/, |
486 |
"$test : query edit callback not code ref"); |
|
487 |
|
|
488 |
|
|
489 |
test 'delete_all'; |
|
490 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
491 |
$dbi->do($CREATE_TABLE->{0}); |
|
492 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
493 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
494 |
$dbi->delete_all('table1'); |
|
many change
|
495 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
496 |
$rows = $result->fetch_hash_all; |
497 |
is_deeply($rows, [], "$test : basic"); |
|
498 |
|
|
499 |
|
|
500 |
test 'select'; |
|
501 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
502 |
$dbi->do($CREATE_TABLE->{0}); |
|
503 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
504 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
505 |
$rows = $dbi->select('table1')->fetch_hash_all; |
|
506 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
507 |
{key1 => 3, key2 => 4}], "$test : table"); |
|
508 |
|
|
refactoring select
|
509 |
$rows = $dbi->select('table1', {columns => ['key1']})->fetch_hash_all; |
packaging one directory
|
510 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key"); |
511 |
|
|
refactoring select
|
512 |
$rows = $dbi->select('table1', {where => {key1 => 1}})->fetch_hash_all; |
packaging one directory
|
513 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key"); |
514 |
|
|
refactoring select
|
515 |
$rows = $dbi->select('table1', {columns => ['key1'], where => {key1 => 3}})->fetch_hash_all; |
packaging one directory
|
516 |
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key"); |
517 |
|
|
refactoring select
|
518 |
$rows = $dbi->select('table1', {append => "order by key1 desc limit 1"})->fetch_hash_all; |
packaging one directory
|
519 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement"); |
520 |
|
|
refactoring select
|
521 |
$rows = $dbi->select('table1', {where => {key1 => 2}, query_edit_cb =>sub { |
packaging one directory
|
522 |
my $query = shift; |
many change
|
523 |
$query->filter(sub { |
many change
|
524 |
my ($value, $table, $column, $dbi) = @_; |
525 |
if ($column eq 'key1') { |
|
packaging one directory
|
526 |
return $value - 1; |
527 |
} |
|
528 |
return $value; |
|
529 |
}); |
|
refactoring select
|
530 |
}})->fetch_hash_all; |
packaging one directory
|
531 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : query edit call back"); |
532 |
|
|
533 |
$dbi->do($CREATE_TABLE->{2}); |
|
534 |
$dbi->insert('table2', {key1 => 1, key3 => 5}); |
|
535 |
$rows = $dbi->select([qw/table1 table2/], |
|
refactoring select
|
536 |
{ |
537 |
columns => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
538 |
where => {'table1.key2' => 2}, |
|
539 |
append => "where table1.key1 = table2.key1" |
|
540 |
} |
|
541 |
)->fetch_hash_all; |
|
packaging one directory
|
542 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join"); |
543 |
|
|
544 |
test 'Cache'; |
|
545 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
546 |
DBIx::Custom->query_cache_max(2); |
|
547 |
$dbi->do($CREATE_TABLE->{0}); |
|
cleanup
|
548 |
delete $DBIx::Custom::CLASS_ATTRS->{_query_caches}; |
549 |
delete $DBIx::Custom::CLASS_ATTRS->{_query_cache_keys}; |
|
packaging one directory
|
550 |
$tmpls[0] = "insert into table1 {insert key1 key2}"; |
551 |
$queries[0] = $dbi->create_query($tmpls[0]); |
|
552 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first"); |
|
553 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first"); |
|
554 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key first"); |
|
555 |
|
|
556 |
$tmpls[1] = "select * from table1"; |
|
557 |
$queries[1] = $dbi->create_query($tmpls[1]); |
|
558 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first"); |
|
559 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first"); |
|
560 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql second"); |
|
561 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos second"); |
|
562 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key second"); |
|
563 |
|
|
564 |
$tmpls[2] = "select key1, key2 from table1"; |
|
565 |
$queries[2] = $dbi->create_query($tmpls[2]); |
|
566 |
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key"); |
|
567 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key"); |
|
568 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos cache overflow deleted key"); |
|
569 |
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key"); |
|
570 |
is(DBIx::Custom->_query_caches->{$tmpls[2]}{key_infos}, $queries[2]->key_infos, "$test : key_infos cache overflow deleted key"); |
|
571 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third"); |
|
572 |
|
|
573 |
$queries[1] = $dbi->create_query($tmpls[1]); |
|
574 |
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key"); |
|
575 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key"); |
|
fix timeformat tests
|
576 |
is_deeply(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos cache overflow deleted key"); |
packaging one directory
|
577 |
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key"); |
fix timeformat tests
|
578 |
is_deeply(DBIx::Custom->_query_caches->{$tmpls[2]}{key_infos}, $queries[2]->key_infos, "$test : key_infos cache overflow deleted key"); |
packaging one directory
|
579 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third"); |
580 |
|
|
fix timeformat tests
|
581 |
$query = $dbi->create_query($tmpls[0]); |
many change
|
582 |
$query->filter('aaa'); |
fix timeformat tests
|
583 |
$query = $dbi->create_query($tmpls[0]); |
many change
|
584 |
ok(!$query->filter, "$test : only cached sql and key_infos"); |
585 |
$query->filter('bbb'); |
|
fix timeformat tests
|
586 |
$query = $dbi->create_query($tmpls[0]); |
many change
|
587 |
ok(!$query->filter, "$test : only cached sql and key_infos"); |
fix timeformat tests
|
588 |
|
589 |
|