cleanup test
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 |
use utf8; |
|
5 |
use Encode qw/encode_utf8 decode_utf8/; |
|
test cleanup
|
6 |
use FindBin; |
cleanup test
|
7 |
use lib "$FindBin::Bin/common"; |
cleanup test
|
8 | |
9 |
BEGIN { |
|
cleanup
|
10 |
eval { require DBD::SQLite; 1 } |
11 |
or plan skip_all => 'DBD::SQLite required'; |
|
12 |
eval { DBD::SQLite->VERSION >= 1.25 } |
|
13 |
or plan skip_all => 'DBD::SQLite >= 1.25 required'; |
|
cleanup test
|
14 | |
cleanup
|
15 |
plan 'no_plan'; |
16 |
use_ok('DBIx::Custom'); |
|
cleanup test
|
17 |
} |
18 | ||
test cleanup
|
19 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
cleanup test
|
20 |
sub test { print "# $_[0]\n" } |
21 | ||
test cleanup
|
22 |
use DBIx::Custom; |
test cleanup
|
23 |
{ |
cleanup
|
24 |
package DBIx::Custom; |
25 |
has dsn => sub { 'dbi:SQLite:dbname=:memory:' } |
|
test cleanup
|
26 |
} |
test cleanup
|
27 | |
cleanup test
|
28 |
# Constant |
cleanup test
|
29 |
my $create_table1 = 'create table table1 (key1 varchar, key2 varchar);'; |
30 |
my $create_table_reserved = 'create table "table" ("select" varchar, "update" varchar)'; |
|
test cleanup
|
31 |
my $q = '"'; |
32 |
my $p = '"'; |
|
cleanup test
|
33 | |
cleanup test
|
34 |
# Variables |
35 |
my $dbi; |
|
36 |
my $result; |
|
37 |
my $row; |
|
38 |
my $rows; |
|
cleanup test
|
39 |
my $binary; |
micro optimization
|
40 |
my $model; |
cleanup test
|
41 | |
42 |
# Prepare table |
|
test cleanup
|
43 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
44 | |
fixed DEBUG messsage bug
|
45 | |
test cleanup
|
46 |
### SQLite only test |
cleanup test
|
47 |
test 'dbi_option default'; |
48 |
$dbi = DBIx::Custom->new; |
|
49 |
is_deeply($dbi->dbi_option, {}); |
|
50 | ||
51 | ||
test cleanup
|
52 |
test 'prefix'; |
53 |
$dbi = DBIx::Custom->connect; |
|
54 |
eval { $dbi->execute('drop table table1') }; |
|
55 |
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));'); |
|
cleanup
|
56 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
57 |
$dbi->insert({key1 => 1, key2 => 4}, table => 'table1', prefix => 'or replace'); |
|
test cleanup
|
58 |
$result = $dbi->execute('select * from table1;'); |
59 |
$rows = $result->all; |
|
60 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
test cleanup
|
61 | |
- insert method created_at a...
|
62 | |
63 |
test 'insert created_at and updated_at scalar reference'; |
|
test cleanup
|
64 |
$dbi = DBIx::Custom->connect; |
65 |
eval { $dbi->execute('drop table table1') }; |
|
- insert method created_at a...
|
66 |
$dbi->execute('create table table1 (key1, key2, key3)'); |
67 |
$dbi->now(\"datetime('now')"); |
|
68 |
$dbi->insert({key1 => \"datetime('now')"}, created_at => 'key2', updated_at => 'key3', table => 'table1'); |
|
69 |
$result = $dbi->select(table => 'table1'); |
|
70 |
$row = $result->one; |
|
71 |
is($row->{key1}, $row->{key2}); |
|
72 |
is($row->{key1}, $row->{key3}); |
|
73 | ||
micro optimization
|
74 |
$dbi = DBIx::Custom->connect; |
75 |
eval { $dbi->execute('drop table table1') }; |
|
76 |
$dbi->execute('create table table1 (key1, key2, key3)'); |
|
77 |
$dbi->now(\"datetime('now')"); |
|
78 |
$model = $dbi->create_model(created_at => 'key2', updated_at => 'key3', table => 'table1'); |
|
79 |
$model->insert({key1 => \"datetime('now')"}); |
|
80 |
$result = $dbi->select(table => 'table1'); |
|
81 |
$row = $result->one; |
|
82 |
is($row->{key1}, $row->{key2}); |
|
83 |
is($row->{key1}, $row->{key3}); |
|
84 | ||
- insert method created_at a...
|
85 |
test 'insert created_at and updated_at scalar reference'; |
86 |
$dbi = DBIx::Custom->connect; |
|
87 |
eval { $dbi->execute('drop table table1') }; |
|
88 |
$dbi->execute('create table table1 (key1, key2, key3)'); |
|
89 |
$dbi->now(\"datetime('now')"); |
|
90 |
$dbi->insert({key1 => \"datetime('now')"}, created_at => 'key2', updated_at => 'key3', table => 'table1'); |
|
91 |
$result = $dbi->select(table => 'table1'); |
|
92 |
$row = $result->one; |
|
93 |
is($row->{key1}, $row->{key2}); |
|
94 |
is($row->{key1}, $row->{key3}); |
|
95 | ||
96 |
test 'update updated_at scalar reference'; |
|
97 |
$dbi = DBIx::Custom->connect; |
|
98 |
eval { $dbi->execute('drop table table1') }; |
|
99 |
$dbi->execute('create table table1 (key1, key2)'); |
|
100 |
$dbi->now(\"datetime('now')"); |
|
101 |
$dbi->insert({key1 => \"datetime('now')"}, updated_at => 'key2', table => 'table1'); |
|
102 |
$result = $dbi->select(table => 'table1'); |
|
103 |
$row = $result->one; |
|
104 |
is($row->{key1}, $row->{key2}); |
|
test cleanup
|
105 | |
micro optimization
|
106 |
test 'update_or_insert created_at and updated_at'; |
107 |
eval { $dbi->execute('drop table table1') }; |
|
108 |
$dbi->execute('create table table1 (key1, key2, key3, key4)'); |
|
109 |
$dbi->now(\"datetime('now')"); |
|
110 |
$model = $dbi->create_model(created_at => 'key2', updated_at => 'key3', table => 'table1', |
|
111 |
primary_key => 'key4'); |
|
112 |
$model->update_or_insert({key1 => \"datetime('now')"}, id => 1); |
|
113 |
$result = $model->select(table => 'table1', id => 1); |
|
114 |
$row = $result->one; |
|
115 |
is($row->{key1}, $row->{key2}); |
|
116 |
is($row->{key1}, $row->{key3}); |
|
117 | ||
118 |
$dbi = DBIx::Custom->connect; |
|
119 |
eval { $dbi->execute('drop table table1') }; |
|
120 |
$dbi->execute('create table table1 (key1, key2)'); |
|
121 |
$dbi->now(\"datetime('now')"); |
|
122 |
$model = $dbi->create_model(updated_at => 'key2', table => 'table1'); |
|
123 |
$model->insert({key1 => \"datetime('now')"}); |
|
124 |
$result = $dbi->select(table => 'table1'); |
|
125 |
$row = $result->one; |
|
126 |
is($row->{key1}, $row->{key2}); |
|
127 | ||
fixed DEBUG messsage bug
|
128 |
test 'DBIX_CUSTOM_DEBUG ok'; |
129 |
{ |
|
cleanup
|
130 |
local $ENV{DBIX_CUSTOM_DEBUG} = 1; |
131 |
$dbi = DBIx::Custom->connect; |
|
132 |
eval { $dbi->execute('drop table table1') }; |
|
133 |
my $error; |
|
134 |
local $SIG{__WARN__} = sub { |
|
135 |
$error = shift; |
|
136 |
}; |
|
137 |
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));'); |
|
138 |
ok($error); |
|
fixed DEBUG messsage bug
|
139 |
} |
test cleanup
|
140 | |
test cleanup
|
141 |
test 'quote'; |
142 |
$dbi = DBIx::Custom->connect; |
|
143 |
$dbi->quote('"'); |
|
144 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
145 |
$dbi->execute($create_table_reserved); |
|
146 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
cleanup
|
147 |
$dbi->insert({select => 1}, table => 'table'); |
test cleanup
|
148 |
$dbi->delete(table => 'table', where => {select => 1}); |
149 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
150 |
$rows = $result->all; |
|
151 |
is_deeply($rows, [], "reserved word"); |
|
152 | ||
test cleanup
|
153 |
test 'finish statement handle'; |
154 |
$dbi = DBIx::Custom->connect; |
|
155 |
$dbi->execute($create_table1); |
|
156 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
157 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
158 | ||
159 |
$result = $dbi->select(table => 'table1'); |
|
- renamed DBIx::Custom::Resu...
|
160 |
$row = $result->fetch_one; |
test cleanup
|
161 |
is_deeply($row, [1, 2], "row"); |
162 |
$row = $result->fetch; |
|
163 |
ok(!$row, "finished"); |
|
164 | ||
165 |
$result = $dbi->select(table => 'table1'); |
|
- renamed DBIx::Custom::Resu...
|
166 |
$row = $result->fetch_hash_one; |
test cleanup
|
167 |
is_deeply($row, {key1 => 1, key2 => 2}, "row"); |
168 |
$row = $result->fetch_hash; |
|
169 |
ok(!$row, "finished"); |
|
170 | ||
171 |
$dbi->execute('create table table2 (key1, key2);'); |
|
172 |
$result = $dbi->select(table => 'table2'); |
|
- renamed DBIx::Custom::Resu...
|
173 |
$row = $result->fetch_hash_one; |
test cleanup
|
174 |
ok(!$row, "no row fetch"); |
175 | ||
176 |
$dbi = DBIx::Custom->connect; |
|
177 |
eval { $dbi->execute('drop table table1') }; |
|
178 |
$dbi->execute($create_table1); |
|
179 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
180 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
181 |
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1'); |
|
182 |
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1'); |
|
183 |
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1'); |
|
184 |
$result = $dbi->select(table => 'table1'); |
|
185 |
$rows = $result->fetch_multi(2); |
|
186 |
is_deeply($rows, [[1, 2], |
|
cleanup
|
187 |
[3, 4]], "fetch_multi first"); |
test cleanup
|
188 |
$rows = $result->fetch_multi(2); |
189 |
is_deeply($rows, [[5, 6], |
|
cleanup
|
190 |
[7, 8]], "fetch_multi secound"); |
test cleanup
|
191 |
$rows = $result->fetch_multi(2); |
192 |
is_deeply($rows, [[9, 10]], "fetch_multi third"); |
|
193 |
$rows = $result->fetch_multi(2); |
|
194 |
ok(!$rows); |
|
195 | ||
196 |
$result = $dbi->select(table => 'table1'); |
|
197 |
eval {$result->fetch_multi}; |
|
198 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
199 | ||
200 |
$result = $dbi->select(table => 'table1'); |
|
201 |
$rows = $result->fetch_hash_multi(2); |
|
202 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
cleanup
|
203 |
{key1 => 3, key2 => 4}], "fetch_multi first"); |
test cleanup
|
204 |
$rows = $result->fetch_hash_multi(2); |
205 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
|
cleanup
|
206 |
{key1 => 7, key2 => 8}], "fetch_multi secound"); |
test cleanup
|
207 |
$rows = $result->fetch_hash_multi(2); |
208 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third"); |
|
209 |
$rows = $result->fetch_hash_multi(2); |
|
210 |
ok(!$rows); |
|
211 | ||
212 |
$result = $dbi->select(table => 'table1'); |
|
213 |
eval {$result->fetch_hash_multi}; |
|
214 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
215 | ||
test cleanup
|
216 | |
cleanup test
|
217 |
test 'type option'; # DEPRECATED! |
218 |
$dbi = DBIx::Custom->connect( |
|
cleanup
|
219 |
data_source => 'dbi:SQLite:dbname=:memory:', |
220 |
dbi_option => { |
|
221 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
222 |
} |
|
cleanup test
|
223 |
); |
224 |
$binary = pack("I3", 1, 2, 3); |
|
225 |
eval { $dbi->execute('drop table table1') }; |
|
226 |
$dbi->execute('create table table1(key1, key2)'); |
|
cleanup
|
227 |
$dbi->insert({key1 => $binary, key2 => 'あ'}, table => 'table1', type => [key1 => DBI::SQL_BLOB]); |
cleanup test
|
228 |
$result = $dbi->select(table => 'table1'); |
229 |
$row = $result->one; |
|
230 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
231 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
232 |
$row = $result->one; |
|
233 |
is($row->{key1_length}, length $binary); |
|
234 | ||
micro optimization
|
235 |
test 'bind_type option'; # DEPRECATED! |
236 |
$binary = pack("I3", 1, 2, 3); |
|
237 |
eval { $dbi->execute('drop table table1') }; |
|
238 |
$dbi->execute('create table table1(key1, key2)'); |
|
239 |
$dbi->insert({key1 => $binary, key2 => 'あ'}, table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]); |
|
240 |
$result = $dbi->select(table => 'table1'); |
|
241 |
$row = $result->one; |
|
242 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
243 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
244 |
$row = $result->one; |
|
245 |
is($row->{key1_length}, length $binary); |
|
246 | ||
cleanup test
|
247 |
test 'type_rule from'; |
248 |
$dbi = DBIx::Custom->connect; |
|
249 |
$dbi->type_rule( |
|
cleanup
|
250 |
from1 => { |
251 |
date => sub { uc $_[0] } |
|
252 |
} |
|
cleanup test
|
253 |
); |
254 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
255 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
256 |
$result = $dbi->select(table => 'table1'); |
|
- renamed DBIx::Custom::Resu...
|
257 |
is($result->fetch_one->[0], 'A'); |
cleanup test
|
258 | |
259 |
$result = $dbi->select(table => 'table1'); |
|
260 |
is($result->one->{key1}, 'A'); |
|
261 | ||
added SQL Server test
|
262 |
test 'select limit'; |
263 |
eval { $dbi->execute('drop table table1') }; |
|
264 |
$dbi->execute($create_table1); |
|
cleanup
|
265 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
266 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
added SQL Server test
|
267 |
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all; |
268 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement"); |
|
269 | ||
270 | ||
271 | ||
test cleanup
|
272 |
# DEPRECATED! test |
273 |
test 'filter __ expression'; |
|
274 |
$dbi = DBIx::Custom->connect; |
|
cleanup test
|
275 |
eval { $dbi->execute('drop table table2') }; |
276 |
eval { $dbi->execute('drop table table3') }; |
|
277 |
$dbi->execute('create table table2 (id, name, table3_id)'); |
|
278 |
$dbi->execute('create table table3 (id, name)'); |
|
279 |
$dbi->apply_filter('table3', |
|
cleanup
|
280 |
name => {in => sub { uc $_[0] } } |
test cleanup
|
281 |
); |
282 | ||
cleanup
|
283 |
$dbi->insert({id => 1, name => 'a', table3_id => 2}, table => 'table2'); |
284 |
$dbi->insert({id => 2, name => 'b'}, table => 'table3'); |
|
test cleanup
|
285 | |
286 |
$result = $dbi->select( |
|
cleanup
|
287 |
table => ['table2', 'table3'], relation => {'table2.table3_id' => 'table3.id'}, |
288 |
column => ['table3.name as table3__name'] |
|
test cleanup
|
289 |
); |
- renamed DBIx::Custom::Resu...
|
290 |
is($result->fetch_one->[0], 'B'); |
test cleanup
|
291 | |
292 |
$result = $dbi->select( |
|
cleanup
|
293 |
table => 'table2', relation => {'table2.table3_id' => 'table3.id'}, |
294 |
column => ['table3.name as table3__name'] |
|
test cleanup
|
295 |
); |
- renamed DBIx::Custom::Resu...
|
296 |
is($result->fetch_one->[0], 'B'); |
test cleanup
|
297 | |
298 |
$result = $dbi->select( |
|
cleanup
|
299 |
table => 'table2', relation => {'table2.table3_id' => 'table3.id'}, |
300 |
column => ['table3.name as "table3.name"'] |
|
test cleanup
|
301 |
); |
- renamed DBIx::Custom::Resu...
|
302 |
is($result->fetch_one->[0], 'B'); |
test cleanup
|
303 | |
304 |
test 'reserved_word_quote'; |
|
305 |
$dbi = DBIx::Custom->connect; |
|
306 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
307 |
$dbi->reserved_word_quote('"'); |
|
308 |
$dbi->execute($create_table_reserved); |
|
309 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
310 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
cleanup
|
311 |
$dbi->insert({select => 1}, table => 'table'); |
312 |
$dbi->update({update => 2}, table => 'table', where => {'table.select' => 1}); |
|
test cleanup
|
313 |
$result = $dbi->execute("select * from ${q}table$p"); |
314 |
$rows = $result->all; |
|
315 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |
|
added SQL Server test
|
316 | |
317 |
test 'limit tag'; |
|
318 |
$dbi = DBIx::Custom->connect; |
|
319 |
eval { $dbi->execute('drop table table1') }; |
|
320 |
$dbi->execute($create_table1); |
|
cleanup
|
321 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
322 |
$dbi->insert({key1 => 1, key2 => 4}, table => 'table1'); |
|
323 |
$dbi->insert({key1 => 1, key2 => 6}, table => 'table1'); |
|
added SQL Server test
|
324 |
$dbi->register_tag( |
cleanup
|
325 |
limit => sub { |
326 |
my ($count, $offset) = @_; |
|
327 |
|
|
328 |
my $s = ''; |
|
329 |
$s .= "limit $count"; |
|
330 |
$s .= " offset $offset" if defined $offset; |
|
331 |
|
|
332 |
return [$s, []]; |
|
333 |
} |
|
added SQL Server test
|
334 |
); |
335 |
$rows = $dbi->select( |
|
cleanup
|
336 |
table => 'table1', |
337 |
where => {key1 => 1}, |
|
338 |
append => "order by key2 {limit 1 0}" |
|
added SQL Server test
|
339 |
)->all; |
340 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
341 |
$rows = $dbi->select( |
|
cleanup
|
342 |
table => 'table1', |
343 |
where => {key1 => 1}, |
|
344 |
append => "order by key2 {limit 2 1}" |
|
added SQL Server test
|
345 |
)->all; |
346 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]); |
|
347 |
$rows = $dbi->select( |
|
cleanup
|
348 |
table => 'table1', |
349 |
where => {key1 => 1}, |
|
350 |
append => "order by key2 {limit 1}" |
|
added SQL Server test
|
351 |
)->all; |
352 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
improved join clause parsing
|
353 | |
354 |
test 'join function'; |
|
355 |
$dbi = DBIx::Custom->connect; |
|
356 |
eval { $dbi->execute("drop table table1") }; |
|
357 |
eval { $dbi->execute("drop table table2") }; |
|
358 |
$dbi->execute($create_table1); |
|
359 |
$dbi->execute("create table table2 (key1, key3)"); |
|
cleanup
|
360 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
361 |
$dbi->insert({key1 => 1, key3 => 4}, table => 'table2'); |
|
362 |
$dbi->insert({key1 => 1, key3 => 1}, table => 'table2'); |
|
improved join clause parsing
|
363 |
$result = $dbi->select( |
cleanup
|
364 |
table => 'table1', |
365 |
column => [{table2 => ['key3']}], |
|
366 |
join => [ |
|
367 |
"left outer join table2 on coalesce(table1.key1, 0) = coalesce(table2.key1, 0) and table2.key3 > '3'" |
|
368 |
] |
|
improved join clause parsing
|
369 |
); |
- update_param is DEPRECATED...
|
370 |
is_deeply($result->all, [{"table2.key3" => 4}]); |
371 | ||
372 |
$dbi = DBIx::Custom->connect; |
|
373 |
eval { $dbi->execute("drop table table1") }; |
|
374 |
eval { $dbi->execute("drop table table2") }; |
|
375 |
$dbi->execute($create_table1); |
|
376 |
$dbi->execute("create table table2 (key1, key3)"); |
|
cleanup
|
377 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
378 |
$dbi->insert({key1 => 1, key3 => 4}, table => 'table2'); |
|
379 |
$dbi->insert({key1 => 1, key3 => 1}, table => 'table2'); |
|
- update_param is DEPRECATED...
|
380 |
$result = $dbi->select( |
cleanup
|
381 |
table => 'table1', |
382 |
column => [{table2 => ['key3']}], |
|
383 |
join => [ |
|
384 |
"left outer join table2 on table2.key3 > '3' and coalesce(table1.key1, 0) = coalesce(table2.key1, 0)" |
|
385 |
] |
|
- update_param is DEPRECATED...
|
386 |
); |
387 |
is_deeply($result->all, [{"table2.key3" => 4}]); |