DBIx-Custom / t / dbix-custom-core-sqlite.t /
Newer Older
1058 lines | 37.587kb
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;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
65
my $table;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
66
my $where;
removed register_format()
yuki-kimoto authored on 2010-05-26
67

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

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

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

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

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

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

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

            
108

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
236

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

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

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

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

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

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

            
286

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

            
300

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

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

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

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

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

            
332

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

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

            
350

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

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

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

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

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

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

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

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

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

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

            
400

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

            
415
test 'filters';
416
$dbi = DBIx::Custom->new;
417

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

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

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

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

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

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

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

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

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

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

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

            
493

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

            
498
$dbi->begin_work;
499

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

            
506
$dbi->rollback if $@;
507

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

            
512
$dbi->begin_work;
513

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

            
519
$dbi->commit unless $@;
520

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

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

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

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

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
553
test 'out filter';
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
554
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
555
$dbi->execute($CREATE_TABLE->{0});
556
$dbi->register_filter(twice => sub { $_[0] * 2 });
557
$dbi->register_filter(three_times => sub { $_[0] * 3});
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
558
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
559
    'table1', 'key1' => {out => 'twice', in => 'three_times'}, 
560
              'key2' => {out => 'three_times', in => 'twice'});
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
561
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
562
$result = $dbi->execute($SELECT_SOURCES->{0});
563
$row   = $result->fetch_hash_first;
564
is_deeply($row, {key1 => 2, key2 => 6}, "$test : insert");
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
565
$result = $dbi->select(table => 'table1');
566
$row   = $result->fetch_hash_first;
567
is_deeply($row, {key1 => 6, key2 => 12}, "$test : insert");
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
568

            
569
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
570
$dbi->execute($CREATE_TABLE->{0});
571
$dbi->register_filter(twice => sub { $_[0] * 2 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
572
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
573
    'table1', 'key1' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
574
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
575
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
576
$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
577
$result = $dbi->execute($SELECT_SOURCES->{0});
578
$row   = $result->fetch_hash_first;
579
is_deeply($row, {key1 => 4, key2 => 2}, "$test : update");
580

            
581
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
582
$dbi->execute($CREATE_TABLE->{0});
583
$dbi->register_filter(twice => sub { $_[0] * 2 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
584
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
585
    'table1', 'key1' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
586
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
587
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
588
$dbi->delete(table => 'table1', where => {key1 => 1});
589
$result = $dbi->execute($SELECT_SOURCES->{0});
590
$rows   = $result->fetch_hash_all;
591
is_deeply($rows, [], "$test : delete");
592

            
593
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
594
$dbi->execute($CREATE_TABLE->{0});
595
$dbi->register_filter(twice => sub { $_[0] * 2 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
596
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
597
    'table1', 'key1' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
598
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
599
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
600
$result = $dbi->select(table => 'table1', where => {key1 => 1});
601
$result->filter({'key2' => 'twice'});
602
$rows   = $result->fetch_hash_all;
603
is_deeply($rows, [{key1 => 4, key2 => 4}], "$test : select");
604

            
605
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
606
$dbi->execute($CREATE_TABLE->{0});
607
$dbi->register_filter(twice => sub { $_[0] * 2 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
608
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
609
    'table1', 'key1' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
610
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
611
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
612
$result = $dbi->execute("select * from table1 where {= key1} and {= key2};",
613
                        param => {key1 => 1, key2 => 2},
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
614
                        table => ['table1']);
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
615
$rows   = $result->fetch_hash_all;
616
is_deeply($rows, [{key1 => 4, key2 => 2}], "$test : execute");
617

            
618
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
619
$dbi->execute($CREATE_TABLE->{0});
620
$dbi->execute($CREATE_TABLE->{2});
621
$dbi->register_filter(twice => sub { $_[0] * 2 });
622
$dbi->register_filter(three_times => sub { $_[0] * 3 });
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
623
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
624
    'table1', 'key2' => {out => 'twice', in => 'twice'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
625
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
626
$dbi->apply_filter(
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
627
    'table2', 'key3' => {out => 'three_times', in => 'three_times'}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
628
);
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
629
$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
630
$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
631
$result = $dbi->select(
632
     table => ['table1', 'table2'],
633
     column => ['key2', 'key3'],
634
     where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
635

            
636
$result->filter({'key2' => 'twice'});
637
$rows   = $result->fetch_hash_all;
638
is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join");
639

            
640
$result = $dbi->select(
641
     table => ['table1', 'table2'],
642
     column => ['key2', 'key3'],
643
     where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
644

            
645
$result->filter({'key2' => 'twice'});
646
$rows   = $result->fetch_hash_all;
647
is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join : omit");
648

            
pod fix
Yuki Kimoto authored on 2011-01-21
649
test 'each_column';
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
650
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
651
$dbi->execute($CREATE_TABLE->{2});
652
$dbi->execute($CREATE_TABLE->{3});
653

            
654
$infos = [];
pod fix
Yuki Kimoto authored on 2011-01-21
655
$dbi->each_column(sub {
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
656
    my ($table, $column, $cinfo) = @_;
657
    
658
    if ($table =~ /^table/) {
659
         my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
660
         push @$infos, $info;
661
    }
662
});
663
$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
664
is_deeply($infos, 
665
    [
666
        ['table1', 'key1', 'key1'],
667
        ['table1', 'key2', 'key2'],
668
        ['table2', 'key1', 'key1'],
669
        ['table2', 'key3', 'key3']
670
    ]
671
    , $test
672
);
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
673

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
674
test 'table';
675
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
676
$dbi->execute($CREATE_TABLE->{0});
677
$table = $dbi->table('table1');
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
678
$table->insert(param => {key1 => 1, key2 => 2});
679
$table->insert(param => {key1 => 3, key2 => 4});
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
680
$rows = $table->select->fetch_hash_all;
681
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}],
682
                 "$test: select");
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
683
$rows = $table->select(where => {key2 => 2}, append => 'order by key1',
684
                              column => ['key1', 'key2'])->fetch_hash_all;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
685
is_deeply($rows, [{key1 => 1, key2 => 2}],
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
686
                 "$test: insert insert select");
687
$table->update(param => {key1 => 3}, where => {key2 => 2});
688
$table->update(param => {key1 => 5}, where => {key2 => 4});
689
$rows = $table->select(where => {key2 => 2})->fetch_hash_all;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
690
is_deeply($rows, [{key1 => 3, key2 => 2}],
691
                 "$test: update");
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
692
$table->delete(where => {key2 => 2});
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
693
$rows = $table->select->fetch_hash_all;
694
is_deeply($rows, [{key1 => 5, key2 => 4}], "$test: delete");
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
695
$table->update_all(param => {key1 => 3});
696
$rows = $table->select->fetch_hash_all;
697
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test: update_all");
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
698
$table->delete_all;
699
$rows = $table->select->fetch_hash_all;
700
is_deeply($rows, [], "$test: delete_all");
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
701

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
702
$dbi->dbh->do($CREATE_TABLE->{2});
703
$dbi->table('table2', ppp => sub {
704
    my $self = shift;
705
    
706
    return $self->name;
707
});
708
is($dbi->table('table2')->ppp, 'table2', "$test : helper");
709

            
many changed
Yuki Kimoto authored on 2011-01-23
710
$dbi->table('table2', {qqq => sub {
711
    my $self = shift;
712
    
713
    return $self->name;
714
}});
715
is($dbi->table('table2')->qqq, 'table2', "$test : helper");
716

            
717

            
add examples
Yuki Kimoto authored on 2011-01-07
718
test 'limit';
719
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
720
$dbi->execute($CREATE_TABLE->{0});
721
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
722
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
723
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
724
$dbi->query_builder->register_tag_processor(
725
    limit => sub {
726
        my ($count, $offset) = @_;
727
        
728
        my $s = '';
729
        $s .= "limit $count";
730
        $s .= " offset $offset" if defined $offset;
731
        
732
        return [$s, []];
733
    }
734
);
735
$rows = $dbi->select(
736
  table => 'table1',
737
  where => {key1 => 1},
738
  append => "order by key2 {limit 1 0}"
739
)->fetch_hash_all;
740
is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
741
$rows = $dbi->select(
742
  table => 'table1',
743
  where => {key1 => 1},
744
  append => "order by key2 {limit 2 1}"
745
)->fetch_hash_all;
746
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}], $test);
747
$rows = $dbi->select(
748
  table => 'table1',
749
  where => {key1 => 1},
750
  append => "order by key2 {limit 1}"
751
)->fetch_hash_all;
752
is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
753

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
754
test 'connect super';
755
{
756
    package MyDBI;
757
    
758
    use base 'DBIx::Custom';
759
    sub connect {
760
        my $self = shift->SUPER::connect(@_);
761
        
762
        return $self;
763
    }
764
    
765
    sub new {
766
        my $self = shift->SUPER::connect(@_);
767
        
768
        return $self;
769
    }
770
}
771

            
772
$dbi = MyDBI->connect($NEW_ARGS->{0});
773
$dbi->execute($CREATE_TABLE->{0});
774
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
775
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1, $test);
776

            
777
$dbi = MyDBI->new($NEW_ARGS->{0});
778
$dbi->execute($CREATE_TABLE->{0});
779
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
780
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1, $test);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
781

            
782

            
783
test 'end_filter';
784
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
785
$dbi->execute($CREATE_TABLE->{0});
786
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
787
$result = $dbi->select(table => 'table1');
788
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
789
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
790
$row = $result->fetch_first;
791
is_deeply($row, [6, 40]);
792

            
many changed
Yuki Kimoto authored on 2011-01-23
793
$dbi->register_filter(five_times => sub { $_[0] * 5 });
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
794
$result = $dbi->select(table => 'table1');
795
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
many changed
Yuki Kimoto authored on 2011-01-23
796
$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
797
$row = $result->fetch_hash_first;
798
is_deeply($row, {key1 => 6, key2 => 40});
fix select method empty wher...
Yuki Kimoto authored on 2011-01-17
799

            
800

            
801
test 'empty where select';
802
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
803
$dbi->execute($CREATE_TABLE->{0});
804
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
805
$result = $dbi->select(table => 'table1', where => {});
806
$row = $result->fetch_hash_first;
807
is_deeply($row, {key1 => 1, key2 => 2});
808

            
809
$result = $dbi->select(table => 'table1', where => [' ', {}]);
810
$row = $result->fetch_hash_first;
811
is_deeply($row, {key1 => 1, key2 => 2});
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
812

            
813

            
814
test 'select query option';
815
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
816
$dbi->execute($CREATE_TABLE->{0});
817
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
818
is(ref $query, 'DBIx::Custom::Query');
819
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
820
is(ref $query, 'DBIx::Custom::Query');
821
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
822
is(ref $query, 'DBIx::Custom::Query');
823
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
824
is(ref $query, 'DBIx::Custom::Query');
825

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
826
test 'DBIx::Custom::Where';
experimental extended select...
Yuki Kimoto authored on 2011-01-17
827
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
828
$dbi->execute($CREATE_TABLE->{0});
829
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
830
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
831
$where = $dbi->where
added test
Yuki Kimoto authored on 2011-01-19
832
             ->clause(['and', '{= key1}', '{= key2}'])
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
833
             ->param({key1 => 1});
added test
Yuki Kimoto authored on 2011-01-19
834

            
experimental extended select...
Yuki Kimoto authored on 2011-01-17
835
$result = $dbi->select(
836
    table => 'table1',
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
837
    where => $where
experimental extended select...
Yuki Kimoto authored on 2011-01-17
838
);
839
$row = $result->fetch_hash_all;
840
is_deeply($row, [{key1 => 1, key2 => 2}]);
841

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
842
$where = $dbi->where
added test
Yuki Kimoto authored on 2011-01-19
843
             ->clause(['and', '{= key1}', '{= key2}'])
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
844
             ->param({key1 => 1, key2 => 2});
experimental extended select...
Yuki Kimoto authored on 2011-01-17
845
$result = $dbi->select(
846
    table => 'table1',
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
847
    where => $where
experimental extended select...
Yuki Kimoto authored on 2011-01-17
848
);
849
$row = $result->fetch_hash_all;
850
is_deeply($row, [{key1 => 1, key2 => 2}]);
851

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
852
$where = $dbi->where
added test
Yuki Kimoto authored on 2011-01-19
853
             ->clause(['and', '{= key1}', '{= key2}'])
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
854
             ->param({});
experimental extended select...
Yuki Kimoto authored on 2011-01-17
855
$result = $dbi->select(
856
    table => 'table1',
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
857
    where => $where,
experimental extended select...
Yuki Kimoto authored on 2011-01-17
858
);
859
$row = $result->fetch_hash_all;
860
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
861

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
862
$where = $dbi->where
added test
Yuki Kimoto authored on 2011-01-19
863
             ->clause(['and', ['or', '{> key1}', '{< key1}'], '{= key2}'])
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
864
             ->param({key1 => [0, 3], key2 => 2});
experimental extended select...
Yuki Kimoto authored on 2011-01-17
865
$result = $dbi->select(
866
    table => 'table1',
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
867
    where => $where,
experimental extended select...
Yuki Kimoto authored on 2011-01-17
868
);
869
$row = $result->fetch_hash_all;
870
is_deeply($row, [{key1 => 1, key2 => 2}]);
871

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
872
$where = $dbi->where;
873
$result = $dbi->select(
874
    table => 'table1',
875
    where => $where
876
);
877
$row = $result->fetch_hash_all;
878
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
879

            
experimental extended select...
Yuki Kimoto authored on 2011-01-17
880
eval {
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
881
$where = $dbi->where
added test
Yuki Kimoto authored on 2011-01-19
882
             ->clause(['uuu']);
experimental extended select...
Yuki Kimoto authored on 2011-01-17
883
$result = $dbi->select(
884
    table => 'table1',
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
885
    where => $where
experimental extended select...
Yuki Kimoto authored on 2011-01-17
886
);
887
};
888
ok($@);
889

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
890
$where = $dbi->where;
891
is("$where", '');
892

            
added test
Yuki Kimoto authored on 2011-01-19
893
$where = $dbi->where
894
             ->clause(['or', ('{= key1}') x 2])
895
             ->param({key1 => [1, 3]});
896
$result = $dbi->select(
897
    table => 'table1',
898
    where => $where,
899
);
900
$row = $result->fetch_hash_all;
901
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
902

            
903
$where = $dbi->where
904
             ->clause(['or', ('{= key1}') x 2])
905
             ->param({key1 => [1]});
906
$result = $dbi->select(
907
    table => 'table1',
908
    where => $where,
909
);
910
$row = $result->fetch_hash_all;
911
is_deeply($row, [{key1 => 1, key2 => 2}]);
912

            
913
$where = $dbi->where
914
             ->clause(['or', ('{= key1}') x 2])
915
             ->param({key1 => 1});
916
$result = $dbi->select(
917
    table => 'table1',
918
    where => $where,
919
);
920
$row = $result->fetch_hash_all;
921
is_deeply($row, [{key1 => 1, key2 => 2}]);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
922

            
many changed
Yuki Kimoto authored on 2011-01-23
923
$where = $dbi->where
924
             ->clause('{= key1}')
925
             ->param({key1 => 1});
926
$result = $dbi->select(
927
    table => 'table1',
928
    where => $where,
929
);
930
$row = $result->fetch_hash_all;
931
is_deeply($row, [{key1 => 1, key2 => 2}]);
932

            
933
$where = $dbi->where
934
             ->clause('{= key1} {= key2}')
935
             ->param({key1 => 1});
936
eval{$where->to_string};
937
like($@, qr/one column/);
938

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
939
test 'dbi_option default';
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
940
$dbi = DBIx::Custom->new;
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
941
is_deeply($dbi->dbi_option, {});
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
942

            
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
943
test 'register_tag_processor';
944
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
945
$dbi->register_tag_processor(
946
    a => sub { 1 }
947
);
948
is($dbi->query_builder->tag_processors->{a}->(), 1);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
949

            
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
950
test 'table not specify exception';
951
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
952
eval {$dbi->insert};
953
like($@, qr/table/);
954
eval {$dbi->update};
955
like($@, qr/table/);
956
eval {$dbi->delete};
957
like($@, qr/table/);
958
eval {$dbi->select};
959
like($@, qr/table/);
many changed
Yuki Kimoto authored on 2011-01-23
960

            
961

            
962
test 'more tests';
963
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
964
eval{$dbi->apply_filter('table', 'column', [])};
965
like($@, qr/apply_filter/);
966

            
967
eval{$dbi->apply_filter('table', 'column', {outer => 2})};
968
like($@, qr/apply_filter/);
969

            
970
$dbi->apply_filter(
971

            
972
);
973
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
974
$dbi->execute($CREATE_TABLE->{0});
975
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
976
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
977
$dbi->apply_filter('table1', 'key2', 
978
                   {in => sub { $_[0] * 3 }, out => sub { $_[0] * 2 }});
979
$rows = $dbi->select(table => 'table1', where => {key2 => 1})->fetch_hash_all;
980
is_deeply($rows, [{key1 => 1, key2 => 6}]);
981

            
982
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
983
$dbi->execute($CREATE_TABLE->{0});
984
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
985
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
986
$dbi->apply_filter('table1', 'key2', {});
987
$rows = $dbi->select(table => 'table1', where => {key2 => 2})->fetch_hash_all;
988
is_deeply($rows, [{key1 => 1, key2 => 2}]);
989

            
990
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
991
eval {$dbi->apply_filter('table1', 'key2', {out => 'no'})};
992
like($@, qr/not registered/);
993
eval {$dbi->apply_filter('table1', 'key2', {in => 'no'})};
994
like($@, qr/not registered/);
995
$dbi->helper({one => sub { 1 }});
996
is($dbi->one, 1);
997

            
998
eval{DBIx::Custom->connect()};
999
like($@, qr/connect/);
1000

            
1001
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1002
$dbi->execute($CREATE_TABLE->{0});
1003
$dbi->register_filter(twice => sub { $_[0] * 2 });
1004
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1005
             filter => {key1 => 'twice'});
1006
$row = $dbi->select(table => 'table1')->fetch_hash_first;
1007
is_deeply($row, {key1 => 2, key2 => 2});
1008
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
1009
             filter => {key1 => 'no'}) };
1010
like($@, qr//);
1011
$dbi->table_class('!!!!');
1012
eval {$dbi->table};
1013
like($@, qr/Invalid table/);
1014

            
1015
$dbi->table_class('NOTEXIST');
1016
eval {$dbi->table};
1017
ok($@);
1018
$dbi->register_filter(one => sub { });
1019
$dbi->default_fetch_filter('one');
1020
ok($dbi->default_fetch_filter);
1021
$dbi->default_bind_filter('one');
1022
ok($dbi->default_bind_filter);
1023
eval{$dbi->default_fetch_filter('no')};
1024
like($@, qr/not registered/);
1025
eval{$dbi->default_bind_filter('no')};
1026
like($@, qr/not registered/);
1027
$dbi->default_bind_filter(undef);
1028
ok(!defined $dbi->default_bind_filter);
1029
$dbi->default_fetch_filter(undef);
1030
ok(!defined $dbi->default_fetch_filter);
1031
eval {$dbi->execute('select * from table1 {= author') };
1032
like($@, qr/Tag not finished/);
1033

            
1034
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
1035
$dbi->execute($CREATE_TABLE->{0});
1036
$dbi->register_filter(one => sub { 1 });
1037
$result = $dbi->select(table => 'table1');
1038
eval {$result->filter(key1 => 'no')};
1039
like($@, qr/not registered/);
1040
eval {$result->end_filter(key1 => 'no')};
1041
like($@, qr/not registered/);
1042
$result->default_filter(undef);
1043
ok(!defined $result->default_filter);
1044
$result->default_filter('one');
1045
is($result->default_filter->(), 1);
1046

            
1047
$dbi->table('book');
1048
eval{$dbi->table('book')->no_exists};
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1049
like($@, qr/locate/);
1050

            
1051
test 'dbi_option';
1052
$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:',
1053
                             dbi_option => {PrintError => 1});
1054
ok($dbi->dbh->{PrintError});
1055
$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:',
1056
                             dbi_options => {PrintError => 1});
1057
ok($dbi->dbh->{PrintError});
1058