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_hash scalar context'; |
|
57 |
$result = query($dbh, $sql); |
|
58 |
@rows = (); |
|
59 |
while (my $row = $result->fetch_hash) { |
|
60 |
push @rows, {%$row}; |
|
61 |
} |
|
62 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], $test); |
|
63 | ||
64 | ||
removed reconnect method
|
65 |
test 'fetch_first'; |
packaging one directory
|
66 |
$result = query($dbh, $sql); |
removed reconnect method
|
67 |
$row = $result->fetch_first; |
packaging one directory
|
68 |
is_deeply($row, [1, 2], "$test : row"); |
69 |
$row = $result->fetch; |
|
70 |
ok(!$row, "$test : finished"); |
|
71 | ||
72 | ||
removed reconnect method
|
73 |
test 'fetch_hash_first'; |
packaging one directory
|
74 |
$result = query($dbh, $sql); |
removed reconnect method
|
75 |
$row = $result->fetch_hash_first; |
packaging one directory
|
76 |
is_deeply($row, {key1 => 1, key2 => 2}, "$test : row"); |
77 |
$row = $result->fetch_hash; |
|
78 |
ok(!$row, "$test : finished"); |
|
79 | ||
80 | ||
renamed fetch_rows to fetch_...
|
81 |
test 'fetch_multi'; |
packaging one directory
|
82 |
$dbh->do("insert into table1 (key1, key2) values ('5', '6');"); |
83 |
$dbh->do("insert into table1 (key1, key2) values ('7', '8');"); |
|
84 |
$dbh->do("insert into table1 (key1, key2) values ('9', '10');"); |
|
85 |
$result = query($dbh, $sql); |
|
renamed fetch_rows to fetch_...
|
86 |
$rows = $result->fetch_multi(2); |
packaging one directory
|
87 |
is_deeply($rows, [[1, 2], |
renamed fetch_rows to fetch_...
|
88 |
[3, 4]], "$test : fetch_multi first"); |
89 |
$rows = $result->fetch_multi(2); |
|
packaging one directory
|
90 |
is_deeply($rows, [[5, 6], |
renamed fetch_rows to fetch_...
|
91 |
[7, 8]], "$test : fetch_multi secound"); |
92 |
$rows = $result->fetch_multi(2); |
|
93 |
is_deeply($rows, [[9, 10]], "$test : fetch_multi third"); |
|
94 |
$rows = $result->fetch_multi(2); |
|
packaging one directory
|
95 |
ok(!$rows); |
96 | ||
97 | ||
renamed fetch_rows to fetch_...
|
98 |
test 'fetch_multi error'; |
packaging one directory
|
99 |
$result = query($dbh, $sql); |
renamed fetch_rows to fetch_...
|
100 |
eval {$result->fetch_multi}; |
packaging one directory
|
101 |
like($@, qr/Row count must be specified/, "$test : Not specified row count"); |
102 | ||
103 | ||
renamed fetch_rows to fetch_...
|
104 |
test 'fetch_hash_multi'; |
packaging one directory
|
105 |
$result = query($dbh, $sql); |
renamed fetch_rows to fetch_...
|
106 |
$rows = $result->fetch_hash_multi(2); |
packaging one directory
|
107 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
renamed fetch_rows to fetch_...
|
108 |
{key1 => 3, key2 => 4}], "$test : fetch_multi first"); |
109 |
$rows = $result->fetch_hash_multi(2); |
|
packaging one directory
|
110 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
renamed fetch_rows to fetch_...
|
111 |
{key1 => 7, key2 => 8}], "$test : fetch_multi secound"); |
112 |
$rows = $result->fetch_hash_multi(2); |
|
113 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "$test : fetch_multi third"); |
|
114 |
$rows = $result->fetch_hash_multi(2); |
|
packaging one directory
|
115 |
ok(!$rows); |
116 | ||
117 | ||
renamed fetch_rows to fetch_...
|
118 |
test 'fetch_multi error'; |
packaging one directory
|
119 |
$result = query($dbh, $sql); |
renamed fetch_rows to fetch_...
|
120 |
eval {$result->fetch_hash_multi}; |
packaging one directory
|
121 |
like($@, qr/Row count must be specified/, "$test : Not specified row count"); |
122 | ||
removed reconnect method
|
123 |
$dbh->do('delete from table1'); |
124 |
$dbh->do("insert into table1 (key1, key2) values ('1', '2');"); |
|
125 |
$dbh->do("insert into table1 (key1, key2) values ('3', '4');"); |
|
packaging one directory
|
126 | |
127 |
test 'fetch_all'; |
|
128 |
$result = query($dbh, $sql); |
|
129 |
$rows = $result->fetch_all; |
|
130 |
is_deeply($rows, [[1, 2], [3, 4]], $test); |
|
131 | ||
132 |
test 'fetch_hash_all'; |
|
133 |
$result = query($dbh, $sql); |
|
removed reconnect method
|
134 |
$rows = $result->fetch_hash_all; |
135 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], $test); |
|
packaging one directory
|
136 | |
137 | ||
138 |
test 'fetch filter'; |
|
add all tests
|
139 |
$result = query($dbh, $sql); |
140 |
$result->filters({three_times => sub { $_[0] * 3}}); |
|
141 |
$result->filter({key1 => 'three_times'}); |
|
142 | ||
packaging one directory
|
143 |
$rows = $result->fetch_all; |
add all tests
|
144 |
is_deeply($rows, [[3, 2], [9, 4]], "$test array"); |
packaging one directory
|
145 | |
146 |
$result = query($dbh, $sql); |
|
add all tests
|
147 |
$result->filters({three_times => sub { $_[0] * 3}}); |
148 |
$result->filter({key1 => 'three_times'}); |
|
packaging one directory
|
149 |
$rows = $result->fetch_hash_all; |
add all tests
|
150 |
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "$test hash"); |
packaging one directory
|
151 |