packing
|
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; |
|
36 |
my $fetch_filter; |
|
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 | ||
82 |
test 'fetch_first'; |
|
83 |
$result = query($dbh, $sql); |
|
84 |
$row = $result->fetch_first; |
|
85 |
is_deeply($row, [1, 2], "$test : row"); |
|
86 |
$row = $result->fetch; |
|
87 |
ok(!$row, "$test : finished"); |
|
88 | ||
89 | ||
90 |
test 'fetch_first list context'; |
|
91 |
$result = query($dbh, $sql); |
|
92 |
@row = $result->fetch_first; |
|
93 |
is_deeply([@row], [1, 2], "$test : row"); |
|
94 |
@row = $result->fetch; |
|
95 |
ok(!@row, "$test : finished"); |
|
96 | ||
97 | ||
98 |
test 'fetch_first_hash'; |
|
99 |
$result = query($dbh, $sql); |
|
100 |
$row = $result->fetch_first_hash; |
|
101 |
is_deeply($row, {key1 => 1, key2 => 2}, "$test : row"); |
|
102 |
$row = $result->fetch_hash; |
|
103 |
ok(!$row, "$test : finished"); |
|
104 | ||
105 | ||
106 |
test 'fetch_first_hash list context'; |
|
107 |
$result = query($dbh, $sql); |
|
108 |
@row = $result->fetch_first_hash; |
|
109 |
is_deeply({@row}, {key1 => 1, key2 => 2}, "$test : row"); |
|
110 |
@row = $result->fetch_hash; |
|
111 |
ok(!@row, "$test : finished"); |
|
112 | ||
113 | ||
114 |
test 'fetch_rows'; |
|
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); |
|
119 |
$rows = $result->fetch_rows(2); |
|
120 |
is_deeply($rows, [[1, 2], |
|
121 |
[3, 4]], "$test : fetch_rows first"); |
|
122 |
$rows = $result->fetch_rows(2); |
|
123 |
is_deeply($rows, [[5, 6], |
|
124 |
[7, 8]], "$test : fetch_rows secound"); |
|
125 |
$rows = $result->fetch_rows(2); |
|
126 |
is_deeply($rows, [[9, 10]], "$test : fetch_rows third"); |
|
127 |
$rows = $result->fetch_rows(2); |
|
128 |
ok(!$rows); |
|
129 | ||
130 | ||
131 |
test 'fetch_rows list context'; |
|
132 |
$result = query($dbh, $sql); |
|
133 |
@rows = $result->fetch_rows(2); |
|
134 |
is_deeply([@rows], [[1, 2], |
|
135 |
[3, 4]], "$test : fetch_rows first"); |
|
136 |
@rows = $result->fetch_rows(2); |
|
137 |
is_deeply([@rows], [[5, 6], |
|
138 |
[7, 8]], "$test : fetch_rows secound"); |
|
139 |
@rows = $result->fetch_rows(2); |
|
140 |
is_deeply([@rows], [[9, 10]], "$test : fetch_rows third"); |
|
141 |
@rows = $result->fetch_rows(2); |
|
142 |
ok(!@rows); |
|
143 | ||
144 | ||
145 |
test 'fetch_rows error'; |
|
146 |
$result = query($dbh, $sql); |
|
147 |
eval {$result->fetch_rows}; |
|
148 |
like($@, qr/Row count must be specified/, "$test : Not specified row count"); |
|
149 | ||
150 | ||
151 |
test 'fetch_rows_hash'; |
|
152 |
$result = query($dbh, $sql); |
|
153 |
$rows = $result->fetch_rows_hash(2); |
|
154 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
155 |
{key1 => 3, key2 => 4}], "$test : fetch_rows first"); |
|
156 |
$rows = $result->fetch_rows_hash(2); |
|
157 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
|
158 |
{key1 => 7, key2 => 8}], "$test : fetch_rows secound"); |
|
159 |
$rows = $result->fetch_rows_hash(2); |
|
160 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "$test : fetch_rows third"); |
|
161 |
$rows = $result->fetch_rows_hash(2); |
|
162 |
ok(!$rows); |
|
163 | ||
164 | ||
165 |
test 'fetch_rows list context'; |
|
166 |
$result = query($dbh, $sql); |
|
167 |
@rows = $result->fetch_rows_hash(2); |
|
168 |
is_deeply([@rows], [{key1 => 1, key2 => 2}, |
|
169 |
{key1 => 3, key2 => 4}], "$test : fetch_rows first"); |
|
170 |
@rows = $result->fetch_rows_hash(2); |
|
171 |
is_deeply([@rows], [{key1 => 5, key2 => 6}, |
|
172 |
{key1 => 7, key2 => 8}], "$test : fetch_rows secound"); |
|
173 |
@rows = $result->fetch_rows_hash(2); |
|
174 |
is_deeply([@rows], [{key1 => 9, key2 => 10}], "$test : fetch_rows third"); |
|
175 |
@rows = $result->fetch_rows_hash(2); |
|
176 |
ok(!@rows); |
|
177 |
$dbh->do("delete from table1 where key1 = 5 or key1 = 7 or key1 = 9"); |
|
178 | ||
179 | ||
180 |
test 'fetch_rows error'; |
|
181 |
$result = query($dbh, $sql); |
|
182 |
eval {$result->fetch_rows_hash}; |
|
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_all_hash'; |
|
198 |
$result = query($dbh, $sql); |
|
199 |
@rows = $result->fetch_all_hash; |
|
200 |
is_deeply($rows, [[1, 2], [3, 4]], $test); |
|
201 | ||
202 | ||
203 |
test 'fetch_all_hash 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'; |
|
210 |
$fetch_filter = sub { |
|
211 |
my ($key, $value, $type, $sth, $i) = @_; |
|
212 |
if ($key eq 'key1' && $value == 1 && $type =~ /char/i && $i == 0 && $sth->{TYPE}->[$i] eq $type) { |
|
213 |
return $value * 3; |
|
214 |
} |
|
215 |
return $value; |
|
216 |
}; |
|
217 | ||
218 |
$result = query($dbh, $sql); |
|
219 |
$result->fetch_filter($fetch_filter); |
|
220 |
$rows = $result->fetch_all; |
|
221 |
is_deeply($rows, [[3, 2], [3, 4]], "$test array"); |
|
222 | ||
223 |
$result = query($dbh, $sql); |
|
224 |
$result->fetch_filter($fetch_filter); |
|
225 |
$rows = $result->fetch_all_hash; |
|
226 |
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 3, key2 => 4}], "$test hash"); |
|
227 | ||
228 |
$result = query($dbh, $sql); |
|
229 |
$result->no_fetch_filters(['key1']); |
|
230 |
$rows = $result->fetch_all; |
|
231 |
is_deeply($rows, [[1, 2], [3, 4]], "$test array no filter keys"); |
|
232 | ||
233 |
$result = query($dbh, $sql); |
|
234 |
$result->no_fetch_filters(['key1']); |
|
235 |
$rows = $result->fetch_all_hash; |
|
236 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test hash no filter keys"); |
|
237 | ||
238 | ||
239 |
test 'finish'; |
|
240 |
$result = query($dbh, $sql); |
|
241 |
$result->fetch; |
|
242 |
$result->finish; |
|
243 |
ok(!$result->fetch, $test); |
|
244 | ||
245 |
test 'error'; # Cannot real test |
|
246 |
$result = query($dbh, $sql); |
|
247 |
$sth = $result->sth; |
|
248 | ||
249 |
@error = $result->error; |
|
250 |
is(scalar @error, 3, "$test list context count"); |
|
251 |
is($error[0], $sth->errstr, "$test list context errstr"); |
|
252 |
is($error[1], $sth->err, "$test list context err"); |
|
253 |
is($error[2], $sth->state, "$test list context state"); |
|
254 | ||
255 |
$error = $result->error; |
|
256 |
is($error, $sth->errstr, "$test scalar context"); |
|
257 |