DBIx-Custom / t / result.t /
Newer Older
157 lines | 4.216kb
cleanup test
Yuki Kimoto authored on 2011-08-06
1
use Test::More;
2
use strict;
3
use warnings;
4
use DBI;
5

            
6
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
7

            
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

            
18
use DBIx::Custom;
19

            
20
sub test { print "# $_[0]\n" }
21

            
22
sub query {
23
    my ($dbh, $sql) = @_;
24
    my $sth = $dbh->prepare($sql);
25
    $sth->execute;
26
    return DBIx::Custom::Result->new(dbi => DBIx::Custom->new, sth => $sth);
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;
37
my $filter;
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

            
48
test 'fetch';
49
$result = query($dbh, $sql);
50
@rows = ();
51
while (my $row = $result->fetch) {
52
    push @rows, [@$row];
53
}
54
is_deeply(\@rows, [[1, 2], [3, 4]]);
55

            
56

            
57
test 'fetch_hash';
58
$result = query($dbh, $sql);
59
@rows = ();
60
while (my $row = $result->fetch_hash) {
61
    push @rows, {%$row};
62
}
63
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
64

            
65

            
66
test 'fetch_first';
67
$result = query($dbh, $sql);
68
$row = $result->fetch_first;
69
is_deeply($row, [1, 2], "row");
70
$row = $result->fetch;
71
ok(!$row, "finished");
72

            
73

            
74
test 'fetch_hash_first';
75
$result = query($dbh, $sql);
76
$row = $result->fetch_hash_first;
77
is_deeply($row, {key1 => 1, key2 => 2}, "row");
78
$row = $result->fetch_hash;
79
ok(!$row, "finished");
80

            
81
$result = query($dbh, 'create table table2 (key1, key2);');
82
$result = query($dbh, 'select * from table2');
83
$row = $result->fetch_hash_first;
84
ok(!$row, "no row fetch");
85

            
86

            
87
test 'fetch_multi';
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);
92
$rows = $result->fetch_multi(2);
93
is_deeply($rows, [[1, 2],
94
                  [3, 4]], "fetch_multi first");
95
$rows = $result->fetch_multi(2);
96
is_deeply($rows, [[5, 6],
97
                  [7, 8]], "fetch_multi secound");
98
$rows = $result->fetch_multi(2);
99
is_deeply($rows, [[9, 10]], "fetch_multi third");
100
$rows = $result->fetch_multi(2);
101
ok(!$rows);
102

            
103

            
104
test 'fetch_multi error';
105
$result = query($dbh, $sql);
106
eval {$result->fetch_multi};
107
like($@, qr/Row count must be specified/, "Not specified row count");
108

            
109

            
110
test 'fetch_hash_multi';
111
$result = query($dbh, $sql);
112
$rows = $result->fetch_hash_multi(2);
113
is_deeply($rows, [{key1 => 1, key2 => 2},
114
                  {key1 => 3, key2 => 4}], "fetch_multi first");
115
$rows = $result->fetch_hash_multi(2);
116
is_deeply($rows, [{key1 => 5, key2 => 6},
117
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
118
$rows = $result->fetch_hash_multi(2);
119
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
120
$rows = $result->fetch_hash_multi(2);
121
ok(!$rows);
122

            
123

            
124
test 'fetch_multi error';
125
$result = query($dbh, $sql);
126
eval {$result->fetch_hash_multi};
127
like($@, qr/Row count must be specified/, "Not specified row count");
128

            
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');");
132

            
133
test 'fetch_all';
134
$result = query($dbh, $sql);
135
$rows = $result->fetch_all;
136
is_deeply($rows, [[1, 2], [3, 4]]);
137

            
138
test 'fetch_hash_all';
139
$result = query($dbh, $sql);
140
$rows = $result->fetch_hash_all;
141
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
142

            
143

            
144
test 'fetch filter';
145
$result = query($dbh, $sql);
146
$result->dbi->filters({three_times => sub { $_[0] * 3}});
147
$result->filter({key1 => 'three_times'});
148

            
149
$rows = $result->fetch_all;
150
is_deeply($rows, [[3, 2], [9, 4]], "array");
151

            
152
$result = query($dbh, $sql);
153
$result->dbi->filters({three_times => sub { $_[0] * 3}});
154
$result->filter({key1 => 'three_times'});
155
$rows = $result->fetch_hash_all;
156
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
157