packaging one directory
|
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 |
dbi_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_template => $SQL_TMPL->{0}, |
|
37 |
); |
|
38 |
is_deeply($dbi,{user => 'a', database => 'a', password => 'b', data_source => 'c', |
|
39 |
dbi_options => {d => 1, e => 2}, filters => {f => 3}, bind_filter => 'f', |
|
40 |
fetch_filter => 'g', result_class => 'g', |
|
41 |
sql_template => $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 |
->dbi_options({d => 1, e => 2}) |
|
56 |
->filters( |
|
57 |
f => 3 |
|
58 |
) |
|
59 |
->formats( |
|
60 |
f => 3 |
|
61 |
) |
|
62 |
->bind_filter('f') |
|
63 |
->fetch_filter('g') |
|
64 |
->result_class('DBIx::Custom::Result') |
|
65 |
->sql_template($SQL_TMPL->{0}) |
|
66 |
; |
|
67 |
} |
|
68 |
$dbi = DBIx::Custom::T1->new( |
|
69 |
user => 'ao', |
|
70 |
database => 'ao', |
|
71 |
password => 'bo', |
|
72 |
data_source => 'co', |
|
73 |
dbi_options => {do => 10, eo => 20}, |
|
74 |
filters => { |
|
75 |
fo => 30, |
|
76 |
}, |
|
77 |
formats => { |
|
78 |
fo => 30, |
|
79 |
}, |
|
80 |
bind_filter => 'fo', |
|
81 |
fetch_filter => 'go', |
|
82 |
result_class => 'ho', |
|
83 |
sql_template => $SQL_TMPL->{0}, |
|
84 |
); |
|
85 |
is($dbi->user, 'ao', "$test : user"); |
|
86 |
is($dbi->database, 'ao', "$test : database"); |
|
87 |
is($dbi->password, 'bo', "$test : passowr"); |
|
88 |
is($dbi->data_source, 'co', "$test : data_source"); |
|
89 |
is_deeply($dbi->dbi_options, {do => 10, eo => 20}, "$test : dbi_options"); |
|
90 |
is_deeply(scalar $dbi->filters, {fo => 30}, "$test : filters"); |
|
91 |
is_deeply(scalar $dbi->formats, {fo => 30}, "$test : formats"); |
|
92 |
is($dbi->bind_filter, 'fo', "$test : bind_filter"); |
|
93 |
is($dbi->fetch_filter, 'go', "$test : fetch_filter"); |
|
94 |
is($dbi->result_class, 'ho', "$test : result_class"); |
|
95 |
is($dbi->sql_template->tag_start, 0, "$test : sql_template"); |
|
96 |
isa_ok($dbi, 'DBIx::Custom::T1'); |
|
97 | ||
98 |
test 'Sub class constructor default'; |
|
99 |
$dbi = DBIx::Custom::T1->new; |
|
100 |
is($dbi->user, 'a', "$test : user"); |
|
101 |
is($dbi->database, 'a', "$test : database"); |
|
102 |
is($dbi->password, 'b', "$test : password"); |
|
103 |
is($dbi->data_source, 'c', "$test : data_source"); |
|
104 |
is_deeply($dbi->dbi_options, {d => 1, e => 2}, "$test : dbi_options"); |
|
105 |
is_deeply({$dbi->filters}, {f => 3}, "$test : filters"); |
|
106 |
is_deeply({$dbi->formats}, {f => 3}, "$test : formats"); |
|
107 |
is($dbi->bind_filter, 'f', "$test : bind_filter"); |
|
108 |
is($dbi->fetch_filter, 'g', "$test : fetch_filter"); |
|
109 |
is($dbi->result_class, 'DBIx::Custom::Result', "$test : result_class"); |
|
110 |
is($dbi->sql_template->tag_start, 0, "$test : sql_template"); |
|
111 |
isa_ok($dbi, 'DBIx::Custom::T1'); |
|
112 | ||
113 | ||
114 |
test 'Sub sub class constructor default'; |
|
115 |
{ |
|
116 |
package DBIx::Custom::T1_2; |
|
117 |
use base 'DBIx::Custom::T1'; |
|
118 |
} |
|
119 |
$dbi = DBIx::Custom::T1_2->new; |
|
120 |
is($dbi->user, 'a', "$test : user"); |
|
121 |
is($dbi->database, 'a', "$test : database"); |
|
122 |
is($dbi->password, 'b', "$test : passowrd"); |
|
123 |
is($dbi->data_source, 'c', "$test : data_source"); |
|
124 |
is_deeply($dbi->dbi_options, {d => 1, e => 2}, "$test : dbi_options"); |
|
125 |
is_deeply(scalar $dbi->filters, {f => 3}, "$test : filters"); |
|
126 |
is_deeply(scalar $dbi->formats, {f => 3}, "$test : formats"); |
|
127 |
is($dbi->bind_filter, 'f', "$test : bind_filter"); |
|
128 |
is($dbi->fetch_filter, 'g', "$test : fetch_filter"); |
|
129 |
is($dbi->result_class, 'DBIx::Custom::Result', "$test : result_class"); |
|
130 |
is($dbi->sql_template->tag_start, 0, "$test sql_template"); |
|
131 |
isa_ok($dbi, 'DBIx::Custom::T1_2'); |
|
132 | ||
133 | ||
134 |
test 'Customized sub class constructor default'; |
|
135 |
{ |
|
136 |
package DBIx::Custom::T1_3; |
|
137 |
use base 'DBIx::Custom::T1'; |
|
138 |
|
|
139 |
__PACKAGE__ |
|
140 |
->user('ao') |
|
141 |
->database('ao') |
|
142 |
->password('bo') |
|
143 |
->data_source('co') |
|
144 |
->dbi_options({do => 10, eo => 20}) |
|
145 |
->filters( |
|
146 |
fo => 30 |
|
147 |
) |
|
148 |
->formats( |
|
149 |
fo => 30 |
|
150 |
) |
|
151 |
->bind_filter('fo') |
|
152 |
->fetch_filter('go') |
|
153 |
->result_class('ho') |
|
154 |
->sql_template($SQL_TMPL->{1}) |
|
155 |
; |
|
156 |
} |
|
157 |
$dbi = DBIx::Custom::T1_3->new; |
|
158 |
is($dbi->user, 'ao', "$test : user"); |
|
159 |
is($dbi->database, 'ao', "$test : database"); |
|
160 |
is($dbi->password, 'bo', "$test : password"); |
|
161 |
is($dbi->data_source, 'co', "$test : data_source"); |
|
162 |
is_deeply($dbi->dbi_options, {do => 10, eo => 20}, "$test : dbi_options"); |
|
163 |
is_deeply(scalar $dbi->filters, {fo => 30}, "$test : filters"); |
|
164 |
is_deeply(scalar $dbi->formats, {fo => 30}, "$test : formats"); |
|
165 |
is($dbi->bind_filter, 'fo', "$test : bind_filter"); |
|
166 |
is($dbi->fetch_filter, 'go', "$test : fetch_filter"); |
|
167 |
is($dbi->result_class, 'ho', "$test : result_class"); |
|
168 |
is($dbi->sql_template->tag_start, 1, "$test : sql_template"); |
|
169 |
isa_ok($dbi, 'DBIx::Custom::T1_3'); |
|
170 | ||
171 | ||
172 |
test 'Customized sub class constructor'; |
|
173 |
$dbi = DBIx::Custom::T1_3->new( |
|
174 |
user => 'a', |
|
175 |
database => 'a', |
|
176 |
password => 'b', |
|
177 |
data_source => 'c', |
|
178 |
dbi_options => {d => 1, e => 2}, |
|
179 |
filters => { |
|
180 |
f => 3, |
|
181 |
}, |
|
182 |
formats => { |
|
183 |
f => 3, |
|
184 |
}, |
|
185 |
bind_filter => 'f', |
|
186 |
fetch_filter => 'g', |
|
187 |
result_class => 'h', |
|
188 |
sql_template => $SQL_TMPL->{2}, |
|
189 |
); |
|
190 |
is($dbi->user, 'a', "$test : user"); |
|
191 |
is($dbi->database, 'a', "$test : database"); |
|
192 |
is($dbi->password, 'b', "$test : password"); |
|
193 |
is($dbi->data_source, 'c', "$test : data_source"); |
|
194 |
is_deeply($dbi->dbi_options, {d => 1, e => 2}, "$test : dbi_options"); |
|
195 |
is_deeply({$dbi->filters}, {f => 3}, "$test : filters"); |
|
196 |
is_deeply({$dbi->formats}, {f => 3}, "$test : formats"); |
|
197 |
is($dbi->bind_filter, 'f', "$test : bind_filter"); |
|
198 |
is($dbi->fetch_filter, 'g', "$test : fetch_filter"); |
|
199 |
is($dbi->result_class, 'h', "$test : result_class"); |
|
200 |
is($dbi->sql_template->tag_start, 2, "$test : sql_template"); |
|
201 |
isa_ok($dbi, 'DBIx::Custom'); |
|
202 | ||
203 | ||
204 |
test 'add_filters'; |
|
205 |
$dbi = DBIx::Custom->new; |
|
206 |
$dbi->add_filter(a => sub {1}); |
|
207 |
is($dbi->filters->{a}->(), 1, $test); |
|
208 | ||
209 |
test 'add_formats'; |
|
210 |
$dbi = DBIx::Custom->new; |
|
211 |
$dbi->add_format(a => sub {1}); |
|
212 |
is($dbi->formats->{a}->(), 1, $test); |
|
213 | ||
214 |
test 'filter_off'; |
|
215 |
$dbi = DBIx::Custom->new; |
|
216 |
$dbi->bind_filter('a'); |
|
217 |
$dbi->fetch_filter('b'); |
|
218 |
$dbi->filter_off; |
|
219 |
ok(!$dbi->bind_filter, "$test : bind_filter off"); |
|
220 |
ok(!$dbi->fetch_filter, "$test : fetch_filter off"); |