DBIx-Custom / t / common.t /
Newer Older
743 lines | 29.06kb
added common test executing ...
Yuki Kimoto authored on 2011-08-07
1
use Test::More;
2
use strict;
3
use warnings;
test cleanup
Yuki Kimoto authored on 2011-08-10
4
use DBIx::Custom;
5
use Encode qw/encode_utf8/;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
6

            
7
my $dbi;
8

            
9
plan skip_all => $ENV{DBIX_CUSTOM_SKIP_MESSAGE} || 'common.t is always skipped'
10
  unless $ENV{DBIX_CUSTOM_TEST_RUN}
11
    && eval { $dbi = DBIx::Custom->connect; 1 };
12

            
13
plan 'no_plan';
14

            
cleanup test
Yuki Kimoto authored on 2011-08-08
15
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
16
sub test { print "# $_[0]\n" }
17

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
18
# Constant
19
my $create_table1 = $dbi->create_table1;
cleanup test
Yuki Kimoto authored on 2011-08-08
20
my $create_table1_2 = $dbi->create_table1_2;
test cleanup
Yuki Kimoto authored on 2011-08-10
21
my $create_table2 = $dbi->create_table2;
22
my $create_table_reserved = $dbi->create_table_reserved;
cleanup test
Yuki Kimoto authored on 2011-08-10
23
my $q = substr($dbi->quote, 0, 1);
24
my $p = substr($dbi->quote, 1, 1) || $q;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
25

            
26
# Variable
cleanup test
Yuki Kimoto authored on 2011-08-08
27
# Variables
28
my $builder;
29
my $datas;
30
my $sth;
31
my $source;
32
my @sources;
33
my $select_source;
34
my $insert_source;
35
my $update_source;
36
my $param;
37
my $params;
38
my $sql;
39
my $result;
40
my $row;
41
my @rows;
42
my $rows;
43
my $query;
44
my @queries;
45
my $select_query;
46
my $insert_query;
47
my $update_query;
48
my $ret_val;
49
my $infos;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
50
my $model;
cleanup test
Yuki Kimoto authored on 2011-08-08
51
my $model2;
52
my $where;
53
my $update_param;
54
my $insert_param;
55
my $join;
cleanup test
Yuki Kimoto authored on 2011-08-10
56
my $binary;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
57

            
58
# Drop table
59
eval { $dbi->execute('drop table table1') };
60

            
61
# Create table
62
$dbi->execute($create_table1);
63
$model = $dbi->create_model(table => 'table1');
64
$model->insert({key1 => 1, key2 => 2});
65
is_deeply($model->select->all, [{key1 => 1, key2 => 2}]);
66

            
cleanup test
Yuki Kimoto authored on 2011-08-08
67
test 'DBIx::Custom::Result test';
68
$dbi->delete_all(table => 'table1');
69
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
70
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
71
$source = "select key1, key2 from table1";
72
$query = $dbi->create_query($source);
73
$result = $dbi->execute($query);
74

            
75
@rows = ();
76
while (my $row = $result->fetch) {
77
    push @rows, [@$row];
78
}
79
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch");
80

            
81
$result = $dbi->execute($query);
82
@rows = ();
83
while (my $row = $result->fetch_hash) {
84
    push @rows, {%$row};
85
}
86
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash");
87

            
88
$result = $dbi->execute($query);
89
$rows = $result->fetch_all;
90
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
91

            
92
$result = $dbi->execute($query);
93
$rows = $result->fetch_hash_all;
94
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all");
95

            
96
test 'Insert query return value';
97
$source = "insert into table1 {insert_param key1 key2}";
98
$query = $dbi->execute($source, {}, query => 1);
99
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2});
100
ok($ret_val);
101

            
102
test 'Direct query';
103
$dbi->delete_all(table => 'table1');
104
$insert_source = "insert into table1 {insert_param key1 key2}";
105
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2});
106
$result = $dbi->execute('select * from table1;');
107
$rows = $result->all;
108
is_deeply($rows, [{key1 => 1, key2 => 2}]);
109

            
110
test 'Filter basic';
111
$dbi->delete_all(table => 'table1');
112
$dbi->register_filter(twice       => sub { $_[0] * 2}, 
113
                    three_times => sub { $_[0] * 3});
114

            
115
$insert_source  = "insert into table1 {insert_param key1 key2};";
116
$insert_query = $dbi->execute($insert_source, {}, query => 1);
117
$insert_query->filter({key1 => 'twice'});
118
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2});
119
$result = $dbi->execute('select * from table1;');
120
$rows = $result->filter({key2 => 'three_times'})->all;
121
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter");
122

            
123
test 'Filter in';
124
$dbi->delete_all(table => 'table1');
125
$insert_source  = "insert into table1 {insert_param key1 key2};";
126
$insert_query = $dbi->execute($insert_source, {}, query => 1);
127
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4});
128
$select_source = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
129
$select_query = $dbi->execute($select_source,{}, query => 1);
130
$select_query->filter({'table1.key1' => 'twice'});
131
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
132
$rows = $result->all;
133
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter");
134

            
cleanup test
Yuki Kimoto authored on 2011-08-08
135
test 'DBIx::Custom::SQLTemplate basic tag';
136
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-08
137
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-08
138
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
139
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
140

            
141
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
142
$query = $dbi->execute($source, {}, query => 1);
143
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
144
$rows = $result->all;
145
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
146

            
147
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};";
148
$query = $dbi->execute($source, {}, query => 1);
149
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
150
$rows = $result->all;
151
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1");
152

            
153
$source = "select * from table1 where {<= key1} and {like key2};";
154
$query = $dbi->execute($source, {}, query => 1);
155
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'});
156
$rows = $result->all;
157
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2");
158

            
159
test 'DIB::Custom::SQLTemplate in tag';
160
$dbi->execute('drop table table1');
cleanup test
Yuki Kimoto authored on 2011-08-08
161
$dbi->execute($create_table1_2);
cleanup test
Yuki Kimoto authored on 2011-08-08
162
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
163
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
164

            
165
$source = "select * from table1 where {in key1 2};";
166
$query = $dbi->execute($source, {}, query => 1);
167
$result = $dbi->execute($query, param => {key1 => [9, 1]});
168
$rows = $result->all;
169
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
170

            
171
test 'DBIx::Custom::SQLTemplate insert tag';
172
$dbi->delete_all(table => 'table1');
173
$insert_source = 'insert into table1 {insert_param key1 key2 key3 key4 key5}';
174
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
175

            
176
$result = $dbi->execute('select * from table1;');
177
$rows = $result->all;
178
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic");
179

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

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

            
189
$result = $dbi->execute('select * from table1 order by key1;');
190
$rows = $result->all;
191
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
192
                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic");
193

            
cleanup test
Yuki Kimoto authored on 2011-08-08
194
test 'Named placeholder';
195
$dbi->execute('drop table table1');
196
$dbi->execute($create_table1_2);
197
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
198
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
199

            
200
$source = "select * from table1 where key1 = :key1 and key2 = :key2";
201
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
202
$rows = $result->all;
203
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
204

            
205
$source = "select * from table1 where key1 = \n:key1\n and key2 = :key2";
206
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2});
207
$rows = $result->all;
208
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
209

            
210
$source = "select * from table1 where key1 = :key1 or key1 = :key1";
211
$result = $dbi->execute($source, param => {key1 => [1, 2]});
212
$rows = $result->all;
213
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
214

            
215
$source = "select * from table1 where key1 = :table1.key1 and key2 = :table1.key2";
216
$result = $dbi->execute(
217
    $source,
218
    param => {'table1.key1' => 1, 'table1.key2' => 1},
219
    filter => {'table1.key2' => sub { $_[0] * 2 }}
220
);
221
$rows = $result->all;
222
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]);
223

            
224
$dbi->execute('drop table table1');
225
$dbi->execute($create_table1);
226
$dbi->insert(table => 'table1', param => {key1 => '2011-10-14 12:19:18', key2 => 2});
227
$source = "select * from table1 where key1 = '2011-10-14 12:19:18' and key2 = :key2";
228
$result = $dbi->execute(
229
    $source,
230
    param => {'key2' => 2},
231
);
232

            
233
$rows = $result->all;
234
is_deeply($rows, [{key1 => '2011-10-14 12:19:18', key2 => 2}]);
235

            
236
$dbi->delete_all(table => 'table1');
237
$dbi->insert(table => 'table1', param => {key1 => 'a:b c:d', key2 => 2});
238
$source = "select * from table1 where key1 = 'a\\:b c\\:d' and key2 = :key2";
239
$result = $dbi->execute(
240
    $source,
241
    param => {'key2' => 2},
242
);
243
$rows = $result->all;
244
is_deeply($rows, [{key1 => 'a:b c:d', key2 => 2}]);
245

            
test cleanup
Yuki Kimoto authored on 2011-08-10
246
test 'Error case';
247
eval {DBIx::Custom->connect(dsn => 'dbi:SQLit')};
248
ok($@, "connect error");
249

            
250
eval{$dbi->execute("{p }", {}, query => 1)};
251
ok($@, "create_query invalid SQL template");
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
252

            
cleanup test
Yuki Kimoto authored on 2011-08-10
253
test 'insert';
cleanup test
Yuki Kimoto authored on 2011-08-10
254
eval { $dbi->execute('drop table table1') };
255
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
256
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
257
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
258
$result = $dbi->execute('select * from table1;');
259
$rows   = $result->all;
260
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
261

            
262
$dbi->execute('delete from table1');
263
$dbi->register_filter(
264
    twice       => sub { $_[0] * 2 },
265
    three_times => sub { $_[0] * 3 }
266
);
267
$dbi->default_bind_filter('twice');
268
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'});
269
$result = $dbi->execute('select * from table1;');
270
$rows   = $result->all;
271
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
272
$dbi->default_bind_filter(undef);
273

            
cleanup test
Yuki Kimoto authored on 2011-08-10
274
$dbi->execute('drop table table1');
275
$dbi->execute($create_table1);
cleanup test
Yuki Kimoto authored on 2011-08-10
276
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
277
$rows = $dbi->select(table => 'table1')->all;
278
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
279

            
280
eval{$dbi->insert(table => 'table1', noexist => 1)};
281
like($@, qr/noexist/, "invalid");
282

            
283
eval{$dbi->insert(table => 'table', param => {';' => 1})};
284
like($@, qr/safety/);
285

            
cleanup test
Yuki Kimoto authored on 2011-08-10
286
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
287
$dbi->execute($create_table_reserved);
cleanup test
Yuki Kimoto authored on 2011-08-10
288
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
289
$dbi->insert(table => 'table', param => {select => 1});
cleanup test
Yuki Kimoto authored on 2011-08-10
290
$result = $dbi->execute("select * from ${q}table$p");
cleanup test
Yuki Kimoto authored on 2011-08-10
291
$rows   = $result->all;
test cleanup
Yuki Kimoto authored on 2011-08-10
292
is_deeply($rows, [{select => 2, update => undef}], "reserved word");
cleanup test
Yuki Kimoto authored on 2011-08-10
293

            
cleanup test
Yuki Kimoto authored on 2011-08-10
294
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
295
$dbi->execute($create_table1);
296
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
297
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
298
$result = $dbi->execute('select * from table1;');
299
$rows   = $result->all;
300
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
301

            
cleanup test
Yuki Kimoto authored on 2011-08-10
302
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
303
$dbi->execute($create_table1);
304
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
305
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
306
$result = $dbi->execute('select * from table1;');
307
$rows   = $result->all;
308
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
309

            
test cleanup
Yuki Kimoto authored on 2011-08-10
310
test 'update';
311
eval { $dbi->execute('drop table table1') };
312
$dbi->execute($create_table1_2);
313
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
314
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
315
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
316
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
317
$rows   = $result->all;
318
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
319
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
320
                  "basic");
321
                  
322
$dbi->execute("delete from table1");
323
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
324
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
325
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
326
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
327
$rows   = $result->all;
328
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
329
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
330
                  "update key same as search key");
331

            
332
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3});
test cleanup
Yuki Kimoto authored on 2011-08-10
333
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
334
$rows   = $result->all;
335
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5},
336
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
337
                  "update key same as search key : param is array ref");
338

            
339
$dbi->execute("delete from table1");
340
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
341
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
342
$dbi->register_filter(twice => sub { $_[0] * 2 });
343
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1},
344
              filter => {key2 => sub { $_[0] * 2 }});
test cleanup
Yuki Kimoto authored on 2011-08-10
345
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
346
$rows   = $result->all;
347
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
348
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
349
                  "filter");
350

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

            
353
eval{$dbi->update(table => 'table1', where => {key1 => 1}, noexist => 1)};
354
like($@, qr/noexist/, "invalid");
355

            
356
eval{$dbi->update(table => 'table1')};
357
like($@, qr/where/, "not contain where");
358

            
359
eval { $dbi->execute('drop table table1') };
360
$dbi->execute($create_table1);
361
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
362
$where = $dbi->where;
363
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
364
$where->param({key1 => 1, key2 => 2});
365
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
366
$result = $dbi->select(table => 'table1');
367
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
368

            
369
eval { $dbi->execute('drop table table1') };
370
$dbi->execute($create_table1);
371
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
372
$dbi->update(
373
    table => 'table1',
374
    param => {key1 => 3},
375
    where => [
376
        ['and', 'key1 = :key1', 'key2 = :key2'],
377
        {key1 => 1, key2 => 2}
378
    ]
379
);
380
$result = $dbi->select(table => 'table1');
381
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
382

            
383
eval { $dbi->execute('drop table table1') };
384
$dbi->execute($create_table1);
385
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
386
$where = $dbi->where;
387
$where->clause(['and', 'key2 = :key2']);
388
$where->param({key2 => 2});
389
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
390
$result = $dbi->select(table => 'table1');
391
is_deeply($result->all, [{key1 => 3, key2 => 2}], 'update() where');
392

            
393
eval{$dbi->update(table => 'table1', param => {';' => 1})};
394
like($@, qr/safety/);
395

            
396
eval{$dbi->update(table => 'table1', param => {'key1' => 1}, where => {';' => 1})};
397
like($@, qr/safety/);
398

            
399
eval { $dbi->execute('drop table table1') };
400
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
401
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
402
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
403
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
404
$dbi->insert(table => 'table', param => {select => 1});
405
$dbi->update(table => 'table', where => {select => 1}, param => {update => 2});
406
$result = $dbi->execute("select * from ${q}table$p");
407
$rows   = $result->all;
408
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
409

            
410
eval {$dbi->update_all(table => 'table', param => {';' => 2}) };
411
like($@, qr/safety/);
412

            
413
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
414
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
415
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
416
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
417
$dbi->insert(table => 'table', param => {select => 1});
418
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
419
$result = $dbi->execute("select * from ${q}table$p");
420
$rows   = $result->all;
421
is_deeply($rows, [{select => 2, update => 6}], "reserved word");
422

            
423
eval { $dbi->execute('drop table table1') };
424
$dbi->execute($create_table1_2);
425
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
426
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
427
$dbi->update({key2 => 11}, table => 'table1', where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
428
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
429
$rows   = $result->all;
430
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
431
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
432
                  "basic");
433

            
434
eval { $dbi->execute('drop table table1') };
435
$dbi->execute($create_table1_2);
436
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
437
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
438
$dbi->update(table => 'table1', param => {key2 => \"'11'"}, where => {key1 => 1});
test cleanup
Yuki Kimoto authored on 2011-08-10
439
$result = $dbi->execute('select * from table1 order by key1;');
test cleanup
Yuki Kimoto authored on 2011-08-10
440
$rows   = $result->all;
441
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5},
442
                  {key1 => 6, key2 => 7,  key3 => 8, key4 => 9, key5 => 10}],
443
                  "basic");
444

            
445
test 'update_all';
446
eval { $dbi->execute('drop table table1') };
447
$dbi->execute($create_table1_2);
448
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
449
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
450
$dbi->register_filter(twice => sub { $_[0] * 2 });
451
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'});
452
$result = $dbi->execute('select * from table1;');
453
$rows   = $result->all;
454
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5},
455
                  {key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}],
456
                  "filter");
457

            
458

            
459
test 'delete';
460
eval { $dbi->execute('drop table table1') };
461
$dbi->execute($create_table1);
462
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
463
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
464
$dbi->delete(table => 'table1', where => {key1 => 1});
465
$result = $dbi->execute('select * from table1;');
466
$rows   = $result->all;
467
is_deeply($rows, [{key1 => 3, key2 => 4}], "basic");
468

            
469
$dbi->execute("delete from table1;");
470
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
471
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
472
$dbi->register_filter(twice => sub { $_[0] * 2 });
473
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'});
474
$result = $dbi->execute('select * from table1;');
475
$rows   = $result->all;
476
is_deeply($rows, [{key1 => 3, key2 => 4}], "filter");
477

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

            
480
$dbi->delete_all(table => 'table1');
481
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
482
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
483
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2});
484
$rows = $dbi->select(table => 'table1')->all;
485
is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
486

            
487
eval{$dbi->delete(table => 'table1', where => {key1 => 1}, noexist => 1)};
488
like($@, qr/noexist/, "invalid");
489

            
490
eval { $dbi->execute('drop table table1') };
491
$dbi->execute($create_table1);
492
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
493
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
494
$where = $dbi->where;
495
$where->clause(['and', 'key1 = :key1', 'key2 = :key2']);
496
$where->param({ke1 => 1, key2 => 2});
497
$dbi->delete(table => 'table1', where => $where);
498
$result = $dbi->select(table => 'table1');
499
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
500

            
501
eval { $dbi->execute('drop table table1') };
502
$dbi->execute($create_table1);
503
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
504
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
505
$dbi->delete(
506
    table => 'table1',
507
    where => [
508
        ['and', 'key1 = :key1', 'key2 = :key2'],
509
        {ke1 => 1, key2 => 2}
510
    ]
511
);
512
$result = $dbi->select(table => 'table1');
513
is_deeply($result->all, [{key1 => 3, key2 => 4}], 'delete() where');
514

            
515
eval { $dbi->execute('drop table table1') };
test cleanup
Yuki Kimoto authored on 2011-08-10
516
$dbi->execute($create_table1);
test cleanup
Yuki Kimoto authored on 2011-08-10
517
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
518
$dbi->delete(table => 'table1', where => {key1 => 1}, prefix => '    ');
519
$result = $dbi->execute('select * from table1;');
520
$rows   = $result->all;
521
is_deeply($rows, [], "basic");
522

            
523
test 'delete error';
524
eval { $dbi->execute('drop table table1') };
525
$dbi->execute($create_table1);
526
eval{$dbi->delete(table => 'table1')};
527
like($@, qr/"where" must be specified/,
528
         "where key-value pairs not specified");
529

            
530
eval{$dbi->delete(table => 'table1', where => {';' => 1})};
531
like($@, qr/safety/);
532

            
533
$dbi = DBIx::Custom->connect;
534
eval { $dbi->execute("drop table ${q}table$p") };
test cleanup
Yuki Kimoto authored on 2011-08-10
535
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
536
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
537
$dbi->insert(table => 'table', param => {select => 1});
538
$dbi->delete(table => 'table', where => {select => 1});
539
$result = $dbi->execute("select * from ${q}table$p");
540
$rows   = $result->all;
541
is_deeply($rows, [], "reserved word");
542

            
543
test 'delete_all';
544
eval { $dbi->execute('drop table table1') };
545
$dbi->execute($create_table1);
546
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
547
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
548
$dbi->delete_all(table => 'table1');
549
$result = $dbi->execute('select * from table1;');
550
$rows   = $result->all;
551
is_deeply($rows, [], "basic");
552

            
553

            
554
test 'select';
555
eval { $dbi->execute('drop table table1') };
556
$dbi->execute($create_table1);
557
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
558
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
559
$rows = $dbi->select(table => 'table1')->all;
560
is_deeply($rows, [{key1 => 1, key2 => 2},
561
                  {key1 => 3, key2 => 4}], "table");
562

            
563
$rows = $dbi->select(table => 'table1', column => ['key1'])->all;
564
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "table and columns and where key");
565

            
566
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->all;
567
is_deeply($rows, [{key1 => 1, key2 => 2}], "table and columns and where key");
568

            
569
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->all;
570
is_deeply($rows, [{key1 => 3}], "table and columns and where key");
571

            
572
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all;
573
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement");
574

            
575
$dbi->register_filter(decrement => sub { $_[0] - 1 });
576
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'})
577
            ->all;
578
is_deeply($rows, [{key1 => 1, key2 => 2}], "filter");
579

            
test cleanup
Yuki Kimoto authored on 2011-08-10
580
eval { $dbi->execute("drop table table2") };
581
$dbi->execute($create_table2);
test cleanup
Yuki Kimoto authored on 2011-08-10
582
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
583
$rows = $dbi->select(
584
    table => [qw/table1 table2/],
585
    column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3',
586
    where   => {'table1.key2' => 2},
587
    relation  => {'table1.key1' => 'table2.key1'}
588
)->all;
589
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : exists where");
590

            
591
$rows = $dbi->select(
592
    table => [qw/table1 table2/],
593
    column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
594
    relation  => {'table1.key1' => 'table2.key1'}
595
)->all;
596
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
597

            
598
eval{$dbi->select(table => 'table1', noexist => 1)};
599
like($@, qr/noexist/, "invalid");
600

            
601
$dbi = DBIx::Custom->connect;
test cleanup
Yuki Kimoto authored on 2011-08-10
602
eval { $dbi->execute("drop table ${q}table$p") };
603
$dbi->execute($create_table_reserved);
test cleanup
Yuki Kimoto authored on 2011-08-10
604
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
605
$dbi->insert(table => 'table', param => {select => 1, update => 2});
606
$result = $dbi->select(table => 'table', where => {select => 1});
607
$rows   = $result->all;
608
is_deeply($rows, [{select => 2, update => 2}], "reserved word");
609

            
610
test 'fetch filter';
611
eval { $dbi->execute('drop table table1') };
612
$dbi->register_filter(
613
    twice       => sub { $_[0] * 2 },
614
    three_times => sub { $_[0] * 3 }
615
);
616
$dbi->default_fetch_filter('twice');
617
$dbi->execute($create_table1);
618
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
619
$result = $dbi->select(table => 'table1');
620
$result->filter({key1 => 'three_times'});
621
$row = $result->one;
622
is_deeply($row, {key1 => 3, key2 => 4}, "default_fetch_filter and filter");
623

            
624
test 'filters';
625
$dbi = DBIx::Custom->new;
626

            
627
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')),
628
   'あ', "decode_utf8");
629

            
630
is($dbi->filters->{encode_utf8}->('あ'),
631
   encode_utf8('あ'), "encode_utf8");
632

            
633
test 'transaction';
634
$dbi = DBIx::Custom->connect;
635
eval { $dbi->execute('drop table table1') };
636
$dbi->execute($create_table1);
637
$dbi->dbh->begin_work;
638
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
639
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
640
$dbi->dbh->commit;
641
$result = $dbi->select(table => 'table1');
642
is_deeply(scalar $result->all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
643
          "commit");
644

            
645
$dbi = DBIx::Custom->connect;
646
eval { $dbi->execute('drop table table1') };
647
$dbi->execute($create_table1);
648
$dbi->dbh->begin_work(0);
649
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
650
$dbi->dbh->rollback;
651

            
652
$result = $dbi->select(table => 'table1');
653
ok(! $result->fetch_first, "rollback");
654

            
655
test 'cache';
656
eval { $dbi->execute('drop table table1') };
657
$dbi->cache(1);
658
$dbi->execute($create_table1);
659
$source = 'select * from table1 where key1 = :key1 and key2 = :key2;';
660
$dbi->execute($source, {}, query => 1);
661
is_deeply($dbi->{_cached}->{$source}, 
662
          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
663

            
664
eval { $dbi->execute('drop table table1') };
665
$dbi->execute($create_table1);
666
$dbi->{_cached} = {};
667
$dbi->cache(0);
668
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
669
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
670

            
671
test 'execute';
672
eval { $dbi->execute('drop table table1') };
673
$dbi->execute($create_table1);
674
{
675
    local $Carp::Verbose = 0;
676
    eval{$dbi->execute('select * frm table1')};
677
    like($@, qr/\Qselect * frm table1;/, "fail prepare");
678
    like($@, qr/\.t /, "fail : not verbose");
679
}
680
{
681
    local $Carp::Verbose = 1;
682
    eval{$dbi->execute('select * frm table1')};
683
    like($@, qr/Custom.*\.t /s, "fail : verbose");
684
}
685

            
686
eval{$dbi->execute('select * from table1', no_exists => 1)};
687
like($@, qr/wrong/, "invald SQL");
688

            
689
$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
690
$dbi->dbh->disconnect;
691
eval{$dbi->execute($query, param => {key1 => {a => 1}})};
692
ok($@, "execute fail");
693

            
694
{
695
    local $Carp::Verbose = 0;
696
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
697
    like($@, qr/\Q.t /, "caller spec : not vebose");
698
}
699
{
700
    local $Carp::Verbose = 1;
701
    eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
702
    like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose");
703
}
704

            
705

            
706
test 'transaction';
707
$dbi = DBIx::Custom->connect;
708
eval { $dbi->execute('drop table table1') };
709
$dbi->execute($create_table1);
710

            
711
$dbi->begin_work;
712

            
713
eval {
714
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
715
    die "Error";
716
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
717
};
718

            
719
$dbi->rollback if $@;
720

            
721
$result = $dbi->select(table => 'table1');
722
$rows = $result->all;
723
is_deeply($rows, [], "rollback");
724

            
725
$dbi->begin_work;
726

            
727
eval {
728
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
729
    $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
730
};
731

            
732
$dbi->commit unless $@;
733

            
734
$result = $dbi->select(table => 'table1');
735
$rows = $result->all;
736
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
737

            
738
$dbi->dbh->{AutoCommit} = 0;
739
eval{ $dbi->begin_work };
740
ok($@, "exception");
741
$dbi->dbh->{AutoCommit} = 1;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
742

            
cleanup test
Yuki Kimoto authored on 2011-08-08
743
1;