add test
|
1 |
use Test::More 'no_plan'; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 | ||
5 |
use DBI::Custom; |
|
6 |
use Scalar::Util qw/blessed/; |
|
7 | ||
cleanup
|
8 |
my $sql_tmpl1 = DBI::Custom::SQL::Template->new->upper_case(0); |
9 |
my $sql_tmpl2 = DBI::Custom::SQL::Template->new->upper_case(1); |
|
10 |
my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2); |
|
11 | ||
add test
|
12 |
{ |
13 |
my $dbi = DBI::Custom->new( |
|
14 |
connect_info => { |
|
cleanup
|
15 |
user => 'a', |
16 |
password => 'b', |
|
17 |
data_source => 'c', |
|
18 |
options => {d => 1, e => 2} |
|
add test
|
19 |
}, |
20 |
filters => { |
|
21 |
f => 3, |
|
22 |
}, |
|
23 |
bind_filter => 'f', |
|
24 |
fetch_filter => 'g', |
|
25 |
dbh => 'e', |
|
cleanup
|
26 |
result_class => 'g', |
27 |
sql_template => $sql_tmpl1, |
|
28 |
valid_connect_info => {i => 1} |
|
add test
|
29 |
); |
add tests
|
30 |
is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', |
31 |
options => {d => 1, e => 2}}, filters => {f => 3}, bind_filter => 'f', |
|
cleanup
|
32 |
fetch_filter => 'g', dbh => 'e', result_class => 'g', |
33 |
sql_template => $sql_tmpl1, valid_connect_info => {i => 1}}, 'new'); |
|
add test
|
34 |
|
35 |
isa_ok($dbi, 'DBI::Custom'); |
|
36 |
} |
|
37 | ||
cleanup
|
38 | |
add test
|
39 |
{ |
40 |
package DBI::Custom::T1; |
|
41 |
use base 'DBI::Custom'; |
|
42 |
|
|
cleanup
|
43 |
my $prototype = __PACKAGE__->prototype; |
44 |
|
|
45 |
$prototype |
|
46 |
->connect_info( |
|
47 |
user => 'a', |
|
48 |
password => 'b', |
|
49 |
data_source => 'c', |
|
50 |
options => {d => 1, e => 2} |
|
51 |
) |
|
52 |
->filters( |
|
53 |
f => 3 |
|
54 |
) |
|
55 |
->bind_filter('f') |
|
56 |
->fetch_filter('g') |
|
57 |
->dbh('e') |
|
58 |
->result_class('DBI::Custom::Result') |
|
59 |
->sql_template($sql_tmpl1) |
|
60 |
->valid_connect_info({p => 1}) |
|
61 |
; |
|
add test
|
62 |
} |
63 |
{ |
|
64 |
my $dbi = DBI::Custom::T1->new( |
|
65 |
connect_info => { |
|
66 |
user => 'ao', |
|
67 |
password => 'bo', |
|
68 |
data_source => 'co', |
|
69 |
options => {do => 10, eo => 20} |
|
70 |
}, |
|
71 |
filters => { |
|
72 |
fo => 30, |
|
73 |
}, |
|
74 |
bind_filter => 'fo', |
|
75 |
fetch_filter => 'go', |
|
cleanup
|
76 |
result_class => 'ho', |
77 |
sql_template => $sql_tmpl1, |
|
78 |
valid_connect_info => {io => 1} |
|
add test
|
79 |
); |
cleanup
|
80 |
my $sql_tmpl = delete $dbi->{sql_template}; |
81 |
is($sql_tmpl->upper_case, 0); |
|
add tests
|
82 |
is_deeply($dbi,{connect_info => {user => 'ao', password => 'bo', data_source => 'co', options => {do => 10, eo => 20}} |
cleanup
|
83 |
,filters => {fo => 30}, bind_filter => 'fo', fetch_filter => 'go', result_class => 'ho', |
84 |
,valid_connect_info => {io => 1} |
|
85 |
}, 'new arguments'); |
|
add test
|
86 |
|
87 |
isa_ok($dbi, 'DBI::Custom::T1'); |
|
88 |
} |
|
89 | ||
90 |
{ |
|
91 |
my $dbi = DBI::Custom::T1->new; |
|
92 |
|
|
cleanup
|
93 |
my $sql_tmpl = delete $dbi->{sql_template}; |
add tests
|
94 |
is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}}, |
cleanup
|
95 |
filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g', result_class => 'DBI::Custom::Result', |
96 |
valid_connect_info => {p => 1}}, 'new custom class'); |
|
add test
|
97 |
|
98 |
isa_ok($dbi, 'DBI::Custom::T1'); |
|
99 |
|
|
100 |
} |
|
101 | ||
102 |
{ |
|
103 |
package DBI::Custom::T1_2; |
|
104 |
use base 'DBI::Custom::T1'; |
|
105 |
} |
|
106 | ||
107 |
{ |
|
108 |
my $dbi = DBI::Custom::T1_2->new; |
|
109 |
|
|
cleanup
|
110 |
my $sql_tmpl = delete $dbi->{sql_template}; |
111 |
is($sql_tmpl->upper_case, 0); |
|
add tests
|
112 |
is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}}, |
cleanup
|
113 |
filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g', result_class => 'DBI::Custom::Result', |
114 |
valid_connect_info => {p => 1}}, 'new custom class inherit'); |
|
add test
|
115 |
|
116 |
isa_ok($dbi, 'DBI::Custom::T1_2'); |
|
117 |
} |
|
118 | ||
119 |
{ |
|
120 |
package DBI::Custom::T1_3; |
|
121 |
use base 'DBI::Custom::T1'; |
|
122 |
|
|
cleanup
|
123 |
my $prototype = __PACKAGE__->prototype; |
add test
|
124 |
|
cleanup
|
125 |
$prototype |
126 |
->connect_info( |
|
127 |
user => 'ao', |
|
128 |
password => 'bo', |
|
129 |
data_source => 'co', |
|
130 |
options => {do => 10, eo => 20} |
|
131 |
) |
|
132 |
->filters( |
|
133 |
fo => 30 |
|
134 |
) |
|
135 |
->bind_filter('fo') |
|
136 |
->fetch_filter('go') |
|
137 |
->dbh('eo') |
|
138 |
->result_class('ho') |
|
139 |
->sql_template($sql_tmpl2) |
|
140 |
->valid_connect_info({p => 3}) |
|
141 |
; |
|
add test
|
142 |
} |
143 | ||
144 |
{ |
|
145 |
my $dbi = DBI::Custom::T1_3->new; |
|
146 |
|
|
cleanup
|
147 |
my $sql_tmpl = delete $dbi->{sql_template}; |
148 |
is($sql_tmpl->upper_case, 1); |
|
add tests
|
149 |
is_deeply($dbi,{connect_info => {user => 'ao', password => 'bo', data_source => 'co', options => {do => 10, eo => 20}}, |
cleanup
|
150 |
filters => {fo => 30}, bind_filter => 'fo', fetch_filter => 'go', result_class => 'ho', |
151 |
valid_connect_info => {p => 3}}, 'new custom class'); |
|
add test
|
152 |
|
153 |
isa_ok($dbi, 'DBI::Custom::T1_3'); |
|
154 |
} |
|
155 | ||
156 |
{ |
|
157 |
my $dbi = DBI::Custom::T1_3->new( |
|
158 |
connect_info => { |
|
159 |
user => 'a', |
|
160 |
password => 'b', |
|
161 |
data_source => 'c', |
|
162 |
options => {d => 1, e => 2} |
|
163 |
}, |
|
164 |
filters => { |
|
165 |
f => 3, |
|
166 |
}, |
|
167 |
bind_filter => 'f', |
|
168 |
fetch_filter => 'g', |
|
169 |
dbh => 'e', |
|
cleanup
|
170 |
result_class => 'h', |
171 |
sql_template => $sql_tmpl3, |
|
172 |
valid_connect_info => {p => 4} |
|
add test
|
173 |
); |
174 |
|
|
cleanup
|
175 |
my $sql_tmpl = delete $dbi->{sql_template}; |
176 |
is($sql_tmpl->upper_case, 2); |
|
add tests
|
177 |
is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}}, |
cleanup
|
178 |
filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g', dbh => 'e', result_class => 'h', |
179 |
valid_connect_info => {p => 4}}, 'new'); |
|
add test
|
180 |
|
181 |
isa_ok($dbi, 'DBI::Custom'); |
|
182 |
} |
|
183 | ||
add test
|
184 | |
185 |
{ |
|
186 |
my $dbi = DBI::Custom->new( |
|
187 |
connect_info => { |
|
188 |
no_exist => 1, |
|
189 |
} |
|
190 |
); |
|
191 |
eval{$dbi->connect}; |
|
add test
|
192 |
|
add test module
|
193 |
like($@, qr/connect_info 'no_exist' is wrong name/, 'no exist'); |
add test
|
194 |
} |
195 | ||
try varioud way
|
196 |
{ |
197 |
my $dbi = DBI::Custom->new; |
|
198 |
my $tmpl = "select * from table where {= title};"; |
|
199 |
my $values = {title => 'a'}; |
|
200 |
my ($sql, @bind) = $dbi->create_sql($tmpl, $values); |
|
try various way
|
201 |
is($sql, "select * from table where title = ?;", 'sql template'); |
202 |
is_deeply(\@bind, ['a'], 'sql template bind' ); |
|
203 |
} |
|
204 | ||
205 |
{ |
|
206 |
# Expand place holer |
|
207 |
my $dbi = DBI::Custom->new; |
|
208 |
my $tmpl = "select * from table where {= k1} && {<> k2} && {< k3} && {> k4} && {>= k5} && {<= k6} && {like k7}"; |
|
209 |
my $values = {k1 => 'a', k2 => 'b', k3 => 'c', k4 => 'd', k5 => 'e', k6 => 'f', k7 => 'g'}; |
|
try varioud way
|
210 |
|
try various way
|
211 |
$dbi->filters(filter => sub { |
212 |
my ($key, $value) = @_; |
|
213 |
if ($key eq 'k1' && $value eq 'a') { |
|
214 |
return uc $value; |
|
215 |
} |
|
216 |
return $value; |
|
217 |
}); |
|
218 |
|
|
219 |
my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter}); |
|
220 |
|
|
try various way
|
221 |
is($sql, "select * from table where k1 = ? && k2 <> ? && k3 < ? && k4 > ? && k5 >= ? && k6 <= ? && k7 like ?;", 'sql template2'); |
try various way
|
222 |
is_deeply(\@bind, ['A', 'b', 'c', 'd', 'e', 'f', 'g'], 'sql template bind2' ); |
223 |
} |
|
224 | ||
225 |
{ |
|
226 |
# Expand place holer upper case |
|
227 |
my $dbi = DBI::Custom->new; |
|
228 |
$dbi->sql_template->upper_case(1); |
|
229 |
my $tmpl = "select * from table where {like k7}"; |
|
230 |
my $values = {k7 => 'g'}; |
|
231 |
|
|
232 |
my ($sql, @bind) = $dbi->create_sql($tmpl, $values); |
|
233 |
is($sql, "select * from table where k7 LIKE ?;", 'sql template2'); |
|
234 |
is_deeply(\@bind, ['g'], 'sql template bind2' ); |
|
235 |
} |
|
236 | ||
237 | ||
238 |
{ |
|
239 |
# Insert values |
|
240 |
my $dbi = DBI::Custom->new; |
|
241 |
my $tmpl = "insert into table {insert_values}"; |
|
242 |
my $values = {insert_values => {k1 => 'a', k2 => 'b'}}; |
|
243 |
|
|
244 |
$dbi->filters(filter => sub { |
|
245 |
my ($key, $value) = @_; |
|
246 |
if ($key eq 'k1' && $value eq 'a') { |
|
247 |
return uc $value; |
|
248 |
} |
|
249 |
return $value; |
|
250 |
}); |
|
251 |
|
|
252 |
my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter}); |
|
253 |
is($sql, "insert into table (k1, k2) values (?, ?);"); |
|
254 |
is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
255 |
} |
|
256 | ||
257 |
{ |
|
258 |
# Update set |
|
259 |
my $dbi = DBI::Custom->new; |
|
260 |
my $tmpl = "update table {update_set}"; |
|
261 |
my $values = {update_set => {k1 => 'a', k2 => 'b'}}; |
|
262 | ||
263 |
$dbi->filters(filter => sub { |
|
264 |
my ($key, $value) = @_; |
|
265 |
if ($key eq 'k1' && $value eq 'a') { |
|
266 |
return uc $value; |
|
267 |
} |
|
268 |
return $value; |
|
269 |
}); |
|
270 |
|
|
271 |
my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter}); |
|
272 |
is($sql, "update table set k1 = ?, k2 = ?;"); |
|
273 |
is_deeply(\@bind, ['A', 'b'], 'sql template bind' ); |
|
try varioud way
|
274 |
} |
275 |