DBIx-Custom / t / sqlite.t /
Newer Older
330 lines | 10.874kb
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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
61
$dbi = DBIx::Custom->connect;
62
eval { $dbi->execute('drop table table1') };
63
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));');
cleanup
Yuki Kimoto authored on 2011-10-21
64
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
65
$dbi->update({key2 => 4}, table => 'table1',
test cleanup
Yuki Kimoto authored on 2011-08-10
66
  where => {key1 => 1}, prefix => 'or replace');
67
$result = $dbi->execute('select * from table1;');
68
$rows   = $result->all;
69
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
70

            
fixed DEBUG messsage bug
Yuki Kimoto authored on 2011-10-24
71
test 'DBIX_CUSTOM_DEBUG ok';
72
{
73
    local $ENV{DBIX_CUSTOM_DEBUG} = 1;
74
    $dbi = DBIx::Custom->connect;
75
    eval { $dbi->execute('drop table table1') };
76
    my $error;
77
    local $SIG{__WARN__} = sub {
78
        $error = shift;
79
    };
80
    $dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));');
81
    ok($error);
82
}
test cleanup
Yuki Kimoto authored on 2011-08-10
83

            
test cleanup
Yuki Kimoto authored on 2011-08-10
84
test 'quote';
85
$dbi = DBIx::Custom->connect;
86
$dbi->quote('"');
87
eval { $dbi->execute("drop table ${q}table$p") };
88
$dbi->execute($create_table_reserved);
89
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
cleanup
Yuki Kimoto authored on 2011-10-21
90
$dbi->insert({select => 1}, table => 'table');
test cleanup
Yuki Kimoto authored on 2011-08-10
91
$dbi->delete(table => 'table', where => {select => 1});
92
$result = $dbi->execute("select * from ${q}table$p");
93
$rows   = $result->all;
94
is_deeply($rows, [], "reserved word");
95

            
test cleanup
Yuki Kimoto authored on 2011-08-10
96
test 'finish statement handle';
97
$dbi = DBIx::Custom->connect;
98
$dbi->execute($create_table1);
99
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
100
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
101

            
102
$result = $dbi->select(table => 'table1');
103
$row = $result->fetch_first;
104
is_deeply($row, [1, 2], "row");
105
$row = $result->fetch;
106
ok(!$row, "finished");
107

            
108
$result = $dbi->select(table => 'table1');
109
$row = $result->fetch_hash_first;
110
is_deeply($row, {key1 => 1, key2 => 2}, "row");
111
$row = $result->fetch_hash;
112
ok(!$row, "finished");
113

            
114
$dbi->execute('create table table2 (key1, key2);');
115
$result = $dbi->select(table => 'table2');
116
$row = $result->fetch_hash_first;
117
ok(!$row, "no row fetch");
118

            
119
$dbi = DBIx::Custom->connect;
120
eval { $dbi->execute('drop table table1') };
121
$dbi->execute($create_table1);
122
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
123
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
124
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
125
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
126
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
127
$result = $dbi->select(table => 'table1');
128
$rows = $result->fetch_multi(2);
129
is_deeply($rows, [[1, 2],
130
                  [3, 4]], "fetch_multi first");
131
$rows = $result->fetch_multi(2);
132
is_deeply($rows, [[5, 6],
133
                  [7, 8]], "fetch_multi secound");
134
$rows = $result->fetch_multi(2);
135
is_deeply($rows, [[9, 10]], "fetch_multi third");
136
$rows = $result->fetch_multi(2);
137
ok(!$rows);
138

            
139
$result = $dbi->select(table => 'table1');
140
eval {$result->fetch_multi};
141
like($@, qr/Row count must be specified/, "Not specified row count");
142

            
143
$result = $dbi->select(table => 'table1');
144
$rows = $result->fetch_hash_multi(2);
145
is_deeply($rows, [{key1 => 1, key2 => 2},
146
                  {key1 => 3, key2 => 4}], "fetch_multi first");
147
$rows = $result->fetch_hash_multi(2);
148
is_deeply($rows, [{key1 => 5, key2 => 6},
149
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
150
$rows = $result->fetch_hash_multi(2);
151
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
152
$rows = $result->fetch_hash_multi(2);
153
ok(!$rows);
154

            
155
$result = $dbi->select(table => 'table1');
156
eval {$result->fetch_hash_multi};
157
like($@, qr/Row count must be specified/, "Not specified row count");
158

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
160
test 'type option'; # DEPRECATED!
161
$dbi = DBIx::Custom->connect(
162
    data_source => 'dbi:SQLite:dbname=:memory:',
163
    dbi_option => {
164
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
165
    }
166
);
167
$binary = pack("I3", 1, 2, 3);
168
eval { $dbi->execute('drop table table1') };
169
$dbi->execute('create table table1(key1, key2)');
cleanup
Yuki Kimoto authored on 2011-10-21
170
$dbi->insert({key1 => $binary, key2 => 'あ'}, table => 'table1', type => [key1 => DBI::SQL_BLOB]);
cleanup test
Yuki Kimoto authored on 2011-08-10
171
$result = $dbi->select(table => 'table1');
172
$row   = $result->one;
173
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
174
$result = $dbi->execute('select length(key1) as key1_length from table1');
175
$row = $result->one;
176
is($row->{key1_length}, length $binary);
177

            
micro optimization
Yuki Kimoto authored on 2011-10-23
178
test 'bind_type option'; # DEPRECATED!
179
$binary = pack("I3", 1, 2, 3);
180
eval { $dbi->execute('drop table table1') };
181
$dbi->execute('create table table1(key1, key2)');
182
$dbi->insert({key1 => $binary, key2 => 'あ'}, table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]);
183
$result = $dbi->select(table => 'table1');
184
$row   = $result->one;
185
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
186
$result = $dbi->execute('select length(key1) as key1_length from table1');
187
$row = $result->one;
188
is($row->{key1_length}, length $binary);
189

            
cleanup test
Yuki Kimoto authored on 2011-08-10
190
test 'type_rule from';
191
$dbi = DBIx::Custom->connect;
192
$dbi->type_rule(
193
    from1 => {
194
        date => sub { uc $_[0] }
195
    }
196
);
197
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
198
$dbi->insert({key1 => 'a'}, table => 'table1');
199
$result = $dbi->select(table => 'table1');
200
is($result->fetch_first->[0], 'A');
201

            
202
$result = $dbi->select(table => 'table1');
203
is($result->one->{key1}, 'A');
204

            
added SQL Server test
Yuki Kimoto authored on 2011-08-14
205
test 'select limit';
206
eval { $dbi->execute('drop table table1') };
207
$dbi->execute($create_table1);
cleanup
Yuki Kimoto authored on 2011-10-21
208
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
209
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
added SQL Server test
Yuki Kimoto authored on 2011-08-14
210
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all;
211
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement");
212

            
213

            
214

            
test cleanup
Yuki Kimoto authored on 2011-08-10
215
# DEPRECATED! test
216
test 'filter __ expression';
217
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
218
eval { $dbi->execute('drop table table2') };
219
eval { $dbi->execute('drop table table3') };
220
$dbi->execute('create table table2 (id, name, table3_id)');
221
$dbi->execute('create table table3 (id, name)');
222
$dbi->apply_filter('table3',
test cleanup
Yuki Kimoto authored on 2011-08-10
223
  name => {in => sub { uc $_[0] } }
224
);
225

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

            
229
$result = $dbi->select(
cleanup test
Yuki Kimoto authored on 2011-08-10
230
    table => ['table2', 'table3'], relation => {'table2.table3_id' => 'table3.id'},
231
    column => ['table3.name as table3__name']
test cleanup
Yuki Kimoto authored on 2011-08-10
232
);
233
is($result->fetch_first->[0], 'B');
234

            
235
$result = $dbi->select(
cleanup test
Yuki Kimoto authored on 2011-08-10
236
    table => 'table2', relation => {'table2.table3_id' => 'table3.id'},
237
    column => ['table3.name as table3__name']
test cleanup
Yuki Kimoto authored on 2011-08-10
238
);
239
is($result->fetch_first->[0], 'B');
240

            
241
$result = $dbi->select(
cleanup test
Yuki Kimoto authored on 2011-08-10
242
    table => 'table2', relation => {'table2.table3_id' => 'table3.id'},
243
    column => ['table3.name as "table3.name"']
test cleanup
Yuki Kimoto authored on 2011-08-10
244
);
245
is($result->fetch_first->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
246

            
247
test 'reserved_word_quote';
248
$dbi = DBIx::Custom->connect;
249
eval { $dbi->execute("drop table ${q}table$p") };
250
$dbi->reserved_word_quote('"');
251
$dbi->execute($create_table_reserved);
252
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
253
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
cleanup
Yuki Kimoto authored on 2011-10-21
254
$dbi->insert({select => 1}, table => 'table');
255
$dbi->update({update => 2}, table => 'table', where => {'table.select' => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
256
$result = $dbi->execute("select * from ${q}table$p");
257
$rows   = $result->all;
258
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
added SQL Server test
Yuki Kimoto authored on 2011-08-14
259

            
260
test 'limit tag';
261
$dbi = DBIx::Custom->connect;
262
eval { $dbi->execute('drop table table1') };
263
$dbi->execute($create_table1);
cleanup
Yuki Kimoto authored on 2011-10-21
264
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
265
$dbi->insert({key1 => 1, key2 => 4}, table => 'table1');
266
$dbi->insert({key1 => 1, key2 => 6}, table => 'table1');
added SQL Server test
Yuki Kimoto authored on 2011-08-14
267
$dbi->register_tag(
268
    limit => sub {
269
        my ($count, $offset) = @_;
270
        
271
        my $s = '';
272
        $s .= "limit $count";
273
        $s .= " offset $offset" if defined $offset;
274
        
275
        return [$s, []];
276
    }
277
);
278
$rows = $dbi->select(
279
  table => 'table1',
280
  where => {key1 => 1},
281
  append => "order by key2 {limit 1 0}"
282
)->all;
283
is_deeply($rows, [{key1 => 1, key2 => 2}]);
284
$rows = $dbi->select(
285
  table => 'table1',
286
  where => {key1 => 1},
287
  append => "order by key2 {limit 2 1}"
288
)->all;
289
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
290
$rows = $dbi->select(
291
  table => 'table1',
292
  where => {key1 => 1},
293
  append => "order by key2 {limit 1}"
294
)->all;
295
is_deeply($rows, [{key1 => 1, key2 => 2}]);
improved join clause parsing
Yuki Kimoto authored on 2011-09-30
296

            
297
test 'join function';
298
$dbi = DBIx::Custom->connect;
299
eval { $dbi->execute("drop table table1") };
300
eval { $dbi->execute("drop table table2") };
301
$dbi->execute($create_table1);
302
$dbi->execute("create table table2 (key1, key3)");
cleanup
Yuki Kimoto authored on 2011-10-21
303
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
304
$dbi->insert({key1 => 1, key3 => 4}, table => 'table2');
305
$dbi->insert({key1 => 1, key3 => 1}, table => 'table2');
improved join clause parsing
Yuki Kimoto authored on 2011-09-30
306
$result = $dbi->select(
307
    table => 'table1',
308
    column => [{table2 => ['key3']}],
309
    join => [
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
310
        "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
311
    ]
312
);
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
313
is_deeply($result->all, [{"table2.key3" => 4}]);
314

            
315
$dbi = DBIx::Custom->connect;
316
eval { $dbi->execute("drop table table1") };
317
eval { $dbi->execute("drop table table2") };
318
$dbi->execute($create_table1);
319
$dbi->execute("create table table2 (key1, key3)");
cleanup
Yuki Kimoto authored on 2011-10-21
320
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
321
$dbi->insert({key1 => 1, key3 => 4}, table => 'table2');
322
$dbi->insert({key1 => 1, key3 => 1}, table => 'table2');
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
323
$result = $dbi->select(
324
    table => 'table1',
325
    column => [{table2 => ['key3']}],
326
    join => [
327
        "left outer join table2 on table2.key3 > '3' and coalesce(table1.key1, 0) = coalesce(table2.key1, 0)"
328
    ]
329
);
330
is_deeply($result->all, [{"table2.key3" => 4}]);