DBIx-Custom / t / 02-sqlite.t /
Newer Older
716 lines | 29.153kb
add test
yuki-kimoto authored on 2009-10-18
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 }
9
        or plan skip_all => 'DBD::SQLite >= 1.00 required';
10

            
11
    plan 'no_plan';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
12
    use_ok('DBIx::Custom');
add test
yuki-kimoto authored on 2009-10-18
13
}
14

            
cleanup
yuki-kimoto authored on 2009-10-29
15
# Function for test name
16
my $test;
17
sub test {
18
    $test = shift;
19
}
add test
yuki-kimoto authored on 2009-10-18
20

            
add tests
yuki-kimoto authored on 2009-11-02
21
# Constant varialbes for test
22
my $CREATE_TABLE = {
add tests
yuki-kimoto authored on 2009-10-31
23
    0 => 'create table table1 (key1 char(255), key2 char(255));',
add tests
yuki-kimoto authored on 2009-11-05
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));'
add prepare
yuki-kimoto authored on 2009-10-31
26
};
27

            
add tests
yuki-kimoto authored on 2009-11-02
28
my $SELECT_TMPL = {
add tests
yuki-kimoto authored on 2009-10-31
29
    0 => 'select * from table1;'
add tests
yuki-kimoto authored on 2009-10-31
30
};
31

            
add tests
yuki-kimoto authored on 2009-11-02
32
my $DROP_TABLE = {
cleanup
yuki-kimoto authored on 2009-10-31
33
    0 => 'drop table table1'
34
};
35

            
add tests
yuki-kimoto authored on 2009-11-02
36
my $NEW_ARGS = {
37
    0 => {data_source => 'dbi:SQLite:dbname=:memory:'}
38
};
39

            
40
# Variables for test
cleanup
yuki-kimoto authored on 2009-10-29
41
my $dbi;
42
my $sth;
43
my $tmpl;
add cahce tests
yuki-kimoto authored on 2009-11-06
44
my @tmpls;
cleanup#
yuki-kimoto authored on 2009-10-30
45
my $select_tmpl;
46
my $insert_tmpl;
add tests
yuki-kimoto authored on 2009-10-31
47
my $update_tmpl;
cleanup
yuki-kimoto authored on 2009-10-29
48
my $params;
49
my $sql;
50
my $result;
51
my @rows;
52
my $rows;
add tests
yuki-kimoto authored on 2009-10-29
53
my $query;
add cahce tests
yuki-kimoto authored on 2009-11-06
54
my @queries;
cleanup#
yuki-kimoto authored on 2009-10-30
55
my $select_query;
56
my $insert_query;
add tests
yuki-kimoto authored on 2009-10-31
57
my $update_query;
add prepare
yuki-kimoto authored on 2009-10-31
58
my $ret_val;
cleanup#
yuki-kimoto authored on 2009-10-30
59

            
cleanup
yuki-kimoto authored on 2009-10-29
60

            
cleanup
yuki-kimoto authored on 2009-10-31
61
test 'disconnect';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
62
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
63
$dbi->connect;
64
$dbi->disconnect;
65
ok(!$dbi->dbh, $test);
add tests
yuki-kimoto authored on 2009-10-31
66

            
add tests
yuki-kimoto authored on 2009-10-31
67

            
cleanup
yuki-kimoto authored on 2009-10-31
68
test 'connected';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
69
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
cleanup
yuki-kimoto authored on 2009-10-31
70
ok(!$dbi->connected, "$test : not connected");
71
$dbi->connect;
72
ok($dbi->connected, "$test : connected");
73

            
add tests
yuki-kimoto authored on 2009-10-31
74

            
75
test 'preapare';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
76
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
77
$sth = $dbi->prepare($CREATE_TABLE->{0});
78
ok($sth, "$test : auto connect");
79
$sth->execute;
80
$sth = $dbi->prepare($DROP_TABLE->{0});
81
ok($sth, "$test : basic");
82

            
83

            
84
test 'do';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
85
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
86
$ret_val = $dbi->do($CREATE_TABLE->{0});
87
ok(defined $ret_val, "$test : auto connect");
88
$ret_val = $dbi->do($DROP_TABLE->{0});
89
ok(defined $ret_val, "$test : basic");
90

            
91

            
cleanup
yuki-kimoto authored on 2009-10-29
92
# Prepare table
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
93
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
cleanup
yuki-kimoto authored on 2009-10-29
94
$dbi->connect;
add prepare
yuki-kimoto authored on 2009-10-31
95
$dbi->do($CREATE_TABLE->{0});
96
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);");
cleanup
yuki-kimoto authored on 2009-10-29
97
$sth->execute(1, 2);
98
$sth->execute(3, 4);
add test
yuki-kimoto authored on 2009-10-18
99

            
add test module
yuki-kimoto authored on 2009-10-19
100

            
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
101
test 'DBIx::Custom::Result test';
add tests
yuki-kimoto authored on 2009-10-29
102
$tmpl = "select key1, key2 from table1";
103
$query = $dbi->create_query($tmpl);
104
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
105

            
106
@rows = ();
107
while (my $row = $result->fetch) {
108
    push @rows, [@$row];
add test module
yuki-kimoto authored on 2009-10-19
109
}
add tests
yuki-kimoto authored on 2009-10-29
110
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
111

            
add tests
yuki-kimoto authored on 2009-10-29
112
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
113
@rows = ();
114
while (my @row = $result->fetch) {
115
    push @rows, [@row];
add test module
yuki-kimoto authored on 2009-10-19
116
}
add tests
yuki-kimoto authored on 2009-10-29
117
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context");
add test module
yuki-kimoto authored on 2009-10-19
118

            
add tests
yuki-kimoto authored on 2009-10-29
119
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
120
@rows = ();
121
while (my $row = $result->fetch_hash) {
122
    push @rows, {%$row};
add test module
yuki-kimoto authored on 2009-10-19
123
}
add tests
yuki-kimoto authored on 2009-10-29
124
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
125

            
add tests
yuki-kimoto authored on 2009-10-29
126
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
127
@rows = ();
128
while (my %row = $result->fetch_hash) {
129
    push @rows, {%row};
add test module
yuki-kimoto authored on 2009-10-19
130
}
add tests
yuki-kimoto authored on 2009-10-29
131
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context");
add test module
yuki-kimoto authored on 2009-10-19
132

            
add tests
yuki-kimoto authored on 2009-10-29
133
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
134
$rows = $result->fetch_all;
add tests
yuki-kimoto authored on 2009-10-29
135
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
136

            
add tests
yuki-kimoto authored on 2009-10-29
137
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
138
@rows = $result->fetch_all;
add tests
yuki-kimoto authored on 2009-10-29
139
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context");
cleanup
yuki-kimoto authored on 2009-10-29
140

            
add tests
yuki-kimoto authored on 2009-10-29
141
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
142
@rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-29
143
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all_hash scalar context");
cleanup
yuki-kimoto authored on 2009-10-29
144

            
add tests
yuki-kimoto authored on 2009-10-29
145
$result = $dbi->execute($query);
cleanup
yuki-kimoto authored on 2009-10-29
146
@rows = $result->fetch_all;
add tests
yuki-kimoto authored on 2009-10-29
147
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all_hash list context");
cleanup
yuki-kimoto authored on 2009-10-29
148

            
add tests
yuki-kimoto authored on 2009-10-31
149

            
add tests
yuki-kimoto authored on 2009-10-31
150
test 'Insert query return value';
cleanup
yuki-kimoto authored on 2009-10-31
151
$dbi->do($DROP_TABLE->{0});
add tests
yuki-kimoto authored on 2009-10-31
152
$dbi->do($CREATE_TABLE->{0});
153
$tmpl = "insert into table1 {insert key1 key2}";
154
$query = $dbi->create_query($tmpl);
155
$ret_val = $dbi->execute($query, {key1 => 1, key2 => 2});
156
ok($ret_val, $test);
157

            
add tests
yuki-kimoto authored on 2009-10-31
158

            
159
test 'Direct execute';
cleanup
yuki-kimoto authored on 2009-10-31
160
$dbi->do($DROP_TABLE->{0});
add tests
yuki-kimoto authored on 2009-10-31
161
$dbi->do($CREATE_TABLE->{0});
162
$insert_tmpl = "insert into table1 {insert key1 key2}";
163
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}, sub {
164
    my $query = shift;
165
    $query->bind_filter(sub {
166
        my ($key, $value) = @_;
167
        if ($key eq 'key2') {
168
            return $value + 1;
169
        }
170
        return $value;
171
    });
172
});
173
$result = $dbi->execute($SELECT_TMPL->{0});
174
$rows = $result->fetch_all_hash;
175
is_deeply($rows, [{key1 => 1, key2 => 3}], $test);
176

            
177

            
add tests
yuki-kimoto authored on 2009-10-31
178
test 'Filter basic';
cleanup
yuki-kimoto authored on 2009-10-31
179
$dbi->do($DROP_TABLE->{0});
add tests
yuki-kimoto authored on 2009-10-31
180
$dbi->do($CREATE_TABLE->{0});
cleanup#
yuki-kimoto authored on 2009-10-30
181

            
add tests
yuki-kimoto authored on 2009-10-31
182
$insert_tmpl  = "insert into table1 {insert key1 key2};";
add prepare
yuki-kimoto authored on 2009-10-31
183
$insert_query = $dbi->create_query($insert_tmpl);
184
$insert_query->bind_filter(sub {
add tests
yuki-kimoto authored on 2009-10-31
185
    my ($key, $value, $table, $column) = @_;
186
    if ($key eq 'key1' && $table eq '' && $column eq 'key1') {
add prepare
yuki-kimoto authored on 2009-10-31
187
        return $value * 2;
cleanup#
yuki-kimoto authored on 2009-10-30
188
    }
189
    return $value;
190
});
add tests
yuki-kimoto authored on 2009-10-31
191
$dbi->execute($insert_query, {key1 => 1, key2 => 2});
192
$select_query = $dbi->create_query($SELECT_TMPL->{0});
add prepare
yuki-kimoto authored on 2009-10-31
193
$select_query->fetch_filter(sub {
add tests
yuki-kimoto authored on 2009-10-31
194
    my ($key, $value, $type, $sth, $i) = @_;
195
    if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) {
add prepare
yuki-kimoto authored on 2009-10-31
196
        return $value * 3;
197
    }
198
    return $value;
199
});
200
$result = $dbi->execute($select_query);
201
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
202
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter");
cleanup#
yuki-kimoto authored on 2009-10-30
203

            
add tests
yuki-kimoto authored on 2009-10-31
204
$dbi->do("delete from table1;");
205
$insert_query->no_bind_filters('key1');
206
$select_query->no_fetch_filters('key2');
207
$dbi->execute($insert_query, {key1 => 1, key2 => 2});
208
$result = $dbi->execute($select_query);
209
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
210
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : no_fetch_filters no_bind_filters");
add tests
yuki-kimoto authored on 2009-10-31
211

            
cleanup
yuki-kimoto authored on 2009-10-31
212
$dbi->do($DROP_TABLE->{0});
add tests
yuki-kimoto authored on 2009-10-31
213
$dbi->do($CREATE_TABLE->{0});
214
$insert_tmpl  = "insert into table1 {insert table1.key1 table1.key2}";
215
$insert_query = $dbi->create_query($insert_tmpl);
216
$insert_query->bind_filter(sub {
217
    my ($key, $value, $table, $column) = @_;
218
    if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') {
219
        return $value * 3;
220
    }
221
    return $value;
222
});
223
$dbi->execute($insert_query, {table1 => {key1 => 1, key2 => 2}});
224
$select_query = $dbi->create_query($SELECT_TMPL->{0});
225
$result       = $dbi->execute($select_query);
226
$rows = $result->fetch_all_hash;
227
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with table name");
228

            
add tests
yuki-kimoto authored on 2009-10-31
229
test 'Filter in';
230
$insert_tmpl  = "insert into table1 {insert key1 key2};";
231
$insert_query = $dbi->create_query($insert_tmpl);
232
$dbi->execute($insert_query, {key1 => 2, key2 => 4});
233
$select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
234
$select_query = $dbi->create_query($select_tmpl);
235
$select_query->bind_filter(sub {
236
    my ($key, $value, $table, $column) = @_;
237
    if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1' || $key eq 'table1.key2') {
238
        return $value * 2;
239
    }
240
    return $value;
241
});
242
$result = $dbi->execute($select_query, {table1 => {key1 => [1,5], key2 => [2,5]}});
243
$rows = $result->fetch_all_hash;
244
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : bind_filter");
245

            
add tests
yuki-kimoto authored on 2009-10-31
246

            
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
247
test 'DBIx::Custom::SQL::Template basic tag';
cleanup
yuki-kimoto authored on 2009-10-31
248
$dbi->do($DROP_TABLE->{0});
add tests
yuki-kimoto authored on 2009-10-31
249
$dbi->do($CREATE_TABLE->{1});
250
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);");
251
$sth->execute(1, 2, 3, 4, 5);
252
$sth->execute(6, 7, 8, 9, 10);
add tests
yuki-kimoto authored on 2009-10-31
253

            
add tests
yuki-kimoto authored on 2009-10-31
254
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};";
255
$query = $dbi->create_query($tmpl);
256
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
cleanup
yuki-kimoto authored on 2009-10-29
257
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
258
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1");
cleanup
yuki-kimoto authored on 2009-10-29
259

            
add tests
yuki-kimoto authored on 2009-10-31
260
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};";
261
$query = $dbi->create_query($tmpl);
262
$result = $dbi->execute($query, {table1 => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}});
263
$rows = $result->fetch_all_hash;
264
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table");
265

            
266
$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};";
267
$query = $dbi->create_query($tmpl);
268
$result = $dbi->execute($query, {'table1.key1' => 1, 'table1.key2' => 3, 'table1.key3' => 4, 'table1.key4' => 3, 'table1.key5' => 5});
269
$rows = $result->fetch_all_hash;
270
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table dot");
271

            
add tests
yuki-kimoto authored on 2009-10-31
272
$tmpl = "select * from table1 where {<= key1} and {like key2};";
273
$query = $dbi->create_query($tmpl);
274
$result = $dbi->execute($query, {key1 => 1, key2 => '%2%'});
275
$rows = $result->fetch_all_hash;
276
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2");
cleanup
yuki-kimoto authored on 2009-10-29
277

            
add tests
yuki-kimoto authored on 2009-10-31
278
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};";
279
$query = $dbi->create_query($tmpl);
280
$result = $dbi->execute($query, {table1 => {key1 => 1, key2 => '%2%'}});
281
$rows = $result->fetch_all_hash;
282
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table");
283

            
284
$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};";
285
$query = $dbi->create_query($tmpl);
286
$result = $dbi->execute($query, {'table1.key1' => 1, 'table1.key2' => '%2%'});
287
$rows = $result->fetch_all_hash;
288
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table dot");
289

            
290

            
add tests
yuki-kimoto authored on 2009-10-31
291
test 'DIB::Custom::SQL::Template in tag';
cleanup
yuki-kimoto authored on 2009-10-31
292
$dbi->do($DROP_TABLE->{0});
add tests
yuki-kimoto authored on 2009-10-31
293
$dbi->do($CREATE_TABLE->{1});
294
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);");
295
$sth->execute(1, 2, 3, 4, 5);
296
$sth->execute(6, 7, 8, 9, 10);
297

            
298
$tmpl = "select * from table1 where {in key1 2};";
299
$query = $dbi->create_query($tmpl);
300
$result = $dbi->execute($query, {key1 => [9, 1]});
301
$rows = $result->fetch_all_hash;
302
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
303

            
304
$tmpl = "select * from table1 where {in table1.key1 2};";
305
$query = $dbi->create_query($tmpl);
306
$result = $dbi->execute($query, {table1 => {key1 => [9, 1]}});
307
$rows = $result->fetch_all_hash;
308
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table");
add tests
yuki-kimoto authored on 2009-10-31
309

            
add tests
yuki-kimoto authored on 2009-10-31
310
$tmpl = "select * from table1 where {in table1.key1 2};";
311
$query = $dbi->create_query($tmpl);
312
$result = $dbi->execute($query, {'table1.key1' => [9, 1]});
313
$rows = $result->fetch_all_hash;
314
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table dot");
add tests
yuki-kimoto authored on 2009-10-31
315

            
cleanup
yuki-kimoto authored on 2009-10-29
316

            
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
317
test 'DBIx::Custom::SQL::Template insert tag';
add tests
yuki-kimoto authored on 2009-10-31
318
$dbi->do("delete from table1");
319
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}';
320
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
cleanup
yuki-kimoto authored on 2009-10-29
321

            
add tests
yuki-kimoto authored on 2009-10-31
322
$result = $dbi->execute($SELECT_TMPL->{0});
add tests
yuki-kimoto authored on 2009-10-31
323
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
324
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
cleanup
yuki-kimoto authored on 2009-10-29
325

            
add tests
yuki-kimoto authored on 2009-10-31
326
$dbi->do("delete from table1");
327
$dbi->execute($insert_tmpl, {'#insert' => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
328
$result = $dbi->execute($SELECT_TMPL->{0});
329
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
330
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert");
add tests
yuki-kimoto authored on 2009-10-31
331

            
332
$dbi->do("delete from table1");
333
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
334
$dbi->execute($insert_tmpl, {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
335
$result = $dbi->execute($SELECT_TMPL->{0});
336
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
337
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name");
338

            
add tests
yuki-kimoto authored on 2009-10-31
339
$dbi->do("delete from table1");
340
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}';
341
$dbi->execute($insert_tmpl, {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5});
342
$result = $dbi->execute($SELECT_TMPL->{0});
343
$rows = $result->fetch_all_hash;
344
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name dot");
add tests
yuki-kimoto authored on 2009-10-31
345

            
346
$dbi->do("delete from table1");
347
$dbi->execute($insert_tmpl, {'#insert' => {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}});
348
$result = $dbi->execute($SELECT_TMPL->{0});
349
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
350
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert with table name");
add tests
yuki-kimoto authored on 2009-10-31
351

            
add tests
yuki-kimoto authored on 2009-10-31
352
$dbi->do("delete from table1");
353
$dbi->execute($insert_tmpl, {'#insert' => {'table1.key1' => 1, 'table1.key2' => 2, 'table1.key3' => 3, 'table1.key4' => 4, 'table1.key5' => 5}});
354
$result = $dbi->execute($SELECT_TMPL->{0});
355
$rows = $result->fetch_all_hash;
356
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert with table name dot");
357

            
add tests
yuki-kimoto authored on 2009-10-31
358

            
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
359
test 'DBIx::Custom::SQL::Template update tag';
add tests
yuki-kimoto authored on 2009-10-31
360
$dbi->do("delete from table1");
361
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}";
362
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
363
$dbi->execute($insert_tmpl, {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
364

            
365
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}';
366
$dbi->execute($update_tmpl, {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5});
367

            
368
$result = $dbi->execute($SELECT_TMPL->{0});
369
$rows = $result->fetch_all_hash;
370
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
add tests
yuki-kimoto authored on 2009-10-31
371
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic");
add tests
yuki-kimoto authored on 2009-10-31
372

            
add tests
yuki-kimoto authored on 2009-10-31
373
$dbi->execute($update_tmpl, {'#update' => {key1 => 2, key2 => 2, key3 => 2, key4 => 2}, key5 => 5});
add tests
yuki-kimoto authored on 2009-10-31
374
$result = $dbi->execute($SELECT_TMPL->{0});
375
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
376
is_deeply($rows, [{key1 => 2, key2 => 2, key3 => 2, key4 => 2, key5 => 5},
377
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : #update");
add tests
yuki-kimoto authored on 2009-10-31
378

            
add tests
yuki-kimoto authored on 2009-10-31
379
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}';
380
$dbi->execute($update_tmpl, {table1 => {key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}});
add tests
yuki-kimoto authored on 2009-10-31
381
$result = $dbi->execute($SELECT_TMPL->{0});
382
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
383
is_deeply($rows, [{key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5},
384
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name");
add tests
yuki-kimoto authored on 2009-10-31
385

            
add tests
yuki-kimoto authored on 2009-10-31
386
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}';
387
$dbi->execute($update_tmpl, {'table1.key1' => 4, 'table1.key2' => 4, 'table1.key3' => 4, 'table1.key4' => 4, 'table1.key5' => 5});
add tests
yuki-kimoto authored on 2009-10-31
388
$result = $dbi->execute($SELECT_TMPL->{0});
389
$rows = $result->fetch_all_hash;
add tests
yuki-kimoto authored on 2009-10-31
390
is_deeply($rows, [{key1 => 4, key2 => 4, key3 => 4, key4 => 4, key5 => 5},
add tests
yuki-kimoto authored on 2009-10-31
391
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name dot");
cleanup
yuki-kimoto authored on 2009-10-29
392

            
add tests
yuki-kimoto authored on 2009-10-31
393
$dbi->execute($update_tmpl, {'#update' => {table1 => {key1 => 5, key2 => 5, key3 => 5, key4 => 5}}, table1 => {key5 => 5}});
394
$result = $dbi->execute($SELECT_TMPL->{0});
395
$rows = $result->fetch_all_hash;
396
is_deeply($rows, [{key1 => 5, key2 => 5, key3 => 5, key4 => 5, key5 => 5},
add tests
yuki-kimoto authored on 2009-10-31
397
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : update tag #update with table name");
cleanup
yuki-kimoto authored on 2009-10-29
398

            
add tests
yuki-kimoto authored on 2009-10-31
399
$dbi->execute($update_tmpl, {'#update' => {'table1.key1' => 6, 'table1.key2' => 6, 'table1.key3' => 6, 'table1.key4' => 6}, 'table1.key5' => 5});
400
$result = $dbi->execute($SELECT_TMPL->{0});
401
$rows = $result->fetch_all_hash;
402
is_deeply($rows, [{key1 => 6, key2 => 6, key3 => 6, key4 => 6, key5 => 5},
add tests
yuki-kimoto authored on 2009-10-31
403
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : update tag #update with table name dot");
add tests
yuki-kimoto authored on 2009-10-31
404

            
cleanup
yuki-kimoto authored on 2009-10-31
405

            
406
test 'run_tansaction';
407
$dbi->do($DROP_TABLE->{0});
408
$dbi->do($CREATE_TABLE->{0});
409
$dbi->run_tranzaction(sub {
410
    $insert_tmpl = 'insert into table1 {insert key1 key2}';
411
    $dbi->execute($insert_tmpl, {key1 => 1, key2 => 2});
412
    $dbi->execute($insert_tmpl, {key1 => 3, key2 => 4});
413
});
414
$result = $dbi->execute($SELECT_TMPL->{0});
415
$rows   = $result->fetch_all_hash;
416
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : commit");
417

            
418
$dbi->do($DROP_TABLE->{0});
419
$dbi->do($CREATE_TABLE->{0});
420
$dbi->dbh->{RaiseError} = 0;
421
eval{
422
    $dbi->run_tranzaction(sub {
423
        $insert_tmpl = 'insert into table1 {insert key1 key2}';
424
        $dbi->execute($insert_tmpl, {key1 => 1, key2 => 2});
425
        die "Fatal Error";
426
        $dbi->execute($insert_tmpl, {key1 => 3, key2 => 4});
427
    })
428
};
429
like($@, qr/Fatal Error.*Rollback is success/ms, "$test : Rollback success message");
430
ok(!$dbi->dbh->{RaiseError}, "$test : restore RaiseError value");
431
$result = $dbi->execute($SELECT_TMPL->{0});
432
$rows   = $result->fetch_all_hash;
433
is_deeply($rows, [], "$test : rollback");
434

            
435

            
add tests
yuki-kimoto authored on 2009-10-31
436
test 'Error case';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
437
$dbi = DBIx::Custom->new;
add tests
yuki-kimoto authored on 2009-10-31
438
eval{$dbi->run_tranzaction};
439
like($@, qr/Not yet connect to database/, "$test : Yet Connected");
440

            
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
441
$dbi = DBIx::Custom->new(data_source => 'dbi:SQLit');
add tests
yuki-kimoto authored on 2009-10-31
442
eval{$dbi->connect;};
443
ok($@, "$test : connect error");
444

            
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
445
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
446
$dbi->connect;
447
$dbi->dbh->{AutoCommit} = 0;
448
eval{$dbi->run_tranzaction()};
449
like($@, qr/AutoCommit must be true before tranzaction start/,
450
         "$test : run_tranzaction auto commit is false");
451

            
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
452
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
453
$sql = 'laksjdf';
454
eval{$dbi->prepare($sql)};
455
like($@, qr/$sql/, "$test : prepare fail");
456

            
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
457
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
458
$sql = 'laksjdf';
459
eval{$dbi->do($sql, qw/1 2 3/)};
460
like($@, qr/$sql/, "$test : do fail");
461

            
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
462
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
463
eval{$dbi->create_query("{p }")};
464
ok($@, "$test : create_query invalid SQL template");
465

            
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
466
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-10-31
467
$dbi->do($CREATE_TABLE->{0});
468
$query = $dbi->create_query("select * from table1 where {= key1}");
469
eval{$dbi->execute($query, {key2 => 1})};
470
like($@, qr/Corresponding key is not found in your parameters/, 
471
        "$test : execute corresponding key not found");
472

            
add tests
yuki-kimoto authored on 2009-11-02
473

            
474
test 'insert';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
475
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-11-02
476
$dbi->do($CREATE_TABLE->{0});
477
$dbi->insert('table1', {key1 => 1, key2 => 2});
478
$dbi->insert('table1', {key1 => 3, key2 => 4});
479
$result = $dbi->execute($SELECT_TMPL->{0});
480
$rows   = $result->fetch_all_hash;
481
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic");
482

            
483
$dbi->do('delete from table1');
484
$dbi->insert('table1', {key1 => 1, key2 => 2}, sub {
485
    my $query = shift;
486
    $query->bind_filter(sub {
487
        my ($key, $value) = @_;
488
        if ($key eq 'key1') {
489
            return $value * 3;
490
        }
491
        return $value;
492
    });
493
});
494
$result = $dbi->execute($SELECT_TMPL->{0});
495
$rows   = $result->fetch_all_hash;
496
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : edit_query_callback");
497

            
add tests
yuki-kimoto authored on 2009-11-03
498

            
499
test 'insert error';
add tests
yuki-kimoto authored on 2009-11-02
500
eval{$dbi->insert('table1')};
add tests
yuki-kimoto authored on 2009-11-03
501
like($@, qr/Key-value pairs for insert must be specified to 'insert' second argument/, "$test : insert key-value not specifed");
add tests
yuki-kimoto authored on 2009-11-02
502

            
503
eval{$dbi->insert('table1', {key1 => 1, key2 => 2}, 'aaa')};
504
like($@, qr/Query edit callback must be code reference/, "$test : query edit callback not code ref");
add tests
yuki-kimoto authored on 2009-11-03
505

            
506

            
507
test 'update';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
508
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-11-03
509
$dbi->do($CREATE_TABLE->{1});
510
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
511
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
512
$dbi->update('table1', {key2 => 11}, {key1 => 1});
513
$result = $dbi->execute($SELECT_TMPL->{0});
514
$rows   = $result->fetch_all_hash;
515
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
516
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
517
                  "$test : basic");
518
                  
519
$dbi->do("delete from table1");
520
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
521
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
add tests
yuki-kimoto authored on 2009-11-05
522
$dbi->update('table1', {key2 => 12}, {key2 => 2, key3 => 3});
add tests
yuki-kimoto authored on 2009-11-03
523
$result = $dbi->execute($SELECT_TMPL->{0});
524
$rows   = $result->fetch_all_hash;
525
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
526
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
527
                  "$test : update key same as search key");
528

            
529
$dbi->do("delete from table1");
530
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
531
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
532
$dbi->update('table1', {key2 => 11}, {key1 => 1}, sub {
533
    my $query = shift;
534
    $query->bind_filter(sub {
535
        my ($key, $value) = @_;
536
        if ($key eq 'key2') {
537
            return $value * 2;
538
        }
539
        return $value;
540
    });
541
});
542
$result = $dbi->execute($SELECT_TMPL->{0});
543
$rows   = $result->fetch_all_hash;
544
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
545
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
546
                  "$test : query edit callback");
547

            
548

            
549
test 'update error';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
550
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-11-03
551
$dbi->do($CREATE_TABLE->{1});
552
eval{$dbi->update('table1')};
553
like($@, qr/Key-value pairs for update must be specified to 'update' second argument/,
554
         "$test : update key-value pairs not specified");
555

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

            
560
eval{$dbi->update('table1', {key2 => 1}, {key2 => 3}, 'aaa')};
561
like($@, qr/Query edit callback must be code reference/, 
562
         "$test : query edit callback not code reference");
563

            
564

            
565
test 'update_all';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
566
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-11-03
567
$dbi->do($CREATE_TABLE->{1});
568
$dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
569
$dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
570
$dbi->update_all('table1', {key2 => 10}, sub {
571
    my $query = shift;
572
    $query->bind_filter(sub {
573
        my ($key, $value) = @_;
574
        return $value * 2;
575
    })
576
});
577
$result = $dbi->execute($SELECT_TMPL->{0});
578
$rows   = $result->fetch_all_hash;
579
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
580
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
581
                  "$test : query edit callback");
582

            
583

            
584
test 'delete';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
585
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-11-03
586
$dbi->do($CREATE_TABLE->{0});
587
$dbi->insert('table1', {key1 => 1, key2 => 2});
588
$dbi->insert('table1', {key1 => 3, key2 => 4});
589
$dbi->delete('table1', {key1 => 1});
590
$result = $dbi->execute($SELECT_TMPL->{0});
591
$rows   = $result->fetch_all_hash;
592
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic");
593

            
594
$dbi->do("delete from table1;");
595
$dbi->insert('table1', {key1 => 1, key2 => 2});
596
$dbi->insert('table1', {key1 => 3, key2 => 4});
597
$dbi->delete('table1', {key2 => 1}, sub {
598
    my $query = shift;
599
    $query->bind_filter(sub {
600
        my ($key, $value) = @_;
601
        return $value * 2;
602
    });
603
});
604
$result = $dbi->execute($SELECT_TMPL->{0});
605
$rows   = $result->fetch_all_hash;
606
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : query edit callback");
607

            
add tests
yuki-kimoto authored on 2009-11-05
608
$dbi->delete_all('table1');
609
$dbi->insert('table1', {key1 => 1, key2 => 2});
610
$dbi->insert('table1', {key1 => 3, key2 => 4});
611
$dbi->delete('table1', {key1 => 1, key2 => 2});
612
$rows = $dbi->select('table1')->fetch_all_hash;
613
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key");
614

            
615

            
add tests
yuki-kimoto authored on 2009-11-03
616
test 'delete error';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
617
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-11-03
618
$dbi->do($CREATE_TABLE->{0});
619
eval{$dbi->delete('table1')};
620
like($@, qr/Key-value pairs for where clause must be specified to 'delete' second argument/,
621
         "$test : where key-value pairs not specified");
622

            
623
eval{$dbi->delete('table1', {key1 => 1}, 'aaa')};
624
like($@, qr/Query edit callback must be code reference/, 
625
         "$test : query edit callback not code ref");
626

            
627

            
628
test 'delete_all';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
629
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add tests
yuki-kimoto authored on 2009-11-03
630
$dbi->do($CREATE_TABLE->{0});
631
$dbi->insert('table1', {key1 => 1, key2 => 2});
632
$dbi->insert('table1', {key1 => 3, key2 => 4});
633
$dbi->delete_all('table1');
634
$result = $dbi->execute($SELECT_TMPL->{0});
635
$rows   = $result->fetch_all_hash;
636
is_deeply($rows, [], "$test : basic");
637

            
638

            
add select
yuki-kimoto authored on 2009-11-05
639
test 'select';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
640
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
add select
yuki-kimoto authored on 2009-11-05
641
$dbi->do($CREATE_TABLE->{0});
642
$dbi->insert('table1', {key1 => 1, key2 => 2});
643
$dbi->insert('table1', {key1 => 3, key2 => 4});
add tests
yuki-kimoto authored on 2009-11-05
644
$rows = $dbi->select('table1')->fetch_all_hash;
add select
yuki-kimoto authored on 2009-11-05
645
is_deeply($rows, [{key1 => 1, key2 => 2},
646
                  {key1 => 3, key2 => 4}], "$test : table");
647

            
add tests
yuki-kimoto authored on 2009-11-05
648
$rows = $dbi->select('table1', ['key1'])->fetch_all_hash;
649
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key");
650

            
651
$rows = $dbi->select('table1', {key1 => 1})->fetch_all_hash;
652
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key");
653

            
654
$rows = $dbi->select('table1', ['key1'], {key1 => 3})->fetch_all_hash;
655
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key");
add tests
yuki-kimoto authored on 2009-11-05
656

            
657
$rows = $dbi->select('table1', "order by key1 desc limit 1")->fetch_all_hash;
658
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement");
659

            
660
$rows = $dbi->select('table1', {key1 => 2}, sub {
661
    my $query = shift;
662
    $query->bind_filter(sub {
663
        my ($key, $value) = @_;
664
        if ($key eq 'key1') {
665
            return $value - 1;
666
        }
667
        return $value;
668
    });
669
})->fetch_all_hash;
670
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : query edit call back");
671

            
672
$dbi->do($CREATE_TABLE->{2});
673
$dbi->insert('table2', {key1 => 1, key3 => 5});
674
$rows = $dbi->select([qw/table1 table2/],
675
                     ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
676
                     {'table1.key2' => 2},
677
                     "where table1.key1 = table2.key1")->fetch_all_hash;
add cahce tests
yuki-kimoto authored on 2009-11-06
678
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join");
679

            
680
test 'Cache';
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
681
$dbi = DBIx::Custom->new($NEW_ARGS->{0});
682
DBIx::Custom->query_cache_max(2);
add cahce tests
yuki-kimoto authored on 2009-11-06
683
$dbi->do($CREATE_TABLE->{0});
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
684
DBIx::Custom->delete_class_attr('_query_caches');
685
DBIx::Custom->delete_class_attr('_query_cache_keys');
add cahce tests
yuki-kimoto authored on 2009-11-06
686
$tmpls[0] = "insert into table1 {insert key1 key2}";
687
$queries[0] = $dbi->create_query($tmpls[0]);
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
688
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
689
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first");
690
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key first");
add cahce tests
yuki-kimoto authored on 2009-11-06
691

            
692
$tmpls[1] = "select * from table1";
693
$queries[1] = $dbi->create_query($tmpls[1]);
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
694
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first");
695
is(DBIx::Custom->_query_caches->{$tmpls[0]}{key_infos}, $queries[0]->key_infos, "$test : key_infos first");
696
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql second");
697
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos second");
698
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls], "$test : cache key second");
add cahce tests
yuki-kimoto authored on 2009-11-06
699

            
700
$tmpls[2] = "select key1, key2 from table1";
701
$queries[2] = $dbi->create_query($tmpls[2]);
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
702
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
703
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
704
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos cache overflow deleted key");
705
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
706
is(DBIx::Custom->_query_caches->{$tmpls[2]}{key_infos}, $queries[2]->key_infos, "$test : key_infos cache overflow deleted key");
707
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
add cahce tests
yuki-kimoto authored on 2009-11-06
708

            
709
$queries[1] = $dbi->create_query($tmpls[1]);
Version 0.0101 release
yuki-kimoto authored on 2009-11-08
710
ok(!exists DBIx::Custom->_query_caches->{$tmpls[0]}, "$test : cache overflow deleted key");
711
is(DBIx::Custom->_query_caches->{$tmpls[1]}{sql}, $queries[1]->sql, "$test : sql cache overflow deleted key");
712
is(DBIx::Custom->_query_caches->{$tmpls[1]}{key_infos}, $queries[1]->key_infos, "$test : key_infos cache overflow deleted key");
713
is(DBIx::Custom->_query_caches->{$tmpls[2]}{sql}, $queries[2]->sql, "$test : sql cache overflow deleted key");
714
is(DBIx::Custom->_query_caches->{$tmpls[2]}{key_infos}, $queries[2]->key_infos, "$test : key_infos cache overflow deleted key");
715
is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
add cahce tests
yuki-kimoto authored on 2009-11-06
716