DBIx-Custom / t / mysql.t /
Newer Older
255 lines | 6.38kb
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

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

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

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

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

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

            
149
$dbi->execute("drop table date_test");
150

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

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

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

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

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

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

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

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

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