DBIx-Custom / t / dbix-custom-result-sqlite.t /
Newer Older
222 lines | 6.047kb
packaging one directory
yuki-kimoto authored on 2009-11-16
1
use Test::More;
2
use strict;
3
use warnings;
4
use DBI;
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('DBIx::Custom::Result');
14
}
15

            
16
my $test;
17
sub test {
18
    $test = shift;
19
}
20

            
21
sub query {
22
    my ($dbh, $sql) = @_;
23
    my $sth = $dbh->prepare($sql);
24
    $sth->execute;
25
    return DBIx::Custom::Result->new(sth => $sth);
26
}
27

            
28
my $dbh;
29
my $sql;
30
my $sth;
31
my @row;
32
my $row;
33
my @rows;
34
my $rows;
35
my $result;
add all tests
yuki-kimoto authored on 2010-05-01
36
my $filter;
packaging one directory
yuki-kimoto authored on 2009-11-16
37
my @error;
38
my $error;
39

            
40
$dbh = DBI->connect('dbi:SQLite:dbname=:memory:', undef, undef, {PrintError => 0, RaiseError => 1});
41
$dbh->do("create table table1 (key1 char(255), key2 char(255));");
42
$dbh->do("insert into table1 (key1, key2) values ('1', '2');");
43
$dbh->do("insert into table1 (key1, key2) values ('3', '4');");
44

            
45
$sql = "select key1, key2 from table1";
46

            
47
test 'fetch scalar context';
48
$result = query($dbh, $sql);
49
@rows = ();
50
while (my $row = $result->fetch) {
51
    push @rows, [@$row];
52
}
53
is_deeply(\@rows, [[1, 2], [3, 4]], $test);
54

            
55

            
56
test 'fetch list context';
57
$result = query($dbh, $sql);
58
@rows = ();
59
while (my @row = $result->fetch) {
60
    push @rows, [@row];
61
}
62
is_deeply(\@rows, [[1, 2], [3, 4]], $test);
63

            
64
test 'fetch_hash scalar context';
65
$result = query($dbh, $sql);
66
@rows = ();
67
while (my $row = $result->fetch_hash) {
68
    push @rows, {%$row};
69
}
70
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], $test);
71

            
72

            
73
test 'fetch hash list context';
74
$result = query($dbh, $sql);
75
@rows = ();
76
while (my %row = $result->fetch_hash) {
77
    push @rows, {%row};
78
}
79
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], $test);
80

            
81

            
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
82
test 'fetch_single';
packaging one directory
yuki-kimoto authored on 2009-11-16
83
$result = query($dbh, $sql);
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
84
$row = $result->fetch_single;
packaging one directory
yuki-kimoto authored on 2009-11-16
85
is_deeply($row, [1, 2], "$test : row");
86
$row = $result->fetch;
87
ok(!$row, "$test : finished");
88

            
89

            
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
90
test 'fetch_single list context';
packaging one directory
yuki-kimoto authored on 2009-11-16
91
$result = query($dbh, $sql);
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
92
@row = $result->fetch_single;
packaging one directory
yuki-kimoto authored on 2009-11-16
93
is_deeply([@row], [1, 2], "$test : row");
94
@row = $result->fetch;
95
ok(!@row, "$test : finished");
96

            
97

            
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
98
test 'fetch_hash_single';
packaging one directory
yuki-kimoto authored on 2009-11-16
99
$result = query($dbh, $sql);
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
100
$row = $result->fetch_hash_single;
packaging one directory
yuki-kimoto authored on 2009-11-16
101
is_deeply($row, {key1 => 1, key2 => 2}, "$test : row");
102
$row = $result->fetch_hash;
103
ok(!$row, "$test : finished");
104

            
105

            
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
106
test 'fetch_hash_single list context';
packaging one directory
yuki-kimoto authored on 2009-11-16
107
$result = query($dbh, $sql);
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
108
@row = $result->fetch_hash_single;
packaging one directory
yuki-kimoto authored on 2009-11-16
109
is_deeply({@row}, {key1 => 1, key2 => 2}, "$test : row");
110
@row = $result->fetch_hash;
111
ok(!@row, "$test : finished");
112

            
113

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
114
test 'fetch_multi';
packaging one directory
yuki-kimoto authored on 2009-11-16
115
$dbh->do("insert into table1 (key1, key2) values ('5', '6');");
116
$dbh->do("insert into table1 (key1, key2) values ('7', '8');");
117
$dbh->do("insert into table1 (key1, key2) values ('9', '10');");
118
$result = query($dbh, $sql);
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
119
$rows = $result->fetch_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
120
is_deeply($rows, [[1, 2],
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
121
                  [3, 4]], "$test : fetch_multi first");
122
$rows = $result->fetch_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
123
is_deeply($rows, [[5, 6],
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
124
                  [7, 8]], "$test : fetch_multi secound");
125
$rows = $result->fetch_multi(2);
126
is_deeply($rows, [[9, 10]], "$test : fetch_multi third");
127
$rows = $result->fetch_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
128
ok(!$rows);
129

            
130

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
131
test 'fetch_multi list context';
packaging one directory
yuki-kimoto authored on 2009-11-16
132
$result = query($dbh, $sql);
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
133
@rows = $result->fetch_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
134
is_deeply([@rows], [[1, 2],
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
135
                  [3, 4]], "$test : fetch_multi first");
136
@rows = $result->fetch_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
137
is_deeply([@rows], [[5, 6],
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
138
                  [7, 8]], "$test : fetch_multi secound");
139
@rows = $result->fetch_multi(2);
140
is_deeply([@rows], [[9, 10]], "$test : fetch_multi third");
141
@rows = $result->fetch_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
142
ok(!@rows);
143

            
144

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
145
test 'fetch_multi error';
packaging one directory
yuki-kimoto authored on 2009-11-16
146
$result = query($dbh, $sql);
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
147
eval {$result->fetch_multi};
packaging one directory
yuki-kimoto authored on 2009-11-16
148
like($@, qr/Row count must be specified/, "$test : Not specified row count");
149

            
150

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
151
test 'fetch_hash_multi';
packaging one directory
yuki-kimoto authored on 2009-11-16
152
$result = query($dbh, $sql);
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
153
$rows = $result->fetch_hash_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
154
is_deeply($rows, [{key1 => 1, key2 => 2},
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
155
                  {key1 => 3, key2 => 4}], "$test : fetch_multi first");
156
$rows = $result->fetch_hash_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
157
is_deeply($rows, [{key1 => 5, key2 => 6},
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
158
                  {key1 => 7, key2 => 8}], "$test : fetch_multi secound");
159
$rows = $result->fetch_hash_multi(2);
160
is_deeply($rows, [{key1 => 9, key2 => 10}], "$test : fetch_multi third");
161
$rows = $result->fetch_hash_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
162
ok(!$rows);
163

            
164

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
165
test 'fetch_multi list context';
packaging one directory
yuki-kimoto authored on 2009-11-16
166
$result = query($dbh, $sql);
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
167
@rows = $result->fetch_hash_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
168
is_deeply([@rows], [{key1 => 1, key2 => 2},
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
169
                    {key1 => 3, key2 => 4}], "$test : fetch_multi first");
170
@rows = $result->fetch_hash_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
171
is_deeply([@rows], [{key1 => 5, key2 => 6},
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
172
                    {key1 => 7, key2 => 8}], "$test : fetch_multi secound");
173
@rows = $result->fetch_hash_multi(2);
174
is_deeply([@rows], [{key1 => 9, key2 => 10}], "$test : fetch_multi third");
175
@rows = $result->fetch_hash_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
176
ok(!@rows);
177
$dbh->do("delete from table1 where key1 = 5 or key1 = 7 or key1 = 9");
178

            
179

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
180
test 'fetch_multi error';
packaging one directory
yuki-kimoto authored on 2009-11-16
181
$result = query($dbh, $sql);
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
182
eval {$result->fetch_hash_multi};
packaging one directory
yuki-kimoto authored on 2009-11-16
183
like($@, qr/Row count must be specified/, "$test : Not specified row count");
184

            
185

            
186
test 'fetch_all';
187
$result = query($dbh, $sql);
188
$rows = $result->fetch_all;
189
is_deeply($rows, [[1, 2], [3, 4]], $test);
190

            
191
test 'fetch_all list context';
192
$result = query($dbh, $sql);
193
@rows = $result->fetch_all;
194
is_deeply(\@rows, [[1, 2], [3, 4]], $test);
195

            
196

            
197
test 'fetch_hash_all';
198
$result = query($dbh, $sql);
199
@rows = $result->fetch_hash_all;
200
is_deeply($rows, [[1, 2], [3, 4]], $test);
201

            
202

            
203
test 'fetch_hash_all list context';
204
$result = query($dbh, $sql);
205
@rows = $result->fetch_all;
206
is_deeply(\@rows, [[1, 2], [3, 4]], $test);
207

            
208

            
209
test 'fetch filter';
add all tests
yuki-kimoto authored on 2010-05-01
210
$result = query($dbh, $sql);
211
$result->filters({three_times => sub { $_[0] * 3}});
212
$result->filter({key1 => 'three_times'});
213

            
packaging one directory
yuki-kimoto authored on 2009-11-16
214
$rows = $result->fetch_all;
add all tests
yuki-kimoto authored on 2010-05-01
215
is_deeply($rows, [[3, 2], [9, 4]], "$test array");
packaging one directory
yuki-kimoto authored on 2009-11-16
216

            
217
$result = query($dbh, $sql);
add all tests
yuki-kimoto authored on 2010-05-01
218
$result->filters({three_times => sub { $_[0] * 3}});
219
$result->filter({key1 => 'three_times'});
packaging one directory
yuki-kimoto authored on 2009-11-16
220
$rows = $result->fetch_hash_all;
add all tests
yuki-kimoto authored on 2010-05-01
221
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "$test hash");
packaging one directory
yuki-kimoto authored on 2009-11-16
222