DBIx-Custom / t / dbix-custom-result-sqlite.t /
Newer Older
156 lines | 4.25kb
packaging one directory
yuki-kimoto authored on 2009-11-16
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
yuki-kimoto authored on 2010-05-01
36
my $filter;
packaging one directory
yuki-kimoto authored on 2009-11-16
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
yuki-kimoto authored on 2010-08-10
47
test 'fetch';
packaging one directory
yuki-kimoto authored on 2009-11-16
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
yuki-kimoto authored on 2010-08-10
56
test 'fetch_hash';
packaging one directory
yuki-kimoto authored on 2009-11-16
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
yuki-kimoto authored on 2010-05-28
65
test 'fetch_first';
packaging one directory
yuki-kimoto authored on 2009-11-16
66
$result = query($dbh, $sql);
removed reconnect method
yuki-kimoto authored on 2010-05-28
67
$row = $result->fetch_first;
packaging one directory
yuki-kimoto authored on 2009-11-16
68
is_deeply($row, [1, 2], "$test : row");
69
$row = $result->fetch;
70
ok(!$row, "$test : finished");
71

            
72

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
73
test 'fetch_hash_first';
packaging one directory
yuki-kimoto authored on 2009-11-16
74
$result = query($dbh, $sql);
removed reconnect method
yuki-kimoto authored on 2010-05-28
75
$row = $result->fetch_hash_first;
packaging one directory
yuki-kimoto authored on 2009-11-16
76
is_deeply($row, {key1 => 1, key2 => 2}, "$test : row");
77
$row = $result->fetch_hash;
78
ok(!$row, "$test : finished");
79

            
add tests
yuki-kimoto authored on 2010-08-10
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
yuki-kimoto authored on 2009-11-16
85

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
86
test 'fetch_multi';
packaging one directory
yuki-kimoto authored on 2009-11-16
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_...
yuki-kimoto authored on 2010-05-01
91
$rows = $result->fetch_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
92
is_deeply($rows, [[1, 2],
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
93
                  [3, 4]], "$test : fetch_multi first");
94
$rows = $result->fetch_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
95
is_deeply($rows, [[5, 6],
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
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
yuki-kimoto authored on 2009-11-16
100
ok(!$rows);
101

            
102

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
103
test 'fetch_multi error';
packaging one directory
yuki-kimoto authored on 2009-11-16
104
$result = query($dbh, $sql);
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
105
eval {$result->fetch_multi};
packaging one directory
yuki-kimoto authored on 2009-11-16
106
like($@, qr/Row count must be specified/, "$test : Not specified row count");
107

            
108

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
109
test 'fetch_hash_multi';
packaging one directory
yuki-kimoto authored on 2009-11-16
110
$result = query($dbh, $sql);
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
111
$rows = $result->fetch_hash_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
112
is_deeply($rows, [{key1 => 1, key2 => 2},
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
113
                  {key1 => 3, key2 => 4}], "$test : fetch_multi first");
114
$rows = $result->fetch_hash_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
115
is_deeply($rows, [{key1 => 5, key2 => 6},
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
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
yuki-kimoto authored on 2009-11-16
120
ok(!$rows);
121

            
122

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
123
test 'fetch_multi error';
packaging one directory
yuki-kimoto authored on 2009-11-16
124
$result = query($dbh, $sql);
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
125
eval {$result->fetch_hash_multi};
packaging one directory
yuki-kimoto authored on 2009-11-16
126
like($@, qr/Row count must be specified/, "$test : Not specified row count");
127

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
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
yuki-kimoto authored on 2009-11-16
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
yuki-kimoto authored on 2010-05-28
139
$rows = $result->fetch_hash_all;
140
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], $test);
packaging one directory
yuki-kimoto authored on 2009-11-16
141

            
142

            
143
test 'fetch filter';
add all tests
yuki-kimoto authored on 2010-05-01
144
$result = query($dbh, $sql);
145
$result->filters({three_times => sub { $_[0] * 3}});
146
$result->filter({key1 => 'three_times'});
147

            
packaging one directory
yuki-kimoto authored on 2009-11-16
148
$rows = $result->fetch_all;
add all tests
yuki-kimoto authored on 2010-05-01
149
is_deeply($rows, [[3, 2], [9, 4]], "$test array");
packaging one directory
yuki-kimoto authored on 2009-11-16
150

            
151
$result = query($dbh, $sql);
add all tests
yuki-kimoto authored on 2010-05-01
152
$result->filters({three_times => sub { $_[0] * 3}});
153
$result->filter({key1 => 'three_times'});
packaging one directory
yuki-kimoto authored on 2009-11-16
154
$rows = $result->fetch_hash_all;
add all tests
yuki-kimoto authored on 2010-05-01
155
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "$test hash");
packaging one directory
yuki-kimoto authored on 2009-11-16
156