DBIx-Custom / t / mysql.t /
Newer Older
304 lines | 8.004kb
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;
micro optimization
Yuki Kimoto authored on 2011-10-31
32
my $row;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
33
my $rows;
34
my $result;
fixed update_or_insert metho...
Yuki Kimoto authored on 2011-10-31
35
my $model;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
36

            
37
test 'connect';
38
eval {
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
39
    $dbi = DBIx::Custom->connect(
added common test executing ...
Yuki Kimoto authored on 2011-08-07
40
        dsn => "dbi:mysql:database=$database;host=localhost;port=10000",
41
        user => $user,
42
        password => $password
43
    );
44
};
45
ok(!$@);
46

            
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
47
eval { $dbi->do('drop table table1') };
48
$dbi->do('create table table1 (key1 varchar(255), key2 varchar(255)) engine=InnoDB');
49

            
added EXPERIMENTAL bulk_inse...
Yuki Kimoto authored on 2011-11-27
50
test 'bulk_insert';
51
$dbi->delete_all(table => 'table1');
52
$dbi->insert(
53
    [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}],
54
    table => 'table1',
55
    bulk_insert => 1
56
);
57
like($dbi->last_sql, qr/(\?.+){4}/);
58
$rows = $dbi->select(table => 'table1')->all;
59
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
60

            
61
$dbi->delete_all(table => 'table1');
62
$dbi->insert(
63
    [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}],
64
    table => 'table1',
65
    bulk_insert => 1,
66
    filter => {key1 => sub { $_[0] * 2 }}
67
);
68
like($dbi->last_sql, qr/(\?.+){4}/);
69
$rows = $dbi->select(table => 'table1')->all;
70
is_deeply($rows, [{key1 => 2, key2 => 2}, {key1 => 6, key2 => 4}]);
71

            
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
72
test 'update_or_insert';
73
$dbi->delete_all(table => 'table1');
74
$dbi->update_or_insert(
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
75
    {key2 => 2},
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
76
    table => 'table1',
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
77
    id => 1,
78
    primary_key => 'key1',
79
    option => {
80
        select => {append => 'for update'},
81
        insert => {append => '    #'},
82
        update => {append => '     #'}
83
    }
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
84
);
85

            
micro optimization
Yuki Kimoto authored on 2011-10-31
86
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one;
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
87
is_deeply($row, {key1 => 1, key2 => 2}, "basic");
88

            
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
89
$dbi->update_or_insert(
90
    {key2 => 3},
91
    table => 'table1',
92
    id => 1,
93
    primary_key => 'key1',
94
    option => {
95
        select => {append => 'for update'},
96
        insert => {append => '    #'},
97
        update => {append => '     #'}
98
    }
99
);
100

            
micro optimization
Yuki Kimoto authored on 2011-10-31
101
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one;
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
102
is_deeply($row, {key1 => 1, key2 => 3}, "basic");
103

            
fixed update_or_insert metho...
Yuki Kimoto authored on 2011-10-31
104
$dbi->delete_all(table => 'table1');
105
$model = $dbi->create_model(
106
    table => 'table1',
107
    primary_key => 'key1',
108
);
109
$model->update_or_insert(
110
    {key2 => 2},
111
    id => 1,
112
    option => {
113
        select => {append => 'for update'},
114
        insert => {append => '    #'},
115
        update => {append => '     #'}
116
    }
117
);
118
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one;
119
is_deeply($row, {key1 => 1, key2 => 2}, "basic");
120
$model->update_or_insert(
121
    {key2 => 3},
122
    id => 1,
123
    option => {
124
        select => {append => 'for update'},
125
        insert => {append => '    #'},
126
        update => {append => '     #'}
127
    }
128
);
129
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one;
130
is_deeply($row, {key1 => 1, key2 => 3}, "basic");
131

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
132
# Test memory leaks
133
for (1 .. 200) {
134
    $dbi = DBIx::Custom->connect(
135
        dsn => "dbi:mysql:database=$database;host=localhost;port=10000",
136
        user => $user,
137
        password => $password
138
    );
139
    $dbi->query_builder;
cleanup
Yuki Kimoto authored on 2011-08-16
140
    $dbi->create_model(table => 'table1');
141
    $dbi->create_model(table => 'table2');
added memory leak check test
Yuki Kimoto authored on 2011-08-15
142
}
143

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
144
test 'limit';
145
$dbi = DBIx::Custom->connect(
146
    dsn => "dbi:mysql:database=$database",
147
    user => $user,
148
    password => $password
149
);
150
$dbi->delete_all(table => 'table1');
151
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
152
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
153
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
test cleanup
Yuki Kimoto authored on 2011-08-10
154
$dbi->register_tag(
added common test executing ...
Yuki Kimoto authored on 2011-08-07
155
    limit => sub {
156
        my ($count, $offset) = @_;
157
        
158
        my $s = '';
159
        $offset = 0 unless defined $offset;
160
        $s .= "limit $offset";
161
        $s .= ", $count";
162
        
163
        return [$s, []];
164
    }
165
);
166
$rows = $dbi->select(
167
  table => 'table1',
168
  where => {key1 => 1},
169
  append => "order by key2 {limit 1 0}"
170
)->fetch_hash_all;
171
is_deeply($rows, [{key1 => 1, key2 => 2}]);
172
$rows = $dbi->select(
173
  table => 'table1',
174
  where => {key1 => 1},
175
  append => "order by key2 {limit 2 1}"
176
)->fetch_hash_all;
177
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
178
$rows = $dbi->select(
179
  table => 'table1',
180
  where => {key1 => 1},
181
  append => "order by key2 {limit 1}"
182
)->fetch_hash_all;
183
is_deeply($rows, [{key1 => 1, key2 => 2}]);
184

            
185
$dbi->dbh->disconnect;
186
$dbi = undef;
187
$dbi = DBIx::Custom->connect(
188
    dsn => "dbi:mysql:database=$database",
189
    user => $user,
190
    password => $password
191
);
192
$rows = $dbi->select(
193
  table => 'table1',
194
  where => {key1 => 1, key2 => 4},
195
  append => "order by key2 limit 0, 1"
196
)->fetch_hash_all;
197
is_deeply($rows, [{key1 => 1, key2 => 4}]);
198
$dbi->delete_all(table => 'table1');
199

            
200
test 'dbh';
201
{
202
    my $connector = DBIx::Connector->new(
203
        "dbi:mysql:database=$database",
204
        $user,
205
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
206
        DBIx::Custom->new->default_option
added common test executing ...
Yuki Kimoto authored on 2011-08-07
207
    );
208

            
209
    my $dbi = DBIx::Custom->connect(connector => $connector);
210
    $dbi->delete_all(table => 'table1');
211
    $dbi->do('insert into table1 (key1, key2) values (1, 2)');
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
212
    is($dbi->select(table => 'table1')->fetch_hash_one->{key1}, 1);
added common test executing ...
Yuki Kimoto authored on 2011-08-07
213
    
214
    $dbi = DBIx::Custom->new;
215
    $dbi->dbh('a');
216
    is($dbi->{dbh}, 'a');
217
}
218

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

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

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

            
251
use DBIx::Custom;
252
use Scalar::Util 'blessed';
253
{
254
    my $dbi = DBIx::Custom->connect(
255
        user => $user,
256
        password => $password,
257
        dsn => "dbi:mysql:dbname=$database"
258
    );
259
    $dbi->connect;
260
    
261
    ok(blessed $dbi->dbh);
262
    can_ok($dbi->dbh, qw/prepare/);
263
    ok($dbi->dbh->{AutoCommit});
264
    ok(!$dbi->dbh->{mysql_enable_utf8});
265
}
266

            
267
{
268
    my $dbi = DBIx::Custom->connect(
269
        user => $user,
270
        password => $password,
271
        dsn => "dbi:mysql:dbname=$database",
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
272
        option => {AutoCommit => 0, mysql_enable_utf8 => 1}
added common test executing ...
Yuki Kimoto authored on 2011-08-07
273
    );
274
    $dbi->connect;
275
    ok(!$dbi->dbh->{AutoCommit});
276
    #ok($dbi->dbh->{mysql_enable_utf8});
277
}
278

            
279
test 'fork';
280
{
281
    my $connector = DBIx::Connector->new(
282
        "dbi:mysql:database=$database",
283
        $user,
284
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
285
        DBIx::Custom->new->default_option
added common test executing ...
Yuki Kimoto authored on 2011-08-07
286
    );
287
    
288
    my $dbi = DBIx::Custom->new(connector => $connector);
289
    $dbi->delete_all(table => 'table1');
290
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
291
    die "Can't fork" unless defined (my $pid = fork);
292

            
293
    if ($pid) {
294
        # Parent
295
        my $result = $dbi->select(table => 'table1');
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
296
        is_deeply($result->fetch_hash_one, {key1 => 1, key2 => 2});
added common test executing ...
Yuki Kimoto authored on 2011-08-07
297
    }
298
    else {
299
        # Child
300
        my $result = $dbi->select(table => 'table1');
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
301
        die "Not OK" unless $result->fetch_hash_one->{key1} == 1;
added common test executing ...
Yuki Kimoto authored on 2011-08-07
302
    }
303
}
304