packaging one directory
|
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
|
36 |
my $filter; |
packaging one directory
|
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_...
|
82 |
test 'fetch_single'; |
packaging one directory
|
83 |
$result = query($dbh, $sql); |
rename fetch_first to fetch_...
|
84 |
$row = $result->fetch_single; |
packaging one directory
|
85 |
is_deeply($row, [1, 2], "$test : row"); |
86 |
$row = $result->fetch; |
|
87 |
ok(!$row, "$test : finished"); |
|
88 | ||
89 | ||
rename fetch_first to fetch_...
|
90 |
test 'fetch_single list context'; |
packaging one directory
|
91 |
$result = query($dbh, $sql); |
rename fetch_first to fetch_...
|
92 |
@row = $result->fetch_single; |
packaging one directory
|
93 |
is_deeply([@row], [1, 2], "$test : row"); |
94 |
@row = $result->fetch; |
|
95 |
ok(!@row, "$test : finished"); |
|
96 | ||
97 | ||
rename fetch_first to fetch_...
|
98 |
test 'fetch_hash_single'; |
packaging one directory
|
99 |
$result = query($dbh, $sql); |
rename fetch_first to fetch_...
|
100 |
$row = $result->fetch_hash_single; |
packaging one directory
|
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_...
|
106 |
test 'fetch_hash_single list context'; |
packaging one directory
|
107 |
$result = query($dbh, $sql); |
rename fetch_first to fetch_...
|
108 |
@row = $result->fetch_hash_single; |
packaging one directory
|
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_hash_rows'; |
|
152 |
$result = query($dbh, $sql); |
|
153 |
$rows = $result->fetch_hash_rows(2); |
|
154 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
155 |
{key1 => 3, key2 => 4}], "$test : fetch_rows first"); |
|
156 |
$rows = $result->fetch_hash_rows(2); |
|
157 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
|
158 |
{key1 => 7, key2 => 8}], "$test : fetch_rows secound"); |
|
159 |
$rows = $result->fetch_hash_rows(2); |
|
160 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "$test : fetch_rows third"); |
|
161 |
$rows = $result->fetch_hash_rows(2); |
|
162 |
ok(!$rows); |
|
163 | ||
164 | ||
165 |
test 'fetch_rows list context'; |
|
166 |
$result = query($dbh, $sql); |
|
167 |
@rows = $result->fetch_hash_rows(2); |
|
168 |
is_deeply([@rows], [{key1 => 1, key2 => 2}, |
|
169 |
{key1 => 3, key2 => 4}], "$test : fetch_rows first"); |
|
170 |
@rows = $result->fetch_hash_rows(2); |
|
171 |
is_deeply([@rows], [{key1 => 5, key2 => 6}, |
|
172 |
{key1 => 7, key2 => 8}], "$test : fetch_rows secound"); |
|
173 |
@rows = $result->fetch_hash_rows(2); |
|
174 |
is_deeply([@rows], [{key1 => 9, key2 => 10}], "$test : fetch_rows third"); |
|
175 |
@rows = $result->fetch_hash_rows(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_hash_rows}; |
|
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
|
210 |
$result = query($dbh, $sql); |
211 |
$result->filters({three_times => sub { $_[0] * 3}}); |
|
212 |
$result->filter({key1 => 'three_times'}); |
|
213 | ||
packaging one directory
|
214 |
$rows = $result->fetch_all; |
add all tests
|
215 |
is_deeply($rows, [[3, 2], [9, 4]], "$test array"); |
packaging one directory
|
216 | |
217 |
$result = query($dbh, $sql); |
|
add all tests
|
218 |
$result->filters({three_times => sub { $_[0] * 3}}); |
219 |
$result->filter({key1 => 'three_times'}); |
|
packaging one directory
|
220 |
$rows = $result->fetch_hash_all; |
add all tests
|
221 |
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "$test hash"); |
packaging one directory
|
222 | |
223 |
test 'finish'; |
|
224 |
$result = query($dbh, $sql); |
|
225 |
$result->fetch; |
|
226 |
$result->finish; |
|
227 |
ok(!$result->fetch, $test); |
|
228 | ||
229 |
test 'error'; # Cannot real test |
|
230 |
$result = query($dbh, $sql); |
|
231 |
$sth = $result->sth; |
|
232 | ||
233 |
@error = $result->error; |
|
234 |
is(scalar @error, 3, "$test list context count"); |
|
235 |
is($error[0], $sth->errstr, "$test list context errstr"); |
|
236 |
is($error[1], $sth->err, "$test list context err"); |
|
237 |
is($error[2], $sth->state, "$test list context state"); |
|
238 | ||
239 |
$error = $result->error; |
|
240 |
is($error, $sth->errstr, "$test scalar context"); |
|
241 |