DBIx-Custom / t / 01-core.t /
Newer Older
265 lines | 7.222kb
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
# user password database
9
our ($U, $P, $D) = connect_info();
10

            
11

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

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

            
75
{
76
    my $dbi = DBI::Custom::T1->new;
77
    
78
    is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}}                       ,filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g'}, 'new custom class');
79
    
80
    isa_ok($dbi, 'DBI::Custom::T1');
81
    
82
}
83

            
84
{
85
    package DBI::Custom::T1_2;
86
    use base 'DBI::Custom::T1';
87
}
88

            
89
{
90
    my $dbi = DBI::Custom::T1_2->new;
91
    
92
    is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}}                       ,filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g'}, 'new custom class inherit');
93
    
94
    isa_ok($dbi, 'DBI::Custom::T1_2');
95
}
96

            
97
{
98
    package DBI::Custom::T1_3;
99
    use base 'DBI::Custom::T1';
100
    
101
    __PACKAGE__->initialize_model(sub {
102
        my $model = shift;
103
        
104
        $model
105
          ->connect_info(
106
            user => 'ao',
107
            password => 'bo',
108
            data_source => 'co',
109
            options => {do => 10, eo => 20}
110
          )
111
          ->filters(
112
            fo => 30
113
          )
114
          ->bind_filter('fo')
115
          ->fetch_filter('go')
116
          ->dbh('eo')
117
    });
118
    
119
}
120

            
121
{
122
    my $dbi = DBI::Custom::T1_3->new;
123
    
124
    is_deeply($dbi,{connect_info => {user => 'ao', password => 'bo', data_source => 'co', options => {do => 10, eo => 20}}                       ,filters => {fo => 30}, bind_filter => 'fo', fetch_filter => 'go'}, 'new custom class');
125
    
126
    isa_ok($dbi, 'DBI::Custom::T1_3');
127
}
128

            
129
{
130
    my $dbi = DBI::Custom::T1_3->new(
131
        connect_info => {
132
            user => 'a',
133
            password => 'b',
134
            data_source => 'c',
135
            options => {d => 1, e => 2}
136
        },
137
        filters => {
138
            f => 3,
139
        },
140
        bind_filter => 'f',
141
        fetch_filter => 'g',
142
        dbh => 'e',
143
    );
144
    
145
    is_deeply($dbi,{connect_info => {user => 'a', password => 'b', data_source => 'c', options => {d => 1, e => 2}}                       ,filters => {f => 3}, bind_filter => 'f', fetch_filter => 'g', dbh => 'e'}, 'new');
146
    
147
    isa_ok($dbi, 'DBI::Custom');
148
}
149

            
150
{
151
    my $dbi = DBI::Custom->new(
152
        connect_info => {
153
            user => $U,
154
            password => $P,
155
            data_source => "dbi:mysql:$D"
156
        }
157
    );
158
    $dbi->connect;
159
    
160
    ok(blessed $dbi->dbh);
161
    can_ok($dbi->dbh, qw/prepare/);
add test
yuki-kimoto authored on 2009-10-17
162
}
163

            
164
{
165
    my $dbi = DBI::Custom->new(
166
        connect_info => {
167
            no_exist => 1,
168
        }
169
    );
170
    eval{$dbi->connect};
add test
yuki-kimoto authored on 2009-10-16
171
    
add test
yuki-kimoto authored on 2009-10-17
172
    like($@, qr/connect_info 'no_exist' is invald/, 'no exist');
add test
yuki-kimoto authored on 2009-10-16
173
}
174

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

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

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

            
216

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

            
236
{
237
    # Update set
238
    my $dbi = DBI::Custom->new;
239
    my $tmpl   = "update table {update_set}";
240
    my $values = {update_set => {k1 => 'a', k2 => 'b'}};
241

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

            
add test
yuki-kimoto authored on 2009-10-16
255
sub connect_info {
256
    my $file = 'password.tmp';
257
    open my $fh, '<', $file
258
      or return;
259
    
260
    my ($user, $password, $database) = split(/\s/, (<$fh>)[0]);
261
    
262
    close $fh;
263
    
264
    return ($user, $password, $database);
265
}