DBIx-Custom / t / common.t /
Newer Older
312 lines | 11.595kb
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;
20

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
21

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
269
$dbi->delete_all(table => 'table1');
270
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => '   ');
271
$rows = $dbi->select(table => 'table1')->all;
272
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
273

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

            
277
eval{$dbi->insert(table => 'table', param => {';' => 1})};
278
like($@, qr/safety/);
279

            
280
__END__
281

            
282
$dbi->quote('"');
283
$dbi->execute('create table "table" ("select")');
284
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
285
$dbi->insert(table => 'table', param => {select => 1});
286
$result = $dbi->execute('select * from "table"');
287
$rows   = $result->all;
288
is_deeply($rows, [{select => 2}], "reserved word");
289

            
290
$dbi->execute($create_table1);
291
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
292
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
293
$result = $dbi->execute('select * from table1;');
294
$rows   = $result->all;
295
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
296

            
297
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))");
298
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
299
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
300
$result = $dbi->execute('select * from table1;');
301
$rows   = $result->all;
302
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
303

            
304
$dbi->execute($create_table1);
305
$dbi->insert(table => 'table1', param => {key1 => \"'1'", key2 => 2});
306
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
307
$result = $dbi->execute('select * from table1;');
308
$rows   = $result->all;
309
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "basic");
310

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

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