DBIx-Custom / t / mysql.t /
Newer Older
217 lines | 5.586kb
added common test executing ...
Yuki Kimoto authored on 2011-08-07
1
use Test::More;
2
use strict;
3
use warnings;
cleanup
Yuki Kimoto authored on 2011-08-16
4
use utf8;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
5

            
6
use FindBin;
cleanup test
Yuki Kimoto authored on 2011-08-15
7
use DBIx::Custom;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
8

            
cleanup test
Yuki Kimoto authored on 2011-08-15
9
my $dbi;
cleanup
Yuki Kimoto authored on 2011-08-16
10
my $dsn;
11
my $args;
12
my $user = 'dbix_custom';
13
my $password = 'dbix_custom';
14
my $database = 'dbix_custom';
cleanup test
Yuki Kimoto authored on 2011-08-15
15

            
cleanup
Yuki Kimoto authored on 2011-08-16
16
$dsn = "dbi:mysql:database=$database";
17
$args = {dsn => $dsn, user => $user, password => $password,};
18

            
19
plan skip_all => 'mysql private test' unless -f "$FindBin::Bin/run/mysql2.run"
20
  && eval { $dbi = DBIx::Custom->connect($args); 1 };
added common test executing ...
Yuki Kimoto authored on 2011-08-07
21
plan 'no_plan';
22

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

            
25
require DBIx::Connector;
26

            
27
# Function for test name
28
sub test { print "# $_[0]\n" }
29

            
30
# Varialbes for tests
31
my $dbname;
32
my $rows;
33
my $result;
34

            
35
test 'connect';
36
eval {
37
    $dbi = DBIx::Custom->new(
38
        dsn => "dbi:mysql:database=$database;host=localhost;port=10000",
39
        user => $user,
40
        password => $password
41
    );
42
};
43
ok(!$@);
44

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
45
# Test memory leaks
46
for (1 .. 200) {
47
    $dbi = DBIx::Custom->connect(
48
        dsn => "dbi:mysql:database=$database;host=localhost;port=10000",
49
        user => $user,
50
        password => $password
51
    );
52
    $dbi->query_builder;
cleanup
Yuki Kimoto authored on 2011-08-16
53
    $dbi->create_model(table => 'table1');
54
    $dbi->create_model(table => 'table2');
added memory leak check test
Yuki Kimoto authored on 2011-08-15
55
}
56

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
57
test 'limit';
58
$dbi = DBIx::Custom->connect(
59
    dsn => "dbi:mysql:database=$database",
60
    user => $user,
61
    password => $password
62
);
63
$dbi->delete_all(table => 'table1');
64
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
65
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
66
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
test cleanup
Yuki Kimoto authored on 2011-08-10
67
$dbi->register_tag(
added common test executing ...
Yuki Kimoto authored on 2011-08-07
68
    limit => sub {
69
        my ($count, $offset) = @_;
70
        
71
        my $s = '';
72
        $offset = 0 unless defined $offset;
73
        $s .= "limit $offset";
74
        $s .= ", $count";
75
        
76
        return [$s, []];
77
    }
78
);
79
$rows = $dbi->select(
80
  table => 'table1',
81
  where => {key1 => 1},
82
  append => "order by key2 {limit 1 0}"
83
)->fetch_hash_all;
84
is_deeply($rows, [{key1 => 1, key2 => 2}]);
85
$rows = $dbi->select(
86
  table => 'table1',
87
  where => {key1 => 1},
88
  append => "order by key2 {limit 2 1}"
89
)->fetch_hash_all;
90
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
91
$rows = $dbi->select(
92
  table => 'table1',
93
  where => {key1 => 1},
94
  append => "order by key2 {limit 1}"
95
)->fetch_hash_all;
96
is_deeply($rows, [{key1 => 1, key2 => 2}]);
97

            
98
$dbi->dbh->disconnect;
99
$dbi = undef;
100
$dbi = DBIx::Custom->connect(
101
    dsn => "dbi:mysql:database=$database",
102
    user => $user,
103
    password => $password
104
);
105
$rows = $dbi->select(
106
  table => 'table1',
107
  where => {key1 => 1, key2 => 4},
108
  append => "order by key2 limit 0, 1"
109
)->fetch_hash_all;
110
is_deeply($rows, [{key1 => 1, key2 => 4}]);
111
$dbi->delete_all(table => 'table1');
112

            
113
test 'dbh';
114
{
115
    my $connector = DBIx::Connector->new(
116
        "dbi:mysql:database=$database",
117
        $user,
118
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
119
        DBIx::Custom->new->default_option
added common test executing ...
Yuki Kimoto authored on 2011-08-07
120
    );
121

            
122
    my $dbi = DBIx::Custom->connect(connector => $connector);
123
    $dbi->delete_all(table => 'table1');
124
    $dbi->do('insert into table1 (key1, key2) values (1, 2)');
125
    is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
126
    
127
    $dbi = DBIx::Custom->new;
128
    $dbi->dbh('a');
129
    is($dbi->{dbh}, 'a');
130
}
131

            
132
test 'transaction';
133
test 'dbh';
134
{
135
    my $connector = DBIx::Connector->new(
136
        "dbi:mysql:database=$database",
137
        $user,
138
        $password,
139
        DBIx::Custom->new->default_dbi_option
140
    );
141

            
142
    my $dbi = DBIx::Custom->connect(connector => $connector);
143
    $dbi->delete_all(table => 'table1');
144
    
145
    $dbi->connector->txn(sub {
146
        $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
147
        $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
148
    });
149
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
150
              [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
151

            
152
    $dbi->delete_all(table => 'table1');
153
    eval {
154
        $dbi->connector->txn(sub {
155
            $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
156
            die "Error";
157
            $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
158
        });
159
    };
160
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
161
              []);
162
}
163

            
164
use DBIx::Custom;
165
use Scalar::Util 'blessed';
166
{
167
    my $dbi = DBIx::Custom->connect(
168
        user => $user,
169
        password => $password,
170
        dsn => "dbi:mysql:dbname=$database"
171
    );
172
    $dbi->connect;
173
    
174
    ok(blessed $dbi->dbh);
175
    can_ok($dbi->dbh, qw/prepare/);
176
    ok($dbi->dbh->{AutoCommit});
177
    ok(!$dbi->dbh->{mysql_enable_utf8});
178
}
179

            
180
{
181
    my $dbi = DBIx::Custom->connect(
182
        user => $user,
183
        password => $password,
184
        dsn => "dbi:mysql:dbname=$database",
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
185
        option => {AutoCommit => 0, mysql_enable_utf8 => 1}
added common test executing ...
Yuki Kimoto authored on 2011-08-07
186
    );
187
    $dbi->connect;
188
    ok(!$dbi->dbh->{AutoCommit});
189
    #ok($dbi->dbh->{mysql_enable_utf8});
190
}
191

            
192
test 'fork';
193
{
194
    my $connector = DBIx::Connector->new(
195
        "dbi:mysql:database=$database",
196
        $user,
197
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
198
        DBIx::Custom->new->default_option
added common test executing ...
Yuki Kimoto authored on 2011-08-07
199
    );
200
    
201
    my $dbi = DBIx::Custom->new(connector => $connector);
202
    $dbi->delete_all(table => 'table1');
203
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
204
    die "Can't fork" unless defined (my $pid = fork);
205

            
206
    if ($pid) {
207
        # Parent
208
        my $result = $dbi->select(table => 'table1');
209
        is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2});
210
    }
211
    else {
212
        # Child
213
        my $result = $dbi->select(table => 'table1');
214
        die "Not OK" unless $result->fetch_hash_first->{key1} == 1;
215
    }
216
}
217