DBIx-Custom / t / 01-core.t /
Newer Older
249 lines | 6.969kb
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/;
7

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

            
32
{
33
    package DBI::Custom::T1;
34
    use base 'DBI::Custom';
35
    
cleanup
yuki-kimoto authored on 2009-10-21
36
    __PACKAGE__->initialize_class(sub {
try various
yuki-kimoto authored on 2009-10-21
37
        my $class = shift;
add test
yuki-kimoto authored on 2009-10-16
38
        
try various
yuki-kimoto authored on 2009-10-21
39
        $class
add test
yuki-kimoto authored on 2009-10-16
40
          ->connect_info(
41
            user => 'a',
42
            password => 'b',
43
            data_source => 'c',
44
            options => {d => 1, e => 2}
45
          )
46
          ->filters(
47
            f => 3
48
          )
49
          ->bind_filter('f')
50
          ->fetch_filter('g')
51
          ->dbh('e')
52
    });
53
}
54
{
55
    my $dbi = DBI::Custom::T1->new(
56
        connect_info => {
57
            user => 'ao',
58
            password => 'bo',
59
            data_source => 'co',
60
            options => {do => 10, eo => 20}
61
        },
62
        filters => {
63
            fo => 30,
64
        },
65
        bind_filter => 'fo',
66
        fetch_filter => 'go',
add tests
yuki-kimoto authored on 2009-10-18
67
        result_class => 'ho'
add test
yuki-kimoto authored on 2009-10-16
68
    );
69
    
add tests
yuki-kimoto authored on 2009-10-18
70
    is_deeply($dbi,{connect_info => {user => 'ao', password => 'bo', data_source => 'co', options => {do => 10, eo => 20}}
71
                    ,filters => {fo => 30}, bind_filter => 'fo', fetch_filter => 'go', result_class => 'ho'}, 'new arguments');
add test
yuki-kimoto authored on 2009-10-16
72
    
73
    isa_ok($dbi, 'DBI::Custom::T1');
74
}
75

            
76
{
77
    my $dbi = DBI::Custom::T1->new;
78
    
add tests
yuki-kimoto authored on 2009-10-18
79
    is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}},
80
                    filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g', result_class => 'DBI::Custom::Result'}, 'new custom class');
add test
yuki-kimoto authored on 2009-10-16
81
    
82
    isa_ok($dbi, 'DBI::Custom::T1');
83
    
84
}
85

            
86
{
87
    package DBI::Custom::T1_2;
88
    use base 'DBI::Custom::T1';
89
}
90

            
91
{
92
    my $dbi = DBI::Custom::T1_2->new;
93
    
add tests
yuki-kimoto authored on 2009-10-18
94
    is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}},
95
                    filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g', result_class => 'DBI::Custom::Result'}, 'new custom class inherit');
add test
yuki-kimoto authored on 2009-10-16
96
    
97
    isa_ok($dbi, 'DBI::Custom::T1_2');
98
}
99

            
100
{
101
    package DBI::Custom::T1_3;
102
    use base 'DBI::Custom::T1';
103
    
cleanup
yuki-kimoto authored on 2009-10-21
104
    __PACKAGE__->initialize_class(sub {
try various
yuki-kimoto authored on 2009-10-21
105
        my $class = shift;
add test
yuki-kimoto authored on 2009-10-16
106
        
try various
yuki-kimoto authored on 2009-10-21
107
        $class
add test
yuki-kimoto authored on 2009-10-16
108
          ->connect_info(
109
            user => 'ao',
110
            password => 'bo',
111
            data_source => 'co',
112
            options => {do => 10, eo => 20}
113
          )
114
          ->filters(
115
            fo => 30
116
          )
117
          ->bind_filter('fo')
118
          ->fetch_filter('go')
119
          ->dbh('eo')
add tests
yuki-kimoto authored on 2009-10-18
120
          ->result_class('ho');
121
       
add test
yuki-kimoto authored on 2009-10-16
122
    });
123
    
124
}
125

            
126
{
127
    my $dbi = DBI::Custom::T1_3->new;
128
    
add tests
yuki-kimoto authored on 2009-10-18
129
    is_deeply($dbi,{connect_info => {user => 'ao', password => 'bo', data_source => 'co', options => {do => 10, eo => 20}},
130
                    filters => {fo => 30}, bind_filter => 'fo', fetch_filter => 'go', result_class => 'ho'}, 'new custom class');
add test
yuki-kimoto authored on 2009-10-16
131
    
132
    isa_ok($dbi, 'DBI::Custom::T1_3');
133
}
134

            
135
{
136
    my $dbi = DBI::Custom::T1_3->new(
137
        connect_info => {
138
            user => 'a',
139
            password => 'b',
140
            data_source => 'c',
141
            options => {d => 1, e => 2}
142
        },
143
        filters => {
144
            f => 3,
145
        },
146
        bind_filter => 'f',
147
        fetch_filter => 'g',
148
        dbh => 'e',
add tests
yuki-kimoto authored on 2009-10-18
149
        result_class => 'h'
add test
yuki-kimoto authored on 2009-10-16
150
    );
151
    
add tests
yuki-kimoto authored on 2009-10-18
152
    is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}},
153
                    filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g', dbh => 'e', result_class => 'h'}, 'new');
add test
yuki-kimoto authored on 2009-10-16
154
    
155
    isa_ok($dbi, 'DBI::Custom');
156
}
157

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

            
159
{
160
    my $dbi = DBI::Custom->new(
161
        connect_info => {
162
            no_exist => 1,
163
        }
164
    );
165
    eval{$dbi->connect};
add test
yuki-kimoto authored on 2009-10-16
166
    
add test module
yuki-kimoto authored on 2009-10-19
167
    like($@, qr/connect_info 'no_exist' is wrong name/, 'no exist');
add test
yuki-kimoto authored on 2009-10-16
168
}
169

            
try varioud way
yuki-kimoto authored on 2009-10-17
170
{
171
    my $dbi = DBI::Custom->new;
172
    my $tmpl   = "select * from table where {= title};";
173
    my $values = {title => 'a'};
174
    my ($sql, @bind) = $dbi->create_sql($tmpl, $values);
try various way
yuki-kimoto authored on 2009-10-17
175
    is($sql, "select * from table where title = ?;", 'sql template');
176
    is_deeply(\@bind, ['a'], 'sql template bind' );
177
}
178

            
179
{
180
    # Expand place holer
181
    my $dbi = DBI::Custom->new;
182
    my $tmpl   = "select * from table where {= k1} && {<> k2} && {< k3} && {> k4} && {>= k5} && {<= k6} && {like k7}";
183
    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
184
    
try various way
yuki-kimoto authored on 2009-10-17
185
    $dbi->filters(filter => sub {
186
        my ($key, $value) = @_;
187
        if ($key eq 'k1' && $value eq 'a') {
188
            return uc $value;
189
        }
190
        return $value;
191
    });
192
    
193
    my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter});
194
    
try various way
yuki-kimoto authored on 2009-10-17
195
    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
196
    is_deeply(\@bind, ['A', 'b', 'c', 'd', 'e', 'f', 'g'], 'sql template bind2' );
197
}
198

            
199
{
200
    # Expand place holer upper case
201
    my $dbi = DBI::Custom->new;
202
    $dbi->sql_template->upper_case(1);
203
    my $tmpl   = "select * from table where {like k7}";
204
    my $values = {k7 => 'g'};
205
    
206
    my ($sql, @bind) = $dbi->create_sql($tmpl, $values);
207
    is($sql, "select * from table where k7 LIKE ?;", 'sql template2');
208
    is_deeply(\@bind, ['g'], 'sql template bind2' );
209
}
210

            
211

            
212
{
213
    # Insert values
214
    my $dbi = DBI::Custom->new;
215
    my $tmpl   = "insert into table {insert_values}";
216
    my $values = {insert_values => {k1 => 'a', k2 => 'b'}};
217
    
218
    $dbi->filters(filter => sub {
219
        my ($key, $value) = @_;
220
        if ($key eq 'k1' && $value eq 'a') {
221
            return uc $value;
222
        }
223
        return $value;
224
    });
225
        
226
    my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter});
227
    is($sql, "insert into table (k1, k2) values (?, ?);");
228
    is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
229
}
230

            
231
{
232
    # Update set
233
    my $dbi = DBI::Custom->new;
234
    my $tmpl   = "update table {update_set}";
235
    my $values = {update_set => {k1 => 'a', k2 => 'b'}};
236

            
237
    $dbi->filters(filter => sub {
238
        my ($key, $value) = @_;
239
        if ($key eq 'k1' && $value eq 'a') {
240
            return uc $value;
241
        }
242
        return $value;
243
    });
244
        
245
    my ($sql, @bind) = $dbi->create_sql($tmpl, $values, $dbi->filters->{filter});
246
    is($sql, "update table set k1 = ?, k2 = ?;");
247
    is_deeply(\@bind, ['A', 'b'], 'sql template bind' );
try varioud way
yuki-kimoto authored on 2009-10-17
248
}
249