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; |
add tests
|
342 |
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : filter"); |
packaging one directory
|
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}); |
|
add tests
|
380 |
$dbi->resist_filter(twice => sub { $_[0] * 2 }); |
381 |
$dbi->update('table1', {key2 => 11}, {where => {key1 => 1}, |
|
382 |
filter => {key2 => 'twice'}}); |
|
many change
|
383 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
384 |
$rows = $result->fetch_hash_all; |
385 |
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5}, |
|
386 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
add tests
|
387 |
"$test : filter"); |
packaging one directory
|
388 |
|
insert, update, delete appnd...
|
389 |
|
add tests
|
390 |
$result = $dbi->update('table1', {key2 => 11}, {where => {key1 => 1}, append => ' '}); |
packaging one directory
|
391 |
|
392 |
test 'update error'; |
|
393 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
394 |
$dbi->do($CREATE_TABLE->{1}); |
|
395 |
eval{$dbi->update('table1')}; |
|
396 |
like($@, qr/Key-value pairs for update must be specified to 'update' second argument/, |
|
397 |
"$test : update key-value pairs not specified"); |
|
398 |
|
|
399 |
eval{$dbi->update('table1', {key2 => 1})}; |
|
400 |
like($@, qr/Key-value pairs for where clause must be specified to 'update' third argument/, |
|
401 |
"$test : where key-value pairs not specified"); |
|
402 |
|
|
403 |
test 'update_all'; |
|
404 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
405 |
$dbi->do($CREATE_TABLE->{1}); |
|
406 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
407 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
add tests
|
408 |
$dbi->resist_filter(twice => sub { $_[0] * 2 }); |
409 |
$dbi->update_all('table1', {key2 => 10}, {filter => {key2 => 'twice'}}); |
|
many change
|
410 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
411 |
$rows = $result->fetch_hash_all; |
412 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
413 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
add tests
|
414 |
"$test : filter"); |
packaging one directory
|
415 |
|
416 |
|
|
417 |
test 'delete'; |
|
418 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
419 |
$dbi->do($CREATE_TABLE->{0}); |
|
420 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
421 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
refactoring delete and delet...
|
422 |
$dbi->delete('table1', {where => {key1 => 1}}); |
many change
|
423 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
424 |
$rows = $result->fetch_hash_all; |
425 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic"); |
|
426 |
|
|
427 |
$dbi->do("delete from table1;"); |
|
428 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
429 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
add tests
|
430 |
$dbi->resist_filter(twice => sub { $_[0] * 2 }); |
431 |
$dbi->delete('table1', {where => {key2 => 1}, filter => {key2 => 'twice'}}); |
|
many change
|
432 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
433 |
$rows = $result->fetch_hash_all; |
add tests
|
434 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter"); |
insert, update, delete appnd...
|
435 |
|
add tests
|
436 |
$dbi->delete('table1', {where => {key1 => 1}, append => ' '}); |
insert, update, delete appnd...
|
437 |
|
packaging one directory
|
438 |
$dbi->delete_all('table1'); |
439 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
440 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
refactoring delete and delet...
|
441 |
$dbi->delete('table1', {where => {key1 => 1, key2 => 2}}); |
packaging one directory
|
442 |
$rows = $dbi->select('table1')->fetch_hash_all; |
443 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key"); |
|
444 |
|
|
445 |
|
|
446 |
test 'delete error'; |
|
447 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
448 |
$dbi->do($CREATE_TABLE->{0}); |
|
449 |
eval{$dbi->delete('table1')}; |
|
450 |
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/, |
|
451 |
"$test : where key-value pairs not specified"); |
|
452 |
|
|
453 |
test 'delete_all'; |
|
454 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
455 |
$dbi->do($CREATE_TABLE->{0}); |
|
456 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
457 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
458 |
$dbi->delete_all('table1'); |
|
many change
|
459 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
460 |
$rows = $result->fetch_hash_all; |
461 |
is_deeply($rows, [], "$test : basic"); |
|
462 |
|
|
463 |
|
|
464 |
test 'select'; |
|
465 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
466 |
$dbi->do($CREATE_TABLE->{0}); |
|
467 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
468 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
469 |
$rows = $dbi->select('table1')->fetch_hash_all; |
|
470 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
471 |
{key1 => 3, key2 => 4}], "$test : table"); |
|
472 |
|
|
refactoring select
|
473 |
$rows = $dbi->select('table1', {columns => ['key1']})->fetch_hash_all; |
packaging one directory
|
474 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key"); |
475 |
|
|
refactoring select
|
476 |
$rows = $dbi->select('table1', {where => {key1 => 1}})->fetch_hash_all; |
packaging one directory
|
477 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key"); |
478 |
|
|
refactoring select
|
479 |
$rows = $dbi->select('table1', {columns => ['key1'], where => {key1 => 3}})->fetch_hash_all; |
packaging one directory
|
480 |
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key"); |
481 |
|
|
refactoring select
|
482 |
$rows = $dbi->select('table1', {append => "order by key1 desc limit 1"})->fetch_hash_all; |
packaging one directory
|
483 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement"); |
484 |
|
|
add tests
|
485 |
$dbi->resist_filter(decrement => sub { $_[0] - 1 }); |
486 |
$rows = $dbi->select('table1', {where => {key1 => 2}, filter => {key1 => 'decrement'}}) |
|
487 |
->fetch_hash_all; |
|
488 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : filter"); |
|
packaging one directory
|
489 |
|
490 |
$dbi->do($CREATE_TABLE->{2}); |
|
491 |
$dbi->insert('table2', {key1 => 1, key3 => 5}); |
|
492 |
$rows = $dbi->select([qw/table1 table2/], |
|
refactoring select
|
493 |
{ |
494 |
columns => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
495 |
where => {'table1.key2' => 2}, |
|
496 |
append => "where table1.key1 = table2.key1" |
|
497 |
} |
|
498 |
)->fetch_hash_all; |
|
packaging one directory
|
499 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join"); |
500 |
|
|
501 |
test 'Cache'; |
|
502 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
503 |
DBIx::Custom->query_cache_max(2); |
|
504 |
$dbi->do($CREATE_TABLE->{0}); |
|
cleanup
|
505 |
delete $DBIx::Custom::CLASS_ATTRS->{_query_caches}; |
506 |
delete $DBIx::Custom::CLASS_ATTRS->{_query_cache_keys}; |
|
packaging one directory
|
507 |
$tmpls[0] = "insert into table1 {insert key1 key2}"; |
508 |
$queries[0] = $dbi->create_query($tmpls[0]); |
|
509 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first"); |
|
add tests
|
510 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{columns}, $queries[0]->columns, "$test : columns first"); |
packaging one directory
|
511 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key first"); |
512 |
|
|
513 |
$tmpls[1] = "select * from table1"; |
|
514 |
$queries[1] = $dbi->create_query($tmpls[1]); |
|
515 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first"); |
|
add tests
|
516 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{columns}, $queries[0]->columns, "$test : columns first"); |
packaging one directory
|
517 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql second"); |
add tests
|
518 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns second"); |
packaging one directory
|
519 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key second"); |
520 |
|
|
521 |
$tmpls[2] = "select key1, key2 from table1"; |
|
522 |
$queries[2] = $dbi->create_query($tmpls[2]); |
|
523 |
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key"); |
|
524 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key"); |
|
add tests
|
525 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns cache overflow deleted key"); |
packaging one directory
|
526 |
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key"); |
add tests
|
527 |
is(DBIx::Custom->_query_caches->{$tmpls[2]}{columns}, $queries[2]->columns, "$test : columns cache overflow deleted key"); |
packaging one directory
|
528 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third"); |
529 |
|
|
530 |
$queries[1] = $dbi->create_query($tmpls[1]); |
|
531 |
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key"); |
|
532 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key"); |
|
add tests
|
533 |
is_deeply(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns cache overflow deleted key"); |
packaging one directory
|
534 |
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key"); |
add tests
|
535 |
is_deeply(DBIx::Custom->_query_caches->{$tmpls[2]}{columns}, $queries[2]->columns, "$test : columns cache overflow deleted key"); |
packaging one directory
|
536 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third"); |
537 |
|
|
fix timeformat tests
|
538 |
$query = $dbi->create_query($tmpls[0]); |
many change
|
539 |
$query->filter('aaa'); |
fix timeformat tests
|
540 |
$query = $dbi->create_query($tmpls[0]); |
add tests
|
541 |
ok(!$query->filter, "$test : only cached sql and columns"); |
many change
|
542 |
$query->filter('bbb'); |
fix timeformat tests
|
543 |
$query = $dbi->create_query($tmpls[0]); |
add tests
|
544 |
ok(!$query->filter, "$test : only cached sql and columns"); |
fix timeformat tests
|
545 |
|
546 |
|