DBIx-Custom / t / next / sqlite.t /
Newer Older
328 lines | 11.03kb
added Next version
Yuki Kimoto authored on 2011-11-16
1
use Test::More;
2
use strict;
3
use warnings;
4
use utf8;
5
use Encode qw/encode_utf8 decode_utf8/;
6
use FindBin;
7
use lib "$FindBin::Bin/common";
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';
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
16
    use_ok('DBIx::Custom');
added Next version
Yuki Kimoto authored on 2011-11-16
17
}
18

            
19
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
20
sub test { print "# $_[0]\n" }
21

            
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
22
use DBIx::Custom;
added Next version
Yuki Kimoto authored on 2011-11-16
23
{
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
24
    package DBIx::Custom;
added Next version
Yuki Kimoto authored on 2011-11-16
25
    has dsn => sub { 'dbi:SQLite:dbname=:memory:' }
26
}
27

            
28
# Constant
29
my $create_table1 = 'create table table1 (key1 varchar, key2 varchar);';
30
my $create_table_quote = 'create table "table" ("select" varchar, "update" varchar)';
31
my $q = '"';
32
my $p = '"';
33

            
34
# Variables
35
my $dbi;
36
my $result;
37
my $row;
38
my $rows;
39
my $binary;
40
my $model;
41

            
42
# Prepare table
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
43
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
44

            
45

            
46
### SQLite only test
47
test 'option default';
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
48
$dbi = DBIx::Custom->new;
added Next version
Yuki Kimoto authored on 2011-11-16
49
is_deeply($dbi->option, {});
50

            
51

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

            
62

            
63
test 'insert created_at and updated_at scalar reference';
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
64
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
65
eval { $dbi->execute('drop table table1') };
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

            
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
74
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
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

            
85
test 'insert created_at and updated_at scalar reference';
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
86
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
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';
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
97
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
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});
105

            
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

            
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
118
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
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

            
128
test 'DBIX_CUSTOM_DEBUG ok';
129
{
130
    local $ENV{DBIX_CUSTOM_DEBUG} = 1;
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
131
    $dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
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
}
140

            
141
test 'quote';
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
142
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
143
$dbi->quote('"');
144
eval { $dbi->execute("drop table ${q}table$p") };
145
$dbi->execute($create_table_quote);
146
$dbi->insert({select => 1}, table => 'table');
147
$dbi->delete(table => 'table', where => {select => 1});
148
$result = $dbi->execute("select * from ${q}table$p");
149
$rows   = $result->all;
150
is_deeply($rows, [], "quote");
151

            
152
test 'finish statement handle';
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
153
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
154
$dbi->execute($create_table1);
155
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
156
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
157

            
158
$result = $dbi->select(table => 'table1');
159
$row = $result->fetch_first;
160
is_deeply($row, [1, 2], "row");
161
$row = $result->fetch;
162
ok(!$row, "finished");
163

            
164
$result = $dbi->select(table => 'table1');
165
$row = $result->fetch_hash_first;
166
is_deeply($row, {key1 => 1, key2 => 2}, "row");
167
$row = $result->fetch_hash;
168
ok(!$row, "finished");
169

            
170
$dbi->execute('create table table2 (key1, key2);');
171
$result = $dbi->select(table => 'table2');
172
$row = $result->fetch_hash_first;
173
ok(!$row, "no row fetch");
174

            
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
175
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
176
eval { $dbi->execute('drop table table1') };
177
$dbi->execute($create_table1);
178
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
179
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
180
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
181
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
182
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
183
$result = $dbi->select(table => 'table1');
184
$rows = $result->fetch_multi(2);
185
is_deeply($rows, [[1, 2],
186
                  [3, 4]], "fetch_multi first");
187
$rows = $result->fetch_multi(2);
188
is_deeply($rows, [[5, 6],
189
                  [7, 8]], "fetch_multi secound");
190
$rows = $result->fetch_multi(2);
191
is_deeply($rows, [[9, 10]], "fetch_multi third");
192
$rows = $result->fetch_multi(2);
193
ok(!$rows);
194

            
195
$result = $dbi->select(table => 'table1');
196
eval {$result->fetch_multi};
197
like($@, qr/Row count must be specified/, "Not specified row count");
198

            
199
$result = $dbi->select(table => 'table1');
200
$rows = $result->fetch_hash_multi(2);
201
is_deeply($rows, [{key1 => 1, key2 => 2},
202
                  {key1 => 3, key2 => 4}], "fetch_multi first");
203
$rows = $result->fetch_hash_multi(2);
204
is_deeply($rows, [{key1 => 5, key2 => 6},
205
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
206
$rows = $result->fetch_hash_multi(2);
207
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
208
$rows = $result->fetch_hash_multi(2);
209
ok(!$rows);
210

            
211
$result = $dbi->select(table => 'table1');
212
eval {$result->fetch_hash_multi};
213
like($@, qr/Row count must be specified/, "Not specified row count");
214

            
215
test 'bind_type option';
216
$binary = pack("I3", 1, 2, 3);
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
217
$dbi = DBIx::Custom->connect(option => {sqlite_unicode => 1});
added Next version
Yuki Kimoto authored on 2011-11-16
218
$dbi->execute('create table table1(key1, key2)');
219
$dbi->insert({key1 => $binary, key2 => 'あ'}, table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
220
$result = $dbi->select(table => 'table1');
221
$row   = $result->one;
222
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
223
$result = $dbi->execute('select length(key1) as key1_length from table1');
224
$row = $result->one;
225
is($row->{key1_length}, length $binary);
226

            
227
test 'type_rule from';
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
228
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
229
$dbi->type_rule(
230
    from1 => {
231
        date => sub { uc $_[0] }
232
    }
233
);
234
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
235
$dbi->insert({key1 => 'a'}, table => 'table1');
236
$result = $dbi->select(table => 'table1');
237
is($result->fetch_first->[0], 'A');
238

            
239
$result = $dbi->select(table => 'table1');
240
is($result->one->{key1}, 'A');
241

            
242
test 'select limit';
243
eval { $dbi->execute('drop table table1') };
244
$dbi->execute($create_table1);
245
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
246
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
247
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all;
248
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement");
249

            
250

            
251

            
252
# DEPRECATED! test
253
test 'filter __ expression';
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
254
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
255
eval { $dbi->execute('drop table table2') };
256
eval { $dbi->execute('drop table table3') };
257
$dbi->execute('create table table2 (id, name, table3_id)');
258
$dbi->execute('create table table3 (id, name)');
259

            
260
$dbi->insert({id => 1, name => 'a', table3_id => 2}, table => 'table2');
261
$dbi->insert({id => 2, name => 'b'}, table => 'table3');
262

            
263
$result = $dbi->select(
264
    table => 'table2',
265
    join => "inner join table3 on table2.table3_id = table3.id",
266
    column => ['table3.name as table3__name']
267
);
268
is($result->fetch_first->[0], 'b');
269

            
270
$result = $dbi->select(
271
    table => 'table2',
272
    join => "inner join table3 on table2.table3_id = table3.id",
273
    column => ['table3.name as table3__name']
274
);
275
is($result->fetch_first->[0], 'b');
276

            
277
$result = $dbi->select(
278
    table => 'table2',
279
    join => "inner join table3 on table2.table3_id = table3.id",
280
    column => ['table3.name as "table3.name"']
281
);
282
is($result->fetch_first->[0], 'b');
283

            
284
test 'quote';
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
285
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
286
eval { $dbi->execute("drop table ${q}table$p") };
287
$dbi->quote('"');
288
$dbi->execute($create_table_quote);
289
$dbi->insert({select => 1}, table => 'table');
290
$dbi->update({update => 2}, table => 'table', where => {'table.select' => 1});
291
$result = $dbi->execute("select * from ${q}table$p");
292
$rows   = $result->all;
293
is_deeply($rows, [{select => 1, update => 2}]);
294

            
295
test 'join function';
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
296
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
297
eval { $dbi->execute("drop table table1") };
298
eval { $dbi->execute("drop table table2") };
299
$dbi->execute($create_table1);
300
$dbi->execute("create table table2 (key1, key3)");
301
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
302
$dbi->insert({key1 => 1, key3 => 4}, table => 'table2');
303
$dbi->insert({key1 => 1, key3 => 1}, table => 'table2');
304
$result = $dbi->select(
305
    table => 'table1',
306
    column => [{table2 => ['key3']}],
307
    join => [
308
        "left outer join table2 on coalesce(table1.key1, 0) = coalesce(table2.key1, 0) and table2.key3 > '3'"
309
    ]
310
);
311
is_deeply($result->all, [{"table2.key3" => 4}]);
312

            
removed DBIx::Custom::Next f...
Yuki Kimoto authored on 2011-11-16
313
$dbi = DBIx::Custom->connect;
added Next version
Yuki Kimoto authored on 2011-11-16
314
eval { $dbi->execute("drop table table1") };
315
eval { $dbi->execute("drop table table2") };
316
$dbi->execute($create_table1);
317
$dbi->execute("create table table2 (key1, key3)");
318
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
319
$dbi->insert({key1 => 1, key3 => 4}, table => 'table2');
320
$dbi->insert({key1 => 1, key3 => 1}, table => 'table2');
321
$result = $dbi->select(
322
    table => 'table1',
323
    column => [{table2 => ['key3']}],
324
    join => [
325
        "left outer join table2 on table2.key3 > '3' and coalesce(table1.key1, 0) = coalesce(table2.key1, 0)"
326
    ]
327
);
328
is_deeply($result->all, [{"table2.key3" => 4}]);