DBIx-Custom / t / 01-core.t /
Newer Older
260 lines | 7.124kb
add test
yuki-kimoto authored on 2009-10-16
1
use Test::More 'no_plan';
2
use strict;
3
use warnings;
4

            
5
use DBI::Custom;
6
use Scalar::Util qw/blessed/;
add tests
yuki-kimoto authored on 2009-10-25
7
use DBI::Custom::SQL::Template;
add test
yuki-kimoto authored on 2009-10-16
8

            
cleanup
yuki-kimoto authored on 2009-10-22
9
my $sql_tmpl1 = DBI::Custom::SQL::Template->new->upper_case(0);
10
my $sql_tmpl2 = DBI::Custom::SQL::Template->new->upper_case(1);
11
my $sql_tmpl3 = DBI::Custom::SQL::Template->new->upper_case(2);
12

            
add test
yuki-kimoto authored on 2009-10-16
13
{
14
    my $dbi = DBI::Custom->new(
update document
yuki-kimoto authored on 2009-10-27
15
        user => 'a',
16
        password => 'b',
17
        data_source => 'c',
18
        dbi_option => {d => 1, e => 2},
add test
yuki-kimoto authored on 2009-10-16
19
        filters => {
20
            f => 3,
21
        },
22
        bind_filter => 'f',
23
        fetch_filter => 'g',
cleanup
yuki-kimoto authored on 2009-10-22
24
        result_class => 'g',
25
        sql_template => $sql_tmpl1,
add test
yuki-kimoto authored on 2009-10-16
26
    );
update document
yuki-kimoto authored on 2009-10-27
27
    is_deeply($dbi,{user => 'a', password => 'b', data_source => 'c', 
28
                    dbi_option => {d => 1, e => 2}, filters => {f => 3}, bind_filter => 'f',
add tests
yuki-kimoto authored on 2009-10-25
29
                    fetch_filter => 'g', result_class => 'g',
update document
yuki-kimoto authored on 2009-10-27
30
                    sql_template => $sql_tmpl1}, 'new');
add test
yuki-kimoto authored on 2009-10-16
31
    
32
    isa_ok($dbi, 'DBI::Custom');
33
}
34

            
cleanup
yuki-kimoto authored on 2009-10-22
35

            
add test
yuki-kimoto authored on 2009-10-16
36
{
37
    package DBI::Custom::T1;
38
    use base 'DBI::Custom';
39
    
add tests
yuki-kimoto authored on 2009-10-25
40
    my $class = __PACKAGE__;
cleanup
yuki-kimoto authored on 2009-10-22
41
    
add tests
yuki-kimoto authored on 2009-10-25
42
    $class
update document
yuki-kimoto authored on 2009-10-27
43
      ->user('a')
44
      ->password('b')
45
      ->data_source('c')
46
      ->dbi_option({d => 1, e => 2})
cleanup
yuki-kimoto authored on 2009-10-22
47
      ->filters(
48
          f => 3
49
      )
50
      ->bind_filter('f')
51
      ->fetch_filter('g')
52
      ->result_class('DBI::Custom::Result')
53
      ->sql_template($sql_tmpl1)
54
    ;
add test
yuki-kimoto authored on 2009-10-16
55
}
56
{
57
    my $dbi = DBI::Custom::T1->new(
update document
yuki-kimoto authored on 2009-10-27
58
        user => 'ao',
59
        password => 'bo',
60
        data_source => 'co',
61
        dbi_option => {do => 10, eo => 20},
add test
yuki-kimoto authored on 2009-10-16
62
        filters => {
63
            fo => 30,
64
        },
65
        bind_filter => 'fo',
66
        fetch_filter => 'go',
cleanup
yuki-kimoto authored on 2009-10-22
67
        result_class => 'ho',
68
        sql_template => $sql_tmpl1,
add test
yuki-kimoto authored on 2009-10-16
69
    );
cleanup
yuki-kimoto authored on 2009-10-22
70
    my $sql_tmpl = delete $dbi->{sql_template};
71
    is($sql_tmpl->upper_case, 0);
update document
yuki-kimoto authored on 2009-10-27
72
    is_deeply($dbi,{ user => 'ao', password => 'bo', data_source => 'co', dbi_option => {do => 10, eo => 20},
cleanup
yuki-kimoto authored on 2009-10-22
73
                    ,filters => {fo => 30}, bind_filter => 'fo', fetch_filter => 'go', result_class => 'ho',
74
                    }, 'new arguments');
add test
yuki-kimoto authored on 2009-10-16
75
    
76
    isa_ok($dbi, 'DBI::Custom::T1');
77
}
78

            
79
{
80
    my $dbi = DBI::Custom::T1->new;
81
    
update document
yuki-kimoto authored on 2009-10-27
82
    is($dbi->user, 'a');
83
    is($dbi->password, 'b');
84
    is($dbi->data_source, 'c');
85
    is_deeply($dbi->dbi_option, {d => 1, e => 2});
add tests
yuki-kimoto authored on 2009-10-25
86
    is_deeply({$dbi->filters}, {f => 3});
87
    is($dbi->bind_filter, 'f');
88
    is($dbi->fetch_filter, 'g');
89
    is($dbi->result_class, 'DBI::Custom::Result');
90
    is($dbi->sql_template->upper_case, 0);
add test
yuki-kimoto authored on 2009-10-16
91
    isa_ok($dbi, 'DBI::Custom::T1');
92
    
93
}
94

            
95
{
96
    package DBI::Custom::T1_2;
97
    use base 'DBI::Custom::T1';
98
}
99

            
100
{
101
    my $dbi = DBI::Custom::T1_2->new;
102
    
update document
yuki-kimoto authored on 2009-10-27
103
    is($dbi->user, 'a');
104
    is($dbi->password, 'b');
105
    is($dbi->data_source, 'c');
106
    is_deeply($dbi->dbi_option, {d => 1, e => 2});
add tests
yuki-kimoto authored on 2009-10-25
107
    is_deeply(scalar $dbi->filters, {f => 3});
108
    is($dbi->bind_filter, 'f');
109
    is($dbi->fetch_filter, 'g');
110
    is($dbi->result_class, 'DBI::Custom::Result');
111
    is($dbi->sql_template->upper_case, 0);
add test
yuki-kimoto authored on 2009-10-16
112
    
113
    isa_ok($dbi, 'DBI::Custom::T1_2');
114
}
115

            
116
{
117
    package DBI::Custom::T1_3;
118
    use base 'DBI::Custom::T1';
119
    
add tests
yuki-kimoto authored on 2009-10-25
120
    my $class = __PACKAGE__;
add test
yuki-kimoto authored on 2009-10-16
121
        
add tests
yuki-kimoto authored on 2009-10-25
122
    $class
update document
yuki-kimoto authored on 2009-10-27
123
      ->user('ao')
124
      ->password('bo')
125
      ->data_source('co')
126
      ->dbi_option({do => 10, eo => 20})
cleanup
yuki-kimoto authored on 2009-10-22
127
      ->filters(
128
        fo => 30
129
      )
130
      ->bind_filter('fo')
131
      ->fetch_filter('go')
132
      ->result_class('ho')
133
      ->sql_template($sql_tmpl2)
134
    ;
add test
yuki-kimoto authored on 2009-10-16
135
}
136

            
137
{
138
    my $dbi = DBI::Custom::T1_3->new;
139
    
update document
yuki-kimoto authored on 2009-10-27
140
    is($dbi->user, 'ao');
141
    is($dbi->password, 'bo');
142
    is($dbi->data_source, 'co');
143
    is_deeply($dbi->dbi_option, {do => 10, eo => 20});
add tests
yuki-kimoto authored on 2009-10-25
144
    is_deeply(scalar $dbi->filters, {fo => 30});
145
    is($dbi->bind_filter, 'fo');
146
    is($dbi->fetch_filter, 'go');
147
    is($dbi->result_class, 'ho');
148
    is($dbi->sql_template->upper_case, 1);
add test
yuki-kimoto authored on 2009-10-16
149
    
150
    isa_ok($dbi, 'DBI::Custom::T1_3');
151
}
152

            
153
{
154
    my $dbi = DBI::Custom::T1_3->new(
update document
yuki-kimoto authored on 2009-10-27
155
        user => 'a',
156
        password => 'b',
157
        data_source => 'c',
158
        dbi_option => {d => 1, e => 2},
add test
yuki-kimoto authored on 2009-10-16
159
        filters => {
160
            f => 3,
161
        },
162
        bind_filter => 'f',
163
        fetch_filter => 'g',
cleanup
yuki-kimoto authored on 2009-10-22
164
        result_class => 'h',
165
        sql_template => $sql_tmpl3,
add test
yuki-kimoto authored on 2009-10-16
166
    );
167
    
update document
yuki-kimoto authored on 2009-10-27
168
    is($dbi->user, 'a');
169
    is($dbi->password, 'b');
170
    is($dbi->data_source, 'c');
171
    is_deeply($dbi->dbi_option, {d => 1, e => 2});
172
    is_deeply({$dbi->filters}, {f => 3});
173
    is($dbi->bind_filter, 'f');
174
    is($dbi->fetch_filter, 'g');
175
    is($dbi->result_class, 'h');
176
    is($dbi->sql_template->upper_case, 2);
add test
yuki-kimoto authored on 2009-10-16
177
    
178
    isa_ok($dbi, 'DBI::Custom');
179
}
180

            
try varioud way
yuki-kimoto authored on 2009-10-17
181
{
182
    my $dbi = DBI::Custom->new;
183
    my $tmpl   = "select * from table where {= title};";
184
    my $values = {title => 'a'};
update document
yuki-kimoto authored on 2009-10-27
185
    my ($sql, @bind) = $dbi->_create_sql($tmpl, $values);
try various way
yuki-kimoto authored on 2009-10-17
186
    is($sql, "select * from table where title = ?;", 'sql template');
187
    is_deeply(\@bind, ['a'], 'sql template bind' );
188
}
189

            
190
{
191
    # Expand place holer
192
    my $dbi = DBI::Custom->new;
193
    my $tmpl   = "select * from table where {= k1} && {<> k2} && {< k3} && {> k4} && {>= k5} && {<= k6} && {like k7}";
194
    my $values = {k1 => 'a', k2 => 'b', k3 => 'c', k4 => 'd', k5 => 'e', k6 => 'f', k7 => 'g'};
try varioud way
yuki-kimoto authored on 2009-10-17
195
    
try various way
yuki-kimoto authored on 2009-10-17
196
    $dbi->filters(filter => sub {
197
        my ($key, $value) = @_;
198
        if ($key eq 'k1' && $value eq 'a') {
199
            return uc $value;
200
        }
201
        return $value;
202
    });
203
    
update document
yuki-kimoto authored on 2009-10-27
204
    my ($sql, @bind) = $dbi->_create_sql($tmpl, $values, $dbi->filters->{filter});
try various way
yuki-kimoto authored on 2009-10-17
205
    
try various way
yuki-kimoto authored on 2009-10-17
206
    is($sql, "select * from table where k1 = ? && k2 <> ? && k3 < ? && k4 > ? && k5 >= ? && k6 <= ? && k7 like ?;", 'sql template2');
try various way
yuki-kimoto authored on 2009-10-17
207
    is_deeply(\@bind, ['A', 'b', 'c', 'd', 'e', 'f', 'g'], 'sql template bind2' );
208
}
209

            
210
{
211
    # Expand place holer upper case
212
    my $dbi = DBI::Custom->new;
213
    $dbi->sql_template->upper_case(1);
214
    my $tmpl   = "select * from table where {like k7}";
215
    my $values = {k7 => 'g'};
216
    
update document
yuki-kimoto authored on 2009-10-27
217
    my ($sql, @bind) = $dbi->_create_sql($tmpl, $values);
try various way
yuki-kimoto authored on 2009-10-17
218
    is($sql, "select * from table where k7 LIKE ?;", 'sql template2');
219
    is_deeply(\@bind, ['g'], 'sql template bind2' );
220
}
221

            
222

            
223
{
224
    # Insert values
225
    my $dbi = DBI::Custom->new;
226
    my $tmpl   = "insert into table {insert_values}";
227
    my $values = {insert_values => {k1 => 'a', k2 => 'b'}};
228
    
229
    $dbi->filters(filter => sub {
230
        my ($key, $value) = @_;
231
        if ($key eq 'k1' && $value eq 'a') {
232
            return uc $value;
233
        }
234
        return $value;
235
    });
236
        
update document
yuki-kimoto authored on 2009-10-27
237
    my ($sql, @bind) = $dbi->_create_sql($tmpl, $values, $dbi->filters->{filter});
try various way
yuki-kimoto authored on 2009-10-17
238
    is($sql, "insert into table (k1, k2) values (?, ?);");
239
    is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
240
}
241

            
242
{
243
    # Update set
244
    my $dbi = DBI::Custom->new;
245
    my $tmpl   = "update table {update_set}";
246
    my $values = {update_set => {k1 => 'a', k2 => 'b'}};
247

            
248
    $dbi->filters(filter => sub {
249
        my ($key, $value) = @_;
250
        if ($key eq 'k1' && $value eq 'a') {
251
            return uc $value;
252
        }
253
        return $value;
254
    });
255
        
update document
yuki-kimoto authored on 2009-10-27
256
    my ($sql, @bind) = $dbi->_create_sql($tmpl, $values, $dbi->filters->{filter});
try various way
yuki-kimoto authored on 2009-10-17
257
    is($sql, "update table set k1 = ?, k2 = ?;");
258
    is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
try varioud way
yuki-kimoto authored on 2009-10-17
259
}
260