DBIx-Custom / t / 02-sqlite.t /
Newer Older
165 lines | 3.967kb
add test
yuki-kimoto authored on 2009-10-18
1
use Test::More;
2
use strict;
3
use warnings;
add test module
yuki-kimoto authored on 2009-10-19
4
use DBI;
add test
yuki-kimoto authored on 2009-10-18
5

            
6
BEGIN {
7
    eval { require DBD::SQLite; 1 }
8
        or plan skip_all => 'DBD::SQLite required';
9
    eval { DBD::SQLite->VERSION >= 1 }
10
        or plan skip_all => 'DBD::SQLite >= 1.00 required';
11

            
12
    plan 'no_plan';
13
    use_ok('DBI::Custom');
14
}
15

            
add test module
yuki-kimoto authored on 2009-10-19
16
package Test::DBI::Custom;
17
use Object::Simple;
add test
yuki-kimoto authored on 2009-10-18
18

            
add test module
yuki-kimoto authored on 2009-10-19
19
sub dbi : Attr {}
add test
yuki-kimoto authored on 2009-10-18
20

            
add test module
yuki-kimoto authored on 2009-10-19
21
sub new {
22
    my $self = shift->SUPER::new;
23
    my $dbi = DBI::Custom->new->connect_info(data_source => 'dbi:SQLite:dbname=:memory:');
24
    
25
    $dbi->connect;
26
    $self->dbi($dbi);
27
    return $self;
28
}
29

            
30
sub create_table {
31
    my ($self, $create_table) = @_;
32
    $self->dbi->query_raw_sql($create_table);
33
    return $self;
34
}
35

            
36
sub create_table1 {
37
    my $self = shift;
38
    $self->create_table("create table t1 (k1 char(255), k2 char(255), k3 char(255), k4 char(255), k5 char(255));");
39
    return $self;
40
}
41

            
42
sub insert {
43
    my ($self, @values_list) = @_;
44
    my $table = ref $values_list[0] ? '' : shift;
45
    $table ||= 't1';
46
    
47
    foreach my $values (@values_list) {
48
        my $sql = $self->dbi->query(
49
            "insert into $table {insert_values}", {insert_values => $values}
50
        );
51
    }
52
    return $self;
53
}
54

            
55
sub test {
56
    my ($self, $code) = @_;
57
    $code->($self->dbi);
58
}
59

            
60
Object::Simple->build_class;
61

            
62
package main;
63
my $t = Test::DBI::Custom->new;
64

            
65
$t->new->create_table1->insert({k1 => 1, k2 => 2}, {k1 => 3, k2 => 4})->test(sub {
66
    my $dbi = shift;
add test
yuki-kimoto authored on 2009-10-18
67
    
cleanup
yuki-kimoto authored on 2009-10-19
68
    my $r;     # resultset
69
    my @rows;
add tests
yuki-kimoto authored on 2009-10-19
70
    my $rows;
cleanup
yuki-kimoto authored on 2009-10-19
71
    
add tests
yuki-kimoto authored on 2009-10-19
72
    #----------
cleanup
yuki-kimoto authored on 2009-10-19
73
    $r = $dbi->query("select k1, k2 from t1");
74
    
75
    @rows = ();
76
    while (my $row = $r->fetch) {
77
        push @rows, [@$row];
78
    }
add tests
yuki-kimoto authored on 2009-10-19
79
    is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch');
80
    
81
    
82
    #----------
add tests
yuki-kimoto authored on 2009-10-19
83
    $r = $dbi->query("select k1, k2 from t1");
84
    
85
    @rows = ();
86
    while (my @row = $r->fetch) {
87
        push @rows, [@row];
88
    }
add tests
yuki-kimoto authored on 2009-10-19
89
    is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch list context');
cleanup
yuki-kimoto authored on 2009-10-19
90
    
add tests
yuki-kimoto authored on 2009-10-19
91
    
add tests
yuki-kimoto authored on 2009-10-19
92
    #-----------
cleanup
yuki-kimoto authored on 2009-10-19
93
    $r = $dbi->query("select k1, k2 from t1;");
94
    
95
    @rows = ();
96
    while (my $row = $r->fetch_hash) {
97
        push @rows, {%$row};
98
    }
add tests
yuki-kimoto authored on 2009-10-19
99
    is_deeply(\@rows, [{k1 => 1, k2 => 2}, {k1 => 3, k2 => 4}], 'fetch_hash');
add tests
yuki-kimoto authored on 2009-10-19
100
    
add tests
yuki-kimoto authored on 2009-10-19
101
    
102
    #-----------
add tests
yuki-kimoto authored on 2009-10-19
103
    $r = $dbi->query("select k1, k2 from t1;");
cleanup
yuki-kimoto authored on 2009-10-19
104
    
add tests
yuki-kimoto authored on 2009-10-19
105
    @rows = ();
106
    while (my %row = $r->fetch_hash) {
107
        push @rows, {%row};
108
    }
add tests
yuki-kimoto authored on 2009-10-19
109
    is_deeply(\@rows, [{k1 => 1, k2 => 2}, {k1 => 3, k2 => 4}], 'fetch hash list context');
cleanup
yuki-kimoto authored on 2009-10-19
110
    
111
    
add tests
yuki-kimoto authored on 2009-10-19
112
    #-----------
add tests
yuki-kimoto authored on 2009-10-19
113
    $r = $dbi->query("select k1, k2 from t1");
114
    
115
    $rows = $r->fetch_all;
add tests
yuki-kimoto authored on 2009-10-19
116
    is_deeply($rows, [[1, 2], [3, 4]], 'fetch_all');
add tests
yuki-kimoto authored on 2009-10-19
117
    
118
    
add tests
yuki-kimoto authored on 2009-10-19
119
    #------------
120
    $r = $dbi->query("select k1, k2 from t1");
121
    
122
    @rows = $r->fetch_all;
123
    is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch_all list context');
124
    
125
    
126
    #------------
127
    $r = $dbi->query("select k1, k2 from t1");
128
    
129
    @rows = $r->fetch_all_hash;
130
    is_deeply($rows, [[1, 2], [3, 4]], 'fetch_all_hash');
cleanup
yuki-kimoto authored on 2009-10-19
131
    
132
    
add tests
yuki-kimoto authored on 2009-10-19
133
    #-------------
134
    $r = $dbi->query("select k1, k2 from t1");
135
    
136
    @rows = $r->fetch_all;
137
    is_deeply(\@rows, [[1, 2], [3, 4]], 'fetch_all_hash list context');
138
    
139
    
140
    #---------------------------------------------------------------------
add test
yuki-kimoto authored on 2009-10-18
141
    $dbi->fetch_filter(sub {
142
        my ($key, $value) = @_;
143
        if ($key eq 'k1' && $value == 1 ) {
144
            return $value * 3;
145
        }
146
        return $value;
147
    });
148
    
add tests
yuki-kimoto authored on 2009-10-19
149
    #-----------------------------------
cleanup
yuki-kimoto authored on 2009-10-19
150
    $r = $dbi->query("select k1, k2 from t1");
add test
yuki-kimoto authored on 2009-10-18
151
    
add tests
yuki-kimoto authored on 2009-10-19
152
    $rows = $r->fetch_all;
153
    
154
    is_deeply($rows, [[3, 2], [3, 4]], 'fetch_filter array');
add test
yuki-kimoto authored on 2009-10-18
155
    
add tests
yuki-kimoto authored on 2009-10-19
156
    
157
    #----------------------------------
158
    $r = $dbi->query("select k1, k2 from t1");
159
    
160
    $rows = $r->fetch_all_hash;
161
    
162
    is_deeply($rows, [{k1 => 3, k2 => 2}, {k1 => 3, k2 => 4}], 'fetch_filter hash');
163

            
add test module
yuki-kimoto authored on 2009-10-19
164
});
165