DBIx-Custom / t / sqlite.t /
Newer Older
387 lines | 13.045kb
cleanup test
Yuki Kimoto authored on 2011-08-06
1
use Test::More;
2
use strict;
3
use warnings;
4
use utf8;
5
use Encode qw/encode_utf8 decode_utf8/;
test cleanup
Yuki Kimoto authored on 2011-08-06
6
use FindBin;
cleanup test
Yuki Kimoto authored on 2011-08-10
7
use lib "$FindBin::Bin/common";
cleanup test
Yuki Kimoto authored on 2011-08-06
8

            
9
BEGIN {
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';
14

            
15
    plan 'no_plan';
16
    use_ok('DBIx::Custom');
17
}
18

            
test cleanup
Yuki Kimoto authored on 2011-08-06
19
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
cleanup test
Yuki Kimoto authored on 2011-08-06
20
sub test { print "# $_[0]\n" }
21

            
test cleanup
Yuki Kimoto authored on 2011-08-10
22
use DBIx::Custom;
test cleanup
Yuki Kimoto authored on 2011-08-10
23
{
24
    package DBIx::Custom;
25
    has dsn => sub { 'dbi:SQLite:dbname=:memory:' }
26
}
test cleanup
Yuki Kimoto authored on 2011-08-10
27

            
cleanup test
Yuki Kimoto authored on 2011-08-06
28
# Constant
cleanup test
Yuki Kimoto authored on 2011-08-10
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
Yuki Kimoto authored on 2011-08-10
31
my $q = '"';
32
my $p = '"';
cleanup test
Yuki Kimoto authored on 2011-08-06
33

            
cleanup test
Yuki Kimoto authored on 2011-08-06
34
# Variables
35
my $dbi;
36
my $result;
37
my $row;
38
my $rows;
cleanup test
Yuki Kimoto authored on 2011-08-10
39
my $binary;
micro optimization
Yuki Kimoto authored on 2011-10-31
40
my $model;
cleanup test
Yuki Kimoto authored on 2011-08-06
41

            
42
# Prepare table
test cleanup
Yuki Kimoto authored on 2011-08-10
43
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
44

            
fixed DEBUG messsage bug
Yuki Kimoto authored on 2011-10-24
45

            
test cleanup
Yuki Kimoto authored on 2011-08-10
46
### SQLite only test
cleanup test
Yuki Kimoto authored on 2011-08-15
47
test 'dbi_option default';
48
$dbi = DBIx::Custom->new;
49
is_deeply($dbi->dbi_option, {});
50

            
51

            
test cleanup
Yuki Kimoto authored on 2011-08-10
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
Yuki Kimoto authored on 2011-10-21
56
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
57
$dbi->insert({key1 => 1, key2 => 4}, table => 'table1', prefix => 'or replace');
test cleanup
Yuki Kimoto authored on 2011-08-10
58
$result = $dbi->execute('select * from table1;');
59
$rows   = $result->all;
60
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
61

            
- insert method created_at a...
Yuki Kimoto authored on 2011-10-27
62

            
63
test 'insert created_at and updated_at scalar reference';
test cleanup
Yuki Kimoto authored on 2011-08-10
64
$dbi = DBIx::Custom->connect;
65
eval { $dbi->execute('drop table table1') };
- insert method created_at a...
Yuki Kimoto authored on 2011-10-27
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
Yuki Kimoto authored on 2011-10-31
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...
Yuki Kimoto authored on 2011-10-27
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
Yuki Kimoto authored on 2011-08-10
105

            
micro optimization
Yuki Kimoto authored on 2011-10-31
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
Yuki Kimoto authored on 2011-10-24
128
test 'DBIX_CUSTOM_DEBUG ok';
129
{
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);
139
}
test cleanup
Yuki Kimoto authored on 2011-08-10
140

            
test cleanup
Yuki Kimoto authored on 2011-08-10
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
Yuki Kimoto authored on 2011-10-21
147
$dbi->insert({select => 1}, table => 'table');
test cleanup
Yuki Kimoto authored on 2011-08-10
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
Yuki Kimoto authored on 2011-08-10
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...
Yuki Kimoto authored on 2012-01-20
160
$row = $result->fetch_one;
test cleanup
Yuki Kimoto authored on 2011-08-10
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...
Yuki Kimoto authored on 2012-01-20
166
$row = $result->fetch_hash_one;
test cleanup
Yuki Kimoto authored on 2011-08-10
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...
Yuki Kimoto authored on 2012-01-20
173
$row = $result->fetch_hash_one;
test cleanup
Yuki Kimoto authored on 2011-08-10
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],
187
                  [3, 4]], "fetch_multi first");
188
$rows = $result->fetch_multi(2);
189
is_deeply($rows, [[5, 6],
190
                  [7, 8]], "fetch_multi secound");
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},
203
                  {key1 => 3, key2 => 4}], "fetch_multi first");
204
$rows = $result->fetch_hash_multi(2);
205
is_deeply($rows, [{key1 => 5, key2 => 6},
206
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
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
Yuki Kimoto authored on 2011-08-10
216

            
cleanup test
Yuki Kimoto authored on 2011-08-10
217
test 'type option'; # DEPRECATED!
218
$dbi = DBIx::Custom->connect(
219
    data_source => 'dbi:SQLite:dbname=:memory:',
220
    dbi_option => {
221
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
222
    }
223
);
224
$binary = pack("I3", 1, 2, 3);
225
eval { $dbi->execute('drop table table1') };
226
$dbi->execute('create table table1(key1, key2)');
cleanup
Yuki Kimoto authored on 2011-10-21
227
$dbi->insert({key1 => $binary, key2 => 'あ'}, table => 'table1', type => [key1 => DBI::SQL_BLOB]);
cleanup test
Yuki Kimoto authored on 2011-08-10
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
Yuki Kimoto authored on 2011-10-23
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
Yuki Kimoto authored on 2011-08-10
247
test 'type_rule from';
248
$dbi = DBIx::Custom->connect;
249
$dbi->type_rule(
250
    from1 => {
251
        date => sub { uc $_[0] }
252
    }
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...
Yuki Kimoto authored on 2012-01-20
257
is($result->fetch_one->[0], 'A');
cleanup test
Yuki Kimoto authored on 2011-08-10
258

            
259
$result = $dbi->select(table => 'table1');
260
is($result->one->{key1}, 'A');
261

            
added SQL Server test
Yuki Kimoto authored on 2011-08-14
262
test 'select limit';
263
eval { $dbi->execute('drop table table1') };
264
$dbi->execute($create_table1);
cleanup
Yuki Kimoto authored on 2011-10-21
265
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
266
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
added SQL Server test
Yuki Kimoto authored on 2011-08-14
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
Yuki Kimoto authored on 2011-08-10
272
# DEPRECATED! test
273
test 'filter __ expression';
274
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
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',
test cleanup
Yuki Kimoto authored on 2011-08-10
280
  name => {in => sub { uc $_[0] } }
281
);
282

            
cleanup
Yuki Kimoto authored on 2011-10-21
283
$dbi->insert({id => 1, name => 'a', table3_id => 2}, table => 'table2');
284
$dbi->insert({id => 2, name => 'b'}, table => 'table3');
test cleanup
Yuki Kimoto authored on 2011-08-10
285

            
286
$result = $dbi->select(
cleanup test
Yuki Kimoto authored on 2011-08-10
287
    table => ['table2', 'table3'], relation => {'table2.table3_id' => 'table3.id'},
288
    column => ['table3.name as table3__name']
test cleanup
Yuki Kimoto authored on 2011-08-10
289
);
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
290
is($result->fetch_one->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
291

            
292
$result = $dbi->select(
cleanup test
Yuki Kimoto authored on 2011-08-10
293
    table => 'table2', relation => {'table2.table3_id' => 'table3.id'},
294
    column => ['table3.name as table3__name']
test cleanup
Yuki Kimoto authored on 2011-08-10
295
);
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
296
is($result->fetch_one->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
297

            
298
$result = $dbi->select(
cleanup test
Yuki Kimoto authored on 2011-08-10
299
    table => 'table2', relation => {'table2.table3_id' => 'table3.id'},
300
    column => ['table3.name as "table3.name"']
test cleanup
Yuki Kimoto authored on 2011-08-10
301
);
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
302
is($result->fetch_one->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
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
Yuki Kimoto authored on 2011-10-21
311
$dbi->insert({select => 1}, table => 'table');
312
$dbi->update({update => 2}, table => 'table', where => {'table.select' => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
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
Yuki Kimoto authored on 2011-08-14
316

            
317
test 'limit tag';
318
$dbi = DBIx::Custom->connect;
319
eval { $dbi->execute('drop table table1') };
320
$dbi->execute($create_table1);
cleanup
Yuki Kimoto authored on 2011-10-21
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
Yuki Kimoto authored on 2011-08-14
324
$dbi->register_tag(
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
    }
334
);
335
$rows = $dbi->select(
336
  table => 'table1',
337
  where => {key1 => 1},
338
  append => "order by key2 {limit 1 0}"
339
)->all;
340
is_deeply($rows, [{key1 => 1, key2 => 2}]);
341
$rows = $dbi->select(
342
  table => 'table1',
343
  where => {key1 => 1},
344
  append => "order by key2 {limit 2 1}"
345
)->all;
346
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
347
$rows = $dbi->select(
348
  table => 'table1',
349
  where => {key1 => 1},
350
  append => "order by key2 {limit 1}"
351
)->all;
352
is_deeply($rows, [{key1 => 1, key2 => 2}]);
improved join clause parsing
Yuki Kimoto authored on 2011-09-30
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
Yuki Kimoto authored on 2011-10-21
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
Yuki Kimoto authored on 2011-09-30
363
$result = $dbi->select(
364
    table => 'table1',
365
    column => [{table2 => ['key3']}],
366
    join => [
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
367
        "left outer join table2 on coalesce(table1.key1, 0) = coalesce(table2.key1, 0) and table2.key3 > '3'"
improved join clause parsing
Yuki Kimoto authored on 2011-09-30
368
    ]
369
);
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
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
Yuki Kimoto authored on 2011-10-21
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...
Yuki Kimoto authored on 2011-10-04
380
$result = $dbi->select(
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
    ]
386
);
387
is_deeply($result->all, [{"table2.key3" => 4}]);