DBIx-Custom / t / dbix-custom-result-sqlite.t /
Newer Older
155 lines | 4.161kb
packaging one directory
yuki-kimoto authored on 2009-11-16
1
use Test::More;
2
use strict;
3
use warnings;
4
use DBI;
5

            
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
6
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
7

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

            
54

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

            
63

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

            
71

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

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

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

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

            
101

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

            
107

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

            
121

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

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

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

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

            
141

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

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

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