DBIx-Custom / t / mysql.t /
Newer Older
252 lines | 6.315kb
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;
6

            
test cleanup
Yuki Kimoto authored on 2011-08-08
7
plan skip_all => 'mysql private test' unless -f "$FindBin::Bin/run/mysql.tmp";
added common test executing ...
Yuki Kimoto authored on 2011-08-07
8
plan 'no_plan';
9

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

            
12
# user password database
13
our ($user, $password, $database) = qw/appuser 123456 usertest/;
14

            
15
require DBIx::Connector;
16

            
17
# Function for test name
18
sub test { print "# $_[0]\n" }
19

            
20
# Varialbes for tests
21
my $dbi;
22
my $dbname;
23
my $rows;
24
my $result;
25

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

            
36
test 'limit';
37
$dbi = DBIx::Custom->connect(
38
    dsn => "dbi:mysql:database=$database",
39
    user => $user,
40
    password => $password
41
);
42
$dbi->delete_all(table => 'table1');
43
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
44
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
45
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
test cleanup
Yuki Kimoto authored on 2011-08-10
46
$dbi->register_tag(
added common test executing ...
Yuki Kimoto authored on 2011-08-07
47
    limit => sub {
48
        my ($count, $offset) = @_;
49
        
50
        my $s = '';
51
        $offset = 0 unless defined $offset;
52
        $s .= "limit $offset";
53
        $s .= ", $count";
54
        
55
        return [$s, []];
56
    }
57
);
58
$rows = $dbi->select(
59
  table => 'table1',
60
  where => {key1 => 1},
61
  append => "order by key2 {limit 1 0}"
62
)->fetch_hash_all;
63
is_deeply($rows, [{key1 => 1, key2 => 2}]);
64
$rows = $dbi->select(
65
  table => 'table1',
66
  where => {key1 => 1},
67
  append => "order by key2 {limit 2 1}"
68
)->fetch_hash_all;
69
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
70
$rows = $dbi->select(
71
  table => 'table1',
72
  where => {key1 => 1},
73
  append => "order by key2 {limit 1}"
74
)->fetch_hash_all;
75
is_deeply($rows, [{key1 => 1, key2 => 2}]);
76

            
77
$dbi->dbh->disconnect;
78
$dbi = undef;
79
$dbi = DBIx::Custom->connect(
80
    dsn => "dbi:mysql:database=$database",
81
    user => $user,
82
    password => $password
83
);
84
$rows = $dbi->select(
85
  table => 'table1',
86
  where => {key1 => 1, key2 => 4},
87
  append => "order by key2 limit 0, 1"
88
)->fetch_hash_all;
89
is_deeply($rows, [{key1 => 1, key2 => 4}]);
90
$dbi->delete_all(table => 'table1');
91

            
92
test 'type_rule';
93
$dbi = DBIx::Custom->connect(
94
    dsn => "dbi:mysql:database=$database",
95
    user => $user,
96
    password => $password
97
);
98
eval{$dbi->execute("create table date_test (date DATE, datetime DATETIME)")};
99
$dbi->each_column(
100
    sub {
101
        my ($self, $table, $column, $column_info) = @_;
102
    }
103
);
104

            
105
$dbi->type_rule(
106
    into1 => {
107
        date=> sub {
108
            my $date = shift;
109
            $date =~ s/aaaaa//g;
110
            return $date;
111
        },
112
        datetime => sub {
113
            my $date = shift;
114
            $date =~ s/ccccc//g;
115
            return $date;
116
        },
117
    },
118
    from1 => {
119
        # DATE
120
        9 => sub {
121
                my $date = shift;
122
                $date .= 'bbbbb';
123
                return $date;
124
        },
125
        # DATETIME or TIMPESTANM
126
        11 => sub {
127
                my $date = shift;
128
                $date .= 'ddddd';
129
                return $date;
130
        }
131
    }
132
);
133

            
134
$dbi->insert(
135
    {
136
        date => 'aaaaa2010-aaaaa11-12aaaaa',
137
        datetime => '2010-11ccccc-12 10:ccccc55:56'
138
    },
139
    table => 'date_test'
140
);
141
is_deeply(
142
    $dbi->select(table => 'date_test')->fetch,
143
    ['2010-11-12bbbbb', '2010-11-12 10:55:56ddddd']
144
);
145

            
146
$dbi->execute("drop table date_test");
147

            
148
test 'dbh';
149
{
150
    my $connector = DBIx::Connector->new(
151
        "dbi:mysql:database=$database",
152
        $user,
153
        $password,
154
        DBIx::Custom->new->default_dbi_option
155
    );
156

            
157
    my $dbi = DBIx::Custom->connect(connector => $connector);
158
    $dbi->delete_all(table => 'table1');
159
    $dbi->do('insert into table1 (key1, key2) values (1, 2)');
160
    is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
161
    
162
    $dbi = DBIx::Custom->new;
163
    $dbi->dbh('a');
164
    is($dbi->{dbh}, 'a');
165
}
166

            
167
test 'transaction';
168
test 'dbh';
169
{
170
    my $connector = DBIx::Connector->new(
171
        "dbi:mysql:database=$database",
172
        $user,
173
        $password,
174
        DBIx::Custom->new->default_dbi_option
175
    );
176

            
177
    my $dbi = DBIx::Custom->connect(connector => $connector);
178
    $dbi->delete_all(table => 'table1');
179
    
180
    $dbi->connector->txn(sub {
181
        $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
182
        $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
183
    });
184
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
185
              [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
186

            
187
    $dbi->delete_all(table => 'table1');
188
    eval {
189
        $dbi->connector->txn(sub {
190
            $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
191
            die "Error";
192
            $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
193
        });
194
    };
195
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
196
              []);
197
}
198

            
199
use DBIx::Custom;
200
use Scalar::Util 'blessed';
201
{
202
    my $dbi = DBIx::Custom->connect(
203
        user => $user,
204
        password => $password,
205
        dsn => "dbi:mysql:dbname=$database"
206
    );
207
    $dbi->connect;
208
    
209
    ok(blessed $dbi->dbh);
210
    can_ok($dbi->dbh, qw/prepare/);
211
    ok($dbi->dbh->{AutoCommit});
212
    ok(!$dbi->dbh->{mysql_enable_utf8});
213
}
214

            
215
{
216
    my $dbi = DBIx::Custom->connect(
217
        user => $user,
218
        password => $password,
219
        dsn => "dbi:mysql:dbname=$database",
220
        dbi_options => {AutoCommit => 0, mysql_enable_utf8 => 1}
221
    );
222
    $dbi->connect;
223
    ok(!$dbi->dbh->{AutoCommit});
224
    #ok($dbi->dbh->{mysql_enable_utf8});
225
}
226

            
227
test 'fork';
228
{
229
    my $connector = DBIx::Connector->new(
230
        "dbi:mysql:database=$database",
231
        $user,
232
        $password,
233
        DBIx::Custom->new->default_dbi_option
234
    );
235
    
236
    my $dbi = DBIx::Custom->new(connector => $connector);
237
    $dbi->delete_all(table => 'table1');
238
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
239
    die "Can't fork" unless defined (my $pid = fork);
240

            
241
    if ($pid) {
242
        # Parent
243
        my $result = $dbi->select(table => 'table1');
244
        is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2});
245
    }
246
    else {
247
        # Child
248
        my $result = $dbi->select(table => 'table1');
249
        die "Not OK" unless $result->fetch_hash_first->{key1} == 1;
250
    }
251
}
252