DBIx-Custom / t / dbix-custom-core-sqlite.t /
Newer Older
521 lines | 20.89kb
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

            
40
# Variables for test
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;
51
my @rows;
52
my $rows;
53
my $query;
54
my @queries;
55
my $select_query;
56
my $insert_query;
57
my $update_query;
58
my $ret_val;
59

            
60

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

            
67

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

            
74

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
148

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

            
157

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
276

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

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

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

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

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

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

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
312
$dbi->execute('delete from table1');
many change
yuki-kimoto authored on 2010-04-30
313
$dbi->resist_filter(three_times => sub { $_[0] * 3});
314
$dbi->insert('table1', {key1 => 1, key2 => 2}, {filter => {key1 => 'three_times'}});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
315
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
316
$rows   = $result->fetch_hash_all;
add tests
yuki-kimoto authored on 2010-05-01
317
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : filter");
packaging one directory
yuki-kimoto authored on 2009-11-16
318

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
319
$dbi->execute($DROP_TABLE->{0});
320
$dbi->execute($CREATE_TABLE->{0});
many change
yuki-kimoto authored on 2010-04-30
321
$dbi->insert('table1', {key1 => 1, key2 => 2}, {append => '   '});
322
$rows = $dbi->select('table1')->fetch_hash_all;
323
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
324

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

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

            
330
test 'update';
331
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
332
$dbi->execute($CREATE_TABLE->{1});
packaging one directory
yuki-kimoto authored on 2009-11-16
333
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
334
$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
335
$dbi->update('table1', {key2 => 11}, {where => {key1 => 1}});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
336
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
337
$rows   = $result->fetch_hash_all;
338
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
339
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
340
                  "$test : basic");
341
                  
rename query() to execute()
yuki-kimoto authored on 2010-05-01
342
$dbi->execute("delete from table1");
many change
yuki-kimoto authored on 2010-02-11
343
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
344
$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
345
$dbi->update('table1', {key2 => 12}, {where => {key2 => 2, key3 => 3}});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
346
$result = $dbi->execute($SELECT_TMPLS->{0});
many change
yuki-kimoto authored on 2010-02-11
347
$rows   = $result->fetch_hash_all;
348
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
349
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
350
                  "$test : update key same as search key");
packaging one directory
yuki-kimoto authored on 2009-11-16
351

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

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

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

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

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

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

            
391

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
402
$dbi->execute("delete from table1;");
packaging one directory
yuki-kimoto authored on 2009-11-16
403
$dbi->insert('table1', {key1 => 1, key2 => 2});
404
$dbi->insert('table1', {key1 => 3, key2 => 4});
add tests
yuki-kimoto authored on 2010-05-01
405
$dbi->resist_filter(twice => sub { $_[0] * 2 });
406
$dbi->delete('table1', {where => {key2 => 1}, filter => {key2 => 'twice'}});
rename query() to execute()
yuki-kimoto authored on 2010-05-01
407
$result = $dbi->execute($SELECT_TMPLS->{0});
packaging one directory
yuki-kimoto authored on 2009-11-16
408
$rows   = $result->fetch_hash_all;
add tests
yuki-kimoto authored on 2010-05-01
409
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter");
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
410

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

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

            
420

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

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

            
438

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

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

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

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

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

            
add tests
yuki-kimoto authored on 2010-05-01
460
$dbi->resist_filter(decrement => sub { $_[0] - 1 });
461
$rows = $dbi->select('table1', {where => {key1 => 2}, filter => {key1 => 'decrement'}})
462
            ->fetch_hash_all;
463
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : filter");
packaging one directory
yuki-kimoto authored on 2009-11-16
464

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

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

            
488
$tmpls[1] = "select * from table1";
489
$queries[1] = $dbi->create_query($tmpls[1]);
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(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql second");
add tests
yuki-kimoto authored on 2010-05-01
493
is(DBIx::Custom->_query_caches->{$tmpls[1]}{columns}, $queries[1]->columns, "$test : columns second");
packaging one directory
yuki-kimoto authored on 2009-11-16
494
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key second");
495

            
496
$tmpls[2] = "select key1, key2 from table1";
497
$queries[2] = $dbi->create_query($tmpls[2]);
498
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
499
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
500
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
501
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
502
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
503
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
504

            
505
$queries[1] = $dbi->create_query($tmpls[1]);
506
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
507
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
508
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
509
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
510
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
511
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
512

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

            
521