DBIx-Custom / t / mysql.t /
Newer Older
232 lines | 6.061kb
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');
50
$dbi->update_or_insert(
51
    {key1 => 1, key2 => 2},
52
    table => 'table1',
53
    where => {key1 => 1},
54
    select_option => {append => 'for update'}
55
);
56

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

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
60
# Test memory leaks
61
for (1 .. 200) {
62
    $dbi = DBIx::Custom->connect(
63
        dsn => "dbi:mysql:database=$database;host=localhost;port=10000",
64
        user => $user,
65
        password => $password
66
    );
67
    $dbi->query_builder;
cleanup
Yuki Kimoto authored on 2011-08-16
68
    $dbi->create_model(table => 'table1');
69
    $dbi->create_model(table => 'table2');
added memory leak check test
Yuki Kimoto authored on 2011-08-15
70
}
71

            
added common test executing ...
Yuki Kimoto authored on 2011-08-07
72
test 'limit';
73
$dbi = DBIx::Custom->connect(
74
    dsn => "dbi:mysql:database=$database",
75
    user => $user,
76
    password => $password
77
);
78
$dbi->delete_all(table => 'table1');
79
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
80
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
81
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
test cleanup
Yuki Kimoto authored on 2011-08-10
82
$dbi->register_tag(
added common test executing ...
Yuki Kimoto authored on 2011-08-07
83
    limit => sub {
84
        my ($count, $offset) = @_;
85
        
86
        my $s = '';
87
        $offset = 0 unless defined $offset;
88
        $s .= "limit $offset";
89
        $s .= ", $count";
90
        
91
        return [$s, []];
92
    }
93
);
94
$rows = $dbi->select(
95
  table => 'table1',
96
  where => {key1 => 1},
97
  append => "order by key2 {limit 1 0}"
98
)->fetch_hash_all;
99
is_deeply($rows, [{key1 => 1, key2 => 2}]);
100
$rows = $dbi->select(
101
  table => 'table1',
102
  where => {key1 => 1},
103
  append => "order by key2 {limit 2 1}"
104
)->fetch_hash_all;
105
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
106
$rows = $dbi->select(
107
  table => 'table1',
108
  where => {key1 => 1},
109
  append => "order by key2 {limit 1}"
110
)->fetch_hash_all;
111
is_deeply($rows, [{key1 => 1, key2 => 2}]);
112

            
113
$dbi->dbh->disconnect;
114
$dbi = undef;
115
$dbi = DBIx::Custom->connect(
116
    dsn => "dbi:mysql:database=$database",
117
    user => $user,
118
    password => $password
119
);
120
$rows = $dbi->select(
121
  table => 'table1',
122
  where => {key1 => 1, key2 => 4},
123
  append => "order by key2 limit 0, 1"
124
)->fetch_hash_all;
125
is_deeply($rows, [{key1 => 1, key2 => 4}]);
126
$dbi->delete_all(table => 'table1');
127

            
128
test 'dbh';
129
{
130
    my $connector = DBIx::Connector->new(
131
        "dbi:mysql:database=$database",
132
        $user,
133
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
134
        DBIx::Custom->new->default_option
added common test executing ...
Yuki Kimoto authored on 2011-08-07
135
    );
136

            
137
    my $dbi = DBIx::Custom->connect(connector => $connector);
138
    $dbi->delete_all(table => 'table1');
139
    $dbi->do('insert into table1 (key1, key2) values (1, 2)');
140
    is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
141
    
142
    $dbi = DBIx::Custom->new;
143
    $dbi->dbh('a');
144
    is($dbi->{dbh}, 'a');
145
}
146

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

            
157
    my $dbi = DBIx::Custom->connect(connector => $connector);
158
    $dbi->delete_all(table => 'table1');
159
    
160
    $dbi->connector->txn(sub {
161
        $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
162
        $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
163
    });
164
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
165
              [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
166

            
167
    $dbi->delete_all(table => 'table1');
168
    eval {
169
        $dbi->connector->txn(sub {
170
            $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
171
            die "Error";
172
            $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
173
        });
174
    };
175
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
176
              []);
177
}
178

            
179
use DBIx::Custom;
180
use Scalar::Util 'blessed';
181
{
182
    my $dbi = DBIx::Custom->connect(
183
        user => $user,
184
        password => $password,
185
        dsn => "dbi:mysql:dbname=$database"
186
    );
187
    $dbi->connect;
188
    
189
    ok(blessed $dbi->dbh);
190
    can_ok($dbi->dbh, qw/prepare/);
191
    ok($dbi->dbh->{AutoCommit});
192
    ok(!$dbi->dbh->{mysql_enable_utf8});
193
}
194

            
195
{
196
    my $dbi = DBIx::Custom->connect(
197
        user => $user,
198
        password => $password,
199
        dsn => "dbi:mysql:dbname=$database",
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
200
        option => {AutoCommit => 0, mysql_enable_utf8 => 1}
added common test executing ...
Yuki Kimoto authored on 2011-08-07
201
    );
202
    $dbi->connect;
203
    ok(!$dbi->dbh->{AutoCommit});
204
    #ok($dbi->dbh->{mysql_enable_utf8});
205
}
206

            
207
test 'fork';
208
{
209
    my $connector = DBIx::Connector->new(
210
        "dbi:mysql:database=$database",
211
        $user,
212
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
213
        DBIx::Custom->new->default_option
added common test executing ...
Yuki Kimoto authored on 2011-08-07
214
    );
215
    
216
    my $dbi = DBIx::Custom->new(connector => $connector);
217
    $dbi->delete_all(table => 'table1');
218
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
219
    die "Can't fork" unless defined (my $pid = fork);
220

            
221
    if ($pid) {
222
        # Parent
223
        my $result = $dbi->select(table => 'table1');
224
        is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2});
225
    }
226
    else {
227
        # Child
228
        my $result = $dbi->select(table => 'table1');
229
        die "Not OK" unless $result->fetch_hash_first->{key1} == 1;
230
    }
231
}
232