DBIx-Custom / t / dbix-custom-core.t /
Newer Older
232 lines | 6.866kb
packaging one directory
yuki-kimoto authored on 2009-11-16
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',
version 0.0901
yuki-kimoto authored on 2009-12-17
29
    options => {d => 1, e => 2},
packaging one directory
yuki-kimoto authored on 2009-11-16
30
    filters => {
31
        f => 3,
32
    },
33
    bind_filter => 'f',
34
    fetch_filter => 'g',
35
    result_class => 'g',
version 0.0901
yuki-kimoto authored on 2009-12-17
36
    sql_tmpl => $SQL_TMPL->{0},
packaging one directory
yuki-kimoto authored on 2009-11-16
37
);
38
is_deeply($dbi,{user => 'a', database => 'a', password => 'b', data_source => 'c', 
version 0.0901
yuki-kimoto authored on 2009-12-17
39
                options => {d => 1, e => 2}, filters => {f => 3}, bind_filter => 'f',
packaging one directory
yuki-kimoto authored on 2009-11-16
40
                fetch_filter => 'g', result_class => 'g',
version 0.0901
yuki-kimoto authored on 2009-12-17
41
                sql_tmpl => $SQL_TMPL->{0}}, $test);
packaging one directory
yuki-kimoto authored on 2009-11-16
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')
version 0.0901
yuki-kimoto authored on 2009-12-17
55
      ->options({d => 1, e => 2})
packaging one directory
yuki-kimoto authored on 2009-11-16
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')
version 0.0901
yuki-kimoto authored on 2009-12-17
65
      ->sql_tmpl($SQL_TMPL->{0})
packaging one directory
yuki-kimoto authored on 2009-11-16
66
    ;
67
}
68
$dbi = DBIx::Custom::T1->new(
69
    user => 'ao',
70
    database => 'ao',
71
    password => 'bo',
72
    data_source => 'co',
version 0.0901
yuki-kimoto authored on 2009-12-17
73
    options => {do => 10, eo => 20},
packaging one directory
yuki-kimoto authored on 2009-11-16
74
    filters => {
75
        fo => 30,
76
    },
77
    formats => {
78
        fo => 30,
79
    },
80
    bind_filter => 'fo',
81
    fetch_filter => 'go',
82
    result_class => 'ho',
version 0.0901
yuki-kimoto authored on 2009-12-17
83
    sql_tmpl => $SQL_TMPL->{0},
packaging one directory
yuki-kimoto authored on 2009-11-16
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");
version 0.0901
yuki-kimoto authored on 2009-12-17
89
is_deeply($dbi->options, {do => 10, eo => 20}, "$test : options");
packaging one directory
yuki-kimoto authored on 2009-11-16
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");
version 0.0901
yuki-kimoto authored on 2009-12-17
95
is($dbi->sql_tmpl->tag_start, 0, "$test : sql_tmpl");
packaging one directory
yuki-kimoto authored on 2009-11-16
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");
version 0.0901
yuki-kimoto authored on 2009-12-17
104
is_deeply($dbi->options, {d => 1, e => 2}, "$test : options");
packaging one directory
yuki-kimoto authored on 2009-11-16
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");
version 0.0901
yuki-kimoto authored on 2009-12-17
110
is($dbi->sql_tmpl->tag_start, 0, "$test : sql_tmpl");
packaging one directory
yuki-kimoto authored on 2009-11-16
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");
version 0.0901
yuki-kimoto authored on 2009-12-17
124
is_deeply($dbi->options, {d => 1, e => 2}, "$test : options");
packaging one directory
yuki-kimoto authored on 2009-11-16
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");
version 0.0901
yuki-kimoto authored on 2009-12-17
130
is($dbi->sql_tmpl->tag_start, 0, "$test sql_tmpl");
packaging one directory
yuki-kimoto authored on 2009-11-16
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')
version 0.0901
yuki-kimoto authored on 2009-12-17
144
      ->options({do => 10, eo => 20})
packaging one directory
yuki-kimoto authored on 2009-11-16
145
      ->filters(
146
        fo => 30
147
      )
148
      ->formats(
149
        fo => 30
150
      )
151
      ->bind_filter('fo')
152
      ->fetch_filter('go')
153
      ->result_class('ho')
version 0.0901
yuki-kimoto authored on 2009-12-17
154
      ->sql_tmpl($SQL_TMPL->{1})
packaging one directory
yuki-kimoto authored on 2009-11-16
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");
version 0.0901
yuki-kimoto authored on 2009-12-17
162
is_deeply($dbi->options, {do => 10, eo => 20}, "$test : options");
packaging one directory
yuki-kimoto authored on 2009-11-16
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");
version 0.0901
yuki-kimoto authored on 2009-12-17
168
is($dbi->sql_tmpl->tag_start, 1, "$test : sql_tmpl");
packaging one directory
yuki-kimoto authored on 2009-11-16
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',
version 0.0901
yuki-kimoto authored on 2009-12-17
178
    options => {d => 1, e => 2},
packaging one directory
yuki-kimoto authored on 2009-11-16
179
    filters => {
180
        f => 3,
181
    },
182
    formats => {
183
        f => 3,
184
    },
185
    bind_filter => 'f',
186
    fetch_filter => 'g',
187
    result_class => 'h',
version 0.0901
yuki-kimoto authored on 2009-12-17
188
    sql_tmpl => $SQL_TMPL->{2},
packaging one directory
yuki-kimoto authored on 2009-11-16
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");
version 0.0901
yuki-kimoto authored on 2009-12-17
194
is_deeply($dbi->options, {d => 1, e => 2}, "$test : options");
packaging one directory
yuki-kimoto authored on 2009-11-16
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");
version 0.0901
yuki-kimoto authored on 2009-12-17
200
is($dbi->sql_tmpl->tag_start, 2, "$test : sql_tmpl");
packaging one directory
yuki-kimoto authored on 2009-11-16
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");
version 0.0901
yuki-kimoto authored on 2009-12-17
221

            
222
__END__
223
test 'Accessor';
224
$dbi = DBIx::Custom->new;
225
$dbi->options(opt1 => 1, opt2 => 2);
226
is_deeply(scalar $dbi->options, {opt1 => 1, opt2 => 2}, "$test : options");
227

            
228
$dbi->no_bind_filters('a', 'b');
229
is_deeply(scalar $dbi->no_bind_filters, ['a', 'b'], "$test: no_bind_filters");
230

            
231
$dbi->no_fetch_filters('a', 'b');
232
is_deeply(scalar $dbi->no_fetch_filters, ['a', 'b'], "$test: no_fetch_filters");