DBIx-Custom / t / common.t /
Newer Older
317 lines | 11.927kb
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; 
added common test executing ...
Yuki Kimoto authored on 2011-08-07
5

            
6
my $dbi;
7

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

            
12
plan 'no_plan';
13

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

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

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

            
55
# Drop table
56
eval { $dbi->execute('drop table table1') };
57

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

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

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

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

            
85
$result = $dbi->execute($query);
86
$rows = $result->fetch_all;
87
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all");
88

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
280
eval{$dbi->insert(table => 'table', param => {';' => 1})};
281
like($@, qr/safety/);
282

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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
300
eval { $dbi->execute('drop table table1') };
cleanup test
Yuki Kimoto authored on 2011-08-10
301
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
302
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
303
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
304
$result = $dbi->execute('select * from table1;');
305
$rows   = $result->all;
306
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
307

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

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
316

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