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 | ||
8 |
# user password database |
|
9 |
our ($U, $P, $D) = connect_info(); |
|
10 | ||
11 | ||
12 |
{ |
|
13 |
my $dbi = DBI::Custom->new( |
|
14 |
connect_info => { |
|
15 |
user => 'a', |
|
16 |
password => 'b', |
|
17 |
data_source => 'c', |
|
18 |
options => {d => 1, e => 2} |
|
19 |
}, |
|
20 |
filters => { |
|
21 |
f => 3, |
|
22 |
}, |
|
23 |
bind_filter => 'f', |
|
24 |
fetch_filter => 'g', |
|
25 |
dbh => 'e', |
|
26 |
); |
|
27 |
|
|
28 |
is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}} ,filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g', dbh => 'e'}, 'new'); |
|
29 |
|
|
30 |
isa_ok($dbi, 'DBI::Custom'); |
|
31 |
} |
|
32 | ||
33 |
{ |
|
34 |
package DBI::Custom::T1; |
|
35 |
use base 'DBI::Custom'; |
|
36 |
|
|
37 |
__PACKAGE__->initialize_model(sub { |
|
38 |
my $model = shift; |
|
39 |
|
|
40 |
$model |
|
41 |
->connect_info( |
|
42 |
user => 'a', |
|
43 |
password => 'b', |
|
44 |
data_source => 'c', |
|
45 |
options => {d => 1, e => 2} |
|
46 |
) |
|
47 |
->filters( |
|
48 |
f => 3 |
|
49 |
) |
|
50 |
->bind_filter('f') |
|
51 |
->fetch_filter('g') |
|
52 |
->dbh('e') |
|
53 |
}); |
|
54 |
} |
|
55 |
{ |
|
56 |
my $dbi = DBI::Custom::T1->new( |
|
57 |
connect_info => { |
|
58 |
user => 'ao', |
|
59 |
password => 'bo', |
|
60 |
data_source => 'co', |
|
61 |
options => {do => 10, eo => 20} |
|
62 |
}, |
|
63 |
filters => { |
|
64 |
fo => 30, |
|
65 |
}, |
|
66 |
bind_filter => 'fo', |
|
67 |
fetch_filter => 'go', |
|
68 |
); |
|
69 |
|
|
70 |
is_deeply($dbi,{connect_info => {user => 'ao', password => 'bo', data_source => 'co', options => {do => 10, eo => 20}} ,filters => {fo => 30}, bind_filter => 'fo', fetch_filter => 'go'}, 'new arguments'); |
|
71 |
|
|
72 |
isa_ok($dbi, 'DBI::Custom::T1'); |
|
73 |
} |
|
74 | ||
75 |
{ |
|
76 |
my $dbi = DBI::Custom::T1->new; |
|
77 |
|
|
78 |
is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}} ,filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g'}, 'new custom class'); |
|
79 |
|
|
80 |
isa_ok($dbi, 'DBI::Custom::T1'); |
|
81 |
|
|
82 |
} |
|
83 | ||
84 |
{ |
|
85 |
package DBI::Custom::T1_2; |
|
86 |
use base 'DBI::Custom::T1'; |
|
87 |
} |
|
88 | ||
89 |
{ |
|
90 |
my $dbi = DBI::Custom::T1_2->new; |
|
91 |
|
|
92 |
is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}} ,filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g'}, 'new custom class inherit'); |
|
93 |
|
|
94 |
isa_ok($dbi, 'DBI::Custom::T1_2'); |
|
95 |
} |
|
96 | ||
97 |
{ |
|
98 |
package DBI::Custom::T1_3; |
|
99 |
use base 'DBI::Custom::T1'; |
|
100 |
|
|
101 |
__PACKAGE__->initialize_model(sub { |
|
102 |
my $model = shift; |
|
103 |
|
|
104 |
$model |
|
105 |
->connect_info( |
|
106 |
user => 'ao', |
|
107 |
password => 'bo', |
|
108 |
data_source => 'co', |
|
109 |
options => {do => 10, eo => 20} |
|
110 |
) |
|
111 |
->filters( |
|
112 |
fo => 30 |
|
113 |
) |
|
114 |
->bind_filter('fo') |
|
115 |
->fetch_filter('go') |
|
116 |
->dbh('eo') |
|
117 |
}); |
|
118 |
|
|
119 |
} |
|
120 | ||
121 |
{ |
|
122 |
my $dbi = DBI::Custom::T1_3->new; |
|
123 |
|
|
124 |
is_deeply($dbi,{connect_info => {user => 'ao', password => 'bo', data_source => 'co', options => {do => 10, eo => 20}} ,filters => {fo => 30}, bind_filter => 'fo', fetch_filter => 'go'}, 'new custom class'); |
|
125 |
|
|
126 |
isa_ok($dbi, 'DBI::Custom::T1_3'); |
|
127 |
} |
|
128 | ||
129 |
{ |
|
130 |
my $dbi = DBI::Custom::T1_3->new( |
|
131 |
connect_info => { |
|
132 |
user => 'a', |
|
133 |
password => 'b', |
|
134 |
data_source => 'c', |
|
135 |
options => {d => 1, e => 2} |
|
136 |
}, |
|
137 |
filters => { |
|
138 |
f => 3, |
|
139 |
}, |
|
140 |
bind_filter => 'f', |
|
141 |
fetch_filter => 'g', |
|
142 |
dbh => 'e', |
|
143 |
); |
|
144 |
|
|
145 |
is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}} ,filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g', dbh => 'e'}, 'new'); |
|
146 |
|
|
147 |
isa_ok($dbi, 'DBI::Custom'); |
|
148 |
} |
|
149 | ||
150 |
{ |
|
151 |
my $dbi = DBI::Custom->new( |
|
152 |
connect_info => { |
|
153 |
user => $U, |
|
154 |
password => $P, |
|
155 |
data_source => "dbi:mysql:$D" |
|
156 |
} |
|
157 |
); |
|
158 |
$dbi->connect; |
|
159 |
|
|
160 |
ok(blessed $dbi->dbh); |
|
161 |
can_ok($dbi->dbh, qw/prepare/); |
|
add test
|
162 |
} |
163 | ||
164 |
{ |
|
165 |
my $dbi = DBI::Custom->new( |
|
166 |
connect_info => { |
|
167 |
no_exist => 1, |
|
168 |
} |
|
169 |
); |
|
170 |
eval{$dbi->connect}; |
|
add test
|
171 |
|
add test
|
172 |
like($@, qr/connect_info 'no_exist' is invald/, 'no exist'); |
add test
|
173 |
} |
174 | ||
try varioud way
|
175 |
{ |
176 |
my $dbi = DBI::Custom->new; |
|
177 |
my $tmpl = "select * from table where {= title};"; |
|
178 |
my $values = {title => 'a'}; |
|
179 |
my ($sql, @bind) = $dbi->create_sql($tmpl, $values); |
|
try various way
|
180 |
is($sql, "select * from table where title = ?;", 'sql template'); |
181 |
is_deeply(\@bind, ['a'], 'sql template bind' ); |
|
182 |
} |
|
183 | ||
184 |
{ |
|
185 |
# Expand place holer |
|
186 |
my $dbi = DBI::Custom->new; |
|
187 |
my $tmpl = "select * from table where {= k1} && {<> k2} && {< k3} && {> k4} && {>= k5} && {<= k6} && {like k7}"; |
|
188 |
my $values = {k1 => 'a', k2 => 'b', k3 => 'c', k4 => 'd', k5 => 'e', k6 => 'f', k7 => 'g'}; |
|
try varioud way
|
189 |
|
try various way
|
190 |
my ($sql, @bind) = $dbi->create_sql($tmpl, $values); |
191 |
is($sql, "select * from table where k1 = ? && k2 <> ? && k3 < ? && k4 > ? && k5 >= ? && k6 <= ? && k7 like ?;", 'sql template2'); |
|
192 |
is_deeply(\@bind, ['a', 'b', 'c', 'd', 'e', 'f', 'g'], 'sql template bind2' ); |
|
try varioud way
|
193 |
} |
194 | ||
add test
|
195 |
sub connect_info { |
196 |
my $file = 'password.tmp'; |
|
197 |
open my $fh, '<', $file |
|
198 |
or return; |
|
199 |
|
|
200 |
my ($user, $password, $database) = split(/\s/, (<$fh>)[0]); |
|
201 |
|
|
202 |
close $fh; |
|
203 |
|
|
204 |
return ($user, $password, $database); |
|
205 |
} |