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