add test
|
1 |
use Test::More 'no_plan'; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 | ||
5 |
use DBI::Custom; |
|
add tests
|
6 |
use DBI::Custom::SQL::Template; |
add test
|
7 | |
cleanup
|
8 |
# Function for test name |
9 |
my $test; |
|
10 |
sub test { |
|
11 |
$test = shift; |
|
12 |
} |
|
13 | ||
cleanup
|
14 |
# Variables for test |
15 |
our $SQL_TMPL = { |
|
16 |
0 => DBI::Custom::SQL::Template->new->tag_start(0), |
|
17 |
1 => DBI::Custom::SQL::Template->new->tag_start(1), |
|
18 |
2 => DBI::Custom::SQL::Template->new->tag_start(2) |
|
19 |
}; |
|
cleanup
|
20 |
my $dbi; |
21 | ||
cleanup
|
22 | |
cleanup
|
23 |
test 'Constructor'; |
cleanup
|
24 |
$dbi = DBI::Custom->new( |
25 |
user => 'a', |
|
26 |
password => 'b', |
|
27 |
data_source => 'c', |
|
28 |
dbi_options => {d => 1, e => 2}, |
|
29 |
filters => { |
|
30 |
f => 3, |
|
31 |
}, |
|
32 |
bind_filter => 'f', |
|
33 |
fetch_filter => 'g', |
|
34 |
result_class => 'g', |
|
cleanup
|
35 |
sql_template => $SQL_TMPL->{0}, |
cleanup
|
36 |
); |
37 |
is_deeply($dbi,{user => 'a', password => 'b', data_source => 'c', |
|
38 |
dbi_options => {d => 1, e => 2}, filters => {f => 3}, bind_filter => 'f', |
|
39 |
fetch_filter => 'g', result_class => 'g', |
|
cleanup
|
40 |
sql_template => $SQL_TMPL->{0}}, $test); |
cleanup
|
41 |
isa_ok($dbi, 'DBI::Custom'); |
add test
|
42 | |
cleanup
|
43 | |
cleanup
|
44 |
test 'Sub class constructor'; |
add test
|
45 |
{ |
46 |
package DBI::Custom::T1; |
|
47 |
use base 'DBI::Custom'; |
|
48 |
|
|
cleanup
|
49 |
__PACKAGE__ |
update document
|
50 |
->user('a') |
51 |
->password('b') |
|
52 |
->data_source('c') |
|
cleanup
|
53 |
->dbi_options({d => 1, e => 2}) |
cleanup
|
54 |
->filters( |
55 |
f => 3 |
|
56 |
) |
|
add formats add_format
|
57 |
->formats( |
58 |
f => 3 |
|
59 |
) |
|
cleanup
|
60 |
->bind_filter('f') |
61 |
->fetch_filter('g') |
|
62 |
->result_class('DBI::Custom::Result') |
|
cleanup
|
63 |
->sql_template($SQL_TMPL->{0}) |
cleanup
|
64 |
; |
add test
|
65 |
} |
cleanup
|
66 |
$dbi = DBI::Custom::T1->new( |
67 |
user => 'ao', |
|
68 |
password => 'bo', |
|
69 |
data_source => 'co', |
|
70 |
dbi_options => {do => 10, eo => 20}, |
|
71 |
filters => { |
|
72 |
fo => 30, |
|
73 |
}, |
|
add formats add_format
|
74 |
formats => { |
75 |
fo => 30, |
|
76 |
}, |
|
cleanup
|
77 |
bind_filter => 'fo', |
78 |
fetch_filter => 'go', |
|
79 |
result_class => 'ho', |
|
cleanup
|
80 |
sql_template => $SQL_TMPL->{0}, |
cleanup
|
81 |
); |
82 |
is($dbi->user, 'ao', "$test : user"); |
|
83 |
is($dbi->password, 'bo', "$test : passowr"); |
|
84 |
is($dbi->data_source, 'co', "$test : data_source"); |
|
85 |
is_deeply($dbi->dbi_options, {do => 10, eo => 20}, "$test : dbi_options"); |
|
86 |
is_deeply(scalar $dbi->filters, {fo => 30}, "$test : filters"); |
|
add formats add_format
|
87 |
is_deeply(scalar $dbi->formats, {fo => 30}, "$test : formats"); |
cleanup
|
88 |
is($dbi->bind_filter, 'fo', "$test : bind_filter"); |
89 |
is($dbi->fetch_filter, 'go', "$test : fetch_filter"); |
|
90 |
is($dbi->result_class, 'ho', "$test : result_class"); |
|
91 |
is($dbi->sql_template->tag_start, 0, "$test : sql_template"); |
|
92 |
isa_ok($dbi, 'DBI::Custom::T1'); |
|
add test
|
93 | |
cleanup
|
94 |
test 'Sub class constructor default'; |
95 |
$dbi = DBI::Custom::T1->new; |
|
96 |
is($dbi->user, 'a', "$test : user"); |
|
97 |
is($dbi->password, 'b', "$test : password"); |
|
98 |
is($dbi->data_source, 'c', "$test : data_source"); |
|
99 |
is_deeply($dbi->dbi_options, {d => 1, e => 2}, "$test : dbi_options"); |
|
100 |
is_deeply({$dbi->filters}, {f => 3}, "$test : filters"); |
|
add formats add_format
|
101 |
is_deeply({$dbi->formats}, {f => 3}, "$test : formats"); |
cleanup
|
102 |
is($dbi->bind_filter, 'f', "$test : bind_filter"); |
103 |
is($dbi->fetch_filter, 'g', "$test : fetch_filter"); |
|
104 |
is($dbi->result_class, 'DBI::Custom::Result', "$test : result_class"); |
|
105 |
is($dbi->sql_template->tag_start, 0, "$test : sql_template"); |
|
106 |
isa_ok($dbi, 'DBI::Custom::T1'); |
|
add test
|
107 | |
cleanup
|
108 | |
cleanup
|
109 |
test 'Sub sub class constructor default'; |
add test
|
110 |
{ |
111 |
package DBI::Custom::T1_2; |
|
112 |
use base 'DBI::Custom::T1'; |
|
113 |
} |
|
cleanup
|
114 |
$dbi = DBI::Custom::T1_2->new; |
115 |
is($dbi->user, 'a', "$test : user"); |
|
116 |
is($dbi->password, 'b', "$test : passowrd"); |
|
117 |
is($dbi->data_source, 'c', "$test : data_source"); |
|
118 |
is_deeply($dbi->dbi_options, {d => 1, e => 2}, "$test : dbi_options"); |
|
119 |
is_deeply(scalar $dbi->filters, {f => 3}, "$test : filters"); |
|
add formats add_format
|
120 |
is_deeply(scalar $dbi->formats, {f => 3}, "$test : formats"); |
cleanup
|
121 |
is($dbi->bind_filter, 'f', "$test : bind_filter"); |
122 |
is($dbi->fetch_filter, 'g', "$test : fetch_filter"); |
|
123 |
is($dbi->result_class, 'DBI::Custom::Result', "$test : result_class"); |
|
124 |
is($dbi->sql_template->tag_start, 0, "$test sql_template"); |
|
125 |
isa_ok($dbi, 'DBI::Custom::T1_2'); |
|
add test
|
126 | |
cleanup
|
127 | |
128 |
test 'Customized sub class constructor default'; |
|
add test
|
129 |
{ |
130 |
package DBI::Custom::T1_3; |
|
131 |
use base 'DBI::Custom::T1'; |
|
132 |
|
|
cleanup
|
133 |
__PACKAGE__ |
update document
|
134 |
->user('ao') |
135 |
->password('bo') |
|
136 |
->data_source('co') |
|
cleanup
|
137 |
->dbi_options({do => 10, eo => 20}) |
cleanup
|
138 |
->filters( |
139 |
fo => 30 |
|
140 |
) |
|
add formats add_format
|
141 |
->formats( |
142 |
fo => 30 |
|
143 |
) |
|
cleanup
|
144 |
->bind_filter('fo') |
145 |
->fetch_filter('go') |
|
146 |
->result_class('ho') |
|
cleanup
|
147 |
->sql_template($SQL_TMPL->{1}) |
cleanup
|
148 |
; |
add test
|
149 |
} |
cleanup
|
150 |
$dbi = DBI::Custom::T1_3->new; |
151 |
is($dbi->user, 'ao', "$test : user"); |
|
152 |
is($dbi->password, 'bo', "$test : password"); |
|
153 |
is($dbi->data_source, 'co', "$test : data_source"); |
|
154 |
is_deeply($dbi->dbi_options, {do => 10, eo => 20}, "$test : dbi_options"); |
|
155 |
is_deeply(scalar $dbi->filters, {fo => 30}, "$test : filters"); |
|
add formats add_format
|
156 |
is_deeply(scalar $dbi->formats, {fo => 30}, "$test : formats"); |
cleanup
|
157 |
is($dbi->bind_filter, 'fo', "$test : bind_filter"); |
158 |
is($dbi->fetch_filter, 'go', "$test : fetch_filter"); |
|
159 |
is($dbi->result_class, 'ho', "$test : result_class"); |
|
160 |
is($dbi->sql_template->tag_start, 1, "$test : sql_template"); |
|
161 |
isa_ok($dbi, 'DBI::Custom::T1_3'); |
|
add test
|
162 | |
163 | ||
cleanup
|
164 |
test 'Customized sub class constructor'; |
165 |
$dbi = DBI::Custom::T1_3->new( |
|
166 |
user => 'a', |
|
167 |
password => 'b', |
|
168 |
data_source => 'c', |
|
169 |
dbi_options => {d => 1, e => 2}, |
|
170 |
filters => { |
|
171 |
f => 3, |
|
172 |
}, |
|
add formats add_format
|
173 |
formats => { |
174 |
f => 3, |
|
175 |
}, |
|
cleanup
|
176 |
bind_filter => 'f', |
177 |
fetch_filter => 'g', |
|
178 |
result_class => 'h', |
|
cleanup
|
179 |
sql_template => $SQL_TMPL->{2}, |
cleanup
|
180 |
); |
181 |
is($dbi->user, 'a', "$test : user"); |
|
182 |
is($dbi->password, 'b', "$test : password"); |
|
183 |
is($dbi->data_source, 'c', "$test : data_source"); |
|
184 |
is_deeply($dbi->dbi_options, {d => 1, e => 2}, "$test : dbi_options"); |
|
185 |
is_deeply({$dbi->filters}, {f => 3}, "$test : filters"); |
|
add formats add_format
|
186 |
is_deeply({$dbi->formats}, {f => 3}, "$test : formats"); |
cleanup
|
187 |
is($dbi->bind_filter, 'f', "$test : bind_filter"); |
188 |
is($dbi->fetch_filter, 'g', "$test : fetch_filter"); |
|
189 |
is($dbi->result_class, 'h', "$test : result_class"); |
|
190 |
is($dbi->sql_template->tag_start, 2, "$test : sql_template"); |
|
191 |
isa_ok($dbi, 'DBI::Custom'); |
|
add test
|
192 | |
cleanup
|
193 | |
194 |
test 'add_filters'; |
|
195 |
$dbi = DBI::Custom->new; |
|
196 |
$dbi->add_filter(a => sub {1}); |
|
197 |
is($dbi->filters->{a}->(), 1, $test); |
|
198 | ||
add formats add_format
|
199 |
test 'add_formats'; |
200 |
$dbi = DBI::Custom->new; |
|
201 |
$dbi->add_format(a => sub {1}); |
|
202 |
is($dbi->formats->{a}->(), 1, $test); |
|
203 |