DBIx-Custom / t / dbix-custom-mysql-private.t /
Newer Older
261 lines | 6.499kb
add examples
Yuki Kimoto authored on 2011-01-07
1
use Test::More;
2
use strict;
3
use warnings;
4

            
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
5
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
6

            
add examples
Yuki Kimoto authored on 2011-01-07
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

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
14
require DBIx::Connector;
15

            
add examples
Yuki Kimoto authored on 2011-01-07
16
# Function for test name
cleanup
Yuki Kimoto authored on 2011-01-23
17
sub test { print "# $_[0]\n" }
add examples
Yuki Kimoto authored on 2011-01-07
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;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
37
my $result;
add examples
Yuki Kimoto authored on 2011-01-07
38

            
fixed test
Yuki Kimoto authored on 2011-04-11
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
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
56
        if (!$self->dsn) {
fixed test
Yuki Kimoto authored on 2011-04-11
57
            my $database = $self->database;
58
            my $host     = $self->host;
59
            my $port     = $self->port;
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
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);
fixed test
Yuki Kimoto authored on 2011-04-11
65
        }
66
        
67
        return $self->SUPER::connect;
68
    }
69

            
70
    1;
71
}
add examples
Yuki Kimoto authored on 2011-01-07
72

            
73
test 'connect';
74
$dbi = DBIx::Custom::MySQL->new(user => $USER, password => $PASSWORD,
75
                    database => $DATABASE, host => 'localhost', port => '10000');
76
$dbi->connect;
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
77
like($dbi->dsn, qr/dbi:mysql:database=.*;host=localhost;port=10000;/, "created data source");
cleanup
Yuki Kimoto authored on 2011-01-23
78
is(ref $dbi->dbh, 'DBI::db');
add examples
Yuki Kimoto authored on 2011-01-07
79

            
80
test 'attributes';
81
$dbi = DBIx::Custom::MySQL->new;
82
$dbi->host('a');
cleanup
Yuki Kimoto authored on 2011-01-23
83
is($dbi->host, 'a', "host");
add examples
Yuki Kimoto authored on 2011-01-07
84
$dbi->port('b');
cleanup
Yuki Kimoto authored on 2011-01-23
85
is($dbi->port, 'b', "port");
add examples
Yuki Kimoto authored on 2011-01-07
86

            
87
test 'limit';
88
$dbi = DBIx::Custom->connect(
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
89
    dsn => "dbi:mysql:database=$DATABASE",
add examples
Yuki Kimoto authored on 2011-01-07
90
    user => $USER,
91
    password => $PASSWORD
92
);
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
93
$dbi->delete_all(table => 'table1');
add examples
Yuki Kimoto authored on 2011-01-07
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;
cleanup
Yuki Kimoto authored on 2011-01-23
114
is_deeply($rows, [{key1 => 1, key2 => 2}]);
add examples
Yuki Kimoto authored on 2011-01-07
115
$rows = $dbi->select(
116
  table => 'table1',
117
  where => {key1 => 1},
118
  append => "order by key2 {limit 2 1}"
119
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
120
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
add examples
Yuki Kimoto authored on 2011-01-07
121
$rows = $dbi->select(
122
  table => 'table1',
123
  where => {key1 => 1},
124
  append => "order by key2 {limit 1}"
125
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
126
is_deeply($rows, [{key1 => 1, key2 => 2}]);
add examples
Yuki Kimoto authored on 2011-01-07
127
$dbi->delete_all(table => 'table1');
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
128

            
improved type_rule method
Yuki Kimoto authored on 2011-06-10
129

            
130
test 'type_rule';
131
$dbi = DBIx::Custom->connect(
132
    dsn => "dbi:mysql:database=$DATABASE",
133
    user => $USER,
134
    password => $PASSWORD
135
);
136
eval{$dbi->execute("create table date_test (date DATE, datetime DATETIME)")};
137
$dbi->type_rule(
138
    DATE => {
139
        into=> sub {
140
            my $date = shift;
141
            $date =~ s/aaaaa//g;
142
            return $date;
143
        },
144
    },
simplify type_rule
Yuki Kimoto authored on 2011-06-10
145
    DATETIME => {
improved type_rule method
Yuki Kimoto authored on 2011-06-10
146
        into => sub {
147
            my $date = shift;
148
            $date =~ s/ccccc//g;
149
            return $date;
150
            
151
        },
152
    },
simplify type_rule
Yuki Kimoto authored on 2011-06-10
153
    # DATE
154
    9 => {
155
        from => sub {
156
            my $date = shift;
157
            $date .= 'bbbbb';
158
            return $date;
159
        }
160
    },
161
    # DATETIME or TIMPESTANM
162
    11 => {
improved type_rule method
Yuki Kimoto authored on 2011-06-10
163
        from => sub {
164
            my $date = shift;
165
            $date .= 'ddddd';
166
            return $date;
167
        }
168
    }
169
);
170

            
171
$dbi->insert(
172
    {
173
        date => 'aaaaa2010-aaaaa11-12aaaaa',
174
        datetime => '2010-11ccccc-12 10:ccccc55:56'
175
    },
176
    table => 'date_test'
177
);
178
is_deeply(
179
    $dbi->select(table => 'date_test')->fetch,
180
    ['2010-11-12bbbbb', '2010-11-12 10:55:56ddddd']
181
);
182

            
183
$dbi->execute("drop table date_test");
184

            
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
185
test 'dbh';
186
{
187
    my $connector = DBIx::Connector->new(
188
        "dbi:mysql:database=$DATABASE",
189
        $USER,
190
        $PASSWORD,
191
        DBIx::Custom->new->default_dbi_option
192
    );
193

            
194
    my $dbi = DBIx::Custom->connect(connector => $connector);
195
    $dbi->delete_all(table => 'table1');
196
    $dbi->do('insert into table1 (key1, key2) values (1, 2)');
197
    is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
198
    
199
    $dbi = DBIx::Custom->new;
200
    $dbi->dbh('a');
201
    is($dbi->{dbh}, 'a');
202
}
203

            
204
test 'transaction';
205
test 'dbh';
206
{
207
    my $connector = DBIx::Connector->new(
208
        "dbi:mysql:database=$DATABASE",
209
        $USER,
210
        $PASSWORD,
211
        DBIx::Custom->new->default_dbi_option
212
    );
213

            
214
    my $dbi = DBIx::Custom->connect(connector => $connector);
215
    $dbi->delete_all(table => 'table1');
216
    
217
    $dbi->connector->txn(sub {
218
        $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
219
        $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
220
    });
221
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
222
              [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
223

            
224
    $dbi->delete_all(table => 'table1');
225
    eval {
226
        $dbi->connector->txn(sub {
227
            $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
228
            die "Error";
229
            $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
230
        });
231
    };
232
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
233
              []);
234
}
235

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
236
test 'fork';
237
{
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
238
    my $connector = DBIx::Connector->new(
239
        "dbi:mysql:database=$DATABASE",
240
        $USER,
241
        $PASSWORD,
242
        DBIx::Custom->new->default_dbi_option
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
243
    );
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
244
    
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
245
    my $dbi = DBIx::Custom->new(connector => $connector);
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
246
    $dbi->delete_all(table => 'table1');
247
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
248
    die "Can't fork" unless defined (my $pid = fork);
249

            
250
    if ($pid) {
251
        # Parent
252
        my $result = $dbi->select(table => 'table1');
253
        is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2});
254
    }
255
    else {
256
        # Child
257
        my $result = $dbi->select(table => 'table1');
258
        die "Not OK" unless $result->fetch_hash_first->{key1} == 1;
259
    }
260
}
261