DBIx-Custom / t / dbix-custom-result-sqlite.t /
Newer Older
153 lines | 4.095kb
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

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
16
sub test { print "# $_[0]\n" }
packaging one directory
yuki-kimoto authored on 2009-11-16
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
yuki-kimoto authored on 2010-05-01
33
my $filter;
packaging one directory
yuki-kimoto authored on 2009-11-16
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
yuki-kimoto authored on 2010-08-10
44
test 'fetch';
packaging one directory
yuki-kimoto authored on 2009-11-16
45
$result = query($dbh, $sql);
46
@rows = ();
47
while (my $row = $result->fetch) {
48
    push @rows, [@$row];
49
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
50
is_deeply(\@rows, [[1, 2], [3, 4]]);
packaging one directory
yuki-kimoto authored on 2009-11-16
51

            
52

            
add tests
yuki-kimoto authored on 2010-08-10
53
test 'fetch_hash';
packaging one directory
yuki-kimoto authored on 2009-11-16
54
$result = query($dbh, $sql);
55
@rows = ();
56
while (my $row = $result->fetch_hash) {
57
    push @rows, {%$row};
58
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
59
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
packaging one directory
yuki-kimoto authored on 2009-11-16
60

            
61

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
62
test 'fetch_first';
packaging one directory
yuki-kimoto authored on 2009-11-16
63
$result = query($dbh, $sql);
removed reconnect method
yuki-kimoto authored on 2010-05-28
64
$row = $result->fetch_first;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
65
is_deeply($row, [1, 2], "row");
packaging one directory
yuki-kimoto authored on 2009-11-16
66
$row = $result->fetch;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
67
ok(!$row, "finished");
packaging one directory
yuki-kimoto authored on 2009-11-16
68

            
69

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
70
test 'fetch_hash_first';
packaging one directory
yuki-kimoto authored on 2009-11-16
71
$result = query($dbh, $sql);
removed reconnect method
yuki-kimoto authored on 2010-05-28
72
$row = $result->fetch_hash_first;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
73
is_deeply($row, {key1 => 1, key2 => 2}, "row");
packaging one directory
yuki-kimoto authored on 2009-11-16
74
$row = $result->fetch_hash;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
75
ok(!$row, "finished");
packaging one directory
yuki-kimoto authored on 2009-11-16
76

            
add tests
yuki-kimoto authored on 2010-08-10
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...
Yuki Kimoto authored on 2011-01-17
80
ok(!$row, "no row fetch");
add tests
yuki-kimoto authored on 2010-08-10
81

            
packaging one directory
yuki-kimoto authored on 2009-11-16
82

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
83
test 'fetch_multi';
packaging one directory
yuki-kimoto authored on 2009-11-16
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_...
yuki-kimoto authored on 2010-05-01
88
$rows = $result->fetch_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
89
is_deeply($rows, [[1, 2],
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
90
                  [3, 4]], "fetch_multi first");
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, [[5, 6],
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
93
                  [7, 8]], "fetch_multi secound");
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
94
$rows = $result->fetch_multi(2);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
95
is_deeply($rows, [[9, 10]], "fetch_multi third");
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
96
$rows = $result->fetch_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
97
ok(!$rows);
98

            
99

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
100
test 'fetch_multi error';
packaging one directory
yuki-kimoto authored on 2009-11-16
101
$result = query($dbh, $sql);
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
102
eval {$result->fetch_multi};
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
103
like($@, qr/Row count must be specified/, "Not specified row count");
packaging one directory
yuki-kimoto authored on 2009-11-16
104

            
105

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
106
test 'fetch_hash_multi';
packaging one directory
yuki-kimoto authored on 2009-11-16
107
$result = query($dbh, $sql);
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
108
$rows = $result->fetch_hash_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
109
is_deeply($rows, [{key1 => 1, key2 => 2},
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
110
                  {key1 => 3, key2 => 4}], "fetch_multi first");
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 => 5, key2 => 6},
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
113
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
114
$rows = $result->fetch_hash_multi(2);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
115
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
116
$rows = $result->fetch_hash_multi(2);
packaging one directory
yuki-kimoto authored on 2009-11-16
117
ok(!$rows);
118

            
119

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
120
test 'fetch_multi error';
packaging one directory
yuki-kimoto authored on 2009-11-16
121
$result = query($dbh, $sql);
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
122
eval {$result->fetch_hash_multi};
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
123
like($@, qr/Row count must be specified/, "Not specified row count");
packaging one directory
yuki-kimoto authored on 2009-11-16
124

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
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
yuki-kimoto authored on 2009-11-16
128

            
129
test 'fetch_all';
130
$result = query($dbh, $sql);
131
$rows = $result->fetch_all;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
132
is_deeply($rows, [[1, 2], [3, 4]]);
packaging one directory
yuki-kimoto authored on 2009-11-16
133

            
134
test 'fetch_hash_all';
135
$result = query($dbh, $sql);
removed reconnect method
yuki-kimoto authored on 2010-05-28
136
$rows = $result->fetch_hash_all;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
137
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
packaging one directory
yuki-kimoto authored on 2009-11-16
138

            
139

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
145
$rows = $result->fetch_all;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
146
is_deeply($rows, [[3, 2], [9, 4]], "array");
packaging one directory
yuki-kimoto authored on 2009-11-16
147

            
148
$result = query($dbh, $sql);
add all tests
yuki-kimoto authored on 2010-05-01
149
$result->filters({three_times => sub { $_[0] * 3}});
150
$result->filter({key1 => 'three_times'});
packaging one directory
yuki-kimoto authored on 2009-11-16
151
$rows = $result->fetch_hash_all;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
152
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash");
packaging one directory
yuki-kimoto authored on 2009-11-16
153