DBIx-Custom / t / dbix-custom-core-sqlite.t /
Newer Older
749 lines | 29.072kb
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));',
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
28
    2 => 'create table table2 (key1 char(255), key3 char(255));',
29
    3 => 'create table table1 (key1 Date, key2 datetime);'
removed register_format()
yuki-kimoto authored on 2010-05-26
30
};
31

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

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

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

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

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

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

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

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

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

            
94
$result = $dbi->execute($query);
removed reconnect method
yuki-kimoto authored on 2010-05-28
95
$rows = $result->fetch_hash_all;
96
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
97

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

            
106

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

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

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

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

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

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

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

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

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

            
173
test 'DBIx::Custom::SQLTemplate insert tag';
174
$dbi->execute("delete from table1");
add tests
yuki-kimoto authored on 2010-08-10
175
$insert_SOURCE = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
176
$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
177

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

            
182
test 'DBIx::Custom::SQLTemplate update tag';
183
$dbi->execute("delete from table1");
add tests
yuki-kimoto authored on 2010-08-10
184
$insert_SOURCE = "insert into table1 {insert_param key1 key2 key3 key4 key5}";
185
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
186
$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
187

            
add tests
yuki-kimoto authored on 2010-08-10
188
$update_SOURCE = 'update table1 {update_param key1 key2 key3 key4} where {= key5}';
189
$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
190

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

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

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

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

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

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

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

            
234

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

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

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

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

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

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

            
284

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

            
298

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

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

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

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

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

            
330

            
removed register_format()
yuki-kimoto authored on 2010-05-26
331
test 'delete error';
332
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
333
$dbi->execute($CREATE_TABLE->{0});
334
eval{$dbi->delete(table => 'table1')};
add tests
yuki-kimoto authored on 2010-08-10
335
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
336
         "$test : where key-value pairs not specified");
337

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

            
348

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

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

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

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

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

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

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

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

            
388
$rows = $dbi->select(
389
    table => [qw/table1 table2/],
390
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
391
    relation  => {'table1.key1' => 'table2.key1'}
392
)->fetch_hash_all;
393
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
394

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

            
398

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

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

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

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

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

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

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

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

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

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

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

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

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

            
491

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

            
496
$dbi->begin_work;
497

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

            
504
$dbi->rollback if $@;
505

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

            
510
$dbi->begin_work;
511

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

            
517
$dbi->commit unless $@;
518

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
725
test 'auto_filter_easy_build';
726
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
727
$dbi->execute($CREATE_TABLE->{2});
728
$dbi->execute($CREATE_TABLE->{3});
729

            
730
$infos = [];
731
$dbi->iterate_all_columns(sub {
732
    my ($table, $column, $cinfo) = @_;
733
    
734
    if ($table =~ /^table/) {
735
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
736
         push @$infos, $info;
737
    }
738
});
739
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
740
is_deeply($infos, 
741
    [
742
        ['table1', 'key1', 'key1'],
743
        ['table1', 'key2', 'key2'],
744
        ['table2', 'key1', 'key1'],
745
        ['table2', 'key3', 'key3']
746
    ]
747
    , $test
748
);
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
749