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