DBIx-Custom / t / 01-core.t /
Newer Older
278 lines | 7.997kb
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(
15
        connect_info => {
cleanup
yuki-kimoto authored on 2009-10-22
16
          user => 'a',
17
          password => 'b',
18
          data_source => 'c',
19
          options => {d => 1, e => 2}
add test
yuki-kimoto authored on 2009-10-16
20
        },
21
        filters => {
22
            f => 3,
23
        },
24
        bind_filter => 'f',
25
        fetch_filter => 'g',
cleanup
yuki-kimoto authored on 2009-10-22
26
        result_class => 'g',
27
        sql_template => $sql_tmpl1,
28
        valid_connect_info => {i => 1}
add test
yuki-kimoto authored on 2009-10-16
29
    );
add tests
yuki-kimoto authored on 2009-10-18
30
    is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', 
31
                    options => {d => 1, e => 2}}, filters => {f => 3}, bind_filter => 'f',
add tests
yuki-kimoto authored on 2009-10-25
32
                    fetch_filter => 'g', result_class => 'g',
cleanup
yuki-kimoto authored on 2009-10-22
33
                    sql_template => $sql_tmpl1, valid_connect_info => {i => 1}}, 'new');
add test
yuki-kimoto authored on 2009-10-16
34
    
35
    isa_ok($dbi, 'DBI::Custom');
36
}
37

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

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

            
89
{
90
    my $dbi = DBI::Custom::T1->new;
91
    
add tests
yuki-kimoto authored on 2009-10-25
92
    is_deeply($dbi->connect_info, {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}});
93
    is_deeply({$dbi->filters}, {f => 3});
94
    is($dbi->bind_filter, 'f');
95
    is($dbi->fetch_filter, 'g');
96
    is($dbi->result_class, 'DBI::Custom::Result');
97
    is_deeply({$dbi->valid_connect_info},{p => 1});
98
    is($dbi->sql_template->upper_case, 0);
add test
yuki-kimoto authored on 2009-10-16
99
    isa_ok($dbi, 'DBI::Custom::T1');
100
    
101
}
102

            
103
{
104
    package DBI::Custom::T1_2;
105
    use base 'DBI::Custom::T1';
106
}
107

            
108
{
109
    my $dbi = DBI::Custom::T1_2->new;
110
    
add tests
yuki-kimoto authored on 2009-10-25
111
    is_deeply($dbi->connect_info, {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}});
112
    is_deeply(scalar $dbi->filters, {f => 3});
113
    is($dbi->bind_filter, 'f');
114
    is($dbi->fetch_filter, 'g');
115
    is($dbi->result_class, 'DBI::Custom::Result');
116
    is_deeply({$dbi->valid_connect_info}, {p => 1});
117
    is($dbi->sql_template->upper_case, 0);
add test
yuki-kimoto authored on 2009-10-16
118
    
119
    isa_ok($dbi, 'DBI::Custom::T1_2');
120
}
121

            
122
{
123
    package DBI::Custom::T1_3;
124
    use base 'DBI::Custom::T1';
125
    
add tests
yuki-kimoto authored on 2009-10-25
126
    my $class = __PACKAGE__;
add test
yuki-kimoto authored on 2009-10-16
127
        
add tests
yuki-kimoto authored on 2009-10-25
128
    $class
cleanup
yuki-kimoto authored on 2009-10-22
129
      ->connect_info(
130
        user => 'ao',
131
        password => 'bo',
132
        data_source => 'co',
133
        options => {do => 10, eo => 20}
134
      )
135
      ->filters(
136
        fo => 30
137
      )
138
      ->bind_filter('fo')
139
      ->fetch_filter('go')
140
      ->result_class('ho')
141
      ->sql_template($sql_tmpl2)
142
      ->valid_connect_info({p => 3})
143
    ;
add test
yuki-kimoto authored on 2009-10-16
144
}
145

            
146
{
147
    my $dbi = DBI::Custom::T1_3->new;
148
    
add tests
yuki-kimoto authored on 2009-10-25
149
    is_deeply($dbi->connect_info, {user => 'ao', password => 'bo', data_source => 'co', options => {do => 10, eo => 20}});
150
    is_deeply(scalar $dbi->filters, {fo => 30});
151
    is($dbi->bind_filter, 'fo');
152
    is($dbi->fetch_filter, 'go');
153
    is($dbi->result_class, 'ho');
154
    is_deeply(scalar $dbi->valid_connect_info, {p => 3});
155
    is($dbi->sql_template->upper_case, 1);
add test
yuki-kimoto authored on 2009-10-16
156
    
157
    isa_ok($dbi, 'DBI::Custom::T1_3');
158
}
159

            
160
{
161
    my $dbi = DBI::Custom::T1_3->new(
162
        connect_info => {
163
            user => 'a',
164
            password => 'b',
165
            data_source => 'c',
166
            options => {d => 1, e => 2}
167
        },
168
        filters => {
169
            f => 3,
170
        },
171
        bind_filter => 'f',
172
        fetch_filter => 'g',
cleanup
yuki-kimoto authored on 2009-10-22
173
        result_class => 'h',
174
        sql_template => $sql_tmpl3,
175
        valid_connect_info => {p => 4}
add test
yuki-kimoto authored on 2009-10-16
176
    );
177
    
cleanup
yuki-kimoto authored on 2009-10-22
178
    my $sql_tmpl = delete $dbi->{sql_template};
179
    is($sql_tmpl->upper_case, 2);
add tests
yuki-kimoto authored on 2009-10-18
180
    is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}},
add tests
yuki-kimoto authored on 2009-10-25
181
                    filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g', result_class => 'h',
cleanup
yuki-kimoto authored on 2009-10-22
182
                    valid_connect_info => {p => 4}}, 'new');
add test
yuki-kimoto authored on 2009-10-16
183
    
184
    isa_ok($dbi, 'DBI::Custom');
185
}
186

            
add test
yuki-kimoto authored on 2009-10-17
187

            
188
{
189
    my $dbi = DBI::Custom->new(
190
        connect_info => {
191
            no_exist => 1,
192
        }
193
    );
194
    eval{$dbi->connect};
add test
yuki-kimoto authored on 2009-10-16
195
    
add test module
yuki-kimoto authored on 2009-10-19
196
    like($@, qr/connect_info 'no_exist' is wrong name/, 'no exist');
add test
yuki-kimoto authored on 2009-10-16
197
}
198

            
try varioud way
yuki-kimoto authored on 2009-10-17
199
{
200
    my $dbi = DBI::Custom->new;
201
    my $tmpl   = "select * from table where {= title};";
202
    my $values = {title => 'a'};
203
    my ($sql, @bind) = $dbi->create_sql($tmpl, $values);
try various way
yuki-kimoto authored on 2009-10-17
204
    is($sql, "select * from table where title = ?;", 'sql template');
205
    is_deeply(\@bind, ['a'], 'sql template bind' );
206
}
207

            
208
{
209
    # Expand place holer
210
    my $dbi = DBI::Custom->new;
211
    my $tmpl   = "select * from table where {= k1} && {<> k2} && {< k3} && {> k4} && {>= k5} && {<= k6} && {like k7}";
212
    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
213
    
try various way
yuki-kimoto authored on 2009-10-17
214
    $dbi->filters(filter => sub {
215
        my ($key, $value) = @_;
216
        if ($key eq 'k1' && $value eq 'a') {
217
            return uc $value;
218
        }
219
        return $value;
220
    });
221
    
222
    my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter});
223
    
try various way
yuki-kimoto authored on 2009-10-17
224
    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
225
    is_deeply(\@bind, ['A', 'b', 'c', 'd', 'e', 'f', 'g'], 'sql template bind2' );
226
}
227

            
228
{
229
    # Expand place holer upper case
230
    my $dbi = DBI::Custom->new;
231
    $dbi->sql_template->upper_case(1);
232
    my $tmpl   = "select * from table where {like k7}";
233
    my $values = {k7 => 'g'};
234
    
235
    my ($sql, @bind) = $dbi->create_sql($tmpl, $values);
236
    is($sql, "select * from table where k7 LIKE ?;", 'sql template2');
237
    is_deeply(\@bind, ['g'], 'sql template bind2' );
238
}
239

            
240

            
241
{
242
    # Insert values
243
    my $dbi = DBI::Custom->new;
244
    my $tmpl   = "insert into table {insert_values}";
245
    my $values = {insert_values => {k1 => 'a', k2 => 'b'}};
246
    
247
    $dbi->filters(filter => sub {
248
        my ($key, $value) = @_;
249
        if ($key eq 'k1' && $value eq 'a') {
250
            return uc $value;
251
        }
252
        return $value;
253
    });
254
        
255
    my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter});
256
    is($sql, "insert into table (k1, k2) values (?, ?);");
257
    is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
258
}
259

            
260
{
261
    # Update set
262
    my $dbi = DBI::Custom->new;
263
    my $tmpl   = "update table {update_set}";
264
    my $values = {update_set => {k1 => 'a', k2 => 'b'}};
265

            
266
    $dbi->filters(filter => sub {
267
        my ($key, $value) = @_;
268
        if ($key eq 'k1' && $value eq 'a') {
269
            return uc $value;
270
        }
271
        return $value;
272
    });
273
        
274
    my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter});
275
    is($sql, "update table set k1 = ?, k2 = ?;");
276
    is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
try varioud way
yuki-kimoto authored on 2009-10-17
277
}
278