DBIx-Custom / t / mysql.t /
Newer Older
253 lines | 6.574kb
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;
32
my $rows;
33
my $result;
34

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

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

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

            
63
my $row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one;
64
is_deeply($row, {key1 => 1, key2 => 2}, "basic");
65

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

            
78
my $row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one;
79
is_deeply($row, {key1 => 1, key2 => 3}, "basic");
80

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
81
# Test memory leaks
82
for (1 .. 200) {
83
    $dbi = DBIx::Custom->connect(
84
        dsn => "dbi:mysql:database=$database;host=localhost;port=10000",
85
        user => $user,
86
        password => $password
87
    );
88
    $dbi->query_builder;
cleanup
Yuki Kimoto authored on 2011-08-16
89
    $dbi->create_model(table => 'table1');
90
    $dbi->create_model(table => 'table2');
added memory leak check test
Yuki Kimoto authored on 2011-08-15
91
}
92

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
93
test 'limit';
94
$dbi = DBIx::Custom->connect(
95
    dsn => "dbi:mysql:database=$database",
96
    user => $user,
97
    password => $password
98
);
99
$dbi->delete_all(table => 'table1');
100
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
101
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
102
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
test cleanup
Yuki Kimoto authored on 2011-08-10
103
$dbi->register_tag(
added common test executing ...
Yuki Kimoto authored on 2011-08-07
104
    limit => sub {
105
        my ($count, $offset) = @_;
106
        
107
        my $s = '';
108
        $offset = 0 unless defined $offset;
109
        $s .= "limit $offset";
110
        $s .= ", $count";
111
        
112
        return [$s, []];
113
    }
114
);
115
$rows = $dbi->select(
116
  table => 'table1',
117
  where => {key1 => 1},
118
  append => "order by key2 {limit 1 0}"
119
)->fetch_hash_all;
120
is_deeply($rows, [{key1 => 1, key2 => 2}]);
121
$rows = $dbi->select(
122
  table => 'table1',
123
  where => {key1 => 1},
124
  append => "order by key2 {limit 2 1}"
125
)->fetch_hash_all;
126
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
127
$rows = $dbi->select(
128
  table => 'table1',
129
  where => {key1 => 1},
130
  append => "order by key2 {limit 1}"
131
)->fetch_hash_all;
132
is_deeply($rows, [{key1 => 1, key2 => 2}]);
133

            
134
$dbi->dbh->disconnect;
135
$dbi = undef;
136
$dbi = DBIx::Custom->connect(
137
    dsn => "dbi:mysql:database=$database",
138
    user => $user,
139
    password => $password
140
);
141
$rows = $dbi->select(
142
  table => 'table1',
143
  where => {key1 => 1, key2 => 4},
144
  append => "order by key2 limit 0, 1"
145
)->fetch_hash_all;
146
is_deeply($rows, [{key1 => 1, key2 => 4}]);
147
$dbi->delete_all(table => 'table1');
148

            
149
test 'dbh';
150
{
151
    my $connector = DBIx::Connector->new(
152
        "dbi:mysql:database=$database",
153
        $user,
154
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
155
        DBIx::Custom->new->default_option
added common test executing ...
Yuki Kimoto authored on 2011-08-07
156
    );
157

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

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

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

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

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

            
216
{
217
    my $dbi = DBIx::Custom->connect(
218
        user => $user,
219
        password => $password,
220
        dsn => "dbi:mysql:dbname=$database",
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
221
        option => {AutoCommit => 0, mysql_enable_utf8 => 1}
added common test executing ...
Yuki Kimoto authored on 2011-08-07
222
    );
223
    $dbi->connect;
224
    ok(!$dbi->dbh->{AutoCommit});
225
    #ok($dbi->dbh->{mysql_enable_utf8});
226
}
227

            
228
test 'fork';
229
{
230
    my $connector = DBIx::Connector->new(
231
        "dbi:mysql:database=$database",
232
        $user,
233
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
234
        DBIx::Custom->new->default_option
added common test executing ...
Yuki Kimoto authored on 2011-08-07
235
    );
236
    
237
    my $dbi = DBIx::Custom->new(connector => $connector);
238
    $dbi->delete_all(table => 'table1');
239
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
240
    die "Can't fork" unless defined (my $pid = fork);
241

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