add test
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 |
|
|
5 |
BEGIN { |
|
6 |
eval { require DBD::SQLite; 1 } |
|
7 |
or plan skip_all => 'DBD::SQLite required'; |
|
8 |
eval { DBD::SQLite->VERSION >= 1 } |
|
9 |
or plan skip_all => 'DBD::SQLite >= 1.00 required'; |
|
10 |
|
|
11 |
plan 'no_plan'; |
|
12 |
use_ok('DBI::Custom'); |
|
13 |
} |
|
14 |
|
|
cleanup
|
15 |
# Function for test name |
16 |
my $test; |
|
17 |
sub test { |
|
18 |
$test = shift; |
|
19 |
} |
|
add test
|
20 |
|
cleanup#
|
21 |
# Varialbes for test |
add prepare
|
22 |
our $CREATE_TABLE = { |
add tests
|
23 |
0 => 'create table table1 (key1 char(255), key2 char(255));', |
24 |
1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));' |
|
add prepare
|
25 |
}; |
26 |
|
|
add tests
|
27 |
our $SELECT_TMPL = { |
add tests
|
28 |
0 => 'select * from table1;' |
add tests
|
29 |
}; |
30 |
|
|
cleanup
|
31 |
my $dbi; |
32 |
my $sth; |
|
33 |
my $tmpl; |
|
cleanup#
|
34 |
my $select_tmpl; |
35 |
my $insert_tmpl; |
|
add tests
|
36 |
my $update_tmpl; |
cleanup
|
37 |
my $params; |
38 |
my $sql; |
|
39 |
my $result; |
|
40 |
my @rows; |
|
41 |
my $rows; |
|
add tests
|
42 |
my $query; |
cleanup#
|
43 |
my $select_query; |
44 |
my $insert_query; |
|
add tests
|
45 |
my $update_query; |
add prepare
|
46 |
my $ret_val; |
cleanup#
|
47 |
|
cleanup
|
48 |
|
49 |
|
|
add tests
|
50 |
|
cleanup
|
51 |
# Prepare table |
52 |
$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
53 |
$dbi->connect; |
|
add prepare
|
54 |
$dbi->do($CREATE_TABLE->{0}); |
55 |
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);"); |
|
cleanup
|
56 |
$sth->execute(1, 2); |
57 |
$sth->execute(3, 4); |
|
add test
|
58 |
|
add test module
|
59 |
|
add tests
|
60 |
test 'DBI::Custom::Result test'; |
61 |
$tmpl = "select key1, key2 from table1"; |
|
62 |
$query = $dbi->create_query($tmpl); |
|
63 |
$result = $dbi->execute($query); |
|
cleanup
|
64 |
|
65 |
@rows = (); |
|
66 |
while (my $row = $result->fetch) { |
|
67 |
push @rows, [@$row]; |
|
add test module
|
68 |
} |
add tests
|
69 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch scalar context"); |
cleanup
|
70 |
|
71 |
|
|
add tests
|
72 |
$result = $dbi->execute($query); |
cleanup
|
73 |
@rows = (); |
74 |
while (my @row = $result->fetch) { |
|
75 |
push @rows, [@row]; |
|
add test module
|
76 |
} |
add tests
|
77 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch list context"); |
add test module
|
78 |
|
cleanup
|
79 |
|
add tests
|
80 |
$result = $dbi->execute($query); |
cleanup
|
81 |
@rows = (); |
82 |
while (my $row = $result->fetch_hash) { |
|
83 |
push @rows, {%$row}; |
|
add test module
|
84 |
} |
add tests
|
85 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch_hash scalar context"); |
cleanup
|
86 |
|
87 |
|
|
add tests
|
88 |
$result = $dbi->execute($query); |
cleanup
|
89 |
@rows = (); |
90 |
while (my %row = $result->fetch_hash) { |
|
91 |
push @rows, {%row}; |
|
add test module
|
92 |
} |
add tests
|
93 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : fetch hash list context"); |
add test module
|
94 |
|
95 |
|
|
add tests
|
96 |
$result = $dbi->execute($query); |
cleanup
|
97 |
$rows = $result->fetch_all; |
add tests
|
98 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all scalar context"); |
cleanup
|
99 |
|
100 |
|
|
add tests
|
101 |
$result = $dbi->execute($query); |
cleanup
|
102 |
@rows = $result->fetch_all; |
add tests
|
103 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all list context"); |
cleanup
|
104 |
|
105 |
|
|
add tests
|
106 |
$result = $dbi->execute($query); |
cleanup
|
107 |
@rows = $result->fetch_all_hash; |
add tests
|
108 |
is_deeply($rows, [[1, 2], [3, 4]], "$test : fetch_all_hash scalar context"); |
cleanup
|
109 |
|
110 |
|
|
add tests
|
111 |
$result = $dbi->execute($query); |
cleanup
|
112 |
@rows = $result->fetch_all; |
add tests
|
113 |
is_deeply(\@rows, [[1, 2], [3, 4]], "$test : fetch_all_hash list context"); |
cleanup
|
114 |
|
add tests
|
115 |
|
add tests
|
116 |
test 'Insert query return value'; |
117 |
$dbi->reconnect; |
|
118 |
$dbi->do($CREATE_TABLE->{0}); |
|
119 |
$tmpl = "insert into table1 {insert key1 key2}"; |
|
120 |
$query = $dbi->create_query($tmpl); |
|
121 |
$ret_val = $dbi->execute($query, {key1 => 1, key2 => 2}); |
|
122 |
ok($ret_val, $test); |
|
123 |
|
|
add tests
|
124 |
|
125 |
test 'Direct execute'; |
|
126 |
$dbi->reconnect; |
|
127 |
$dbi->do($CREATE_TABLE->{0}); |
|
128 |
$insert_tmpl = "insert into table1 {insert key1 key2}"; |
|
129 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2}, sub { |
|
130 |
my $query = shift; |
|
131 |
$query->bind_filter(sub { |
|
132 |
my ($key, $value) = @_; |
|
133 |
if ($key eq 'key2') { |
|
134 |
return $value + 1; |
|
135 |
} |
|
136 |
return $value; |
|
137 |
}); |
|
138 |
}); |
|
139 |
|
|
140 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
141 |
|
|
142 |
$rows = $result->fetch_all_hash; |
|
143 |
is_deeply($rows, [{key1 => 1, key2 => 3}], $test); |
|
144 |
|
|
145 |
|
|
cleanup#
|
146 |
test 'Filter'; |
147 |
$dbi->reconnect; |
|
add tests
|
148 |
$dbi->do($CREATE_TABLE->{0}); |
cleanup#
|
149 |
|
add tests
|
150 |
$insert_tmpl = "insert into table1 {insert key1 key2};"; |
add prepare
|
151 |
$insert_query = $dbi->create_query($insert_tmpl); |
152 |
$insert_query->bind_filter(sub { |
|
add tests
|
153 |
my ($key, $value, $table, $column) = @_; |
154 |
if ($key eq 'key1' && $table eq '' && $column eq 'key1') { |
|
add prepare
|
155 |
return $value * 2; |
cleanup#
|
156 |
} |
157 |
return $value; |
|
158 |
}); |
|
add tests
|
159 |
|
add tests
|
160 |
$dbi->execute($insert_query, {key1 => 1, key2 => 2}); |
add prepare
|
161 |
|
add tests
|
162 |
$select_query = $dbi->create_query($SELECT_TMPL->{0}); |
add prepare
|
163 |
$select_query->fetch_filter(sub { |
add tests
|
164 |
my ($key, $value, $type, $sth, $i) = @_; |
165 |
if ($key eq 'key2' && $type =~ /char/ && $sth->can('execute') && $i == 1) { |
|
add prepare
|
166 |
return $value * 3; |
167 |
} |
|
168 |
return $value; |
|
169 |
}); |
|
170 |
$result = $dbi->execute($select_query); |
|
cleanup#
|
171 |
|
add prepare
|
172 |
$rows = $result->fetch_all_hash; |
add tests
|
173 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : bind_filter fetch_filter"); |
cleanup#
|
174 |
|
add tests
|
175 |
|
176 |
$dbi->do("delete from table1;"); |
|
177 |
$insert_query->no_bind_filters('key1'); |
|
178 |
$select_query->no_fetch_filters('key2'); |
|
179 |
|
|
180 |
$dbi->execute($insert_query, {key1 => 1, key2 => 2}); |
|
181 |
$result = $dbi->execute($select_query); |
|
182 |
$rows = $result->fetch_all_hash; |
|
183 |
is_deeply($rows, [{key1 => 1, key2 => 2}], 'no_fetch_filters no_bind_filters'); |
|
184 |
|
|
185 |
|
|
add tests
|
186 |
$dbi->reconnect; |
187 |
$dbi->do($CREATE_TABLE->{0}); |
|
188 |
$insert_tmpl = "insert into table1 {insert table1.key1 table1.key2}"; |
|
189 |
|
|
190 |
$insert_query = $dbi->create_query($insert_tmpl); |
|
191 |
$insert_query->bind_filter(sub { |
|
192 |
my ($key, $value, $table, $column) = @_; |
|
193 |
if ($key eq 'table1.key1' && $table eq 'table1' && $column eq 'key1') { |
|
194 |
return $value * 3; |
|
195 |
} |
|
196 |
return $value; |
|
197 |
}); |
|
198 |
|
|
199 |
$dbi->execute($insert_query, {table1 => {key1 => 1, key2 => 2}}); |
|
200 |
|
|
201 |
$select_query = $dbi->create_query($SELECT_TMPL->{0}); |
|
202 |
$result = $dbi->execute($select_query); |
|
203 |
$rows = $result->fetch_all_hash; |
|
204 |
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with table name"); |
|
205 |
|
|
add tests
|
206 |
test 'DBI::Custom::SQL::Template basic tag'; |
add tests
|
207 |
$dbi->reconnect; |
208 |
$dbi->do($CREATE_TABLE->{1}); |
|
209 |
$sth = $dbi->prepare("insert into table1 (key1, key2, key3, key4, key5) values (?, ?, ?, ?, ?);"); |
|
210 |
$sth->execute(1, 2, 3, 4, 5); |
|
211 |
$sth->execute(6, 7, 8, 9, 10); |
|
add tests
|
212 |
|
add tests
|
213 |
$tmpl = "select * from table1 where {= key1} and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
214 |
$query = $dbi->create_query($tmpl); |
|
215 |
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
|
cleanup
|
216 |
$rows = $result->fetch_all_hash; |
add tests
|
217 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1"); |
cleanup
|
218 |
|
add tests
|
219 |
$tmpl = "select * from table1 where {<= key1} and {like key2};"; |
220 |
$query = $dbi->create_query($tmpl); |
|
221 |
$result = $dbi->execute($query, {key1 => 1, key2 => '%2%'}); |
|
222 |
$rows = $result->fetch_all_hash; |
|
223 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2"); |
|
cleanup
|
224 |
|
225 |
|
|
add tests
|
226 |
test 'DBI::Custom::SQL::Template insert tag'; |
add tests
|
227 |
$dbi->do("delete from table1"); |
228 |
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}'; |
|
229 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
cleanup
|
230 |
|
add tests
|
231 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
add tests
|
232 |
$rows = $result->fetch_all_hash; |
add tests
|
233 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic"); |
cleanup
|
234 |
|
add tests
|
235 |
$dbi->do("delete from table1"); |
236 |
$dbi->execute($insert_tmpl, {'#insert' => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}); |
|
237 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
238 |
$rows = $result->fetch_all_hash; |
|
add tests
|
239 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert"); |
add tests
|
240 |
|
241 |
|
|
242 |
$dbi->do("delete from table1"); |
|
243 |
$insert_tmpl = 'insert into table1 {insert table1.key1 table1.key2 table1.key3 table1.key4 table1.key5}'; |
|
244 |
$dbi->execute($insert_tmpl, {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}); |
|
245 |
|
|
246 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
247 |
$rows = $result->fetch_all_hash; |
|
add tests
|
248 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name"); |
249 |
|
|
add tests
|
250 |
|
251 |
$dbi->do("delete from table1"); |
|
252 |
$dbi->execute($insert_tmpl, {'#insert' => {table1 => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}}}); |
|
253 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
254 |
$rows = $result->fetch_all_hash; |
|
add tests
|
255 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : #insert with table name"); |
add tests
|
256 |
|
257 |
|
|
add tests
|
258 |
test 'DBI::Custom::SQL::Template update tag'; |
add tests
|
259 |
$dbi->do("delete from table1"); |
260 |
$insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}"; |
|
261 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
262 |
$dbi->execute($insert_tmpl, {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
263 |
|
|
264 |
$update_tmpl = 'update table1 {update key1 key2 key3 key4} where {= key5}'; |
|
265 |
$dbi->execute($update_tmpl, {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
|
266 |
|
|
267 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
|
268 |
$rows = $result->fetch_all_hash; |
|
269 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
add tests
|
270 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic"); |
add tests
|
271 |
|
add tests
|
272 |
$dbi->execute($update_tmpl, {'#update' => {key1 => 2, key2 => 2, key3 => 2, key4 => 2}, key5 => 5}); |
add tests
|
273 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
274 |
$rows = $result->fetch_all_hash; |
|
add tests
|
275 |
is_deeply($rows, [{key1 => 2, key2 => 2, key3 => 2, key4 => 2, key5 => 5}, |
276 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : #update"); |
|
add tests
|
277 |
|
add tests
|
278 |
$update_tmpl = 'update table1 {update table1.key1 table1.key2 table1.key3 table1.key4} where {= table1.key5}'; |
279 |
$dbi->execute($update_tmpl, {table1 => {key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}}); |
|
add tests
|
280 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
281 |
$rows = $result->fetch_all_hash; |
|
add tests
|
282 |
is_deeply($rows, [{key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}, |
283 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name"); |
|
add tests
|
284 |
|
add tests
|
285 |
$dbi->execute($update_tmpl, {'#update' => {table1 => {key1 => 4, key2 => 4, key3 => 4, key4 => 4}}, table1 => {key5 => 5}}); |
add tests
|
286 |
$result = $dbi->execute($SELECT_TMPL->{0}); |
287 |
$rows = $result->fetch_all_hash; |
|
add tests
|
288 |
is_deeply($rows, [{key1 => 4, key2 => 4, key3 => 4, key4 => 4, key5 => 5}, |
289 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test update tag #update with table name"); |
|
add tests
|
290 |
|
cleanup
|
291 |
|
add tests
|
292 |
__END__ |
cleanup
|
293 |
|
294 |
|
|
add tests
|
295 |
|
cleanup#
|
296 |
$dbi->disconnnect; |
add test module
|
297 |
|
add tests
|
298 |
# Tag 'in' is easy to wrong |
299 |
|