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