DBIx-Custom / t / dbix-custom-core-sqlite.t /
Newer Older
724 lines | 28.372kb
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

            
add tests
yuki-kimoto authored on 2010-08-10
456
test 'execute';
457
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
458
$dbi->execute($CREATE_TABLE->{0});
removed experimental registe...
yuki-kimoto authored on 2010-08-24
459
{
460
    local $Carp::Verbose = 0;
461
    eval{$dbi->execute('select * frm table1')};
462
    like($@, qr/\Qselect * frm table1;/, "$test : fail prepare");
463
    like($@, qr/\.t /, "$test: fail : not verbose");
464
}
465
{
466
    local $Carp::Verbose = 1;
467
    eval{$dbi->execute('select * frm table1')};
468
    like($@, qr/Custom.*\.t /s, "$test : fail : verbose");
469
}
add tests
yuki-kimoto authored on 2010-08-10
470

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

            
474
$query = $dbi->create_query('select * from table1 where {= key1}');
475
$dbi->dbh->disconnect;
476
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
477
ok($@, "$test: execute fail");
478

            
removed experimental registe...
yuki-kimoto authored on 2010-08-24
479
{
480
    local $Carp::Verbose = 0;
481
    eval{$dbi->create_query('select * from table1 where {0 key1}')};
482
    like($@, qr/\Q.t /, "$test : caller spec : not vebose");
483
}
484
{
485
    local $Carp::Verbose = 1;
486
    eval{$dbi->create_query('select * from table1 where {0 key1}')};
487
    like($@, qr/QueryBuilder.*\.t /s, "$test : caller spec : not vebose");
488
}
cleanup
yuki-kimoto authored on 2010-10-17
489

            
490

            
491
test 'transaction';
492
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
493
$dbi->execute($CREATE_TABLE->{0});
494

            
495
$dbi->begin_work;
496

            
497
eval {
498
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
499
    die "Error";
500
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
501
};
502

            
503
$dbi->rollback if $@;
504

            
505
$result = $dbi->select(table => 'table1');
506
$rows = $result->fetch_hash_all;
507
is_deeply($rows, [], "$test : rollback");
508

            
509
$dbi->begin_work;
510

            
511
eval {
512
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
513
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
514
};
515

            
516
$dbi->commit unless $@;
517

            
518
$result = $dbi->select(table => 'table1');
519
$rows = $result->fetch_hash_all;
520
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit");
521

            
522
$dbi->dbh->{AutoCommit} = 0;
523
eval{ $dbi->begin_work };
524
ok($@, "$test : exception");
525
$dbi->dbh->{AutoCommit} = 1;
526

            
added helper method
yuki-kimoto authored on 2010-10-17
527

            
528
test 'helper';
529
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
530
$dbi->helper(
531
    one => sub { 1 }
532
);
533
$dbi->helper(
534
    two => sub { 2 }
535
);
536
$dbi->helper({
537
    twice => sub {
538
        my $self = shift;
539
        return $_[0] * 2;
540
    }
541
});
542

            
543
is($dbi->one, 1, "$test : first");
544
is($dbi->two, 2, "$test : second");
545
is($dbi->twice(5), 10 , "$test : second");
546

            
547
eval {$dbi->XXXXXX};
548
like($@, qr/\QCan't locate object method "XXXXXX" via "DBIx::Custom"/, "$test : not exists");
549

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
550
test 'auto bind filter';
551
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
552
$dbi->execute($CREATE_TABLE->{0});
553
$dbi->register_filter(twice => sub { $_[0] * 2 });
554
$dbi->register_filter(three_times => sub { $_[0] * 3});
555
$dbi->auto_filter(
556
    'table1', ['key1', 'twice', 'twice'], ['key2', 'three_times', 'three_times']);
557
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
558
$result = $dbi->execute($SELECT_SOURCES->{0});
559
$row   = $result->fetch_hash_first;
560
is_deeply($row, {key1 => 2, key2 => 6}, "$test : insert");
561

            
562
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
563
$dbi->execute($CREATE_TABLE->{0});
564
$dbi->register_filter(twice => sub { $_[0] * 2 });
565
$dbi->register_filter(three_times => sub { $_[0] * 3});
566
$dbi->auto_filter(
567
    'table1', ['key1', 'twice', 'twice'], ['key2', 'three_times', 'three_times']);
568
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => undef);
569
$result = $dbi->execute($SELECT_SOURCES->{0});
570
$row   = $result->fetch_hash_first;
571
is_deeply($row, {key1 => 1, key2 => 2}, "$test : insert disabe auto_filter_table 1");
572

            
573
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
574
$dbi->execute($CREATE_TABLE->{0});
575
$dbi->register_filter(twice => sub { $_[0] * 2 });
576
$dbi->register_filter(three_times => sub { $_[0] * 3});
577
$dbi->auto_filter(
578
    'table1', ['key1', 'twice', 'twice'], ['key2', 'three_times', 'three_times']);
579
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => []);
580
$result = $dbi->execute($SELECT_SOURCES->{0});
581
$row   = $result->fetch_hash_first;
582
is_deeply($row, {key1 => 1, key2 => 2}, "$test : insert disabe auto_filter_table 2");
583

            
584
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
585
$dbi->execute($CREATE_TABLE->{0});
586
$dbi->register_filter(twice => sub { $_[0] * 2 });
587
$dbi->auto_filter(
588
    'table1', ['key1', 'twice', 'twice']
589
);
590
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => undef);
591
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
592
$result = $dbi->execute($SELECT_SOURCES->{0});
593
$row   = $result->fetch_hash_first;
594
is_deeply($row, {key1 => 4, key2 => 2}, "$test : update");
595

            
596
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
597
$dbi->execute($CREATE_TABLE->{0});
598
$dbi->register_filter(twice => sub { $_[0] * 2 });
599
$dbi->auto_filter(
600
    'table1', ['key1', 'twice', 'twice']
601
);
602
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => undef);
603
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}, auto_filter_table => []);
604
$result = $dbi->execute($SELECT_SOURCES->{0});
605
$row   = $result->fetch_hash_first;
606
is_deeply($row, {key1 => 2, key2 => 2}, "$test : update : disable bind filter1");
607

            
608
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
609
$dbi->execute($CREATE_TABLE->{0});
610
$dbi->register_filter(twice => sub { $_[0] * 2 });
611
$dbi->auto_filter(
612
    'table1', ['key1', 'twice', 'twice']
613
);
614
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => undef);
615
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}, auto_filter_table => undef);
616
$result = $dbi->execute($SELECT_SOURCES->{0});
617
$row   = $result->fetch_hash_first;
618
is_deeply($row, {key1 => 2, key2 => 2}, "$test : update : disable bind filter2");
619

            
620
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
621
$dbi->execute($CREATE_TABLE->{0});
622
$dbi->register_filter(twice => sub { $_[0] * 2 });
623
$dbi->auto_filter(
624
    'table1', ['key1', 'twice', 'twice']
625
);
626
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef);
627
$dbi->delete(table => 'table1', where => {key1 => 1});
628
$result = $dbi->execute($SELECT_SOURCES->{0});
629
$rows   = $result->fetch_hash_all;
630
is_deeply($rows, [], "$test : delete");
631

            
632
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
633
$dbi->execute($CREATE_TABLE->{0});
634
$dbi->register_filter(twice => sub { $_[0] * 2 });
635
$dbi->auto_filter(
636
    'table1', ['key1', 'twice', 'twice']
637
);
638
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef);
639
$dbi->delete(table => 'table1', where => {key1 => 1}, auto_filter_table => undef);
640
$result = $dbi->execute($SELECT_SOURCES->{0});
641
$rows   = $result->fetch_hash_all;
642
is_deeply($rows, [{key1 => 2, key2 => 2}], "$test : delete : disable1");
643

            
644
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
645
$dbi->execute($CREATE_TABLE->{0});
646
$dbi->register_filter(twice => sub { $_[0] * 2 });
647
$dbi->auto_filter(
648
    'table1', ['key1', 'twice', 'twice']
649
);
650
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef);
651
$dbi->delete(table => 'table1', where => {key1 => 1}, auto_filter_table => []);
652
$result = $dbi->execute($SELECT_SOURCES->{0});
653
$rows   = $result->fetch_hash_all;
654
is_deeply($rows, [{key1 => 2, key2 => 2}], "$test : delete : disable2");
655

            
656
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
657
$dbi->execute($CREATE_TABLE->{0});
658
$dbi->register_filter(twice => sub { $_[0] * 2 });
659
$dbi->auto_filter(
660
    'table1', ['key1', 'twice', 'twice']
661
);
662
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef);
663
$result = $dbi->select(table => 'table1', where => {key1 => 1});
664
$result->filter({'key2' => 'twice'});
665
$rows   = $result->fetch_hash_all;
666
is_deeply($rows, [{key1 => 4, key2 => 4}], "$test : select");
667

            
668
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
669
$dbi->execute($CREATE_TABLE->{0});
670
$dbi->register_filter(twice => sub { $_[0] * 2 });
671
$dbi->auto_filter(
672
    'table1', ['key1', 'twice', 'twice']
673
);
674
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef);
675
$result = $dbi->select(table => 'table1', where => {key1 => 2}, auto_filter_table => []);
676
$result->filter({'key2' => 'twice'});
677
$rows   = $result->fetch_hash_all;
678
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : select : disable");
679

            
680
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
681
$dbi->execute($CREATE_TABLE->{0});
682
$dbi->register_filter(twice => sub { $_[0] * 2 });
683
$dbi->auto_filter(
684
    'table1', ['key1', 'twice', 'twice']
685
);
686
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef);
687
$result = $dbi->execute("select * from table1 where {= key1} and {= key2};",
688
                        param => {key1 => 1, key2 => 2},
689
                        auto_filter_table => ['table1']);
690
$rows   = $result->fetch_hash_all;
691
is_deeply($rows, [{key1 => 4, key2 => 2}], "$test : execute");
692

            
693
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
694
$dbi->execute($CREATE_TABLE->{0});
695
$dbi->execute($CREATE_TABLE->{2});
696
$dbi->register_filter(twice => sub { $_[0] * 2 });
697
$dbi->register_filter(three_times => sub { $_[0] * 3 });
698
$dbi->auto_filter(
699
    'table1', ['key2', 'twice', 'twice']
700
);
701
$dbi->auto_filter(
702
    'table2', ['key3', 'three_times', 'three_times']
703
);
704
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, auto_filter_table => undef);
705
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, auto_filter_table => undef);
706
$result = $dbi->select(
707
     table => ['table1', 'table2'],
708
     column => ['key2', 'key3'],
709
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
710

            
711
$result->filter({'key2' => 'twice'});
712
$rows   = $result->fetch_hash_all;
713
is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join");
714

            
715
$result = $dbi->select(
716
     table => ['table1', 'table2'],
717
     column => ['key2', 'key3'],
718
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
719

            
720
$result->filter({'key2' => 'twice'});
721
$rows   = $result->fetch_hash_all;
722
is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join : omit");
723

            
724