removed register_format()
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 | ||
5 |
use utf8; |
|
6 |
use Encode qw/encode_utf8 decode_utf8/; |
|
7 | ||
8 |
BEGIN { |
|
9 |
eval { require DBD::SQLite; 1 } |
|
10 |
or plan skip_all => 'DBD::SQLite required'; |
|
11 |
eval { DBD::SQLite->VERSION >= 1.25 } |
|
12 |
or plan skip_all => 'DBD::SQLite >= 1.25 required'; |
|
13 | ||
14 |
plan 'no_plan'; |
|
15 |
use_ok('DBIx::Custom'); |
|
16 |
} |
|
17 | ||
18 |
# Function for test name |
|
19 |
my $test; |
|
20 |
sub test { |
|
21 |
$test = shift; |
|
22 |
} |
|
23 | ||
added register_method() meth...
|
24 |
use DBIx::Custom::SQLite; |
25 | ||
removed register_format()
|
26 |
# Constant varialbes for test |
27 |
my $CREATE_TABLE = { |
|
28 |
0 => 'create table table1 (key1 char(255), key2 char(255));', |
|
29 |
1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));', |
|
30 |
2 => 'create table table2 (key1 char(255), key3 char(255));' |
|
31 |
}; |
|
32 | ||
add tests
|
33 |
my $SELECT_SOURCES = { |
removed register_format()
|
34 |
0 => 'select * from table1;' |
35 |
}; |
|
36 | ||
37 |
my $DROP_TABLE = { |
|
38 |
0 => 'drop table table1' |
|
39 |
}; |
|
40 | ||
41 |
my $NEW_ARGS = { |
|
42 |
0 => {data_source => 'dbi:SQLite:dbname=:memory:'} |
|
43 |
}; |
|
44 | ||
45 |
# Variables |
|
46 |
my $dbi; |
|
47 |
my $sth; |
|
renamed build_query to creat...
|
48 |
my $source; |
49 |
my @sources; |
|
add tests
|
50 |
my $select_SOURCE; |
51 |
my $insert_SOURCE; |
|
52 |
my $update_SOURCE; |
|
removed register_format()
|
53 |
my $params; |
54 |
my $sql; |
|
55 |
my $result; |
|
56 |
my $row; |
|
57 |
my @rows; |
|
58 |
my $rows; |
|
59 |
my $query; |
|
60 |
my @queries; |
|
61 |
my $select_query; |
|
62 |
my $insert_query; |
|
63 |
my $update_query; |
|
64 |
my $ret_val; |
|
65 | ||
66 | ||
67 |
# Prepare table |
|
68 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
69 |
$dbi->execute($CREATE_TABLE->{0}); |
|
70 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
71 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
72 | ||
73 |
test 'DBIx::Custom::Result test'; |
|
renamed build_query to creat...
|
74 |
$source = "select key1, key2 from table1"; |
75 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
76 |
$result = $dbi->execute($query); |
77 | ||
78 |
@rows = (); |
|
79 |
while (my $row = $result->fetch) { |
|
80 |
push @rows, [@$row]; |
|
81 |
} |
|
removed reconnect method
|
82 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch"); |
removed register_format()
|
83 | |
84 |
$result = $dbi->execute($query); |
|
85 |
@rows = (); |
|
86 |
while (my $row = $result->fetch_hash) { |
|
87 |
push @rows, {%$row}; |
|
88 |
} |
|
removed reconnect method
|
89 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash"); |
removed register_format()
|
90 | |
91 |
$result = $dbi->execute($query); |
|
92 |
$rows = $result->fetch_all; |
|
removed reconnect method
|
93 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all"); |
removed register_format()
|
94 | |
95 |
$result = $dbi->execute($query); |
|
removed reconnect method
|
96 |
$rows = $result->fetch_hash_all; |
97 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash_all"); |
|
removed register_format()
|
98 | |
99 |
test 'Insert query return value'; |
|
100 |
$dbi->execute($DROP_TABLE->{0}); |
|
101 |
$dbi->execute($CREATE_TABLE->{0}); |
|
renamed update tag to update...
|
102 |
$source = "insert into table1 {insert_param key1 key2}"; |
renamed build_query to creat...
|
103 |
$query = $dbi->create_query($source); |
removed register_format()
|
104 |
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2}); |
105 |
ok($ret_val, $test); |
|
106 | ||
107 | ||
108 |
test 'Direct query'; |
|
109 |
$dbi->execute($DROP_TABLE->{0}); |
|
110 |
$dbi->execute($CREATE_TABLE->{0}); |
|
add tests
|
111 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2}"; |
112 |
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2}); |
|
113 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
removed register_format()
|
114 |
$rows = $result->fetch_hash_all; |
115 |
is_deeply($rows, [{key1 => 1, key2 => 2}], $test); |
|
116 | ||
117 |
test 'Filter basic'; |
|
118 |
$dbi->execute($DROP_TABLE->{0}); |
|
119 |
$dbi->execute($CREATE_TABLE->{0}); |
|
120 |
$dbi->register_filter(twice => sub { $_[0] * 2}, |
|
121 |
three_times => sub { $_[0] * 3}); |
|
122 | ||
add tests
|
123 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2};"; |
124 |
$insert_query = $dbi->create_query($insert_SOURCE); |
|
removed register_format()
|
125 |
$insert_query->filter({key1 => 'twice'}); |
126 |
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2}); |
|
add tests
|
127 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
128 |
$rows = $result->filter({key2 => 'three_times'})->fetch_hash_all; |
129 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : filter fetch_filter"); |
|
130 |
$dbi->execute($DROP_TABLE->{0}); |
|
131 | ||
132 |
test 'Filter in'; |
|
133 |
$dbi->execute($CREATE_TABLE->{0}); |
|
add tests
|
134 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2};"; |
135 |
$insert_query = $dbi->create_query($insert_SOURCE); |
|
removed register_format()
|
136 |
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4}); |
add tests
|
137 |
$select_SOURCE = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
138 |
$select_query = $dbi->create_query($select_SOURCE); |
|
removed register_format()
|
139 |
$select_query->filter({'table1.key1' => 'twice'}); |
140 |
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]}); |
|
141 |
$rows = $result->fetch_hash_all; |
|
142 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : filter"); |
|
143 | ||
144 |
test 'DBIx::Custom::SQLTemplate basic tag'; |
|
145 |
$dbi->execute($DROP_TABLE->{0}); |
|
146 |
$dbi->execute($CREATE_TABLE->{1}); |
|
147 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
148 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
149 | ||
renamed build_query to creat...
|
150 |
$source = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
151 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
152 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
153 |
$rows = $result->fetch_hash_all; |
|
154 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1"); |
|
155 | ||
renamed build_query to creat...
|
156 |
$source = "select * from table1 where {<= key1} and {like key2};"; |
157 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
158 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'}); |
159 |
$rows = $result->fetch_hash_all; |
|
160 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2"); |
|
161 | ||
162 |
test 'DIB::Custom::SQLTemplate in tag'; |
|
163 |
$dbi->execute($DROP_TABLE->{0}); |
|
164 |
$dbi->execute($CREATE_TABLE->{1}); |
|
165 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
166 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
167 | ||
renamed build_query to creat...
|
168 |
$source = "select * from table1 where {in key1 2};"; |
169 |
$query = $dbi->create_query($source); |
|
removed register_format()
|
170 |
$result = $dbi->execute($query, param => {key1 => [9, 1]}); |
171 |
$rows = $result->fetch_hash_all; |
|
172 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
173 | ||
174 |
test 'DBIx::Custom::SQLTemplate insert tag'; |
|
175 |
$dbi->execute("delete from table1"); |
|
add tests
|
176 |
$insert_SOURCE = 'insert into table1 {insert_param key1 key2 key3 key4 key5}'; |
177 |
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
removed register_format()
|
178 | |
add tests
|
179 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
180 |
$rows = $result->fetch_hash_all; |
181 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
|
182 | ||
183 |
test 'DBIx::Custom::SQLTemplate update tag'; |
|
184 |
$dbi->execute("delete from table1"); |
|
add tests
|
185 |
$insert_SOURCE = "insert into table1 {insert_param key1 key2 key3 key4 key5}"; |
186 |
$dbi->execute($insert_SOURCE, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
187 |
$dbi->execute($insert_SOURCE, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
removed register_format()
|
188 | |
add tests
|
189 |
$update_SOURCE = 'update table1 {update_param key1 key2 key3 key4} where {= key5}'; |
190 |
$dbi->execute($update_SOURCE, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
|
removed register_format()
|
191 | |
add tests
|
192 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
193 |
$rows = $result->fetch_hash_all; |
194 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
195 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic"); |
|
196 | ||
197 |
test 'Error case'; |
|
198 |
eval {DBIx::Custom->connect(data_source => 'dbi:SQLit')}; |
|
199 |
ok($@, "$test : connect error"); |
|
200 | ||
201 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
renamed build_query to creat...
|
202 |
eval{$dbi->create_query("{p }")}; |
203 |
ok($@, "$test : create_query invalid SQL template"); |
|
removed register_format()
|
204 | |
205 |
test 'insert'; |
|
206 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
207 |
$dbi->execute($CREATE_TABLE->{0}); |
|
208 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
209 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
add tests
|
210 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
211 |
$rows = $result->fetch_hash_all; |
212 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic"); |
|
213 | ||
214 |
$dbi->execute('delete from table1'); |
|
215 |
$dbi->register_filter( |
|
216 |
twice => sub { $_[0] * 2 }, |
|
217 |
three_times => sub { $_[0] * 3 } |
|
218 |
); |
|
renamed default_query_filter...
|
219 |
$dbi->default_bind_filter('twice'); |
removed register_format()
|
220 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => 'three_times'}); |
add tests
|
221 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
222 |
$rows = $result->fetch_hash_all; |
223 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter"); |
|
renamed default_query_filter...
|
224 |
$dbi->default_bind_filter(undef); |
removed register_format()
|
225 | |
226 |
$dbi->execute($DROP_TABLE->{0}); |
|
227 |
$dbi->execute($CREATE_TABLE->{0}); |
|
228 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, append => ' '); |
|
229 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
230 |
is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append'); |
|
231 | ||
changed argument of tag proc...
|
232 |
eval{$dbi->insert(table => 'table1', noexist => 1)}; |
add tests
|
233 |
like($@, qr/noexist/, "$test: invalid argument"); |
changed argument of tag proc...
|
234 | |
235 | ||
removed register_format()
|
236 |
test 'update'; |
237 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
238 |
$dbi->execute($CREATE_TABLE->{1}); |
|
239 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
240 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
241 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}); |
|
add tests
|
242 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
243 |
$rows = $result->fetch_hash_all; |
244 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
245 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
246 |
"$test : basic"); |
|
247 |
|
|
248 |
$dbi->execute("delete from table1"); |
|
249 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
250 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
251 |
$dbi->update(table => 'table1', param => {key2 => 12}, where => {key2 => 2, key3 => 3}); |
|
add tests
|
252 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
253 |
$rows = $result->fetch_hash_all; |
254 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
255 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
256 |
"$test : update key same as search key"); |
|
257 | ||
add tests
|
258 |
$dbi->update(table => 'table1', param => {key2 => [12]}, where => {key2 => 2, key3 => 3}); |
259 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
|
260 |
$rows = $result->fetch_hash_all; |
|
261 |
is_deeply($rows, [{key1 => 1, key2 => 12, key3 => 3, key4 => 4, key5 => 5}, |
|
262 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
263 |
"$test : update key same as search key : param is array ref"); |
|
264 | ||
removed register_format()
|
265 |
$dbi->execute("delete from table1"); |
266 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
267 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
268 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
269 |
$dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, |
|
270 |
filter => {key2 => 'twice'}); |
|
add tests
|
271 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
272 |
$rows = $result->fetch_hash_all; |
273 |
is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5}, |
|
274 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
275 |
"$test : filter"); |
|
276 | ||
277 |
$result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => ' '); |
|
278 | ||
changed argument of tag proc...
|
279 |
eval{$dbi->update(table => 'table1', noexist => 1)}; |
add tests
|
280 |
like($@, qr/noexist/, "$test: invalid argument"); |
changed argument of tag proc...
|
281 | |
282 |
eval{$dbi->update(table => 'table1')}; |
|
283 |
like($@, qr/where/, "$test: not contain where"); |
|
284 | ||
285 | ||
removed register_format()
|
286 |
test 'update_all'; |
287 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
288 |
$dbi->execute($CREATE_TABLE->{1}); |
|
289 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
290 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
291 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
292 |
$dbi->update_all(table => 'table1', param => {key2 => 10}, filter => {key2 => 'twice'}); |
|
add tests
|
293 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
294 |
$rows = $result->fetch_hash_all; |
295 |
is_deeply($rows, [{key1 => 1, key2 => 20, key3 => 3, key4 => 4, key5 => 5}, |
|
296 |
{key1 => 6, key2 => 20, key3 => 8, key4 => 9, key5 => 10}], |
|
297 |
"$test : filter"); |
|
298 | ||
299 | ||
300 |
test 'delete'; |
|
301 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
302 |
$dbi->execute($CREATE_TABLE->{0}); |
|
303 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
304 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
305 |
$dbi->delete(table => 'table1', where => {key1 => 1}); |
|
add tests
|
306 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
307 |
$rows = $result->fetch_hash_all; |
308 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : basic"); |
|
309 | ||
310 |
$dbi->execute("delete from table1;"); |
|
311 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
312 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
313 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
314 |
$dbi->delete(table => 'table1', where => {key2 => 1}, filter => {key2 => 'twice'}); |
|
add tests
|
315 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
316 |
$rows = $result->fetch_hash_all; |
317 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : filter"); |
|
318 | ||
319 |
$dbi->delete(table => 'table1', where => {key1 => 1}, append => ' '); |
|
320 | ||
321 |
$dbi->delete_all(table => 'table1'); |
|
322 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
323 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
324 |
$dbi->delete(table => 'table1', where => {key1 => 1, key2 => 2}); |
|
325 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
326 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : delete multi key"); |
|
327 | ||
changed argument of tag proc...
|
328 |
eval{$dbi->delete(table => 'table1', noexist => 1)}; |
add tests
|
329 |
like($@, qr/noexist/, "$test: invalid argument"); |
changed argument of tag proc...
|
330 | |
331 | ||
removed register_format()
|
332 |
test 'delete error'; |
333 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
334 |
$dbi->execute($CREATE_TABLE->{0}); |
|
335 |
eval{$dbi->delete(table => 'table1')}; |
|
add tests
|
336 |
like($@, qr/"where" argument must be specified and contains the pairs of column name and value/, |
removed register_format()
|
337 |
"$test : where key-value pairs not specified"); |
338 | ||
339 |
test 'delete_all'; |
|
340 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
341 |
$dbi->execute($CREATE_TABLE->{0}); |
|
342 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
343 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
344 |
$dbi->delete_all(table => 'table1'); |
|
add tests
|
345 |
$result = $dbi->execute($SELECT_SOURCES->{0}); |
removed register_format()
|
346 |
$rows = $result->fetch_hash_all; |
347 |
is_deeply($rows, [], "$test : basic"); |
|
348 | ||
349 | ||
350 |
test 'select'; |
|
351 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
352 |
$dbi->execute($CREATE_TABLE->{0}); |
|
353 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
354 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
355 |
$rows = $dbi->select(table => 'table1')->fetch_hash_all; |
|
356 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
357 |
{key1 => 3, key2 => 4}], "$test : table"); |
|
358 | ||
update document
|
359 |
$rows = $dbi->select(table => 'table1', column => ['key1'])->fetch_hash_all; |
removed register_format()
|
360 |
is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key"); |
361 | ||
362 |
$rows = $dbi->select(table => 'table1', where => {key1 => 1})->fetch_hash_all; |
|
363 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key"); |
|
364 | ||
update document
|
365 |
$rows = $dbi->select(table => 'table1', where => ['{= key1} and {= key2}', {key1 => 1, key2 => 2}])->fetch_hash_all; |
366 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where string"); |
|
367 | ||
update document
|
368 |
$rows = $dbi->select(table => 'table1', column => ['key1'], where => {key1 => 3})->fetch_hash_all; |
removed register_format()
|
369 |
is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key"); |
370 | ||
371 |
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->fetch_hash_all; |
|
372 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement"); |
|
373 | ||
374 |
$dbi->register_filter(decrement => sub { $_[0] - 1 }); |
|
update document
|
375 |
$rows = $dbi->select(table => 'table1', where => {key1 => 2}, filter => {key1 => 'decrement'}) |
removed register_format()
|
376 |
->fetch_hash_all; |
377 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : filter"); |
|
378 | ||
379 |
$dbi->execute($CREATE_TABLE->{2}); |
|
380 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
381 |
$rows = $dbi->select( |
|
382 |
table => [qw/table1 table2/], |
|
update document
|
383 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
removed register_format()
|
384 |
where => {'table1.key2' => 2}, |
added commit method
|
385 |
relation => {'table1.key1' => 'table2.key1'} |
removed register_format()
|
386 |
)->fetch_hash_all; |
added commit method
|
387 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : relation : exists where"); |
388 | ||
389 |
$rows = $dbi->select( |
|
390 |
table => [qw/table1 table2/], |
|
391 |
column => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'], |
|
392 |
relation => {'table1.key1' => 'table2.key1'} |
|
393 |
)->fetch_hash_all; |
|
394 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : relation : no exists where"); |
|
removed register_format()
|
395 | |
changed argument of tag proc...
|
396 |
eval{$dbi->select(table => 'table1', noexist => 1)}; |
add tests
|
397 |
like($@, qr/noexist/, "$test: invalid argument"); |
changed argument of tag proc...
|
398 | |
399 | ||
removed register_format()
|
400 |
test 'fetch filter'; |
401 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
402 |
$dbi->register_filter( |
|
403 |
twice => sub { $_[0] * 2 }, |
|
404 |
three_times => sub { $_[0] * 3 } |
|
405 |
); |
|
406 |
$dbi->default_fetch_filter('twice'); |
|
407 |
$dbi->execute($CREATE_TABLE->{0}); |
|
408 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
409 |
$result = $dbi->select(table => 'table1'); |
|
410 |
$result->filter({key1 => 'three_times'}); |
|
removed reconnect method
|
411 |
$row = $result->fetch_hash_first; |
removed register_format()
|
412 |
is_deeply($row, {key1 => 3, key2 => 4}, "$test: default_fetch_filter and filter"); |
413 | ||
414 |
test 'filters'; |
|
415 |
$dbi = DBIx::Custom->new; |
|
416 | ||
update document
|
417 |
is($dbi->filters->{decode_utf8}->(encode_utf8('あ')), |
418 |
'あ', "$test : decode_utf8"); |
|
removed register_format()
|
419 | |
420 |
is($dbi->filters->{encode_utf8}->('あ'), |
|
421 |
encode_utf8('あ'), "$test : encode_utf8"); |
|
422 | ||
added commit method
|
423 |
test 'transaction'; |
424 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
425 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed DBIx::Custom commit ...
|
426 |
$dbi->dbh->begin_work; |
added commit method
|
427 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
428 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
removed DBIx::Custom commit ...
|
429 |
$dbi->dbh->commit; |
added commit method
|
430 |
$result = $dbi->select(table => 'table1'); |
431 |
is_deeply(scalar $result->fetch_hash_all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}], |
|
432 |
"$test : commit"); |
|
433 | ||
434 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
435 |
$dbi->execute($CREATE_TABLE->{0}); |
|
removed DBIx::Custom commit ...
|
436 |
$dbi->dbh->begin_work(0); |
added commit method
|
437 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
removed DBIx::Custom commit ...
|
438 |
$dbi->dbh->rollback; |
added commit method
|
439 | |
440 |
$result = $dbi->select(table => 'table1'); |
|
removed reconnect method
|
441 |
ok(! $result->fetch_first, "$test: rollback"); |
add cache attribute
|
442 | |
443 |
test 'cache'; |
|
444 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
445 |
$dbi->execute($CREATE_TABLE->{0}); |
|
renamed build_query to creat...
|
446 |
$source = 'select * from table1 where {= key1} and {= key2};'; |
447 |
$dbi->create_query($source); |
|
448 |
is_deeply($dbi->{_cached}->{$source}, |
|
add cache attribute
|
449 |
{sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2']}, "$test : cache"); |
450 | ||
451 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
452 |
$dbi->execute($CREATE_TABLE->{0}); |
|
453 |
$dbi->{_cached} = {}; |
|
454 |
$dbi->cache(0); |
|
455 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
456 |
is(scalar keys %{$dbi->{_cached}}, 0, 'not cache'); |
|
457 | ||
changed argument of tag proc...
|
458 | |
added check_filter attribute
|
459 |
test 'filter_check in fetching'; |
460 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
461 |
$dbi->execute($CREATE_TABLE->{0}); |
|
462 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
463 |
$dbi->default_fetch_filter('not_exists'); |
|
464 |
$result = $dbi->select(table => 'table1'); |
|
465 |
eval{$result->fetch_first}; |
|
466 |
like($@, qr/\QDefault fetch filter "not_exists" is not registered/, "$test : array :default_fetch_filter"); |
|
467 | ||
468 |
$dbi->default_fetch_filter(undef); |
|
469 |
$result = $dbi->select(table => 'table1'); |
|
470 |
$result->filter({key1 => 'not_exists'}); |
|
471 |
eval{$result->fetch_first}; |
|
472 |
like($@, qr/\QFetch filter "not_exists" is not registered/, "$test : array :fetch_filter"); |
|
473 | ||
474 |
$result = $dbi->select(table => 'table1'); |
|
475 |
$result->filter({not_exists => 'encode_utf8'}); |
|
476 |
eval{$result->fetch_first}; |
|
477 |
like($@, qr/\QColumn name "not_exists" in fetch filter is not found in result columns/, |
|
478 |
"$test : array : fetch_filter"); |
|
479 | ||
480 |
$result = $dbi->select(table => 'table1'); |
|
481 |
$result->filter({Key1 => 'encode_utf8'}); |
|
482 |
eval{$result->fetch_first}; |
|
483 |
like($@, qr/\QColumn name "Key1" in fetch filter must lower case string/, |
|
484 |
"$test : array : contain upper case charactor"); |
|
485 | ||
486 |
$dbi->filter_check(0); |
|
487 |
$result = $dbi->select(table => 'table1'); |
|
488 |
$result->filter({Key1 => 'encode_utf8'}); |
|
489 |
eval{$result->fetch_first}; |
|
490 |
ok(!$@, "$test : array : filter_check off"); |
|
491 | ||
492 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
493 |
$dbi->execute($CREATE_TABLE->{0}); |
|
494 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
495 |
$dbi->default_fetch_filter('not_exists'); |
|
496 |
$result = $dbi->select(table => 'table1'); |
|
497 |
eval{$result->fetch_hash_first}; |
|
498 |
like($@, qr/\QDefault fetch filter "not_exists" is not registered/, "$test : hash :default_fetch_filter"); |
|
499 | ||
500 |
$dbi->default_fetch_filter(undef); |
|
501 |
$result = $dbi->select(table => 'table1'); |
|
502 |
$result->filter({key1 => 'not_exists'}); |
|
503 |
eval{$result->fetch_hash_first}; |
|
504 |
like($@, qr/\QFetch filter "not_exists" is not registered/, "$test : hash :fetch_filter"); |
|
505 | ||
506 |
$result = $dbi->select(table => 'table1'); |
|
507 |
$result->filter({not_exists => 'encode_utf8'}); |
|
508 |
eval{$result->fetch_hash_first}; |
|
509 |
like($@, qr/\QColumn name "not_exists" in fetch filter is not found in result columns/, |
|
510 |
"$test : hash : fetch_filter"); |
|
511 | ||
512 |
$result = $dbi->select(table => 'table1'); |
|
513 |
$result->filter({Key1 => 'encode_utf8'}); |
|
514 |
eval{$result->fetch_hash_first}; |
|
515 |
like($@, qr/\QColumn name "Key1" in fetch filter must lower case string/, |
|
516 |
"$test : hash : contain upper case charactor"); |
|
517 | ||
518 |
$dbi->filter_check(0); |
|
519 |
$result = $dbi->select(table => 'table1'); |
|
520 |
$result->filter({Key1 => 'encode_utf8'}); |
|
521 |
eval{$result->fetch_hash_first}; |
|
522 |
ok(!$@, "$test : hash : filter_check off"); |
|
523 | ||
524 | ||
add tests
|
525 |
test 'filter_check in parameter binding'; |
added check_filter attribute
|
526 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
527 |
$dbi->execute($CREATE_TABLE->{0}); |
|
528 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
529 | ||
add tests
|
530 |
$dbi->default_bind_filter('not_exists'); |
531 |
eval{$dbi->select(table => 'table1')}; |
|
532 |
like($@, qr/\QDefault bind filter "not_exists" is not registered/, "$test : default_bind_filter"); |
|
added check_filter attribute
|
533 | |
add tests
|
534 |
$dbi->default_bind_filter(undef); |
535 |
eval{$dbi->select(table => 'table1', filter => {key1 => 'not_exists'})}; |
|
536 |
like($@, qr/\QBind filter "not_exists" is not registered/, "$test : bind_filter"); |
|
added check_filter attribute
|
537 | |
add tests
|
538 |
eval{$dbi->select(table => 'table1', filter => {not_exists => 'encode_utf8'})}; |
539 |
like($@, qr/\QColumn name "not_exists" in bind filter is not found in paramters/, |
|
540 |
"$test : fetch_filter"); |
|
added check_filter attribute
|
541 | |
add tests
|
542 |
test 'execute'; |
543 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
544 |
$dbi->execute($CREATE_TABLE->{0}); |
|
545 |
eval{$dbi->execute('select * frm table1')}; |
|
546 |
like($@, qr/\Qselect * frm table1;/, "$test : fail prepare"); |
|
547 | ||
548 |
eval{$dbi->execute('select * from table1', no_exists => 1)}; |
|
549 |
like($@, qr/\Q"no_exists" is invalid argument/, "$test : invald SQL"); |
|
550 | ||
551 |
$query = $dbi->create_query('select * from table1 where {= key1}'); |
|
552 |
$dbi->dbh->disconnect; |
|
553 |
eval{$dbi->execute($query, param => {key1 => {a => 1}})}; |
|
554 |
ok($@, "$test: execute fail"); |
|
555 | ||
556 |
eval{$dbi->create_query('select * from table1 where {0 key1}')}; |
|
557 |
like($@, qr/\Q.t /, "$test : caller spec"); |
|
558 | ||
added register_method() meth...
|
559 | |
560 |
test 'register_method'; |
|
561 |
$dbi = DBIx::Custom::SQLite->new; |
|
562 |
$dbi->register_method( |
|
563 |
one => sub { 1 }, |
|
564 |
); |
|
565 |
$dbi->register_method({ |
|
566 |
two => sub { 2 } |
|
567 |
}); |
|
568 |
is($dbi->one, 1, "$test : hash"); |
|
569 |
is($dbi->two, 2, "$test : hash reference"); |