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