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

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

            
cleanup test
Yuki Kimoto authored on 2011-08-15
8
my $dbi;
9

            
10
plan skip_all => 'mysql private test' unless -f "$FindBin::Bin/run/mysql.run"
11
  && eval { $dbi = DBIx::Custom->connect; 1 };
added common test executing ...
Yuki Kimoto authored on 2011-08-07
12
plan 'no_plan';
13

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

            
16
# user password database
17
our ($user, $password, $database) = qw/appuser 123456 usertest/;
18

            
19
require DBIx::Connector;
20

            
21
# Function for test name
22
sub test { print "# $_[0]\n" }
23

            
24
# Varialbes for tests
25
my $dbname;
26
my $rows;
27
my $result;
28

            
29
test 'connect';
30
eval {
31
    $dbi = DBIx::Custom->new(
32
        dsn => "dbi:mysql:database=$database;host=localhost;port=10000",
33
        user => $user,
34
        password => $password
35
    );
36
};
37
ok(!$@);
38

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
39
# Test memory leaks
40
for (1 .. 200) {
41
    $dbi = DBIx::Custom->connect(
42
        dsn => "dbi:mysql:database=$database;host=localhost;port=10000",
43
        user => $user,
44
        password => $password
45
    );
46
    $dbi->query_builder;
47
    $dbi->create_model(table => $table1);
48
    $dbi->create_model(table => $table2);
49
}
50

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

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

            
107
test 'type_rule';
108
$dbi = DBIx::Custom->connect(
109
    dsn => "dbi:mysql:database=$database",
110
    user => $user,
111
    password => $password
112
);
113
eval{$dbi->execute("create table date_test (date DATE, datetime DATETIME)")};
114
$dbi->each_column(
115
    sub {
116
        my ($self, $table, $column, $column_info) = @_;
117
    }
118
);
119

            
120
$dbi->type_rule(
121
    into1 => {
122
        date=> sub {
123
            my $date = shift;
124
            $date =~ s/aaaaa//g;
125
            return $date;
126
        },
127
        datetime => sub {
128
            my $date = shift;
129
            $date =~ s/ccccc//g;
130
            return $date;
131
        },
132
    },
133
    from1 => {
134
        # DATE
135
        9 => sub {
136
                my $date = shift;
137
                $date .= 'bbbbb';
138
                return $date;
139
        },
140
        # DATETIME or TIMPESTANM
141
        11 => sub {
142
                my $date = shift;
143
                $date .= 'ddddd';
144
                return $date;
145
        }
146
    }
147
);
148

            
149
$dbi->insert(
150
    {
151
        date => 'aaaaa2010-aaaaa11-12aaaaa',
152
        datetime => '2010-11ccccc-12 10:ccccc55:56'
153
    },
154
    table => 'date_test'
155
);
156
is_deeply(
157
    $dbi->select(table => 'date_test')->fetch,
158
    ['2010-11-12bbbbb', '2010-11-12 10:55:56ddddd']
159
);
160

            
161
$dbi->execute("drop table date_test");
162

            
163
test 'dbh';
164
{
165
    my $connector = DBIx::Connector->new(
166
        "dbi:mysql:database=$database",
167
        $user,
168
        $password,
169
        DBIx::Custom->new->default_dbi_option
170
    );
171

            
172
    my $dbi = DBIx::Custom->connect(connector => $connector);
173
    $dbi->delete_all(table => 'table1');
174
    $dbi->do('insert into table1 (key1, key2) values (1, 2)');
175
    is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
176
    
177
    $dbi = DBIx::Custom->new;
178
    $dbi->dbh('a');
179
    is($dbi->{dbh}, 'a');
180
}
181

            
182
test 'transaction';
183
test 'dbh';
184
{
185
    my $connector = DBIx::Connector->new(
186
        "dbi:mysql:database=$database",
187
        $user,
188
        $password,
189
        DBIx::Custom->new->default_dbi_option
190
    );
191

            
192
    my $dbi = DBIx::Custom->connect(connector => $connector);
193
    $dbi->delete_all(table => 'table1');
194
    
195
    $dbi->connector->txn(sub {
196
        $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
197
        $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
198
    });
199
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
200
              [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
201

            
202
    $dbi->delete_all(table => 'table1');
203
    eval {
204
        $dbi->connector->txn(sub {
205
            $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
206
            die "Error";
207
            $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
208
        });
209
    };
210
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
211
              []);
212
}
213

            
214
use DBIx::Custom;
215
use Scalar::Util 'blessed';
216
{
217
    my $dbi = DBIx::Custom->connect(
218
        user => $user,
219
        password => $password,
220
        dsn => "dbi:mysql:dbname=$database"
221
    );
222
    $dbi->connect;
223
    
224
    ok(blessed $dbi->dbh);
225
    can_ok($dbi->dbh, qw/prepare/);
226
    ok($dbi->dbh->{AutoCommit});
227
    ok(!$dbi->dbh->{mysql_enable_utf8});
228
}
229

            
230
{
231
    my $dbi = DBIx::Custom->connect(
232
        user => $user,
233
        password => $password,
234
        dsn => "dbi:mysql:dbname=$database",
235
        dbi_options => {AutoCommit => 0, mysql_enable_utf8 => 1}
236
    );
237
    $dbi->connect;
238
    ok(!$dbi->dbh->{AutoCommit});
239
    #ok($dbi->dbh->{mysql_enable_utf8});
240
}
241

            
242
test 'fork';
243
{
244
    my $connector = DBIx::Connector->new(
245
        "dbi:mysql:database=$database",
246
        $user,
247
        $password,
248
        DBIx::Custom->new->default_dbi_option
249
    );
250
    
251
    my $dbi = DBIx::Custom->new(connector => $connector);
252
    $dbi->delete_all(table => 'table1');
253
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
254
    die "Can't fork" unless defined (my $pid = fork);
255

            
256
    if ($pid) {
257
        # Parent
258
        my $result = $dbi->select(table => 'table1');
259
        is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2});
260
    }
261
    else {
262
        # Child
263
        my $result = $dbi->select(table => 'table1');
264
        die "Not OK" unless $result->fetch_hash_first->{key1} == 1;
265
    }
266
}
267