DBIx-Custom / t / dbix-custom-mysql-private.t /
Newer Older
256 lines | 6.477kb
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(
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
138
    into => {
139
        DATE=> sub {
improved type_rule method
Yuki Kimoto authored on 2011-06-10
140
            my $date = shift;
141
            $date =~ s/aaaaa//g;
142
            return $date;
143
        },
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
144
        DATETIME => sub {
improved type_rule method
Yuki Kimoto authored on 2011-06-10
145
            my $date = shift;
146
            $date =~ s/ccccc//g;
147
            return $date;
148
        },
149
    },
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
150
    from => {
151
        # DATE
152
        9 => sub {
153
                my $date = shift;
154
                $date .= 'bbbbb';
155
                return $date;
156
        },
157
        # DATETIME or TIMPESTANM
158
        11 => sub {
159
                my $date = shift;
160
                $date .= 'ddddd';
161
                return $date;
improved type_rule method
Yuki Kimoto authored on 2011-06-10
162
        }
163
    }
164
);
165

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

            
178
$dbi->execute("drop table date_test");
179

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

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

            
199
test 'transaction';
200
test 'dbh';
201
{
202
    my $connector = DBIx::Connector->new(
203
        "dbi:mysql:database=$DATABASE",
204
        $USER,
205
        $PASSWORD,
206
        DBIx::Custom->new->default_dbi_option
207
    );
208

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

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

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

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