DBIx-Custom / t / private-mysql.t /
Newer Older
303 lines | 7.573kb
cleanup test
Yuki Kimoto authored on 2011-08-06
1
use Test::More;
2
use strict;
3
use warnings;
4

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

            
7
# user password database
8
our ($USER, $PASSWORD, $DATABASE) = connect_info();
9

            
10
plan skip_all => 'private MySQL test' unless $USER;
11

            
12
plan 'no_plan';
13

            
14
require DBIx::Connector;
15

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

            
19
# Functions for tests
20
sub connect_info {
21
    my $file = 'password.tmp';
22
    open my $fh, '<', $file
23
      or return;
24
    
25
    my ($user, $password, $database) = split(/\s/, (<$fh>)[0]);
26
    
27
    close $fh;
28
    
29
    return ($user, $password, $database);
30
}
31

            
32

            
33
# Varialbes for tests
34
my $dbi;
35
my $dbname;
36
my $rows;
37
my $result;
38

            
39
{
40
    package DBIx::Custom::MySQL;
41

            
42
    use strict;
43
    use warnings;
44

            
45
    use base 'DBIx::Custom';
46

            
47
    __PACKAGE__->attr([qw/database host port/]);
48

            
49
    sub connect {
50
        my $proto = shift;
51
        
52
        # Create a new object
53
        my $self = ref $proto ? $proto : $proto->new(@_);
54
        
55
        # Data source
56
        if (!$self->dsn) {
57
            my $database = $self->database;
58
            my $host     = $self->host;
59
            my $port     = $self->port;
60
            my $dsn = "dbi:mysql:";
61
            $dsn .= "database=$database;" if $database;
62
            $dsn .= "host=$host;"         if $host;
63
            $dsn .= "port=$port;"         if $port;
64
            $self->dsn($dsn);
65
        }
66
        
67
        return $self->SUPER::connect;
68
    }
69

            
70
    1;
71
}
72

            
73
test 'connect';
74
$dbi = DBIx::Custom::MySQL->new(user => $USER, password => $PASSWORD,
75
                    database => $DATABASE, host => 'localhost', port => '10000');
76
$dbi->connect;
77
like($dbi->dsn, qr/dbi:mysql:database=.*;host=localhost;port=10000;/, "created data source");
78
is(ref $dbi->dbh, 'DBI::db');
79

            
80
test 'attributes';
81
$dbi = DBIx::Custom::MySQL->new;
82
$dbi->host('a');
83
is($dbi->host, 'a', "host");
84
$dbi->port('b');
85
is($dbi->port, 'b', "port");
86

            
87
test 'limit';
88
$dbi = DBIx::Custom->connect(
89
    dsn => "dbi:mysql:database=$DATABASE",
90
    user => $USER,
91
    password => $PASSWORD
92
);
93
$dbi->delete_all(table => 'table1');
94
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
95
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
96
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
97
$dbi->query_builder->register_tag_processor(
98
    limit => sub {
99
        my ($count, $offset) = @_;
100
        
101
        my $s = '';
102
        $offset = 0 unless defined $offset;
103
        $s .= "limit $offset";
104
        $s .= ", $count";
105
        
106
        return [$s, []];
107
    }
108
);
109
$rows = $dbi->select(
110
  table => 'table1',
111
  where => {key1 => 1},
112
  append => "order by key2 {limit 1 0}"
113
)->fetch_hash_all;
114
is_deeply($rows, [{key1 => 1, key2 => 2}]);
115
$rows = $dbi->select(
116
  table => 'table1',
117
  where => {key1 => 1},
118
  append => "order by key2 {limit 2 1}"
119
)->fetch_hash_all;
120
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
121
$rows = $dbi->select(
122
  table => 'table1',
123
  where => {key1 => 1},
124
  append => "order by key2 {limit 1}"
125
)->fetch_hash_all;
126
is_deeply($rows, [{key1 => 1, key2 => 2}]);
127

            
128
$dbi->dbh->disconnect;
129
$dbi = undef;
130
$dbi = DBIx::Custom->connect(
131
    dsn => "dbi:mysql:database=$DATABASE",
132
    user => $USER,
133
    password => $PASSWORD
134
);
135
$rows = $dbi->select(
136
  table => 'table1',
137
  where => {key1 => 1, key2 => 4},
138
  append => "order by key2 limit 0, 1"
139
)->fetch_hash_all;
140
is_deeply($rows, [{key1 => 1, key2 => 4}]);
141
$dbi->delete_all(table => 'table1');
142

            
143
test 'type_rule';
144
$dbi = DBIx::Custom->connect(
145
    dsn => "dbi:mysql:database=$DATABASE",
146
    user => $USER,
147
    password => $PASSWORD
148
);
149
eval{$dbi->execute("create table date_test (date DATE, datetime DATETIME)")};
150
$dbi->each_column(
151
    sub {
152
        my ($self, $table, $column, $column_info) = @_;
153
    }
154
);
155

            
156
$dbi->type_rule(
157
    into1 => {
158
        date=> sub {
159
            my $date = shift;
160
            $date =~ s/aaaaa//g;
161
            return $date;
162
        },
163
        datetime => sub {
164
            my $date = shift;
165
            $date =~ s/ccccc//g;
166
            return $date;
167
        },
168
    },
169
    from1 => {
170
        # DATE
171
        9 => sub {
172
                my $date = shift;
173
                $date .= 'bbbbb';
174
                return $date;
175
        },
176
        # DATETIME or TIMPESTANM
177
        11 => sub {
178
                my $date = shift;
179
                $date .= 'ddddd';
180
                return $date;
181
        }
182
    }
183
);
184

            
185
$dbi->insert(
186
    {
187
        date => 'aaaaa2010-aaaaa11-12aaaaa',
188
        datetime => '2010-11ccccc-12 10:ccccc55:56'
189
    },
190
    table => 'date_test'
191
);
192
is_deeply(
193
    $dbi->select(table => 'date_test')->fetch,
194
    ['2010-11-12bbbbb', '2010-11-12 10:55:56ddddd']
195
);
196

            
197
$dbi->execute("drop table date_test");
198

            
199
test 'dbh';
200
{
201
    my $connector = DBIx::Connector->new(
202
        "dbi:mysql:database=$DATABASE",
203
        $USER,
204
        $PASSWORD,
205
        DBIx::Custom->new->default_dbi_option
206
    );
207

            
208
    my $dbi = DBIx::Custom->connect(connector => $connector);
209
    $dbi->delete_all(table => 'table1');
210
    $dbi->do('insert into table1 (key1, key2) values (1, 2)');
211
    is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
212
    
213
    $dbi = DBIx::Custom->new;
214
    $dbi->dbh('a');
215
    is($dbi->{dbh}, 'a');
216
}
217

            
218
test 'transaction';
219
test 'dbh';
220
{
221
    my $connector = DBIx::Connector->new(
222
        "dbi:mysql:database=$DATABASE",
223
        $USER,
224
        $PASSWORD,
225
        DBIx::Custom->new->default_dbi_option
226
    );
227

            
228
    my $dbi = DBIx::Custom->connect(connector => $connector);
229
    $dbi->delete_all(table => 'table1');
230
    
231
    $dbi->connector->txn(sub {
232
        $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
233
        $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
234
    });
235
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
236
              [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
237

            
238
    $dbi->delete_all(table => 'table1');
239
    eval {
240
        $dbi->connector->txn(sub {
241
            $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
242
            die "Error";
243
            $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
244
        });
245
    };
246
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
247
              []);
248
}
249

            
cleanup test
Yuki Kimoto authored on 2011-08-06
250
use DBIx::Custom;
251
use Scalar::Util 'blessed';
252
{
253
    my $dbi = DBIx::Custom->connect(
254
        user => $USER,
255
        password => $PASSWORD,
256
        dsn => "dbi:mysql:dbname=$DATABASE"
257
    );
258
    $dbi->connect;
259
    
260
    ok(blessed $dbi->dbh);
261
    can_ok($dbi->dbh, qw/prepare/);
262
    ok($dbi->dbh->{AutoCommit});
263
    ok(!$dbi->dbh->{mysql_enable_utf8});
264
}
265

            
266
{
267
    my $dbi = DBIx::Custom->connect(
268
        user => $USER,
269
        password => $PASSWORD,
270
        dsn => "dbi:mysql:dbname=$DATABASE",
271
        dbi_options => {AutoCommit => 0, mysql_enable_utf8 => 1}
272
    );
273
    $dbi->connect;
274
    ok(!$dbi->dbh->{AutoCommit});
275
    #ok($dbi->dbh->{mysql_enable_utf8});
276
}
277

            
cleanup test
Yuki Kimoto authored on 2011-08-06
278
test 'fork';
279
{
280
    my $connector = DBIx::Connector->new(
281
        "dbi:mysql:database=$DATABASE",
282
        $USER,
283
        $PASSWORD,
284
        DBIx::Custom->new->default_dbi_option
285
    );
286
    
287
    my $dbi = DBIx::Custom->new(connector => $connector);
288
    $dbi->delete_all(table => 'table1');
289
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
290
    die "Can't fork" unless defined (my $pid = fork);
291

            
292
    if ($pid) {
293
        # Parent
294
        my $result = $dbi->select(table => 'table1');
295
        is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2});
296
    }
297
    else {
298
        # Child
299
        my $result = $dbi->select(table => 'table1');
300
        die "Not OK" unless $result->fetch_hash_first->{key1} == 1;
301
    }
302
}
303