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 | ||
add tests
|
47 |
test 'fetch'; |
packaging one directory
|
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 | ||
add tests
|
56 |
test 'fetch_hash'; |
packaging one directory
|
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 | ||
add tests
|
80 |
$result = query($dbh, 'create table table2 (key1, key2);'); |
81 |
$result = query($dbh, 'select * from table2'); |
|
82 |
$row = $result->fetch_hash_first; |
|
83 |
ok(!$row, "$test : no row fetch"); |
|
84 | ||
packaging one directory
|
85 | |
renamed fetch_rows to fetch_...
|
86 |
test 'fetch_multi'; |
packaging one directory
|
87 |
$dbh->do("insert into table1 (key1, key2) values ('5', '6');"); |
88 |
$dbh->do("insert into table1 (key1, key2) values ('7', '8');"); |
|
89 |
$dbh->do("insert into table1 (key1, key2) values ('9', '10');"); |
|
90 |
$result = query($dbh, $sql); |
|
renamed fetch_rows to fetch_...
|
91 |
$rows = $result->fetch_multi(2); |
packaging one directory
|
92 |
is_deeply($rows, [[1, 2], |
renamed fetch_rows to fetch_...
|
93 |
[3, 4]], "$test : fetch_multi first"); |
94 |
$rows = $result->fetch_multi(2); |
|
packaging one directory
|
95 |
is_deeply($rows, [[5, 6], |
renamed fetch_rows to fetch_...
|
96 |
[7, 8]], "$test : fetch_multi secound"); |
97 |
$rows = $result->fetch_multi(2); |
|
98 |
is_deeply($rows, [[9, 10]], "$test : fetch_multi third"); |
|
99 |
$rows = $result->fetch_multi(2); |
|
packaging one directory
|
100 |
ok(!$rows); |
101 | ||
102 | ||
renamed fetch_rows to fetch_...
|
103 |
test 'fetch_multi error'; |
packaging one directory
|
104 |
$result = query($dbh, $sql); |
renamed fetch_rows to fetch_...
|
105 |
eval {$result->fetch_multi}; |
packaging one directory
|
106 |
like($@, qr/Row count must be specified/, "$test : Not specified row count"); |
107 | ||
108 | ||
renamed fetch_rows to fetch_...
|
109 |
test 'fetch_hash_multi'; |
packaging one directory
|
110 |
$result = query($dbh, $sql); |
renamed fetch_rows to fetch_...
|
111 |
$rows = $result->fetch_hash_multi(2); |
packaging one directory
|
112 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
renamed fetch_rows to fetch_...
|
113 |
{key1 => 3, key2 => 4}], "$test : fetch_multi first"); |
114 |
$rows = $result->fetch_hash_multi(2); |
|
packaging one directory
|
115 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
renamed fetch_rows to fetch_...
|
116 |
{key1 => 7, key2 => 8}], "$test : fetch_multi secound"); |
117 |
$rows = $result->fetch_hash_multi(2); |
|
118 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "$test : fetch_multi third"); |
|
119 |
$rows = $result->fetch_hash_multi(2); |
|
packaging one directory
|
120 |
ok(!$rows); |
121 | ||
122 | ||
renamed fetch_rows to fetch_...
|
123 |
test 'fetch_multi error'; |
packaging one directory
|
124 |
$result = query($dbh, $sql); |
renamed fetch_rows to fetch_...
|
125 |
eval {$result->fetch_hash_multi}; |
packaging one directory
|
126 |
like($@, qr/Row count must be specified/, "$test : Not specified row count"); |
127 | ||
removed reconnect method
|
128 |
$dbh->do('delete from table1'); |
129 |
$dbh->do("insert into table1 (key1, key2) values ('1', '2');"); |
|
130 |
$dbh->do("insert into table1 (key1, key2) values ('3', '4');"); |
|
packaging one directory
|
131 | |
132 |
test 'fetch_all'; |
|
133 |
$result = query($dbh, $sql); |
|
134 |
$rows = $result->fetch_all; |
|
135 |
is_deeply($rows, [[1, 2], [3, 4]], $test); |
|
136 | ||
137 |
test 'fetch_hash_all'; |
|
138 |
$result = query($dbh, $sql); |
|
removed reconnect method
|
139 |
$rows = $result->fetch_hash_all; |
140 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], $test); |
|
packaging one directory
|
141 | |
142 | ||
143 |
test 'fetch filter'; |
|
add all tests
|
144 |
$result = query($dbh, $sql); |
145 |
$result->filters({three_times => sub { $_[0] * 3}}); |
|
146 |
$result->filter({key1 => 'three_times'}); |
|
147 | ||
packaging one directory
|
148 |
$rows = $result->fetch_all; |
add all tests
|
149 |
is_deeply($rows, [[3, 2], [9, 4]], "$test array"); |
packaging one directory
|
150 | |
151 |
$result = query($dbh, $sql); |
|
add all tests
|
152 |
$result->filters({three_times => sub { $_[0] * 3}}); |
153 |
$result->filter({key1 => 'three_times'}); |
|
packaging one directory
|
154 |
$rows = $result->fetch_hash_all; |
add all tests
|
155 |
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "$test hash"); |
packaging one directory
|
156 |