DBIx-Custom / t / sqlite.t /
Newer Older
230 lines | 7.751kb
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_table1_2 = 'create table table1 (key1 varchar, key2 varchar, key3 varchar, key4 varchar, key5 varchar);';
31
my $create_table2 = 'create table table2 (key1 varchar, key3 varchar);';
32
my $create_table2_2 = "create table table2 (key1 varchar, key2 varchar, key3 varchar)";
33
my $create_table3 = "create table table3 (key1 varchar, key2 varchar, key3 varchar)";
34
my $create_table_reserved = 'create table "table" ("select" varchar, "update" varchar)';
test cleanup
Yuki Kimoto authored on 2011-08-10
35

            
test cleanup
Yuki Kimoto authored on 2011-08-10
36
my $q = '"';
37
my $p = '"';
cleanup test
Yuki Kimoto authored on 2011-08-06
38

            
cleanup test
Yuki Kimoto authored on 2011-08-06
39
# Variables
40
my $dbi;
41
my $param;
42
my $params;
43
my $sql;
44
my $result;
45
my $row;
46
my @rows;
47
my $rows;
48
my $model;
49
my $model2;
50
my $where;
cleanup test
Yuki Kimoto authored on 2011-08-10
51
my $binary;
cleanup test
Yuki Kimoto authored on 2011-08-06
52

            
53
# Prepare table
test cleanup
Yuki Kimoto authored on 2011-08-10
54
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
55

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

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

            
77

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
90
test 'finish statement handle';
91
$dbi = DBIx::Custom->connect;
92
$dbi->execute($create_table1);
93
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
94
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
95

            
96
$result = $dbi->select(table => 'table1');
97
$row = $result->fetch_first;
98
is_deeply($row, [1, 2], "row");
99
$row = $result->fetch;
100
ok(!$row, "finished");
101

            
102
$result = $dbi->select(table => 'table1');
103
$row = $result->fetch_hash_first;
104
is_deeply($row, {key1 => 1, key2 => 2}, "row");
105
$row = $result->fetch_hash;
106
ok(!$row, "finished");
107

            
108
$dbi->execute('create table table2 (key1, key2);');
109
$result = $dbi->select(table => 'table2');
110
$row = $result->fetch_hash_first;
111
ok(!$row, "no row fetch");
112

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

            
133
$result = $dbi->select(table => 'table1');
134
eval {$result->fetch_multi};
135
like($@, qr/Row count must be specified/, "Not specified row count");
136

            
137
$result = $dbi->select(table => 'table1');
138
$rows = $result->fetch_hash_multi(2);
139
is_deeply($rows, [{key1 => 1, key2 => 2},
140
                  {key1 => 3, key2 => 4}], "fetch_multi first");
141
$rows = $result->fetch_hash_multi(2);
142
is_deeply($rows, [{key1 => 5, key2 => 6},
143
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
144
$rows = $result->fetch_hash_multi(2);
145
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
146
$rows = $result->fetch_hash_multi(2);
147
ok(!$rows);
148

            
149
$result = $dbi->select(table => 'table1');
150
eval {$result->fetch_hash_multi};
151
like($@, qr/Row count must be specified/, "Not specified row count");
152

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

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

            
172
test 'type_rule from';
173
$dbi = DBIx::Custom->connect;
174
$dbi->type_rule(
175
    from1 => {
176
        date => sub { uc $_[0] }
177
    }
178
);
179
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
180
$dbi->insert({key1 => 'a'}, table => 'table1');
181
$result = $dbi->select(table => 'table1');
182
is($result->fetch_first->[0], 'A');
183

            
184
$result = $dbi->select(table => 'table1');
185
is($result->one->{key1}, 'A');
186

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

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

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

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

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

            
219
test 'reserved_word_quote';
220
$dbi = DBIx::Custom->connect;
221
eval { $dbi->execute("drop table ${q}table$p") };
222
$dbi->reserved_word_quote('"');
223
$dbi->execute($create_table_reserved);
224
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
225
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
226
$dbi->insert(table => 'table', param => {select => 1});
227
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
228
$result = $dbi->execute("select * from ${q}table$p");
229
$rows   = $result->all;
230
is_deeply($rows, [{select => 2, update => 6}], "reserved word");