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 | ||
added experimental DBIx::Cus...
|
16 |
sub test { print "# $_[0]\n" } |
packaging one directory
|
17 | |
18 |
sub query { |
|
19 |
my ($dbh, $sql) = @_; |
|
20 |
my $sth = $dbh->prepare($sql); |
|
21 |
$sth->execute; |
|
22 |
return DBIx::Custom::Result->new(sth => $sth); |
|
23 |
} |
|
24 | ||
25 |
my $dbh; |
|
26 |
my $sql; |
|
27 |
my $sth; |
|
28 |
my @row; |
|
29 |
my $row; |
|
30 |
my @rows; |
|
31 |
my $rows; |
|
32 |
my $result; |
|
add all tests
|
33 |
my $filter; |
packaging one directory
|
34 |
my @error; |
35 |
my $error; |
|
36 | ||
37 |
$dbh = DBI->connect('dbi:SQLite:dbname=:memory:', undef, undef, {PrintError => 0, RaiseError => 1}); |
|
38 |
$dbh->do("create table table1 (key1 char(255), key2 char(255));"); |
|
39 |
$dbh->do("insert into table1 (key1, key2) values ('1', '2');"); |
|
40 |
$dbh->do("insert into table1 (key1, key2) values ('3', '4');"); |
|
41 | ||
42 |
$sql = "select key1, key2 from table1"; |
|
43 | ||
add tests
|
44 |
test 'fetch'; |
packaging one directory
|
45 |
$result = query($dbh, $sql); |
46 |
@rows = (); |
|
47 |
while (my $row = $result->fetch) { |
|
48 |
push @rows, [@$row]; |
|
49 |
} |
|
added experimental DBIx::Cus...
|
50 |
is_deeply(\@rows, [[1, 2], [3, 4]]); |
packaging one directory
|
51 | |
52 | ||
add tests
|
53 |
test 'fetch_hash'; |
packaging one directory
|
54 |
$result = query($dbh, $sql); |
55 |
@rows = (); |
|
56 |
while (my $row = $result->fetch_hash) { |
|
57 |
push @rows, {%$row}; |
|
58 |
} |
|
added experimental DBIx::Cus...
|
59 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
packaging one directory
|
60 | |
61 | ||
removed reconnect method
|
62 |
test 'fetch_first'; |
packaging one directory
|
63 |
$result = query($dbh, $sql); |
removed reconnect method
|
64 |
$row = $result->fetch_first; |
added experimental DBIx::Cus...
|
65 |
is_deeply($row, [1, 2], "row"); |
packaging one directory
|
66 |
$row = $result->fetch; |
added experimental DBIx::Cus...
|
67 |
ok(!$row, "finished"); |
packaging one directory
|
68 | |
69 | ||
removed reconnect method
|
70 |
test 'fetch_hash_first'; |
packaging one directory
|
71 |
$result = query($dbh, $sql); |
removed reconnect method
|
72 |
$row = $result->fetch_hash_first; |
added experimental DBIx::Cus...
|
73 |
is_deeply($row, {key1 => 1, key2 => 2}, "row"); |
packaging one directory
|
74 |
$row = $result->fetch_hash; |
added experimental DBIx::Cus...
|
75 |
ok(!$row, "finished"); |
packaging one directory
|
76 | |
add tests
|
77 |
$result = query($dbh, 'create table table2 (key1, key2);'); |
78 |
$result = query($dbh, 'select * from table2'); |
|
79 |
$row = $result->fetch_hash_first; |
|
added experimental DBIx::Cus...
|
80 |
ok(!$row, "no row fetch"); |
add tests
|
81 | |
packaging one directory
|
82 | |
renamed fetch_rows to fetch_...
|
83 |
test 'fetch_multi'; |
packaging one directory
|
84 |
$dbh->do("insert into table1 (key1, key2) values ('5', '6');"); |
85 |
$dbh->do("insert into table1 (key1, key2) values ('7', '8');"); |
|
86 |
$dbh->do("insert into table1 (key1, key2) values ('9', '10');"); |
|
87 |
$result = query($dbh, $sql); |
|
renamed fetch_rows to fetch_...
|
88 |
$rows = $result->fetch_multi(2); |
packaging one directory
|
89 |
is_deeply($rows, [[1, 2], |
added experimental DBIx::Cus...
|
90 |
[3, 4]], "fetch_multi first"); |
renamed fetch_rows to fetch_...
|
91 |
$rows = $result->fetch_multi(2); |
packaging one directory
|
92 |
is_deeply($rows, [[5, 6], |
added experimental DBIx::Cus...
|
93 |
[7, 8]], "fetch_multi secound"); |
renamed fetch_rows to fetch_...
|
94 |
$rows = $result->fetch_multi(2); |
added experimental DBIx::Cus...
|
95 |
is_deeply($rows, [[9, 10]], "fetch_multi third"); |
renamed fetch_rows to fetch_...
|
96 |
$rows = $result->fetch_multi(2); |
packaging one directory
|
97 |
ok(!$rows); |
98 | ||
99 | ||
renamed fetch_rows to fetch_...
|
100 |
test 'fetch_multi error'; |
packaging one directory
|
101 |
$result = query($dbh, $sql); |
renamed fetch_rows to fetch_...
|
102 |
eval {$result->fetch_multi}; |
added experimental DBIx::Cus...
|
103 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
packaging one directory
|
104 | |
105 | ||
renamed fetch_rows to fetch_...
|
106 |
test 'fetch_hash_multi'; |
packaging one directory
|
107 |
$result = query($dbh, $sql); |
renamed fetch_rows to fetch_...
|
108 |
$rows = $result->fetch_hash_multi(2); |
packaging one directory
|
109 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
added experimental DBIx::Cus...
|
110 |
{key1 => 3, key2 => 4}], "fetch_multi first"); |
renamed fetch_rows to fetch_...
|
111 |
$rows = $result->fetch_hash_multi(2); |
packaging one directory
|
112 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
added experimental DBIx::Cus...
|
113 |
{key1 => 7, key2 => 8}], "fetch_multi secound"); |
renamed fetch_rows to fetch_...
|
114 |
$rows = $result->fetch_hash_multi(2); |
added experimental DBIx::Cus...
|
115 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third"); |
renamed fetch_rows to fetch_...
|
116 |
$rows = $result->fetch_hash_multi(2); |
packaging one directory
|
117 |
ok(!$rows); |
118 | ||
119 | ||
renamed fetch_rows to fetch_...
|
120 |
test 'fetch_multi error'; |
packaging one directory
|
121 |
$result = query($dbh, $sql); |
renamed fetch_rows to fetch_...
|
122 |
eval {$result->fetch_hash_multi}; |
added experimental DBIx::Cus...
|
123 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
packaging one directory
|
124 | |
removed reconnect method
|
125 |
$dbh->do('delete from table1'); |
126 |
$dbh->do("insert into table1 (key1, key2) values ('1', '2');"); |
|
127 |
$dbh->do("insert into table1 (key1, key2) values ('3', '4');"); |
|
packaging one directory
|
128 | |
129 |
test 'fetch_all'; |
|
130 |
$result = query($dbh, $sql); |
|
131 |
$rows = $result->fetch_all; |
|
added experimental DBIx::Cus...
|
132 |
is_deeply($rows, [[1, 2], [3, 4]]); |
packaging one directory
|
133 | |
134 |
test 'fetch_hash_all'; |
|
135 |
$result = query($dbh, $sql); |
|
removed reconnect method
|
136 |
$rows = $result->fetch_hash_all; |
added experimental DBIx::Cus...
|
137 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
packaging one directory
|
138 | |
139 | ||
140 |
test 'fetch filter'; |
|
add all tests
|
141 |
$result = query($dbh, $sql); |
142 |
$result->filters({three_times => sub { $_[0] * 3}}); |
|
143 |
$result->filter({key1 => 'three_times'}); |
|
144 | ||
packaging one directory
|
145 |
$rows = $result->fetch_all; |
added experimental DBIx::Cus...
|
146 |
is_deeply($rows, [[3, 2], [9, 4]], "array"); |
packaging one directory
|
147 | |
148 |
$result = query($dbh, $sql); |
|
add all tests
|
149 |
$result->filters({three_times => sub { $_[0] * 3}}); |
150 |
$result->filter({key1 => 'three_times'}); |
|
packaging one directory
|
151 |
$rows = $result->fetch_hash_all; |
added experimental DBIx::Cus...
|
152 |
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash"); |
packaging one directory
|
153 |