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 = { |
28 |
0 => 'select key1, key2 from table1;' |
|
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'; |
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 |
$dbi->do("delete from table1"); |
227 |
$insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}'; |
|
228 |
$dbi->execute($insert_tmpl, {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
cleanup
|
229 |
|
add tests
|
230 |
$select_tmpl = 'select * from table1'; |
231 |
$result = $dbi->execute($select_tmpl); |
|
232 |
$rows = $result->fetch_all_hash; |
|
233 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test insert tag"); |
|
cleanup
|
234 |
|
235 |
|
|
add tests
|
236 |
__END__ |
cleanup
|
237 |
|
238 |
# Insert values |
|
239 |
$dbi = DBI::Custom->new; |
|
add tests
|
240 |
$tmpl = "insert into table {insert}"; |
241 |
$params = {insert => {key1 => 'a', key2 => 'b'}}; |
|
cleanup
|
242 |
|
243 |
$dbi->filters(filter => sub { |
|
244 |
my ($key, $value) = @_; |
|
245 |
if ($key eq 'key1' && $value eq 'a') { |
|
246 |
return uc $value; |
|
cleanup
|
247 |
} |
cleanup
|
248 |
return $value; |
249 |
}); |
|
cleanup
|
250 |
|
cleanup
|
251 |
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter}); |
252 |
is($sql, "insert into table (key1, key2) values (?, ?);"); |
|
253 |
is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
254 |
|
|
255 |
# Update set |
|
256 |
$dbi = DBI::Custom->new; |
|
add tests
|
257 |
$tmpl = "update table {update}"; |
258 |
$params = {update => {key1 => 'a', key2 => 'b'}}; |
|
cleanup
|
259 |
|
260 |
$dbi->filters(filter => sub { |
|
261 |
my ($key, $value) = @_; |
|
262 |
if ($key eq 'key1' && $value eq 'a') { |
|
263 |
return uc $value; |
|
add tests
|
264 |
} |
cleanup
|
265 |
return $value; |
266 |
}); |
|
add tests
|
267 |
|
cleanup
|
268 |
($sql, @bind_values) = $dbi->_create_sql($tmpl, $params, $dbi->filters->{filter}); |
269 |
is($sql, "update table set key1 = ?, key2 = ?;"); |
|
270 |
is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
add tests
|
271 |
|
cleanup#
|
272 |
$dbi->disconnnect; |
add test module
|
273 |
|
add tests
|
274 |
# Tag 'in' is easy to wrong |
275 |
|