DBIx-Custom / t / sqlite.t /
Newer Older
353 lines | 11.736kb
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;
cleanup test
Yuki Kimoto authored on 2011-08-06
40

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

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

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

            
50

            
test cleanup
Yuki Kimoto authored on 2011-08-10
51
test 'prefix';
52
$dbi = DBIx::Custom->connect;
53
eval { $dbi->execute('drop table table1') };
54
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));');
cleanup
Yuki Kimoto authored on 2011-10-21
55
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
56
$dbi->insert({key1 => 1, key2 => 4}, table => 'table1', prefix => 'or replace');
test cleanup
Yuki Kimoto authored on 2011-08-10
57
$result = $dbi->execute('select * from table1;');
58
$rows   = $result->all;
59
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
60

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

            
62
test 'insert created_at and updated_at scalar reference';
test cleanup
Yuki Kimoto authored on 2011-08-10
63
$dbi = DBIx::Custom->connect;
64
eval { $dbi->execute('drop table table1') };
- insert method created_at a...
Yuki Kimoto authored on 2011-10-27
65
$dbi->execute('create table table1 (key1, key2, key3)');
66
$dbi->now(\"datetime('now')");
67
$dbi->insert({key1 => \"datetime('now')"}, created_at => 'key2', updated_at => 'key3', table => 'table1');
68
$result = $dbi->select(table => 'table1');
69
$row   = $result->one;
70
is($row->{key1}, $row->{key2});
71
is($row->{key1}, $row->{key3});
72

            
73
test 'insert created_at and updated_at scalar reference';
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
$dbi->insert({key1 => \"datetime('now')"}, created_at => 'key2', updated_at => 'key3', table => 'table1');
79
$result = $dbi->select(table => 'table1');
80
$row   = $result->one;
81
is($row->{key1}, $row->{key2});
82
is($row->{key1}, $row->{key3});
83

            
84
test 'update updated_at scalar reference';
85
$dbi = DBIx::Custom->connect;
86
eval { $dbi->execute('drop table table1') };
87
$dbi->execute('create table table1 (key1, key2)');
88
$dbi->now(\"datetime('now')");
89
$dbi->insert({key1 => \"datetime('now')"}, updated_at => 'key2', table => 'table1');
90
$result = $dbi->select(table => 'table1');
91
$row   = $result->one;
92
is($row->{key1}, $row->{key2});
test cleanup
Yuki Kimoto authored on 2011-08-10
93

            
fixed DEBUG messsage bug
Yuki Kimoto authored on 2011-10-24
94
test 'DBIX_CUSTOM_DEBUG ok';
95
{
96
    local $ENV{DBIX_CUSTOM_DEBUG} = 1;
97
    $dbi = DBIx::Custom->connect;
98
    eval { $dbi->execute('drop table table1') };
99
    my $error;
100
    local $SIG{__WARN__} = sub {
101
        $error = shift;
102
    };
103
    $dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));');
104
    ok($error);
105
}
test cleanup
Yuki Kimoto authored on 2011-08-10
106

            
test cleanup
Yuki Kimoto authored on 2011-08-10
107
test 'quote';
108
$dbi = DBIx::Custom->connect;
109
$dbi->quote('"');
110
eval { $dbi->execute("drop table ${q}table$p") };
111
$dbi->execute($create_table_reserved);
112
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
cleanup
Yuki Kimoto authored on 2011-10-21
113
$dbi->insert({select => 1}, table => 'table');
test cleanup
Yuki Kimoto authored on 2011-08-10
114
$dbi->delete(table => 'table', where => {select => 1});
115
$result = $dbi->execute("select * from ${q}table$p");
116
$rows   = $result->all;
117
is_deeply($rows, [], "reserved word");
118

            
test cleanup
Yuki Kimoto authored on 2011-08-10
119
test 'finish statement handle';
120
$dbi = DBIx::Custom->connect;
121
$dbi->execute($create_table1);
122
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
123
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
124

            
125
$result = $dbi->select(table => 'table1');
126
$row = $result->fetch_first;
127
is_deeply($row, [1, 2], "row");
128
$row = $result->fetch;
129
ok(!$row, "finished");
130

            
131
$result = $dbi->select(table => 'table1');
132
$row = $result->fetch_hash_first;
133
is_deeply($row, {key1 => 1, key2 => 2}, "row");
134
$row = $result->fetch_hash;
135
ok(!$row, "finished");
136

            
137
$dbi->execute('create table table2 (key1, key2);');
138
$result = $dbi->select(table => 'table2');
139
$row = $result->fetch_hash_first;
140
ok(!$row, "no row fetch");
141

            
142
$dbi = DBIx::Custom->connect;
143
eval { $dbi->execute('drop table table1') };
144
$dbi->execute($create_table1);
145
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
146
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
147
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
148
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
149
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
150
$result = $dbi->select(table => 'table1');
151
$rows = $result->fetch_multi(2);
152
is_deeply($rows, [[1, 2],
153
                  [3, 4]], "fetch_multi first");
154
$rows = $result->fetch_multi(2);
155
is_deeply($rows, [[5, 6],
156
                  [7, 8]], "fetch_multi secound");
157
$rows = $result->fetch_multi(2);
158
is_deeply($rows, [[9, 10]], "fetch_multi third");
159
$rows = $result->fetch_multi(2);
160
ok(!$rows);
161

            
162
$result = $dbi->select(table => 'table1');
163
eval {$result->fetch_multi};
164
like($@, qr/Row count must be specified/, "Not specified row count");
165

            
166
$result = $dbi->select(table => 'table1');
167
$rows = $result->fetch_hash_multi(2);
168
is_deeply($rows, [{key1 => 1, key2 => 2},
169
                  {key1 => 3, key2 => 4}], "fetch_multi first");
170
$rows = $result->fetch_hash_multi(2);
171
is_deeply($rows, [{key1 => 5, key2 => 6},
172
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
173
$rows = $result->fetch_hash_multi(2);
174
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
175
$rows = $result->fetch_hash_multi(2);
176
ok(!$rows);
177

            
178
$result = $dbi->select(table => 'table1');
179
eval {$result->fetch_hash_multi};
180
like($@, qr/Row count must be specified/, "Not specified row count");
181

            
test cleanup
Yuki Kimoto authored on 2011-08-10
182

            
cleanup test
Yuki Kimoto authored on 2011-08-10
183
test 'type option'; # DEPRECATED!
184
$dbi = DBIx::Custom->connect(
185
    data_source => 'dbi:SQLite:dbname=:memory:',
186
    dbi_option => {
187
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
188
    }
189
);
190
$binary = pack("I3", 1, 2, 3);
191
eval { $dbi->execute('drop table table1') };
192
$dbi->execute('create table table1(key1, key2)');
cleanup
Yuki Kimoto authored on 2011-10-21
193
$dbi->insert({key1 => $binary, key2 => 'あ'}, table => 'table1', type => [key1 => DBI::SQL_BLOB]);
cleanup test
Yuki Kimoto authored on 2011-08-10
194
$result = $dbi->select(table => 'table1');
195
$row   = $result->one;
196
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
197
$result = $dbi->execute('select length(key1) as key1_length from table1');
198
$row = $result->one;
199
is($row->{key1_length}, length $binary);
200

            
micro optimization
Yuki Kimoto authored on 2011-10-23
201
test 'bind_type option'; # DEPRECATED!
202
$binary = pack("I3", 1, 2, 3);
203
eval { $dbi->execute('drop table table1') };
204
$dbi->execute('create table table1(key1, key2)');
205
$dbi->insert({key1 => $binary, key2 => 'あ'}, table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
206
$result = $dbi->select(table => 'table1');
207
$row   = $result->one;
208
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
209
$result = $dbi->execute('select length(key1) as key1_length from table1');
210
$row = $result->one;
211
is($row->{key1_length}, length $binary);
212

            
cleanup test
Yuki Kimoto authored on 2011-08-10
213
test 'type_rule from';
214
$dbi = DBIx::Custom->connect;
215
$dbi->type_rule(
216
    from1 => {
217
        date => sub { uc $_[0] }
218
    }
219
);
220
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
221
$dbi->insert({key1 => 'a'}, table => 'table1');
222
$result = $dbi->select(table => 'table1');
223
is($result->fetch_first->[0], 'A');
224

            
225
$result = $dbi->select(table => 'table1');
226
is($result->one->{key1}, 'A');
227

            
added SQL Server test
Yuki Kimoto authored on 2011-08-14
228
test 'select limit';
229
eval { $dbi->execute('drop table table1') };
230
$dbi->execute($create_table1);
cleanup
Yuki Kimoto authored on 2011-10-21
231
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
232
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
added SQL Server test
Yuki Kimoto authored on 2011-08-14
233
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all;
234
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement");
235

            
236

            
237

            
test cleanup
Yuki Kimoto authored on 2011-08-10
238
# DEPRECATED! test
239
test 'filter __ expression';
240
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
241
eval { $dbi->execute('drop table table2') };
242
eval { $dbi->execute('drop table table3') };
243
$dbi->execute('create table table2 (id, name, table3_id)');
244
$dbi->execute('create table table3 (id, name)');
245
$dbi->apply_filter('table3',
test cleanup
Yuki Kimoto authored on 2011-08-10
246
  name => {in => sub { uc $_[0] } }
247
);
248

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

            
252
$result = $dbi->select(
cleanup test
Yuki Kimoto authored on 2011-08-10
253
    table => ['table2', 'table3'], relation => {'table2.table3_id' => 'table3.id'},
254
    column => ['table3.name as table3__name']
test cleanup
Yuki Kimoto authored on 2011-08-10
255
);
256
is($result->fetch_first->[0], 'B');
257

            
258
$result = $dbi->select(
cleanup test
Yuki Kimoto authored on 2011-08-10
259
    table => 'table2', relation => {'table2.table3_id' => 'table3.id'},
260
    column => ['table3.name as table3__name']
test cleanup
Yuki Kimoto authored on 2011-08-10
261
);
262
is($result->fetch_first->[0], 'B');
263

            
264
$result = $dbi->select(
cleanup test
Yuki Kimoto authored on 2011-08-10
265
    table => 'table2', relation => {'table2.table3_id' => 'table3.id'},
266
    column => ['table3.name as "table3.name"']
test cleanup
Yuki Kimoto authored on 2011-08-10
267
);
268
is($result->fetch_first->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
269

            
270
test 'reserved_word_quote';
271
$dbi = DBIx::Custom->connect;
272
eval { $dbi->execute("drop table ${q}table$p") };
273
$dbi->reserved_word_quote('"');
274
$dbi->execute($create_table_reserved);
275
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
276
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
cleanup
Yuki Kimoto authored on 2011-10-21
277
$dbi->insert({select => 1}, table => 'table');
278
$dbi->update({update => 2}, table => 'table', where => {'table.select' => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
279
$result = $dbi->execute("select * from ${q}table$p");
280
$rows   = $result->all;
281
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
added SQL Server test
Yuki Kimoto authored on 2011-08-14
282

            
283
test 'limit tag';
284
$dbi = DBIx::Custom->connect;
285
eval { $dbi->execute('drop table table1') };
286
$dbi->execute($create_table1);
cleanup
Yuki Kimoto authored on 2011-10-21
287
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
288
$dbi->insert({key1 => 1, key2 => 4}, table => 'table1');
289
$dbi->insert({key1 => 1, key2 => 6}, table => 'table1');
added SQL Server test
Yuki Kimoto authored on 2011-08-14
290
$dbi->register_tag(
291
    limit => sub {
292
        my ($count, $offset) = @_;
293
        
294
        my $s = '';
295
        $s .= "limit $count";
296
        $s .= " offset $offset" if defined $offset;
297
        
298
        return [$s, []];
299
    }
300
);
301
$rows = $dbi->select(
302
  table => 'table1',
303
  where => {key1 => 1},
304
  append => "order by key2 {limit 1 0}"
305
)->all;
306
is_deeply($rows, [{key1 => 1, key2 => 2}]);
307
$rows = $dbi->select(
308
  table => 'table1',
309
  where => {key1 => 1},
310
  append => "order by key2 {limit 2 1}"
311
)->all;
312
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
313
$rows = $dbi->select(
314
  table => 'table1',
315
  where => {key1 => 1},
316
  append => "order by key2 {limit 1}"
317
)->all;
318
is_deeply($rows, [{key1 => 1, key2 => 2}]);
improved join clause parsing
Yuki Kimoto authored on 2011-09-30
319

            
320
test 'join function';
321
$dbi = DBIx::Custom->connect;
322
eval { $dbi->execute("drop table table1") };
323
eval { $dbi->execute("drop table table2") };
324
$dbi->execute($create_table1);
325
$dbi->execute("create table table2 (key1, key3)");
cleanup
Yuki Kimoto authored on 2011-10-21
326
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
327
$dbi->insert({key1 => 1, key3 => 4}, table => 'table2');
328
$dbi->insert({key1 => 1, key3 => 1}, table => 'table2');
improved join clause parsing
Yuki Kimoto authored on 2011-09-30
329
$result = $dbi->select(
330
    table => 'table1',
331
    column => [{table2 => ['key3']}],
332
    join => [
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
333
        "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
334
    ]
335
);
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
336
is_deeply($result->all, [{"table2.key3" => 4}]);
337

            
338
$dbi = DBIx::Custom->connect;
339
eval { $dbi->execute("drop table table1") };
340
eval { $dbi->execute("drop table table2") };
341
$dbi->execute($create_table1);
342
$dbi->execute("create table table2 (key1, key3)");
cleanup
Yuki Kimoto authored on 2011-10-21
343
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
344
$dbi->insert({key1 => 1, key3 => 4}, table => 'table2');
345
$dbi->insert({key1 => 1, key3 => 1}, table => 'table2');
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
346
$result = $dbi->select(
347
    table => 'table1',
348
    column => [{table2 => ['key3']}],
349
    join => [
350
        "left outer join table2 on table2.key3 > '3' and coalesce(table1.key1, 0) = coalesce(table2.key1, 0)"
351
    ]
352
);
353
is_deeply($result->all, [{"table2.key3" => 4}]);