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

            
50
test 'update_or_insert';
51
$dbi->delete_all(table => 'table1');
52
$dbi->update_or_insert(
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
53
    {key2 => 2},
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
54
    table => 'table1',
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
55
    id => 1,
56
    primary_key => 'key1',
57
    option => {
58
        select => {append => 'for update'},
59
        insert => {append => '    #'},
60
        update => {append => '     #'}
61
    }
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
62
);
63

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

            
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
67
$dbi->update_or_insert(
68
    {key2 => 3},
69
    table => 'table1',
70
    id => 1,
71
    primary_key => 'key1',
72
    option => {
73
        select => {append => 'for update'},
74
        insert => {append => '    #'},
75
        update => {append => '     #'}
76
    }
77
);
78

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

            
fixed update_or_insert metho...
Yuki Kimoto authored on 2011-10-31
82
$dbi->delete_all(table => 'table1');
83
$model = $dbi->create_model(
84
    table => 'table1',
85
    primary_key => 'key1',
86
);
87
$model->update_or_insert(
88
    {key2 => 2},
89
    id => 1,
90
    option => {
91
        select => {append => 'for update'},
92
        insert => {append => '    #'},
93
        update => {append => '     #'}
94
    }
95
);
96
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one;
97
is_deeply($row, {key1 => 1, key2 => 2}, "basic");
98
$model->update_or_insert(
99
    {key2 => 3},
100
    id => 1,
101
    option => {
102
        select => {append => 'for update'},
103
        insert => {append => '    #'},
104
        update => {append => '     #'}
105
    }
106
);
107
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one;
108
is_deeply($row, {key1 => 1, key2 => 3}, "basic");
109

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
110
# Test memory leaks
111
for (1 .. 200) {
112
    $dbi = DBIx::Custom->connect(
113
        dsn => "dbi:mysql:database=$database;host=localhost;port=10000",
114
        user => $user,
115
        password => $password
116
    );
117
    $dbi->query_builder;
cleanup
Yuki Kimoto authored on 2011-08-16
118
    $dbi->create_model(table => 'table1');
119
    $dbi->create_model(table => 'table2');
added memory leak check test
Yuki Kimoto authored on 2011-08-15
120
}
121

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
122
test 'limit';
123
$dbi = DBIx::Custom->connect(
124
    dsn => "dbi:mysql:database=$database",
125
    user => $user,
126
    password => $password
127
);
128
$dbi->delete_all(table => 'table1');
129
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
130
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
131
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
test cleanup
Yuki Kimoto authored on 2011-08-10
132
$dbi->register_tag(
added common test executing ...
Yuki Kimoto authored on 2011-08-07
133
    limit => sub {
134
        my ($count, $offset) = @_;
135
        
136
        my $s = '';
137
        $offset = 0 unless defined $offset;
138
        $s .= "limit $offset";
139
        $s .= ", $count";
140
        
141
        return [$s, []];
142
    }
143
);
144
$rows = $dbi->select(
145
  table => 'table1',
146
  where => {key1 => 1},
147
  append => "order by key2 {limit 1 0}"
148
)->fetch_hash_all;
149
is_deeply($rows, [{key1 => 1, key2 => 2}]);
150
$rows = $dbi->select(
151
  table => 'table1',
152
  where => {key1 => 1},
153
  append => "order by key2 {limit 2 1}"
154
)->fetch_hash_all;
155
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
156
$rows = $dbi->select(
157
  table => 'table1',
158
  where => {key1 => 1},
159
  append => "order by key2 {limit 1}"
160
)->fetch_hash_all;
161
is_deeply($rows, [{key1 => 1, key2 => 2}]);
162

            
163
$dbi->dbh->disconnect;
164
$dbi = undef;
165
$dbi = DBIx::Custom->connect(
166
    dsn => "dbi:mysql:database=$database",
167
    user => $user,
168
    password => $password
169
);
170
$rows = $dbi->select(
171
  table => 'table1',
172
  where => {key1 => 1, key2 => 4},
173
  append => "order by key2 limit 0, 1"
174
)->fetch_hash_all;
175
is_deeply($rows, [{key1 => 1, key2 => 4}]);
176
$dbi->delete_all(table => 'table1');
177

            
178
test 'dbh';
179
{
180
    my $connector = DBIx::Connector->new(
181
        "dbi:mysql:database=$database",
182
        $user,
183
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
184
        DBIx::Custom->new->default_option
added common test executing ...
Yuki Kimoto authored on 2011-08-07
185
    );
186

            
187
    my $dbi = DBIx::Custom->connect(connector => $connector);
188
    $dbi->delete_all(table => 'table1');
189
    $dbi->do('insert into table1 (key1, key2) values (1, 2)');
190
    is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
191
    
192
    $dbi = DBIx::Custom->new;
193
    $dbi->dbh('a');
194
    is($dbi->{dbh}, 'a');
195
}
196

            
197
test 'transaction';
198
test 'dbh';
199
{
200
    my $connector = DBIx::Connector->new(
201
        "dbi:mysql:database=$database",
202
        $user,
203
        $password,
204
        DBIx::Custom->new->default_dbi_option
205
    );
206

            
207
    my $dbi = DBIx::Custom->connect(connector => $connector);
208
    $dbi->delete_all(table => 'table1');
209
    
210
    $dbi->connector->txn(sub {
211
        $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
212
        $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
213
    });
214
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
215
              [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
216

            
217
    $dbi->delete_all(table => 'table1');
218
    eval {
219
        $dbi->connector->txn(sub {
220
            $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
221
            die "Error";
222
            $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
223
        });
224
    };
225
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
226
              []);
227
}
228

            
229
use DBIx::Custom;
230
use Scalar::Util 'blessed';
231
{
232
    my $dbi = DBIx::Custom->connect(
233
        user => $user,
234
        password => $password,
235
        dsn => "dbi:mysql:dbname=$database"
236
    );
237
    $dbi->connect;
238
    
239
    ok(blessed $dbi->dbh);
240
    can_ok($dbi->dbh, qw/prepare/);
241
    ok($dbi->dbh->{AutoCommit});
242
    ok(!$dbi->dbh->{mysql_enable_utf8});
243
}
244

            
245
{
246
    my $dbi = DBIx::Custom->connect(
247
        user => $user,
248
        password => $password,
249
        dsn => "dbi:mysql:dbname=$database",
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
250
        option => {AutoCommit => 0, mysql_enable_utf8 => 1}
added common test executing ...
Yuki Kimoto authored on 2011-08-07
251
    );
252
    $dbi->connect;
253
    ok(!$dbi->dbh->{AutoCommit});
254
    #ok($dbi->dbh->{mysql_enable_utf8});
255
}
256

            
257
test 'fork';
258
{
259
    my $connector = DBIx::Connector->new(
260
        "dbi:mysql:database=$database",
261
        $user,
262
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
263
        DBIx::Custom->new->default_option
added common test executing ...
Yuki Kimoto authored on 2011-08-07
264
    );
265
    
266
    my $dbi = DBIx::Custom->new(connector => $connector);
267
    $dbi->delete_all(table => 'table1');
268
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
269
    die "Can't fork" unless defined (my $pid = fork);
270

            
271
    if ($pid) {
272
        # Parent
273
        my $result = $dbi->select(table => 'table1');
274
        is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2});
275
    }
276
    else {
277
        # Child
278
        my $result = $dbi->select(table => 'table1');
279
        die "Not OK" unless $result->fetch_hash_first->{key1} == 1;
280
    }
281
}
282