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

            
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
18
use DBIx::Custom;
19

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
20
sub test { print "# $_[0]\n" }
packaging one directory
yuki-kimoto authored on 2009-11-16
21

            
22
sub query {
23
    my ($dbh, $sql) = @_;
24
    my $sth = $dbh->prepare($sql);
25
    $sth->execute;
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
26
    return DBIx::Custom::Result->new(dbi => DBIx::Custom->new, sth => $sth);
packaging one directory
yuki-kimoto authored on 2009-11-16
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
yuki-kimoto authored on 2010-05-01
37
my $filter;
packaging one directory
yuki-kimoto authored on 2009-11-16
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
yuki-kimoto authored on 2010-08-10
48
test 'fetch';
packaging one directory
yuki-kimoto authored on 2009-11-16
49
$result = query($dbh, $sql);
50
@rows = ();
51
while (my $row = $result->fetch) {
52
    push @rows, [@$row];
53
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
54
is_deeply(\@rows, [[1, 2], [3, 4]]);
packaging one directory
yuki-kimoto authored on 2009-11-16
55

            
56

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

            
65

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

            
73

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

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

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

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

            
103

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

            
109

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

            
123

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

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

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

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

            
143

            
144
test 'fetch filter';
add all tests
yuki-kimoto authored on 2010-05-01
145
$result = query($dbh, $sql);
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
146
$result->dbi->filters({three_times => sub { $_[0] * 3}});
add all tests
yuki-kimoto authored on 2010-05-01
147
$result->filter({key1 => 'three_times'});
148

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

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