DBIx-Custom / t / dbix-custom-core-sqlite.t /
Newer Older
540 lines | 21.516kb
packaging one directory
yuki-kimoto authored on 2009-11-16
1
use Test::More;
2
use strict;
3
use warnings;
4

            
5
BEGIN {
6
    eval { require DBD::SQLite; 1 }
7
        or plan skip_all => 'DBD::SQLite required';
8
    eval { DBD::SQLite->VERSION >= 1.25 }
9
        or plan skip_all => 'DBD::SQLite >= 1.25 required';
10

            
11
    plan 'no_plan';
12
    use_ok('DBIx::Custom');
13
}
14

            
15
# Function for test name
16
my $test;
17
sub test {
18
    $test = shift;
19
}
20

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

            
many change
yuki-kimoto authored on 2010-02-11
28
my $SELECT_TMPLS = {
packaging one directory
yuki-kimoto authored on 2009-11-16
29
    0 => 'select * from table1;'
30
};
31

            
32
my $DROP_TABLE = {
33
    0 => 'drop table table1'
34
};
35

            
36
my $NEW_ARGS = {
37
    0 => {data_source => 'dbi:SQLite:dbname=:memory:'}
38
};
39

            
fixed default_fetch_filter
yuki-kimoto authored on 2010-05-01
40
# Variables
packaging one directory
yuki-kimoto authored on 2009-11-16
41
my $dbi;
42
my $sth;
43
my $tmpl;
44
my @tmpls;
45
my $select_tmpl;
46
my $insert_tmpl;
47
my $update_tmpl;
48
my $params;
49
my $sql;
50
my $result;
fixed default_fetch_filter
yuki-kimoto authored on 2010-05-01
51
my $row;
packaging one directory
yuki-kimoto authored on 2009-11-16
52
my @rows;
53
my $rows;
54
my $query;
55
my @queries;
56
my $select_query;
57
my $insert_query;
58
my $update_query;
59
my $ret_val;
60

            
61

            
62
test 'disconnect';
63
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
64
$dbi->connect;
65
$dbi->disconnect;
66
ok(!$dbi->dbh, $test);
67

            
68

            
69
test 'connected';
70
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
71
ok(!$dbi->connected, "$test : not connected");
72
$dbi->connect;
73
ok($dbi->connected, "$test : connected");
74

            
75

            
version 0.0901
yuki-kimoto authored on 2009-12-17
76
test 'create_table';
77
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
78
$ret_val = $dbi->create_table(
79
                   'table1',
80
                   'key1 char(255)',
81
                   'key2 char(255)'
82
                 );
83
ok(defined $ret_val, "$test : create_table");
84

            
cleanup
yuki-kimoto authored on 2010-02-11
85
$dbi->insert('table1', {key1 => 1, key2 => 2});
version 0.0901
yuki-kimoto authored on 2009-12-17
86
ok(!$@, "$test : table exist");
87

            
88
$ret_val = $dbi->drop_table('table1');
89
ok(defined $ret_val, "$test : drop table");
90

            
91
eval{$dbi->select('table1')};
92
ok($@, "$test : table not exist");
packaging one directory
yuki-kimoto authored on 2009-11-16
93

            
94
# Prepare table
95
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
96
$dbi->connect;
rename query() to execute()
yuki-kimoto authored on 2010-05-01
97
$dbi->execute($CREATE_TABLE->{0});
add all tests
yuki-kimoto authored on 2010-05-01
98
$dbi->insert('table1', {key1 => 1, key2 => 2});
99
$dbi->insert('table1', {key1 => 3, key2 => 4});
packaging one directory
yuki-kimoto authored on 2009-11-16
100

            
101
test 'DBIx::Custom::Result test';
102
$tmpl = "select key1, key2 from table1";
103
$query = $dbi->create_query($tmpl);
rename query() to execute()
yuki-kimoto authored on 2010-05-01
104
$result = $dbi->execute($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
105

            
106
@rows = ();
107
while (my $row = $result->fetch) {
108
    push @rows, [@$row];
109
}
110
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context");
111

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
112
$result = $dbi->execute($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
113
@rows = ();
114
while (my @row = $result->fetch) {
115
    push @rows, [@row];
116
}
117
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context");
118

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
119
$result = $dbi->execute($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
120
@rows = ();
121
while (my $row = $result->fetch_hash) {
122
    push @rows, {%$row};
123
}
124
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context");
125

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
126
$result = $dbi->execute($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
127
@rows = ();
128
while (my %row = $result->fetch_hash) {
129
    push @rows, {%row};
130
}
131
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context");
132

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
133
$result = $dbi->execute($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
134
$rows = $result->fetch_all;
135
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context");
136

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
137
$result = $dbi->execute($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
138
@rows = $result->fetch_all;
139
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context");
140

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
141
$result = $dbi->execute($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
142
@rows = $result->fetch_hash_all;
143
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_hash_all scalar context");
144

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
145
$result = $dbi->execute($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
146
@rows = $result->fetch_all;
147
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_hash_all list context");
148

            
149

            
150
test 'Insert query return value';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
151
$dbi->execute($DROP_TABLE->{0});
152
$dbi->execute($CREATE_TABLE->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
153
$tmpl = "insert into table1 {insert key1 key2}";
154
$query = $dbi->create_query($tmpl);
rename query() to execute()
yuki-kimoto authored on 2010-05-01
155
$ret_val = $dbi->execute($query, {key1 => 1, key2 => 2});
packaging one directory
yuki-kimoto authored on 2009-11-16
156
ok($ret_val, $test);
157

            
158

            
version 0.0901
yuki-kimoto authored on 2009-12-17
159
test 'Direct query';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
160
$dbi->execute($DROP_TABLE->{0});
161
$dbi->execute($CREATE_TABLE->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
162
$insert_tmpl = "insert into table1 {insert key1 key2}";
rename query() to execute()
yuki-kimoto authored on 2010-05-01
163
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2});
164
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
165
$rows = $result->fetch_hash_all;
many change
yuki-kimoto authored on 2010-04-30
166
is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
packaging one directory
yuki-kimoto authored on 2009-11-16
167

            
168
test 'Filter basic';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
169
$dbi->execute($DROP_TABLE->{0});
170
$dbi->execute($CREATE_TABLE->{0});
some changed
yuki-kimoto authored on 2010-05-02
171
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
many change
yuki-kimoto authored on 2010-04-30
172
                    three_times => sub { $_[0] * 3});
packaging one directory
yuki-kimoto authored on 2009-11-16
173

            
174
$insert_tmpl  = "insert into table1 {insert key1 key2};";
175
$insert_query = $dbi->create_query($insert_tmpl);
many change
yuki-kimoto authored on 2010-04-30
176
$insert_query->filter({key1 => 'twice'});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
177
$dbi->execute($insert_query, {key1 => 1, key2 => 2});
178
$result = $dbi->execute($SELECT_TMPLS->{0});
many change
yuki-kimoto authored on 2010-04-30
179
$rows = $result->filter({key2 => 'three_times'})->fetch_hash_all;
180
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : filter fetch_filter");
rename query() to execute()
yuki-kimoto authored on 2010-05-01
181
$dbi->execute($DROP_TABLE->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
182

            
183
test 'Filter in';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
184
$dbi->execute($CREATE_TABLE->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
185
$insert_tmpl  = "insert into table1 {insert key1 key2};";
186
$insert_query = $dbi->create_query($insert_tmpl);
rename query() to execute()
yuki-kimoto authored on 2010-05-01
187
$dbi->execute($insert_query, {key1 => 2, key2 => 4});
packaging one directory
yuki-kimoto authored on 2009-11-16
188
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
189
$select_query = $dbi->create_query($select_tmpl);
many change
yuki-kimoto authored on 2010-04-30
190
$select_query->filter({'table1.key1' => 'twice'});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
191
$result = $dbi->execute($select_query, {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
packaging one directory
yuki-kimoto authored on 2009-11-16
192
$rows = $result->fetch_hash_all;
many change
yuki-kimoto authored on 2010-04-30
193
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : filter");
packaging one directory
yuki-kimoto authored on 2009-11-16
194

            
many many changes
yuki-kimoto authored on 2010-04-30
195
test 'DBIx::Custom::SQLTemplate basic tag';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
196
$dbi->execute($DROP_TABLE->{0});
197
$dbi->execute($CREATE_TABLE->{1});
add all tests
yuki-kimoto authored on 2010-05-01
198
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
199
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
packaging one directory
yuki-kimoto authored on 2009-11-16
200

            
201
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};";
202
$query = $dbi->create_query($tmpl);
rename query() to execute()
yuki-kimoto authored on 2010-05-01
203
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
packaging one directory
yuki-kimoto authored on 2009-11-16
204
$rows = $result->fetch_hash_all;
205
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1");
206

            
207
$tmpl = "select * from table1 where {<= key1} and {like key2};";
208
$query = $dbi->create_query($tmpl);
rename query() to execute()
yuki-kimoto authored on 2010-05-01
209
$result = $dbi->execute($query, {key1 => 1, key2 => '%2%'});
packaging one directory
yuki-kimoto authored on 2009-11-16
210
$rows = $result->fetch_hash_all;
211
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2");
212

            
many many changes
yuki-kimoto authored on 2010-04-30
213
test 'DIB::Custom::SQLTemplate in tag';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
214
$dbi->execute($DROP_TABLE->{0});
215
$dbi->execute($CREATE_TABLE->{1});
add all tests
yuki-kimoto authored on 2010-05-01
216
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
217
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
packaging one directory
yuki-kimoto authored on 2009-11-16
218

            
219
$tmpl = "select * from table1 where {in key1 2};";
220
$query = $dbi->create_query($tmpl);
rename query() to execute()
yuki-kimoto authored on 2010-05-01
221
$result = $dbi->execute($query, {key1 => [9, 1]});
packaging one directory
yuki-kimoto authored on 2009-11-16
222
$rows = $result->fetch_hash_all;
223
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
224

            
many many changes
yuki-kimoto authored on 2010-04-30
225
test 'DBIx::Custom::SQLTemplate insert tag';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
226
$dbi->execute("delete from table1");
packaging one directory
yuki-kimoto authored on 2009-11-16
227
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
228
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
packaging one directory
yuki-kimoto authored on 2009-11-16
229

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
230
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
231
$rows = $result->fetch_hash_all;
232
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
233

            
many many changes
yuki-kimoto authored on 2010-04-30
234
test 'DBIx::Custom::SQLTemplate update tag';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
235
$dbi->execute("delete from table1");
packaging one directory
yuki-kimoto authored on 2009-11-16
236
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}";
rename query() to execute()
yuki-kimoto authored on 2010-05-01
237
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
238
$dbi->execute($insert_tmpl, {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
packaging one directory
yuki-kimoto authored on 2009-11-16
239

            
240
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
241
$dbi->execute($update_tmpl, {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5});
packaging one directory
yuki-kimoto authored on 2009-11-16
242

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
243
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
244
$rows = $result->fetch_hash_all;
245
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
246
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic");
247

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
248
test 'transaction';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
249
$dbi->execute($DROP_TABLE->{0});
250
$dbi->execute($CREATE_TABLE->{0});
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
251
$dbi->run_transaction(sub {
packaging one directory
yuki-kimoto authored on 2009-11-16
252
    $insert_tmpl = 'insert into table1 {insert key1 key2}';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
253
    $dbi->execute($insert_tmpl, {key1 => 1, key2 => 2});
254
    $dbi->execute($insert_tmpl, {key1 => 3, key2 => 4});
packaging one directory
yuki-kimoto authored on 2009-11-16
255
});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
256
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
257
$rows   = $result->fetch_hash_all;
258
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit");
259

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
260
$dbi->execute($DROP_TABLE->{0});
261
$dbi->execute($CREATE_TABLE->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
262
$dbi->dbh->{RaiseError} = 0;
263
eval{
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
264
    $dbi->run_transaction(sub {
packaging one directory
yuki-kimoto authored on 2009-11-16
265
        $insert_tmpl = 'insert into table1 {insert key1 key2}';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
266
        $dbi->execute($insert_tmpl, {key1 => 1, key2 => 2});
packaging one directory
yuki-kimoto authored on 2009-11-16
267
        die "Fatal Error";
rename query() to execute()
yuki-kimoto authored on 2010-05-01
268
        $dbi->execute($insert_tmpl, {key1 => 3, key2 => 4});
packaging one directory
yuki-kimoto authored on 2009-11-16
269
    })
270
};
271
like($@, qr/Fatal Error.*Rollback is success/ms, "$test : Rollback success message");
272
ok(!$dbi->dbh->{RaiseError}, "$test : restore RaiseError value");
rename query() to execute()
yuki-kimoto authored on 2010-05-01
273
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
274
$rows   = $result->fetch_hash_all;
275
is_deeply($rows, [], "$test : rollback");
276

            
277

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
278

            
packaging one directory
yuki-kimoto authored on 2009-11-16
279
test 'Error case';
280
$dbi = DBIx::Custom->new;
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
281
eval{$dbi->run_transaction};
packaging one directory
yuki-kimoto authored on 2009-11-16
282
like($@, qr/Not yet connect to database/, "$test : Yet Connected");
283

            
284
$dbi = DBIx::Custom->new(data_source => 'dbi:SQLit');
285
eval{$dbi->connect;};
286
ok($@, "$test : connect error");
287

            
288
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
289
$dbi->connect;
290
$dbi->dbh->{AutoCommit} = 0;
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
291
eval{$dbi->run_transaction};
packaging one directory
yuki-kimoto authored on 2009-11-16
292
like($@, qr/AutoCommit must be true before transaction start/,
remove run_transaction().
yuki-kimoto authored on 2010-01-30
293
         "$test : transaction auto commit is false");
packaging one directory
yuki-kimoto authored on 2009-11-16
294

            
295
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
296
$sql = 'laksjdf';
rename query() to execute()
yuki-kimoto authored on 2010-05-01
297
eval{$dbi->execute($sql, qw/1 2 3/)};
add all tests
yuki-kimoto authored on 2010-05-01
298
like($@, qr/$sql/, "$test : query fail");
packaging one directory
yuki-kimoto authored on 2009-11-16
299

            
300
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
301
eval{$dbi->create_query("{p }")};
302
ok($@, "$test : create_query invalid SQL template");
303

            
304
test 'insert';
305
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
306
$dbi->execute($CREATE_TABLE->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
307
$dbi->insert('table1', {key1 => 1, key2 => 2});
308
$dbi->insert('table1', {key1 => 3, key2 => 4});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
309
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
310
$rows   = $result->fetch_hash_all;
311
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic");
312

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
313
$dbi->execute('delete from table1');
some changed
yuki-kimoto authored on 2010-05-02
314
$dbi->register_filter(
fixed default_fetch_filter
yuki-kimoto authored on 2010-05-01
315
    twice       => sub { $_[0] * 2 },
316
    three_times => sub { $_[0] * 3 }
317
);
318
$dbi->default_query_filter('twice');
many change
yuki-kimoto authored on 2010-04-30
319
$dbi->insert('table1', {key1 => 1, key2 => 2}, {filter => {key1 => 'three_times'}});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
320
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
321
$rows   = $result->fetch_hash_all;
fixed default_fetch_filter
yuki-kimoto authored on 2010-05-01
322
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter");
323
$dbi->default_query_filter(undef);
packaging one directory
yuki-kimoto authored on 2009-11-16
324

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
325
$dbi->execute($DROP_TABLE->{0});
326
$dbi->execute($CREATE_TABLE->{0});
many change
yuki-kimoto authored on 2010-04-30
327
$dbi->insert('table1', {key1 => 1, key2 => 2}, {append => '   '});
328
$rows = $dbi->select('table1')->fetch_hash_all;
329
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
330

            
packaging one directory
yuki-kimoto authored on 2009-11-16
331

            
332
test 'insert error';
333
eval{$dbi->insert('table1')};
334
like($@, qr/Key-value pairs for insert must be specified to 'insert' second argument/, "$test : insert key-value not specifed");
335

            
336
test 'update';
337
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
338
$dbi->execute($CREATE_TABLE->{1});
packaging one directory
yuki-kimoto authored on 2009-11-16
339
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
340
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
341
$dbi->update('table1', {key2 => 11}, {where => {key1 => 1}});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
342
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
343
$rows   = $result->fetch_hash_all;
344
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
345
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
346
                  "$test : basic");
347
                  
rename query() to execute()
yuki-kimoto authored on 2010-05-01
348
$dbi->execute("delete from table1");
many change
yuki-kimoto authored on 2010-02-11
349
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
350
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
351
$dbi->update('table1', {key2 => 12}, {where => {key2 => 2, key3 => 3}});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
352
$result = $dbi->execute($SELECT_TMPLS->{0});
many change
yuki-kimoto authored on 2010-02-11
353
$rows   = $result->fetch_hash_all;
354
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
355
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
356
                  "$test : update key same as search key");
packaging one directory
yuki-kimoto authored on 2009-11-16
357

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
358
$dbi->execute("delete from table1");
packaging one directory
yuki-kimoto authored on 2009-11-16
359
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
360
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
some changed
yuki-kimoto authored on 2010-05-02
361
$dbi->register_filter(twice => sub { $_[0] * 2 });
add tests
yuki-kimoto authored on 2010-05-01
362
$dbi->update('table1', {key2 => 11}, {where => {key1 => 1},
363
              filter => {key2 => 'twice'}});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
364
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
365
$rows   = $result->fetch_hash_all;
366
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
367
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
add tests
yuki-kimoto authored on 2010-05-01
368
                  "$test : filter");
packaging one directory
yuki-kimoto authored on 2009-11-16
369

            
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
370

            
add tests
yuki-kimoto authored on 2010-05-01
371
$result = $dbi->update('table1', {key2 => 11}, {where => {key1 => 1}, append => '   '});
packaging one directory
yuki-kimoto authored on 2009-11-16
372

            
373
test 'update error';
374
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
375
$dbi->execute($CREATE_TABLE->{1});
packaging one directory
yuki-kimoto authored on 2009-11-16
376
eval{$dbi->update('table1')};
377
like($@, qr/Key-value pairs for update must be specified to 'update' second argument/,
378
         "$test : update key-value pairs not specified");
379

            
380
eval{$dbi->update('table1', {key2 => 1})};
381
like($@, qr/Key-value pairs for where clause must be specified to 'update' third argument/,
382
         "$test : where key-value pairs not specified");
383

            
384
test 'update_all';
385
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
386
$dbi->execute($CREATE_TABLE->{1});
packaging one directory
yuki-kimoto authored on 2009-11-16
387
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
388
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
some changed
yuki-kimoto authored on 2010-05-02
389
$dbi->register_filter(twice => sub { $_[0] * 2 });
add tests
yuki-kimoto authored on 2010-05-01
390
$dbi->update_all('table1', {key2 => 10}, {filter => {key2 => 'twice'}});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
391
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
392
$rows   = $result->fetch_hash_all;
393
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
394
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
add tests
yuki-kimoto authored on 2010-05-01
395
                  "$test : filter");
packaging one directory
yuki-kimoto authored on 2009-11-16
396

            
397

            
398
test 'delete';
399
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
400
$dbi->execute($CREATE_TABLE->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
401
$dbi->insert('table1', {key1 => 1, key2 => 2});
402
$dbi->insert('table1', {key1 => 3, key2 => 4});
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
403
$dbi->delete('table1', {where => {key1 => 1}});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
404
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
405
$rows   = $result->fetch_hash_all;
406
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic");
407

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
408
$dbi->execute("delete from table1;");
packaging one directory
yuki-kimoto authored on 2009-11-16
409
$dbi->insert('table1', {key1 => 1, key2 => 2});
410
$dbi->insert('table1', {key1 => 3, key2 => 4});
some changed
yuki-kimoto authored on 2010-05-02
411
$dbi->register_filter(twice => sub { $_[0] * 2 });
add tests
yuki-kimoto authored on 2010-05-01
412
$dbi->delete('table1', {where => {key2 => 1}, filter => {key2 => 'twice'}});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
413
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
414
$rows   = $result->fetch_hash_all;
add tests
yuki-kimoto authored on 2010-05-01
415
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter");
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
416

            
add tests
yuki-kimoto authored on 2010-05-01
417
$dbi->delete('table1', {where => {key1 => 1}, append => '   '});
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
418

            
packaging one directory
yuki-kimoto authored on 2009-11-16
419
$dbi->delete_all('table1');
420
$dbi->insert('table1', {key1 => 1, key2 => 2});
421
$dbi->insert('table1', {key1 => 3, key2 => 4});
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
422
$dbi->delete('table1', {where => {key1 => 1, key2 => 2}});
packaging one directory
yuki-kimoto authored on 2009-11-16
423
$rows = $dbi->select('table1')->fetch_hash_all;
424
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key");
425

            
426

            
427
test 'delete error';
428
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
429
$dbi->execute($CREATE_TABLE->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
430
eval{$dbi->delete('table1')};
431
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/,
432
         "$test : where key-value pairs not specified");
433

            
434
test 'delete_all';
435
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
436
$dbi->execute($CREATE_TABLE->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
437
$dbi->insert('table1', {key1 => 1, key2 => 2});
438
$dbi->insert('table1', {key1 => 3, key2 => 4});
439
$dbi->delete_all('table1');
rename query() to execute()
yuki-kimoto authored on 2010-05-01
440
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
441
$rows   = $result->fetch_hash_all;
442
is_deeply($rows, [], "$test : basic");
443

            
444

            
445
test 'select';
446
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
447
$dbi->execute($CREATE_TABLE->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
448
$dbi->insert('table1', {key1 => 1, key2 => 2});
449
$dbi->insert('table1', {key1 => 3, key2 => 4});
450
$rows = $dbi->select('table1')->fetch_hash_all;
451
is_deeply($rows, [{key1 => 1, key2 => 2},
452
                  {key1 => 3, key2 => 4}], "$test : table");
453

            
refactoring select
yuki-kimoto authored on 2010-04-28
454
$rows = $dbi->select('table1', {columns => ['key1']})->fetch_hash_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
455
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key");
456

            
refactoring select
yuki-kimoto authored on 2010-04-28
457
$rows = $dbi->select('table1', {where => {key1 => 1}})->fetch_hash_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
458
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key");
459

            
refactoring select
yuki-kimoto authored on 2010-04-28
460
$rows = $dbi->select('table1', {columns => ['key1'], where => {key1 => 3}})->fetch_hash_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
461
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key");
462

            
refactoring select
yuki-kimoto authored on 2010-04-28
463
$rows = $dbi->select('table1', {append => "order by key1 desc limit 1"})->fetch_hash_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
464
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement");
465

            
some changed
yuki-kimoto authored on 2010-05-02
466
$dbi->register_filter(decrement => sub { $_[0] - 1 });
add tests
yuki-kimoto authored on 2010-05-01
467
$rows = $dbi->select('table1', {where => {key1 => 2}, filter => {key1 => 'decrement'}})
468
            ->fetch_hash_all;
469
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : filter");
packaging one directory
yuki-kimoto authored on 2009-11-16
470

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
471
$dbi->execute($CREATE_TABLE->{2});
packaging one directory
yuki-kimoto authored on 2009-11-16
472
$dbi->insert('table2', {key1 => 1, key3 => 5});
473
$rows = $dbi->select([qw/table1 table2/],
refactoring select
yuki-kimoto authored on 2010-04-28
474
                      {
475
                         columns => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
476
                         where   => {'table1.key2' => 2},
477
                         append  => "where table1.key1 = table2.key1"
478
                      }
479
                    )->fetch_hash_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
480
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join");
481

            
482
test 'Cache';
483
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
484
DBIx::Custom->query_cache_max(2);
rename query() to execute()
yuki-kimoto authored on 2010-05-01
485
$dbi->execute($CREATE_TABLE->{0});
cleanup
yuki-kimoto authored on 2009-12-22
486
delete $DBIx::Custom::CLASS_ATTRS->{_query_caches};
487
delete $DBIx::Custom::CLASS_ATTRS->{_query_cache_keys};
packaging one directory
yuki-kimoto authored on 2009-11-16
488
$tmpls[0] = "insert into table1 {insert key1 key2}";
489
$queries[0] = $dbi->create_query($tmpls[0]);
490
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
add tests
yuki-kimoto authored on 2010-05-01
491
is(DBIx::Custom->_query_caches->{$tmpls[0]}{columns}, $queries[0]->columns, "$test : columns first");
packaging one directory
yuki-kimoto authored on 2009-11-16
492
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key first");
493

            
494
$tmpls[1] = "select * from table1";
495
$queries[1] = $dbi->create_query($tmpls[1]);
496
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
add tests
yuki-kimoto authored on 2010-05-01
497
is(DBIx::Custom->_query_caches->{$tmpls[0]}{columns}, $queries[0]->columns, "$test : columns first");
packaging one directory
yuki-kimoto authored on 2009-11-16
498
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql second");
add tests
yuki-kimoto authored on 2010-05-01
499
is(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns second");
packaging one directory
yuki-kimoto authored on 2009-11-16
500
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key second");
501

            
502
$tmpls[2] = "select key1, key2 from table1";
503
$queries[2] = $dbi->create_query($tmpls[2]);
504
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
505
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
add tests
yuki-kimoto authored on 2010-05-01
506
is(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns cache overflow deleted key");
packaging one directory
yuki-kimoto authored on 2009-11-16
507
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
add tests
yuki-kimoto authored on 2010-05-01
508
is(DBIx::Custom->_query_caches->{$tmpls[2]}{columns}, $queries[2]->columns, "$test : columns cache overflow deleted key");
packaging one directory
yuki-kimoto authored on 2009-11-16
509
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
510

            
511
$queries[1] = $dbi->create_query($tmpls[1]);
512
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
513
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
add tests
yuki-kimoto authored on 2010-05-01
514
is_deeply(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns cache overflow deleted key");
packaging one directory
yuki-kimoto authored on 2009-11-16
515
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
add tests
yuki-kimoto authored on 2010-05-01
516
is_deeply(DBIx::Custom->_query_caches->{$tmpls[2]}{columns}, $queries[2]->columns, "$test : columns cache overflow deleted key");
packaging one directory
yuki-kimoto authored on 2009-11-16
517
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
518

            
fix timeformat tests
yuki-kimoto authored on 2009-11-23
519
$query = $dbi->create_query($tmpls[0]);
many change
yuki-kimoto authored on 2010-04-30
520
$query->filter('aaa');
fix timeformat tests
yuki-kimoto authored on 2009-11-23
521
$query = $dbi->create_query($tmpls[0]);
add tests
yuki-kimoto authored on 2010-05-01
522
ok(!$query->filter, "$test : only cached sql and columns");
many change
yuki-kimoto authored on 2010-04-30
523
$query->filter('bbb');
fix timeformat tests
yuki-kimoto authored on 2009-11-23
524
$query = $dbi->create_query($tmpls[0]);
add tests
yuki-kimoto authored on 2010-05-01
525
ok(!$query->filter, "$test : only cached sql and columns");
fix timeformat tests
yuki-kimoto authored on 2009-11-23
526

            
fixed default_fetch_filter
yuki-kimoto authored on 2010-05-01
527
test 'fetch filter';
528
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
some changed
yuki-kimoto authored on 2010-05-02
529
$dbi->register_filter(
fixed default_fetch_filter
yuki-kimoto authored on 2010-05-01
530
    twice       => sub { $_[0] * 2 },
531
    three_times => sub { $_[0] * 3 }
532
);
533
$dbi->default_fetch_filter('twice');
534
$dbi->execute($CREATE_TABLE->{0});
535
$dbi->insert('table1', {key1 => 1, key2 => 2});
536
$result = $dbi->select('table1');
537
$result->filter({key1 => 'three_times'});
538
$row = $result->fetch_hash_single;
539
is_deeply($row, {key1 => 3, key2 => 4}, "$test: default_fetch_filter and filter");
fix timeformat tests
yuki-kimoto authored on 2009-11-23
540