DBIx-Custom / t / dbix-custom-core-sqlite.t /
Newer Older
610 lines | 23.68kb
removed register_format()
yuki-kimoto authored on 2010-05-26
1
use Test::More;
2
use strict;
3
use warnings;
4

            
5
use utf8;
6
use Encode qw/encode_utf8 decode_utf8/;
7

            
8
BEGIN {
9
    eval { require DBD::SQLite; 1 }
10
        or plan skip_all => 'DBD::SQLite required';
11
    eval { DBD::SQLite->VERSION >= 1.25 }
12
        or plan skip_all => 'DBD::SQLite >= 1.25 required';
13

            
14
    plan 'no_plan';
15
    use_ok('DBIx::Custom');
16
}
17

            
18
# Function for test name
19
my $test;
20
sub test {
21
    $test = shift;
22
}
23

            
24
# Constant varialbes for test
25
my $CREATE_TABLE = {
26
    0 => 'create table table1 (key1 char(255), key2 char(255));',
27
    1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));',
28
    2 => 'create table table2 (key1 char(255), key3 char(255));'
29
};
30

            
add tests
yuki-kimoto authored on 2010-08-10
31
my $SELECT_SOURCES = {
removed register_format()
yuki-kimoto authored on 2010-05-26
32
    0 => 'select * from table1;'
33
};
34

            
35
my $DROP_TABLE = {
36
    0 => 'drop table table1'
37
};
38

            
39
my $NEW_ARGS = {
40
    0 => {data_source => 'dbi:SQLite:dbname=:memory:'}
41
};
42

            
43
# Variables
44
my $dbi;
45
my $sth;
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
46
my $source;
47
my @sources;
add tests
yuki-kimoto authored on 2010-08-10
48
my $select_SOURCE;
49
my $insert_SOURCE;
50
my $update_SOURCE;
removed register_format()
yuki-kimoto authored on 2010-05-26
51
my $params;
52
my $sql;
53
my $result;
54
my $row;
55
my @rows;
56
my $rows;
57
my $query;
58
my @queries;
59
my $select_query;
60
my $insert_query;
61
my $update_query;
62
my $ret_val;
63

            
64

            
65
# Prepare table
66
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
67
$dbi->execute($CREATE_TABLE->{0});
68
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
69
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
70

            
71
test 'DBIx::Custom::Result test';
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
72
$source = "select key1, key2 from table1";
73
$query = $dbi->create_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
74
$result = $dbi->execute($query);
75

            
76
@rows = ();
77
while (my $row = $result->fetch) {
78
    push @rows, [@$row];
79
}
removed reconnect method
yuki-kimoto authored on 2010-05-28
80
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch");
removed register_format()
yuki-kimoto authored on 2010-05-26
81

            
82
$result = $dbi->execute($query);
83
@rows = ();
84
while (my $row = $result->fetch_hash) {
85
    push @rows, {%$row};
86
}
removed reconnect method
yuki-kimoto authored on 2010-05-28
87
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash");
removed register_format()
yuki-kimoto authored on 2010-05-26
88

            
89
$result = $dbi->execute($query);
90
$rows = $result->fetch_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
91
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all");
removed register_format()
yuki-kimoto authored on 2010-05-26
92

            
93
$result = $dbi->execute($query);
removed reconnect method
yuki-kimoto authored on 2010-05-28
94
$rows = $result->fetch_hash_all;
95
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash_all");
removed register_format()
yuki-kimoto authored on 2010-05-26
96

            
97
test 'Insert query return value';
98
$dbi->execute($DROP_TABLE->{0});
99
$dbi->execute($CREATE_TABLE->{0});
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
100
$source = "insert into table1 {insert_param key1 key2}";
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
101
$query = $dbi->create_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
102
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2});
103
ok($ret_val, $test);
104

            
105

            
106
test 'Direct query';
107
$dbi->execute($DROP_TABLE->{0});
108
$dbi->execute($CREATE_TABLE->{0});
add tests
yuki-kimoto authored on 2010-08-10
109
$insert_SOURCE = "insert into table1 {insert_param key1 key2}";
110
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2});
111
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
112
$rows = $result->fetch_hash_all;
113
is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
114

            
115
test 'Filter basic';
116
$dbi->execute($DROP_TABLE->{0});
117
$dbi->execute($CREATE_TABLE->{0});
118
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
119
                    three_times => sub { $_[0] * 3});
120

            
add tests
yuki-kimoto authored on 2010-08-10
121
$insert_SOURCE  = "insert into table1 {insert_param key1 key2};";
122
$insert_query = $dbi->create_query($insert_SOURCE);
removed register_format()
yuki-kimoto authored on 2010-05-26
123
$insert_query->filter({key1 => 'twice'});
124
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2});
add tests
yuki-kimoto authored on 2010-08-10
125
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
126
$rows = $result->filter({key2 => 'three_times'})->fetch_hash_all;
127
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : filter fetch_filter");
128
$dbi->execute($DROP_TABLE->{0});
129

            
130
test 'Filter in';
131
$dbi->execute($CREATE_TABLE->{0});
add tests
yuki-kimoto authored on 2010-08-10
132
$insert_SOURCE  = "insert into table1 {insert_param key1 key2};";
133
$insert_query = $dbi->create_query($insert_SOURCE);
removed register_format()
yuki-kimoto authored on 2010-05-26
134
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4});
add tests
yuki-kimoto authored on 2010-08-10
135
$select_SOURCE = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
136
$select_query = $dbi->create_query($select_SOURCE);
removed register_format()
yuki-kimoto authored on 2010-05-26
137
$select_query->filter({'table1.key1' => 'twice'});
138
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
139
$rows = $result->fetch_hash_all;
140
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : filter");
141

            
142
test 'DBIx::Custom::SQLTemplate basic tag';
143
$dbi->execute($DROP_TABLE->{0});
144
$dbi->execute($CREATE_TABLE->{1});
145
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
146
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
147

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
148
$source = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};";
149
$query = $dbi->create_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
150
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
151
$rows = $result->fetch_hash_all;
152
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1");
153

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
154
$source = "select * from table1 where {<= key1} and {like key2};";
155
$query = $dbi->create_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
156
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
157
$rows = $result->fetch_hash_all;
158
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2");
159

            
160
test 'DIB::Custom::SQLTemplate in tag';
161
$dbi->execute($DROP_TABLE->{0});
162
$dbi->execute($CREATE_TABLE->{1});
163
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
164
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
165

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
166
$source = "select * from table1 where {in key1 2};";
167
$query = $dbi->create_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
168
$result = $dbi->execute($query, param => {key1 => [9, 1]});
169
$rows = $result->fetch_hash_all;
170
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
171

            
172
test 'DBIx::Custom::SQLTemplate insert tag';
173
$dbi->execute("delete from table1");
add tests
yuki-kimoto authored on 2010-08-10
174
$insert_SOURCE = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
175
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
removed register_format()
yuki-kimoto authored on 2010-05-26
176

            
add tests
yuki-kimoto authored on 2010-08-10
177
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
178
$rows = $result->fetch_hash_all;
179
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
180

            
181
test 'DBIx::Custom::SQLTemplate update tag';
182
$dbi->execute("delete from table1");
add tests
yuki-kimoto authored on 2010-08-10
183
$insert_SOURCE = "insert into table1 {insert_param key1 key2 key3 key4 key5}";
184
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
185
$dbi->execute($insert_SOURCE, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
removed register_format()
yuki-kimoto authored on 2010-05-26
186

            
add tests
yuki-kimoto authored on 2010-08-10
187
$update_SOURCE = 'update table1 {update_param key1 key2 key3 key4} where {= key5}';
188
$dbi->execute($update_SOURCE, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5});
removed register_format()
yuki-kimoto authored on 2010-05-26
189

            
add tests
yuki-kimoto authored on 2010-08-10
190
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
191
$rows = $result->fetch_hash_all;
192
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
193
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic");
194

            
195
test 'Error case';
196
eval {DBIx::Custom->connect(data_source => 'dbi:SQLit')};
197
ok($@, "$test : connect error");
198

            
199
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
200
eval{$dbi->create_query("{p }")};
201
ok($@, "$test : create_query invalid SQL template");
removed register_format()
yuki-kimoto authored on 2010-05-26
202

            
203
test 'insert';
204
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
205
$dbi->execute($CREATE_TABLE->{0});
206
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
207
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
add tests
yuki-kimoto authored on 2010-08-10
208
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
209
$rows   = $result->fetch_hash_all;
210
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic");
211

            
212
$dbi->execute('delete from table1');
213
$dbi->register_filter(
214
    twice       => sub { $_[0] * 2 },
215
    three_times => sub { $_[0] * 3 }
216
);
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
217
$dbi->default_bind_filter('twice');
removed register_format()
yuki-kimoto authored on 2010-05-26
218
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
add tests
yuki-kimoto authored on 2010-08-10
219
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
220
$rows   = $result->fetch_hash_all;
221
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter");
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
222
$dbi->default_bind_filter(undef);
removed register_format()
yuki-kimoto authored on 2010-05-26
223

            
224
$dbi->execute($DROP_TABLE->{0});
225
$dbi->execute($CREATE_TABLE->{0});
226
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
227
$rows = $dbi->select(table => 'table1')->fetch_hash_all;
228
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
229

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
230
eval{$dbi->insert(table => 'table1', noexist => 1)};
add tests
yuki-kimoto authored on 2010-08-10
231
like($@, qr/noexist/, "$test: invalid argument");
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
232

            
233

            
removed register_format()
yuki-kimoto authored on 2010-05-26
234
test 'update';
235
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
236
$dbi->execute($CREATE_TABLE->{1});
237
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
238
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
239
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
add tests
yuki-kimoto authored on 2010-08-10
240
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
241
$rows   = $result->fetch_hash_all;
242
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
243
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
244
                  "$test : basic");
245
                  
246
$dbi->execute("delete from table1");
247
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
248
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
249
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
add tests
yuki-kimoto authored on 2010-08-10
250
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
251
$rows   = $result->fetch_hash_all;
252
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
253
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
254
                  "$test : update key same as search key");
255

            
add tests
yuki-kimoto authored on 2010-08-10
256
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3});
257
$result = $dbi->execute($SELECT_SOURCES->{0});
258
$rows   = $result->fetch_hash_all;
259
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
260
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
261
                  "$test : update key same as search key : param is array ref");
262

            
removed register_format()
yuki-kimoto authored on 2010-05-26
263
$dbi->execute("delete from table1");
264
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
265
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
266
$dbi->register_filter(twice => sub { $_[0] * 2 });
267
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
268
              filter => {key2 => 'twice'});
add tests
yuki-kimoto authored on 2010-08-10
269
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
270
$rows   = $result->fetch_hash_all;
271
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
272
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
273
                  "$test : filter");
274

            
275
$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => '   ');
276

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
277
eval{$dbi->update(table => 'table1', noexist => 1)};
add tests
yuki-kimoto authored on 2010-08-10
278
like($@, qr/noexist/, "$test: invalid argument");
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
279

            
280
eval{$dbi->update(table => 'table1')};
281
like($@, qr/where/, "$test: not contain where");
282

            
283

            
removed register_format()
yuki-kimoto authored on 2010-05-26
284
test 'update_all';
285
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
286
$dbi->execute($CREATE_TABLE->{1});
287
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
288
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
289
$dbi->register_filter(twice => sub { $_[0] * 2 });
290
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
add tests
yuki-kimoto authored on 2010-08-10
291
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
292
$rows   = $result->fetch_hash_all;
293
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
294
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
295
                  "$test : filter");
296

            
297

            
298
test 'delete';
299
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
300
$dbi->execute($CREATE_TABLE->{0});
301
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
302
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
303
$dbi->delete(table => 'table1', where => {key1 => 1});
add tests
yuki-kimoto authored on 2010-08-10
304
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
305
$rows   = $result->fetch_hash_all;
306
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic");
307

            
308
$dbi->execute("delete from table1;");
309
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
310
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
311
$dbi->register_filter(twice => sub { $_[0] * 2 });
312
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
add tests
yuki-kimoto authored on 2010-08-10
313
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
314
$rows   = $result->fetch_hash_all;
315
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter");
316

            
317
$dbi->delete(table => 'table1', where => {key1 => 1}, append => '   ');
318

            
319
$dbi->delete_all(table => 'table1');
320
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
321
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
322
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
323
$rows = $dbi->select(table => 'table1')->fetch_hash_all;
324
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key");
325

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
326
eval{$dbi->delete(table => 'table1', noexist => 1)};
add tests
yuki-kimoto authored on 2010-08-10
327
like($@, qr/noexist/, "$test: invalid argument");
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
328

            
329

            
removed register_format()
yuki-kimoto authored on 2010-05-26
330
test 'delete error';
331
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
332
$dbi->execute($CREATE_TABLE->{0});
333
eval{$dbi->delete(table => 'table1')};
add tests
yuki-kimoto authored on 2010-08-10
334
like($@, qr/"where" argument must be specified and contains the pairs of column name and value/,
removed register_format()
yuki-kimoto authored on 2010-05-26
335
         "$test : where key-value pairs not specified");
336

            
337
test 'delete_all';
338
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
339
$dbi->execute($CREATE_TABLE->{0});
340
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
341
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
342
$dbi->delete_all(table => 'table1');
add tests
yuki-kimoto authored on 2010-08-10
343
$result = $dbi->execute($SELECT_SOURCES->{0});
removed register_format()
yuki-kimoto authored on 2010-05-26
344
$rows   = $result->fetch_hash_all;
345
is_deeply($rows, [], "$test : basic");
346

            
347

            
348
test 'select';
349
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
350
$dbi->execute($CREATE_TABLE->{0});
351
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
352
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
353
$rows = $dbi->select(table => 'table1')->fetch_hash_all;
354
is_deeply($rows, [{key1 => 1, key2 => 2},
355
                  {key1 => 3, key2 => 4}], "$test : table");
356

            
update document
yuki-kimoto authored on 2010-05-27
357
$rows = $dbi->select(table => 'table1', column => ['key1'])->fetch_hash_all;
removed register_format()
yuki-kimoto authored on 2010-05-26
358
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key");
359

            
360
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->fetch_hash_all;
361
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key");
362

            
update document
yuki-kimoto authored on 2010-08-07
363
$rows = $dbi->select(table => 'table1', where => ['{= key1} and {= key2}', {key1 => 1, key2 => 2}])->fetch_hash_all;
364
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where string");
365

            
update document
yuki-kimoto authored on 2010-05-27
366
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->fetch_hash_all;
removed register_format()
yuki-kimoto authored on 2010-05-26
367
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key");
368

            
369
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->fetch_hash_all;
370
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement");
371

            
372
$dbi->register_filter(decrement => sub { $_[0] - 1 });
update document
yuki-kimoto authored on 2010-05-27
373
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
removed register_format()
yuki-kimoto authored on 2010-05-26
374
            ->fetch_hash_all;
375
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : filter");
376

            
377
$dbi->execute($CREATE_TABLE->{2});
378
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
379
$rows = $dbi->select(
380
    table => [qw/table1 table2/],
update document
yuki-kimoto authored on 2010-05-27
381
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
removed register_format()
yuki-kimoto authored on 2010-05-26
382
    where   => {'table1.key2' => 2},
added commit method
yuki-kimoto authored on 2010-05-27
383
    relation  => {'table1.key1' => 'table2.key1'}
removed register_format()
yuki-kimoto authored on 2010-05-26
384
)->fetch_hash_all;
added commit method
yuki-kimoto authored on 2010-05-27
385
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : relation : exists where");
386

            
387
$rows = $dbi->select(
388
    table => [qw/table1 table2/],
389
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
390
    relation  => {'table1.key1' => 'table2.key1'}
391
)->fetch_hash_all;
392
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : relation : no exists where");
removed register_format()
yuki-kimoto authored on 2010-05-26
393

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
394
eval{$dbi->select(table => 'table1', noexist => 1)};
add tests
yuki-kimoto authored on 2010-08-10
395
like($@, qr/noexist/, "$test: invalid argument");
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
396

            
397

            
removed register_format()
yuki-kimoto authored on 2010-05-26
398
test 'fetch filter';
399
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
400
$dbi->register_filter(
401
    twice       => sub { $_[0] * 2 },
402
    three_times => sub { $_[0] * 3 }
403
);
404
$dbi->default_fetch_filter('twice');
405
$dbi->execute($CREATE_TABLE->{0});
406
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
407
$result = $dbi->select(table => 'table1');
408
$result->filter({key1 => 'three_times'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
409
$row = $result->fetch_hash_first;
removed register_format()
yuki-kimoto authored on 2010-05-26
410
is_deeply($row, {key1 => 3, key2 => 4}, "$test: default_fetch_filter and filter");
411

            
412
test 'filters';
413
$dbi = DBIx::Custom->new;
414

            
update document
yuki-kimoto authored on 2010-05-27
415
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
416
   'あ', "$test : decode_utf8");
removed register_format()
yuki-kimoto authored on 2010-05-26
417

            
418
is($dbi->filters->{encode_utf8}->('あ'),
419
   encode_utf8('あ'), "$test : encode_utf8");
420

            
added commit method
yuki-kimoto authored on 2010-05-27
421
test 'transaction';
422
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
423
$dbi->execute($CREATE_TABLE->{0});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
424
$dbi->dbh->begin_work;
added commit method
yuki-kimoto authored on 2010-05-27
425
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
426
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
427
$dbi->dbh->commit;
added commit method
yuki-kimoto authored on 2010-05-27
428
$result = $dbi->select(table => 'table1');
429
is_deeply(scalar $result->fetch_hash_all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
430
          "$test : commit");
431

            
432
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
433
$dbi->execute($CREATE_TABLE->{0});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
434
$dbi->dbh->begin_work(0);
added commit method
yuki-kimoto authored on 2010-05-27
435
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
436
$dbi->dbh->rollback;
added commit method
yuki-kimoto authored on 2010-05-27
437

            
438
$result = $dbi->select(table => 'table1');
removed reconnect method
yuki-kimoto authored on 2010-05-28
439
ok(! $result->fetch_first, "$test: rollback");
add cache attribute
yuki-kimoto authored on 2010-06-14
440

            
441
test 'cache';
442
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
443
$dbi->execute($CREATE_TABLE->{0});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
444
$source = 'select * from table1 where {= key1} and {= key2};';
445
$dbi->create_query($source);
446
is_deeply($dbi->{_cached}->{$source}, 
add cache attribute
yuki-kimoto authored on 2010-06-14
447
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2']}, "$test : cache");
448

            
449
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
450
$dbi->execute($CREATE_TABLE->{0});
451
$dbi->{_cached} = {};
452
$dbi->cache(0);
453
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
454
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
455

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
456

            
added check_filter attribute
yuki-kimoto authored on 2010-08-08
457
test 'filter_check in fetching';
458
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
459
$dbi->execute($CREATE_TABLE->{0});
460
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
461
$dbi->default_fetch_filter('not_exists');
462
$result = $dbi->select(table => 'table1');
463
eval{$result->fetch_first};
464
like($@, qr/\QDefault fetch filter "not_exists" is not registered/, "$test : array :default_fetch_filter");
465

            
466
$dbi->default_fetch_filter(undef);
467
$result = $dbi->select(table => 'table1');
468
$result->filter({key1 => 'not_exists'});
469
eval{$result->fetch_first};
470
like($@, qr/\QFetch filter "not_exists" is not registered/, "$test :  array :fetch_filter");
471

            
472
$result = $dbi->select(table => 'table1');
473
$result->filter({not_exists => 'encode_utf8'});
474
eval{$result->fetch_first};
475
like($@, qr/\QColumn name "not_exists" in fetch filter is not found in result columns/,
476
     "$test :  array : fetch_filter");
477

            
478
$result = $dbi->select(table => 'table1');
479
$result->filter({Key1 => 'encode_utf8'});
480
eval{$result->fetch_first};
481
like($@, qr/\QColumn name "Key1" in fetch filter must lower case string/,
482
     "$test :  array : contain upper case charactor");
483

            
484
$dbi->filter_check(0);
485
$result = $dbi->select(table => 'table1');
486
$result->filter({Key1 => 'encode_utf8'});
487
eval{$result->fetch_first};
488
ok(!$@, "$test : array : filter_check off");
489

            
490
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
491
$dbi->execute($CREATE_TABLE->{0});
492
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
493
$dbi->default_fetch_filter('not_exists');
494
$result = $dbi->select(table => 'table1');
495
eval{$result->fetch_hash_first};
496
like($@, qr/\QDefault fetch filter "not_exists" is not registered/, "$test : hash :default_fetch_filter");
497

            
498
$dbi->default_fetch_filter(undef);
499
$result = $dbi->select(table => 'table1');
500
$result->filter({key1 => 'not_exists'});
501
eval{$result->fetch_hash_first};
502
like($@, qr/\QFetch filter "not_exists" is not registered/, "$test : hash :fetch_filter");
503

            
504
$result = $dbi->select(table => 'table1');
505
$result->filter({not_exists => 'encode_utf8'});
506
eval{$result->fetch_hash_first};
507
like($@, qr/\QColumn name "not_exists" in fetch filter is not found in result columns/,
508
     "$test : hash : fetch_filter");
509

            
510
$result = $dbi->select(table => 'table1');
511
$result->filter({Key1 => 'encode_utf8'});
512
eval{$result->fetch_hash_first};
513
like($@, qr/\QColumn name "Key1" in fetch filter must lower case string/,
514
     "$test : hash : contain upper case charactor");
515

            
516
$dbi->filter_check(0);
517
$result = $dbi->select(table => 'table1');
518
$result->filter({Key1 => 'encode_utf8'});
519
eval{$result->fetch_hash_first};
520
ok(!$@, "$test : hash : filter_check off");
521

            
522

            
add tests
yuki-kimoto authored on 2010-08-08
523
test 'filter_check in parameter binding';
added check_filter attribute
yuki-kimoto authored on 2010-08-08
524
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
525
$dbi->execute($CREATE_TABLE->{0});
526
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
527

            
add tests
yuki-kimoto authored on 2010-08-08
528
$dbi->default_bind_filter('not_exists');
529
eval{$dbi->select(table => 'table1')};
530
like($@, qr/\QDefault bind filter "not_exists" is not registered/, "$test : default_bind_filter");
added check_filter attribute
yuki-kimoto authored on 2010-08-08
531

            
add tests
yuki-kimoto authored on 2010-08-08
532
$dbi->default_bind_filter(undef);
533
eval{$dbi->select(table => 'table1', filter => {key1 => 'not_exists'})};
534
like($@, qr/\QBind filter "not_exists" is not registered/, "$test : bind_filter");
added check_filter attribute
yuki-kimoto authored on 2010-08-08
535

            
add tests
yuki-kimoto authored on 2010-08-08
536
eval{$dbi->select(table => 'table1', filter => {not_exists => 'encode_utf8'})};
537
like($@, qr/\QColumn name "not_exists" in bind filter is not found in paramters/,
538
     "$test : fetch_filter");
added check_filter attribute
yuki-kimoto authored on 2010-08-08
539

            
add tests
yuki-kimoto authored on 2010-08-10
540
test 'execute';
541
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
542
$dbi->execute($CREATE_TABLE->{0});
removed experimental registe...
yuki-kimoto authored on 2010-08-24
543
{
544
    local $Carp::Verbose = 0;
545
    eval{$dbi->execute('select * frm table1')};
546
    like($@, qr/\Qselect * frm table1;/, "$test : fail prepare");
547
    like($@, qr/\.t /, "$test: fail : not verbose");
548
}
549
{
550
    local $Carp::Verbose = 1;
551
    eval{$dbi->execute('select * frm table1')};
552
    like($@, qr/Custom.*\.t /s, "$test : fail : verbose");
553
}
add tests
yuki-kimoto authored on 2010-08-10
554

            
555
eval{$dbi->execute('select * from table1', no_exists => 1)};
556
like($@, qr/\Q"no_exists" is invalid argument/, "$test : invald SQL");
557

            
558
$query = $dbi->create_query('select * from table1 where {= key1}');
559
$dbi->dbh->disconnect;
560
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
561
ok($@, "$test: execute fail");
562

            
removed experimental registe...
yuki-kimoto authored on 2010-08-24
563
{
564
    local $Carp::Verbose = 0;
565
    eval{$dbi->create_query('select * from table1 where {0 key1}')};
566
    like($@, qr/\Q.t /, "$test : caller spec : not vebose");
567
}
568
{
569
    local $Carp::Verbose = 1;
570
    eval{$dbi->create_query('select * from table1 where {0 key1}')};
571
    like($@, qr/QueryBuilder.*\.t /s, "$test : caller spec : not vebose");
572
}
cleanup
yuki-kimoto authored on 2010-10-17
573

            
574

            
575
test 'transaction';
576
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
577
$dbi->execute($CREATE_TABLE->{0});
578

            
579
$dbi->begin_work;
580

            
581
eval {
582
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
583
    die "Error";
584
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
585
};
586

            
587
$dbi->rollback if $@;
588

            
589
$result = $dbi->select(table => 'table1');
590
$rows = $result->fetch_hash_all;
591
is_deeply($rows, [], "$test : rollback");
592

            
593
$dbi->begin_work;
594

            
595
eval {
596
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
597
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
598
};
599

            
600
$dbi->commit unless $@;
601

            
602
$result = $dbi->select(table => 'table1');
603
$rows = $result->fetch_hash_all;
604
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit");
605

            
606
$dbi->dbh->{AutoCommit} = 0;
607
eval{ $dbi->begin_work };
608
ok($@, "$test : exception");
609
$dbi->dbh->{AutoCommit} = 1;
610