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 |
|
|
version 0.0901
|
75 |
test 'create_table'; |
76 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
77 |
$ret_val = $dbi->create_table( |
|
78 |
'table1', |
|
79 |
'key1 char(255)', |
|
80 |
'key2 char(255)' |
|
81 |
); |
|
82 |
ok(defined $ret_val, "$test : create_table"); |
|
83 |
|
|
cleanup
|
84 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
version 0.0901
|
85 |
ok(!$@, "$test : table exist"); |
86 |
|
|
87 |
$ret_val = $dbi->drop_table('table1'); |
|
88 |
ok(defined $ret_val, "$test : drop table"); |
|
89 |
|
|
90 |
eval{$dbi->select('table1')}; |
|
91 |
ok($@, "$test : table not exist"); |
|
packaging one directory
|
92 |
|
93 |
# Prepare table |
|
94 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
95 |
$dbi->connect; |
|
add all tests
|
96 |
$dbi->query($CREATE_TABLE->{0}); |
97 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
98 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
packaging one directory
|
99 |
|
100 |
test 'DBIx::Custom::Result test'; |
|
101 |
$tmpl = "select key1, key2 from table1"; |
|
102 |
$query = $dbi->create_query($tmpl); |
|
version 0.0901
|
103 |
$result = $dbi->query($query); |
packaging one directory
|
104 |
|
105 |
@rows = (); |
|
106 |
while (my $row = $result->fetch) { |
|
107 |
push @rows, [@$row]; |
|
108 |
} |
|
109 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context"); |
|
110 |
|
|
version 0.0901
|
111 |
$result = $dbi->query($query); |
packaging one directory
|
112 |
@rows = (); |
113 |
while (my @row = $result->fetch) { |
|
114 |
push @rows, [@row]; |
|
115 |
} |
|
116 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context"); |
|
117 |
|
|
version 0.0901
|
118 |
$result = $dbi->query($query); |
packaging one directory
|
119 |
@rows = (); |
120 |
while (my $row = $result->fetch_hash) { |
|
121 |
push @rows, {%$row}; |
|
122 |
} |
|
123 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context"); |
|
124 |
|
|
version 0.0901
|
125 |
$result = $dbi->query($query); |
packaging one directory
|
126 |
@rows = (); |
127 |
while (my %row = $result->fetch_hash) { |
|
128 |
push @rows, {%row}; |
|
129 |
} |
|
130 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context"); |
|
131 |
|
|
version 0.0901
|
132 |
$result = $dbi->query($query); |
packaging one directory
|
133 |
$rows = $result->fetch_all; |
134 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context"); |
|
135 |
|
|
version 0.0901
|
136 |
$result = $dbi->query($query); |
packaging one directory
|
137 |
@rows = $result->fetch_all; |
138 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context"); |
|
139 |
|
|
version 0.0901
|
140 |
$result = $dbi->query($query); |
packaging one directory
|
141 |
@rows = $result->fetch_hash_all; |
142 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_hash_all scalar context"); |
|
143 |
|
|
version 0.0901
|
144 |
$result = $dbi->query($query); |
packaging one directory
|
145 |
@rows = $result->fetch_all; |
146 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_hash_all list context"); |
|
147 |
|
|
148 |
|
|
149 |
test 'Insert query return value'; |
|
add all tests
|
150 |
$dbi->query($DROP_TABLE->{0}); |
151 |
$dbi->query($CREATE_TABLE->{0}); |
|
packaging one directory
|
152 |
$tmpl = "insert into table1 {insert key1 key2}"; |
153 |
$query = $dbi->create_query($tmpl); |
|
version 0.0901
|
154 |
$ret_val = $dbi->query($query, {key1 => 1, key2 => 2}); |
packaging one directory
|
155 |
ok($ret_val, $test); |
156 |
|
|
157 |
|
|
version 0.0901
|
158 |
test 'Direct query'; |
add all tests
|
159 |
$dbi->query($DROP_TABLE->{0}); |
160 |
$dbi->query($CREATE_TABLE->{0}); |
|
packaging one directory
|
161 |
$insert_tmpl = "insert into table1 {insert key1 key2}"; |
many change
|
162 |
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2}); |
163 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
|
packaging one directory
|
164 |
$rows = $result->fetch_hash_all; |
many change
|
165 |
is_deeply($rows, [{key1 => 1, key2 => 2}], $test); |
packaging one directory
|
166 |
|
167 |
test 'Filter basic'; |
|
add all tests
|
168 |
$dbi->query($DROP_TABLE->{0}); |
169 |
$dbi->query($CREATE_TABLE->{0}); |
|
many change
|
170 |
$dbi->resist_filter(twice => sub { $_[0] * 2}, |
171 |
three_times => sub { $_[0] * 3}); |
|
packaging one directory
|
172 |
|
173 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
|
174 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
many change
|
175 |
$insert_query->filter({key1 => 'twice'}); |
version 0.0901
|
176 |
$dbi->query($insert_query, {key1 => 1, key2 => 2}); |
many change
|
177 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
178 |
$rows = $result->filter({key2 => 'three_times'})->fetch_hash_all; |
|
179 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : filter fetch_filter"); |
|
add all tests
|
180 |
$dbi->query($DROP_TABLE->{0}); |
packaging one directory
|
181 |
|
182 |
test 'Filter in'; |
|
add all tests
|
183 |
$dbi->query($CREATE_TABLE->{0}); |
packaging one directory
|
184 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
185 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
version 0.0901
|
186 |
$dbi->query($insert_query, {key1 => 2, key2 => 4}); |
packaging one directory
|
187 |
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
188 |
$select_query = $dbi->create_query($select_tmpl); |
|
many change
|
189 |
$select_query->filter({'table1.key1' => 'twice'}); |
190 |
$result = $dbi->query($select_query, {'table1.key1' => [1,5], 'table1.key2' => [2,4]}); |
|
packaging one directory
|
191 |
$rows = $result->fetch_hash_all; |
many change
|
192 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : filter"); |
packaging one directory
|
193 |
|
many many changes
|
194 |
test 'DBIx::Custom::SQLTemplate basic tag'; |
add all tests
|
195 |
$dbi->query($DROP_TABLE->{0}); |
196 |
$dbi->query($CREATE_TABLE->{1}); |
|
197 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
198 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
packaging one directory
|
199 |
|
200 |
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
|
201 |
$query = $dbi->create_query($tmpl); |
|
version 0.0901
|
202 |
$result = $dbi->query($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
packaging one directory
|
203 |
$rows = $result->fetch_hash_all; |
204 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1"); |
|
205 |
|
|
206 |
$tmpl = "select * from table1 where {<= key1} and {like key2};"; |
|
207 |
$query = $dbi->create_query($tmpl); |
|
version 0.0901
|
208 |
$result = $dbi->query($query, {key1 => 1, key2 => '%2%'}); |
packaging one directory
|
209 |
$rows = $result->fetch_hash_all; |
210 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2"); |
|
211 |
|
|
many many changes
|
212 |
test 'DIB::Custom::SQLTemplate in tag'; |
add all tests
|
213 |
$dbi->query($DROP_TABLE->{0}); |
214 |
$dbi->query($CREATE_TABLE->{1}); |
|
215 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
216 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
packaging one directory
|
217 |
|
218 |
$tmpl = "select * from table1 where {in key1 2};"; |
|
219 |
$query = $dbi->create_query($tmpl); |
|
version 0.0901
|
220 |
$result = $dbi->query($query, {key1 => [9, 1]}); |
packaging one directory
|
221 |
$rows = $result->fetch_hash_all; |
222 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
223 |
|
|
many many changes
|
224 |
test 'DBIx::Custom::SQLTemplate insert tag'; |
add all tests
|
225 |
$dbi->query("delete from table1"); |
packaging one directory
|
226 |
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}'; |
version 0.0901
|
227 |
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
packaging one directory
|
228 |
|
many change
|
229 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
230 |
$rows = $result->fetch_hash_all; |
231 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
232 |
|
|
many many changes
|
233 |
test 'DBIx::Custom::SQLTemplate update tag'; |
add all tests
|
234 |
$dbi->query("delete from table1"); |
packaging one directory
|
235 |
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}"; |
version 0.0901
|
236 |
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
237 |
$dbi->query($insert_tmpl, {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
packaging one directory
|
238 |
|
239 |
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}'; |
|
version 0.0901
|
240 |
$dbi->query($update_tmpl, {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
packaging one directory
|
241 |
|
many change
|
242 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
243 |
$rows = $result->fetch_hash_all; |
244 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
245 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic"); |
|
246 |
|
|
remove run_transaction().
|
247 |
test 'transaction'; |
add all tests
|
248 |
$dbi->query($DROP_TABLE->{0}); |
249 |
$dbi->query($CREATE_TABLE->{0}); |
|
remove DBIx::Custom::Transac...
|
250 |
$dbi->run_transaction(sub { |
packaging one directory
|
251 |
$insert_tmpl = 'insert into table1 {insert key1 key2}'; |
version 0.0901
|
252 |
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2}); |
253 |
$dbi->query($insert_tmpl, {key1 => 3, key2 => 4}); |
|
packaging one directory
|
254 |
}); |
many change
|
255 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
256 |
$rows = $result->fetch_hash_all; |
257 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit"); |
|
258 |
|
|
add all tests
|
259 |
$dbi->query($DROP_TABLE->{0}); |
260 |
$dbi->query($CREATE_TABLE->{0}); |
|
packaging one directory
|
261 |
$dbi->dbh->{RaiseError} = 0; |
262 |
eval{ |
|
remove DBIx::Custom::Transac...
|
263 |
$dbi->run_transaction(sub { |
packaging one directory
|
264 |
$insert_tmpl = 'insert into table1 {insert key1 key2}'; |
version 0.0901
|
265 |
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2}); |
packaging one directory
|
266 |
die "Fatal Error"; |
version 0.0901
|
267 |
$dbi->query($insert_tmpl, {key1 => 3, key2 => 4}); |
packaging one directory
|
268 |
}) |
269 |
}; |
|
270 |
like($@, qr/Fatal Error.*Rollback is success/ms, "$test : Rollback success message"); |
|
271 |
ok(!$dbi->dbh->{RaiseError}, "$test : restore RaiseError value"); |
|
many change
|
272 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
273 |
$rows = $result->fetch_hash_all; |
274 |
is_deeply($rows, [], "$test : rollback"); |
|
275 |
|
|
276 |
|
|
remove run_transaction().
|
277 |
|
packaging one directory
|
278 |
test 'Error case'; |
279 |
$dbi = DBIx::Custom->new; |
|
remove DBIx::Custom::Transac...
|
280 |
eval{$dbi->run_transaction}; |
packaging one directory
|
281 |
like($@, qr/Not yet connect to database/, "$test : Yet Connected"); |
282 |
|
|
283 |
$dbi = DBIx::Custom->new(data_source => 'dbi:SQLit'); |
|
284 |
eval{$dbi->connect;}; |
|
285 |
ok($@, "$test : connect error"); |
|
286 |
|
|
287 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
288 |
$dbi->connect; |
|
289 |
$dbi->dbh->{AutoCommit} = 0; |
|
remove DBIx::Custom::Transac...
|
290 |
eval{$dbi->run_transaction}; |
packaging one directory
|
291 |
like($@, qr/AutoCommit must be true before transaction start/, |
remove run_transaction().
|
292 |
"$test : transaction auto commit is false"); |
packaging one directory
|
293 |
|
294 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
295 |
$sql = 'laksjdf'; |
|
add all tests
|
296 |
eval{$dbi->query($sql, qw/1 2 3/)}; |
297 |
like($@, qr/$sql/, "$test : query fail"); |
|
packaging one directory
|
298 |
|
299 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
300 |
eval{$dbi->create_query("{p }")}; |
|
301 |
ok($@, "$test : create_query invalid SQL template"); |
|
302 |
|
|
303 |
test 'insert'; |
|
304 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
add all tests
|
305 |
$dbi->query($CREATE_TABLE->{0}); |
packaging one directory
|
306 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
307 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
many change
|
308 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
309 |
$rows = $result->fetch_hash_all; |
310 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic"); |
|
311 |
|
|
add all tests
|
312 |
$dbi->query('delete from table1'); |
many change
|
313 |
$dbi->resist_filter(three_times => sub { $_[0] * 3}); |
314 |
$dbi->insert('table1', {key1 => 1, key2 => 2}, {filter => {key1 => 'three_times'}}); |
|
many change
|
315 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
316 |
$rows = $result->fetch_hash_all; |
add tests
|
317 |
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : filter"); |
packaging one directory
|
318 |
|
add all tests
|
319 |
$dbi->query($DROP_TABLE->{0}); |
320 |
$dbi->query($CREATE_TABLE->{0}); |
|
many change
|
321 |
$dbi->insert('table1', {key1 => 1, key2 => 2}, {append => ' '}); |
322 |
$rows = $dbi->select('table1')->fetch_hash_all; |
|
323 |
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append'); |
|
324 |
|
|
packaging one directory
|
325 |
|
326 |
test 'insert error'; |
|
327 |
eval{$dbi->insert('table1')}; |
|
328 |
like($@, qr/Key-value pairs for insert must be specified to 'insert' second argument/, "$test : insert key-value not specifed"); |
|
329 |
|
|
330 |
test 'update'; |
|
331 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
add all tests
|
332 |
$dbi->query($CREATE_TABLE->{1}); |
packaging one directory
|
333 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
334 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
cleanup update and update_al...
|
335 |
$dbi->update('table1', {key2 => 11}, {where => {key1 => 1}}); |
many change
|
336 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
337 |
$rows = $result->fetch_hash_all; |
338 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
339 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
340 |
"$test : basic"); |
|
341 |
|
|
add all tests
|
342 |
$dbi->query("delete from table1"); |
many change
|
343 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
344 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
cleanup update and update_al...
|
345 |
$dbi->update('table1', {key2 => 12}, {where => {key2 => 2, key3 => 3}}); |
many change
|
346 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
347 |
$rows = $result->fetch_hash_all; |
|
348 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
349 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
350 |
"$test : update key same as search key"); |
|
packaging one directory
|
351 |
|
add all tests
|
352 |
$dbi->query("delete from table1"); |
packaging one directory
|
353 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
354 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
add tests
|
355 |
$dbi->resist_filter(twice => sub { $_[0] * 2 }); |
356 |
$dbi->update('table1', {key2 => 11}, {where => {key1 => 1}, |
|
357 |
filter => {key2 => 'twice'}}); |
|
many change
|
358 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
359 |
$rows = $result->fetch_hash_all; |
360 |
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5}, |
|
361 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
add tests
|
362 |
"$test : filter"); |
packaging one directory
|
363 |
|
insert, update, delete appnd...
|
364 |
|
add tests
|
365 |
$result = $dbi->update('table1', {key2 => 11}, {where => {key1 => 1}, append => ' '}); |
packaging one directory
|
366 |
|
367 |
test 'update error'; |
|
368 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
add all tests
|
369 |
$dbi->query($CREATE_TABLE->{1}); |
packaging one directory
|
370 |
eval{$dbi->update('table1')}; |
371 |
like($@, qr/Key-value pairs for update must be specified to 'update' second argument/, |
|
372 |
"$test : update key-value pairs not specified"); |
|
373 |
|
|
374 |
eval{$dbi->update('table1', {key2 => 1})}; |
|
375 |
like($@, qr/Key-value pairs for where clause must be specified to 'update' third argument/, |
|
376 |
"$test : where key-value pairs not specified"); |
|
377 |
|
|
378 |
test 'update_all'; |
|
379 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
add all tests
|
380 |
$dbi->query($CREATE_TABLE->{1}); |
packaging one directory
|
381 |
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
382 |
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
add tests
|
383 |
$dbi->resist_filter(twice => sub { $_[0] * 2 }); |
384 |
$dbi->update_all('table1', {key2 => 10}, {filter => {key2 => 'twice'}}); |
|
many change
|
385 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
386 |
$rows = $result->fetch_hash_all; |
387 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
388 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
add tests
|
389 |
"$test : filter"); |
packaging one directory
|
390 |
|
391 |
|
|
392 |
test 'delete'; |
|
393 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
add all tests
|
394 |
$dbi->query($CREATE_TABLE->{0}); |
packaging one directory
|
395 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
396 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
refactoring delete and delet...
|
397 |
$dbi->delete('table1', {where => {key1 => 1}}); |
many change
|
398 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
399 |
$rows = $result->fetch_hash_all; |
400 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic"); |
|
401 |
|
|
add all tests
|
402 |
$dbi->query("delete from table1;"); |
packaging one directory
|
403 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
404 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
add tests
|
405 |
$dbi->resist_filter(twice => sub { $_[0] * 2 }); |
406 |
$dbi->delete('table1', {where => {key2 => 1}, filter => {key2 => 'twice'}}); |
|
many change
|
407 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
408 |
$rows = $result->fetch_hash_all; |
add tests
|
409 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter"); |
insert, update, delete appnd...
|
410 |
|
add tests
|
411 |
$dbi->delete('table1', {where => {key1 => 1}, append => ' '}); |
insert, update, delete appnd...
|
412 |
|
packaging one directory
|
413 |
$dbi->delete_all('table1'); |
414 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
415 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
refactoring delete and delet...
|
416 |
$dbi->delete('table1', {where => {key1 => 1, key2 => 2}}); |
packaging one directory
|
417 |
$rows = $dbi->select('table1')->fetch_hash_all; |
418 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key"); |
|
419 |
|
|
420 |
|
|
421 |
test 'delete error'; |
|
422 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
add all tests
|
423 |
$dbi->query($CREATE_TABLE->{0}); |
packaging one directory
|
424 |
eval{$dbi->delete('table1')}; |
425 |
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/, |
|
426 |
"$test : where key-value pairs not specified"); |
|
427 |
|
|
428 |
test 'delete_all'; |
|
429 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
add all tests
|
430 |
$dbi->query($CREATE_TABLE->{0}); |
packaging one directory
|
431 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
432 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
433 |
$dbi->delete_all('table1'); |
|
many change
|
434 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
packaging one directory
|
435 |
$rows = $result->fetch_hash_all; |
436 |
is_deeply($rows, [], "$test : basic"); |
|
437 |
|
|
438 |
|
|
439 |
test 'select'; |
|
440 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
add all tests
|
441 |
$dbi->query($CREATE_TABLE->{0}); |
packaging one directory
|
442 |
$dbi->insert('table1', {key1 => 1, key2 => 2}); |
443 |
$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
444 |
$rows = $dbi->select('table1')->fetch_hash_all; |
|
445 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
446 |
{key1 => 3, key2 => 4}], "$test : table"); |
|
447 |
|
|
refactoring select
|
448 |
$rows = $dbi->select('table1', {columns => ['key1']})->fetch_hash_all; |
packaging one directory
|
449 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key"); |
450 |
|
|
refactoring select
|
451 |
$rows = $dbi->select('table1', {where => {key1 => 1}})->fetch_hash_all; |
packaging one directory
|
452 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key"); |
453 |
|
|
refactoring select
|
454 |
$rows = $dbi->select('table1', {columns => ['key1'], where => {key1 => 3}})->fetch_hash_all; |
packaging one directory
|
455 |
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key"); |
456 |
|
|
refactoring select
|
457 |
$rows = $dbi->select('table1', {append => "order by key1 desc limit 1"})->fetch_hash_all; |
packaging one directory
|
458 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement"); |
459 |
|
|
add tests
|
460 |
$dbi->resist_filter(decrement => sub { $_[0] - 1 }); |
461 |
$rows = $dbi->select('table1', {where => {key1 => 2}, filter => {key1 => 'decrement'}}) |
|
462 |
->fetch_hash_all; |
|
463 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : filter"); |
|
packaging one directory
|
464 |
|
add all tests
|
465 |
$dbi->query($CREATE_TABLE->{2}); |
packaging one directory
|
466 |
$dbi->insert('table2', {key1 => 1, key3 => 5}); |
467 |
$rows = $dbi->select([qw/table1 table2/], |
|
refactoring select
|
468 |
{ |
469 |
columns => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
470 |
where => {'table1.key2' => 2}, |
|
471 |
append => "where table1.key1 = table2.key1" |
|
472 |
} |
|
473 |
)->fetch_hash_all; |
|
packaging one directory
|
474 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join"); |
475 |
|
|
476 |
test 'Cache'; |
|
477 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
|
478 |
DBIx::Custom->query_cache_max(2); |
|
add all tests
|
479 |
$dbi->query($CREATE_TABLE->{0}); |
cleanup
|
480 |
delete $DBIx::Custom::CLASS_ATTRS->{_query_caches}; |
481 |
delete $DBIx::Custom::CLASS_ATTRS->{_query_cache_keys}; |
|
packaging one directory
|
482 |
$tmpls[0] = "insert into table1 {insert key1 key2}"; |
483 |
$queries[0] = $dbi->create_query($tmpls[0]); |
|
484 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first"); |
|
add tests
|
485 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{columns}, $queries[0]->columns, "$test : columns first"); |
packaging one directory
|
486 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key first"); |
487 |
|
|
488 |
$tmpls[1] = "select * from table1"; |
|
489 |
$queries[1] = $dbi->create_query($tmpls[1]); |
|
490 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first"); |
|
add tests
|
491 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{columns}, $queries[0]->columns, "$test : columns first"); |
packaging one directory
|
492 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql second"); |
add tests
|
493 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns second"); |
packaging one directory
|
494 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key second"); |
495 |
|
|
496 |
$tmpls[2] = "select key1, key2 from table1"; |
|
497 |
$queries[2] = $dbi->create_query($tmpls[2]); |
|
498 |
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key"); |
|
499 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key"); |
|
add tests
|
500 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns cache overflow deleted key"); |
packaging one directory
|
501 |
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key"); |
add tests
|
502 |
is(DBIx::Custom->_query_caches->{$tmpls[2]}{columns}, $queries[2]->columns, "$test : columns cache overflow deleted key"); |
packaging one directory
|
503 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third"); |
504 |
|
|
505 |
$queries[1] = $dbi->create_query($tmpls[1]); |
|
506 |
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key"); |
|
507 |
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key"); |
|
add tests
|
508 |
is_deeply(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns cache overflow deleted key"); |
packaging one directory
|
509 |
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key"); |
add tests
|
510 |
is_deeply(DBIx::Custom->_query_caches->{$tmpls[2]}{columns}, $queries[2]->columns, "$test : columns cache overflow deleted key"); |
packaging one directory
|
511 |
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third"); |
512 |
|
|
fix timeformat tests
|
513 |
$query = $dbi->create_query($tmpls[0]); |
many change
|
514 |
$query->filter('aaa'); |
fix timeformat tests
|
515 |
$query = $dbi->create_query($tmpls[0]); |
add tests
|
516 |
ok(!$query->filter, "$test : only cached sql and columns"); |
many change
|
517 |
$query->filter('bbb'); |
fix timeformat tests
|
518 |
$query = $dbi->create_query($tmpls[0]); |
add tests
|
519 |
ok(!$query->filter, "$test : only cached sql and columns"); |
fix timeformat tests
|
520 |
|
521 |
|