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

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

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

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

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

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

            
31
my $SELECT_TMPLS = {
32
    0 => 'select * from table1;'
33
};
34

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

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

            
43
# Variables
44
my $dbi;
45
my $sth;
46
my $tmpl;
47
my @tmpls;
48
my $select_tmpl;
49
my $insert_tmpl;
50
my $update_tmpl;
51
my $params;
52
my $sql;
53
my $result;
54
my $row;
55
my @rows;
56
my $rows;
57
my $query;
58
my @queries;
59
my $select_query;
60
my $insert_query;
61
my $update_query;
62
my $ret_val;
63

            
64

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

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

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

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

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

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

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

            
105

            
106
test 'Direct query';
107
$dbi->execute($DROP_TABLE->{0});
108
$dbi->execute($CREATE_TABLE->{0});
109
$insert_tmpl = "insert into table1 {insert key1 key2}";
110
$dbi->execute($insert_tmpl, param => {key1 => 1, key2 => 2});
111
$result = $dbi->execute($SELECT_TMPLS->{0});
112
$rows = $result->fetch_hash_all;
113
is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
114

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

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

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

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

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

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

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

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

            
172
test 'DBIx::Custom::SQLTemplate insert tag';
173
$dbi->execute("delete from table1");
174
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}';
175
$dbi->execute($insert_tmpl, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
176

            
177
$result = $dbi->execute($SELECT_TMPLS->{0});
178
$rows = $result->fetch_hash_all;
179
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
180

            
181
test 'DBIx::Custom::SQLTemplate update tag';
182
$dbi->execute("delete from table1");
183
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}";
184
$dbi->execute($insert_tmpl, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
185
$dbi->execute($insert_tmpl, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
186

            
187
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}';
188
$dbi->execute($update_tmpl, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5});
189

            
190
$result = $dbi->execute($SELECT_TMPLS->{0});
191
$rows = $result->fetch_hash_all;
192
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
193
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic");
194

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

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

            
203
test 'insert';
204
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
205
$dbi->execute($CREATE_TABLE->{0});
206
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
207
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
208
$result = $dbi->execute($SELECT_TMPLS->{0});
209
$rows   = $result->fetch_hash_all;
210
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic");
211

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

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

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

            
233

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

            
256
$dbi->execute("delete from table1");
257
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
258
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
259
$dbi->register_filter(twice => sub { $_[0] * 2 });
260
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
261
              filter => {key2 => 'twice'});
262
$result = $dbi->execute($SELECT_TMPLS->{0});
263
$rows   = $result->fetch_hash_all;
264
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
265
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
266
                  "$test : filter");
267

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

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
270
eval{$dbi->update(table => 'table1', noexist => 1)};
271
like($@, qr/noexist/, "$test: invalid name");
272

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

            
276

            
removed register_format()
yuki-kimoto authored on 2010-05-26
277
test 'update_all';
278
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
279
$dbi->execute($CREATE_TABLE->{1});
280
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
281
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
282
$dbi->register_filter(twice => sub { $_[0] * 2 });
283
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
284
$result = $dbi->execute($SELECT_TMPLS->{0});
285
$rows   = $result->fetch_hash_all;
286
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
287
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
288
                  "$test : filter");
289

            
290

            
291
test 'delete';
292
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
293
$dbi->execute($CREATE_TABLE->{0});
294
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
295
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
296
$dbi->delete(table => 'table1', where => {key1 => 1});
297
$result = $dbi->execute($SELECT_TMPLS->{0});
298
$rows   = $result->fetch_hash_all;
299
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic");
300

            
301
$dbi->execute("delete from table1;");
302
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
303
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
304
$dbi->register_filter(twice => sub { $_[0] * 2 });
305
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
306
$result = $dbi->execute($SELECT_TMPLS->{0});
307
$rows   = $result->fetch_hash_all;
308
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter");
309

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

            
312
$dbi->delete_all(table => 'table1');
313
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
314
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
315
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
316
$rows = $dbi->select(table => 'table1')->fetch_hash_all;
317
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key");
318

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
319
eval{$dbi->delete(table => 'table1', noexist => 1)};
320
like($@, qr/noexist/, "$test: invalid name");
321

            
322

            
removed register_format()
yuki-kimoto authored on 2010-05-26
323
test 'delete error';
324
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
325
$dbi->execute($CREATE_TABLE->{0});
326
eval{$dbi->delete(table => 'table1')};
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
327
like($@, qr/Key-value pairs for where clause must be specified to "delete" second argument/,
removed register_format()
yuki-kimoto authored on 2010-05-26
328
         "$test : where key-value pairs not specified");
329

            
330
test 'delete_all';
331
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
332
$dbi->execute($CREATE_TABLE->{0});
333
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
334
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
335
$dbi->delete_all(table => 'table1');
336
$result = $dbi->execute($SELECT_TMPLS->{0});
337
$rows   = $result->fetch_hash_all;
338
is_deeply($rows, [], "$test : basic");
339

            
340

            
341
test 'select';
342
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
343
$dbi->execute($CREATE_TABLE->{0});
344
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
345
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
346
$rows = $dbi->select(table => 'table1')->fetch_hash_all;
347
is_deeply($rows, [{key1 => 1, key2 => 2},
348
                  {key1 => 3, key2 => 4}], "$test : table");
349

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

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

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

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

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

            
367
$dbi->execute($CREATE_TABLE->{2});
368
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
369
$rows = $dbi->select(
370
    table => [qw/table1 table2/],
update document
yuki-kimoto authored on 2010-05-27
371
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
removed register_format()
yuki-kimoto authored on 2010-05-26
372
    where   => {'table1.key2' => 2},
added commit method
yuki-kimoto authored on 2010-05-27
373
    relation  => {'table1.key1' => 'table2.key1'}
removed register_format()
yuki-kimoto authored on 2010-05-26
374
)->fetch_hash_all;
added commit method
yuki-kimoto authored on 2010-05-27
375
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : relation : exists where");
376

            
377
$rows = $dbi->select(
378
    table => [qw/table1 table2/],
379
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
380
    relation  => {'table1.key1' => 'table2.key1'}
381
)->fetch_hash_all;
382
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
383

            
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
384
eval{$dbi->select(table => 'table1', noexist => 1)};
385
like($@, qr/noexist/, "$test: invalid name");
386

            
387

            
removed register_format()
yuki-kimoto authored on 2010-05-26
388
test 'fetch filter';
389
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
390
$dbi->register_filter(
391
    twice       => sub { $_[0] * 2 },
392
    three_times => sub { $_[0] * 3 }
393
);
394
$dbi->default_fetch_filter('twice');
395
$dbi->execute($CREATE_TABLE->{0});
396
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
397
$result = $dbi->select(table => 'table1');
398
$result->filter({key1 => 'three_times'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
399
$row = $result->fetch_hash_first;
removed register_format()
yuki-kimoto authored on 2010-05-26
400
is_deeply($row, {key1 => 3, key2 => 4}, "$test: default_fetch_filter and filter");
401

            
402
test 'filters';
403
$dbi = DBIx::Custom->new;
404

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

            
408
is($dbi->filters->{encode_utf8}->('あ'),
409
   encode_utf8('あ'), "$test : encode_utf8");
410

            
added commit method
yuki-kimoto authored on 2010-05-27
411
test 'transaction';
412
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
413
$dbi->execute($CREATE_TABLE->{0});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
414
$dbi->dbh->begin_work;
added commit method
yuki-kimoto authored on 2010-05-27
415
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
416
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
417
$dbi->dbh->commit;
added commit method
yuki-kimoto authored on 2010-05-27
418
$result = $dbi->select(table => 'table1');
419
is_deeply(scalar $result->fetch_hash_all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
420
          "$test : commit");
421

            
422
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
423
$dbi->execute($CREATE_TABLE->{0});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
424
$dbi->dbh->begin_work(0);
added commit method
yuki-kimoto authored on 2010-05-27
425
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
426
$dbi->dbh->rollback;
added commit method
yuki-kimoto authored on 2010-05-27
427

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

            
431
test 'cache';
432
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
433
$dbi->execute($CREATE_TABLE->{0});
434
$tmpl = 'select * from table1 where {= key1} and {= key2};';
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
435
$dbi->build_query($tmpl);
add cache attribute
yuki-kimoto authored on 2010-06-14
436
is_deeply($dbi->{_cached}->{$tmpl}, 
437
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2']}, "$test : cache");
438

            
439
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
440
$dbi->execute($CREATE_TABLE->{0});
441
$dbi->{_cached} = {};
442
$dbi->cache(0);
443
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
444
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
445

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