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

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

            
49

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

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

            
70

            
test cleanup
Yuki Kimoto authored on 2011-08-10
71
test 'quote';
72
$dbi = DBIx::Custom->connect;
73
$dbi->quote('"');
74
eval { $dbi->execute("drop table ${q}table$p") };
75
$dbi->execute($create_table_reserved);
76
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
77
$dbi->insert(table => 'table', param => {select => 1});
78
$dbi->delete(table => 'table', where => {select => 1});
79
$result = $dbi->execute("select * from ${q}table$p");
80
$rows   = $result->all;
81
is_deeply($rows, [], "reserved word");
82

            
test cleanup
Yuki Kimoto authored on 2011-08-10
83
test 'finish statement handle';
84
$dbi = DBIx::Custom->connect;
85
$dbi->execute($create_table1);
86
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
87
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
88

            
89
$result = $dbi->select(table => 'table1');
90
$row = $result->fetch_first;
91
is_deeply($row, [1, 2], "row");
92
$row = $result->fetch;
93
ok(!$row, "finished");
94

            
95
$result = $dbi->select(table => 'table1');
96
$row = $result->fetch_hash_first;
97
is_deeply($row, {key1 => 1, key2 => 2}, "row");
98
$row = $result->fetch_hash;
99
ok(!$row, "finished");
100

            
101
$dbi->execute('create table table2 (key1, key2);');
102
$result = $dbi->select(table => 'table2');
103
$row = $result->fetch_hash_first;
104
ok(!$row, "no row fetch");
105

            
106
$dbi = DBIx::Custom->connect;
107
eval { $dbi->execute('drop table table1') };
108
$dbi->execute($create_table1);
109
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
110
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
111
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
112
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
113
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
114
$result = $dbi->select(table => 'table1');
115
$rows = $result->fetch_multi(2);
116
is_deeply($rows, [[1, 2],
117
                  [3, 4]], "fetch_multi first");
118
$rows = $result->fetch_multi(2);
119
is_deeply($rows, [[5, 6],
120
                  [7, 8]], "fetch_multi secound");
121
$rows = $result->fetch_multi(2);
122
is_deeply($rows, [[9, 10]], "fetch_multi third");
123
$rows = $result->fetch_multi(2);
124
ok(!$rows);
125

            
126
$result = $dbi->select(table => 'table1');
127
eval {$result->fetch_multi};
128
like($@, qr/Row count must be specified/, "Not specified row count");
129

            
130
$result = $dbi->select(table => 'table1');
131
$rows = $result->fetch_hash_multi(2);
132
is_deeply($rows, [{key1 => 1, key2 => 2},
133
                  {key1 => 3, key2 => 4}], "fetch_multi first");
134
$rows = $result->fetch_hash_multi(2);
135
is_deeply($rows, [{key1 => 5, key2 => 6},
136
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
137
$rows = $result->fetch_hash_multi(2);
138
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
139
$rows = $result->fetch_hash_multi(2);
140
ok(!$rows);
141

            
142
$result = $dbi->select(table => 'table1');
143
eval {$result->fetch_hash_multi};
144
like($@, qr/Row count must be specified/, "Not specified row count");
145

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
147
test 'type option'; # DEPRECATED!
148
$dbi = DBIx::Custom->connect(
149
    data_source => 'dbi:SQLite:dbname=:memory:',
150
    dbi_option => {
151
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
152
    }
153
);
154
$binary = pack("I3", 1, 2, 3);
155
eval { $dbi->execute('drop table table1') };
156
$dbi->execute('create table table1(key1, key2)');
157
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
158
$result = $dbi->select(table => 'table1');
159
$row   = $result->one;
160
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
161
$result = $dbi->execute('select length(key1) as key1_length from table1');
162
$row = $result->one;
163
is($row->{key1_length}, length $binary);
164

            
165
test 'type_rule from';
166
$dbi = DBIx::Custom->connect;
167
$dbi->type_rule(
168
    from1 => {
169
        date => sub { uc $_[0] }
170
    }
171
);
172
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
173
$dbi->insert({key1 => 'a'}, table => 'table1');
174
$result = $dbi->select(table => 'table1');
175
is($result->fetch_first->[0], 'A');
176

            
177
$result = $dbi->select(table => 'table1');
178
is($result->one->{key1}, 'A');
179

            
added SQL Server test
Yuki Kimoto authored on 2011-08-14
180
test 'select limit';
181
eval { $dbi->execute('drop table table1') };
182
$dbi->execute($create_table1);
183
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
184
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
185
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all;
186
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement");
187

            
188

            
189

            
test cleanup
Yuki Kimoto authored on 2011-08-10
190
# DEPRECATED! test
191
test 'filter __ expression';
192
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
193
eval { $dbi->execute('drop table table2') };
194
eval { $dbi->execute('drop table table3') };
195
$dbi->execute('create table table2 (id, name, table3_id)');
196
$dbi->execute('create table table3 (id, name)');
197
$dbi->apply_filter('table3',
test cleanup
Yuki Kimoto authored on 2011-08-10
198
  name => {in => sub { uc $_[0] } }
199
);
200

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

            
204
$result = $dbi->select(
cleanup test
Yuki Kimoto authored on 2011-08-10
205
    table => ['table2', 'table3'], relation => {'table2.table3_id' => 'table3.id'},
206
    column => ['table3.name as table3__name']
test cleanup
Yuki Kimoto authored on 2011-08-10
207
);
208
is($result->fetch_first->[0], 'B');
209

            
210
$result = $dbi->select(
cleanup test
Yuki Kimoto authored on 2011-08-10
211
    table => 'table2', relation => {'table2.table3_id' => 'table3.id'},
212
    column => ['table3.name as table3__name']
test cleanup
Yuki Kimoto authored on 2011-08-10
213
);
214
is($result->fetch_first->[0], 'B');
215

            
216
$result = $dbi->select(
cleanup test
Yuki Kimoto authored on 2011-08-10
217
    table => 'table2', relation => {'table2.table3_id' => 'table3.id'},
218
    column => ['table3.name as "table3.name"']
test cleanup
Yuki Kimoto authored on 2011-08-10
219
);
220
is($result->fetch_first->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
221

            
222
test 'reserved_word_quote';
223
$dbi = DBIx::Custom->connect;
224
eval { $dbi->execute("drop table ${q}table$p") };
225
$dbi->reserved_word_quote('"');
226
$dbi->execute($create_table_reserved);
227
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
228
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
229
$dbi->insert(table => 'table', param => {select => 1});
230
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
231
$result = $dbi->execute("select * from ${q}table$p");
232
$rows   = $result->all;
233
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
added SQL Server test
Yuki Kimoto authored on 2011-08-14
234

            
235
test 'limit tag';
236
$dbi = DBIx::Custom->connect;
237
eval { $dbi->execute('drop table table1') };
238
$dbi->execute($create_table1);
239
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
240
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
241
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
242
$dbi->register_tag(
243
    limit => sub {
244
        my ($count, $offset) = @_;
245
        
246
        my $s = '';
247
        $s .= "limit $count";
248
        $s .= " offset $offset" if defined $offset;
249
        
250
        return [$s, []];
251
    }
252
);
253
$rows = $dbi->select(
254
  table => 'table1',
255
  where => {key1 => 1},
256
  append => "order by key2 {limit 1 0}"
257
)->all;
258
is_deeply($rows, [{key1 => 1, key2 => 2}]);
259
$rows = $dbi->select(
260
  table => 'table1',
261
  where => {key1 => 1},
262
  append => "order by key2 {limit 2 1}"
263
)->all;
264
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
265
$rows = $dbi->select(
266
  table => 'table1',
267
  where => {key1 => 1},
268
  append => "order by key2 {limit 1}"
269
)->all;
270
is_deeply($rows, [{key1 => 1, key2 => 2}]);
improved join clause parsing
Yuki Kimoto authored on 2011-09-30
271

            
272
test 'join function';
273
$dbi = DBIx::Custom->connect;
274
eval { $dbi->execute("drop table table1") };
275
eval { $dbi->execute("drop table table2") };
276
$dbi->execute($create_table1);
277
$dbi->execute("create table table2 (key1, key3)");
278
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
279
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
280
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 1});
281
$result = $dbi->select(
282
    table => 'table1',
283
    column => [{table2 => ['key3']}],
284
    join => [
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
285
        "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
286
    ]
287
);
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
288
is_deeply($result->all, [{"table2.key3" => 4}]);
289

            
290
$dbi = DBIx::Custom->connect;
291
eval { $dbi->execute("drop table table1") };
292
eval { $dbi->execute("drop table table2") };
293
$dbi->execute($create_table1);
294
$dbi->execute("create table table2 (key1, key3)");
295
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
296
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
297
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 1});
298
$result = $dbi->select(
299
    table => 'table1',
300
    column => [{table2 => ['key3']}],
301
    join => [
302
        "left outer join table2 on table2.key3 > '3' and coalesce(table1.key1, 0) = coalesce(table2.key1, 0)"
303
    ]
304
);
305
is_deeply($result->all, [{"table2.key3" => 4}]);