DBIx-Custom / t / dbix-custom-core-sqlite.t /
Newer Older
925 lines | 33.284kb
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},
271
              filter => {key2 => 'twice'});
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

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

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
746
test 'connect super';
747
{
748
    package MyDBI;
749
    
750
    use base 'DBIx::Custom';
751
    sub connect {
752
        my $self = shift->SUPER::connect(@_);
753
        
754
        return $self;
755
    }
756
    
757
    sub new {
758
        my $self = shift->SUPER::connect(@_);
759
        
760
        return $self;
761
    }
762
}
763

            
764
$dbi = MyDBI->connect($NEW_ARGS->{0});
765
$dbi->execute($CREATE_TABLE->{0});
766
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
767
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1, $test);
768

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

            
774

            
775
test 'end_filter';
776
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
777
$dbi->execute($CREATE_TABLE->{0});
778
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
779
$result = $dbi->select(table => 'table1');
780
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
781
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
782
$row = $result->fetch_first;
783
is_deeply($row, [6, 40]);
784

            
785
$result = $dbi->select(table => 'table1');
786
$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
787
$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
788
$row = $result->fetch_hash_first;
789
is_deeply($row, {key1 => 6, key2 => 40});
fix select method empty wher...
Yuki Kimoto authored on 2011-01-17
790

            
791

            
792
test 'empty where select';
793
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
794
$dbi->execute($CREATE_TABLE->{0});
795
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
796
$result = $dbi->select(table => 'table1', where => {});
797
$row = $result->fetch_hash_first;
798
is_deeply($row, {key1 => 1, key2 => 2});
799

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

            
804

            
805
test 'select query option';
806
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
807
$dbi->execute($CREATE_TABLE->{0});
808
$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
809
is(ref $query, 'DBIx::Custom::Query');
810
$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
811
is(ref $query, 'DBIx::Custom::Query');
812
$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
813
is(ref $query, 'DBIx::Custom::Query');
814
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
815
is(ref $query, 'DBIx::Custom::Query');
816

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
817
test 'DBIx::Custom::Where';
experimental extended select...
Yuki Kimoto authored on 2011-01-17
818
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
819
$dbi->execute($CREATE_TABLE->{0});
820
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
821
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
822
$where = $dbi->where
added test
Yuki Kimoto authored on 2011-01-19
823
             ->clause(['and', '{= key1}', '{= key2}'])
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
824
             ->param({key1 => 1});
added test
Yuki Kimoto authored on 2011-01-19
825

            
experimental extended select...
Yuki Kimoto authored on 2011-01-17
826
$result = $dbi->select(
827
    table => 'table1',
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
828
    where => $where
experimental extended select...
Yuki Kimoto authored on 2011-01-17
829
);
830
$row = $result->fetch_hash_all;
831
is_deeply($row, [{key1 => 1, key2 => 2}]);
832

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

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

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

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
863
$where = $dbi->where;
864
$result = $dbi->select(
865
    table => 'table1',
866
    where => $where
867
);
868
$row = $result->fetch_hash_all;
869
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
870

            
experimental extended select...
Yuki Kimoto authored on 2011-01-17
871
eval {
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
872
$where = $dbi->where
added test
Yuki Kimoto authored on 2011-01-19
873
             ->clause(['uuu']);
experimental extended select...
Yuki Kimoto authored on 2011-01-17
874
$result = $dbi->select(
875
    table => 'table1',
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
876
    where => $where
experimental extended select...
Yuki Kimoto authored on 2011-01-17
877
);
878
};
879
ok($@);
880

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

            
added test
Yuki Kimoto authored on 2011-01-19
884
$where = $dbi->where
885
             ->clause(['or', ('{= key1}') x 2])
886
             ->param({key1 => [1, 3]});
887
$result = $dbi->select(
888
    table => 'table1',
889
    where => $where,
890
);
891
$row = $result->fetch_hash_all;
892
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
893

            
894
$where = $dbi->where
895
             ->clause(['or', ('{= key1}') x 2])
896
             ->param({key1 => [1]});
897
$result = $dbi->select(
898
    table => 'table1',
899
    where => $where,
900
);
901
$row = $result->fetch_hash_all;
902
is_deeply($row, [{key1 => 1, key2 => 2}]);
903

            
904
$where = $dbi->where
905
             ->clause(['or', ('{= key1}') x 2])
906
             ->param({key1 => 1});
907
$result = $dbi->select(
908
    table => 'table1',
909
    where => $where,
910
);
911
$row = $result->fetch_hash_all;
912
is_deeply($row, [{key1 => 1, key2 => 2}]);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
913

            
914
test 'dbi_options default';
915
$dbi = DBIx::Custom->new;
916
is_deeply($dbi->dbi_options, {});
917

            
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
918
test 'register_tag_processor';
919
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
920
$dbi->register_tag_processor(
921
    a => sub { 1 }
922
);
923
is($dbi->query_builder->tag_processors->{a}->(), 1);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
924

            
experimental extended select...
Yuki Kimoto authored on 2011-01-17
925