DBIx-Custom / t / dbix-custom-mysql-private.t /
Newer Older
205 lines | 5.34kb
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

            
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
129
test 'dbh';
130
{
131
    my $connector = DBIx::Connector->new(
132
        "dbi:mysql:database=$DATABASE",
133
        $USER,
134
        $PASSWORD,
135
        DBIx::Custom->new->default_dbi_option
136
    );
137

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

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

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

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

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
180
test 'fork';
181
{
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
182
    my $connector = DBIx::Connector->new(
183
        "dbi:mysql:database=$DATABASE",
184
        $USER,
185
        $PASSWORD,
186
        DBIx::Custom->new->default_dbi_option
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
187
    );
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
188
    
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
189
    my $dbi = DBIx::Custom->new(connector => $connector);
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
190
    $dbi->delete_all(table => 'table1');
191
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
192
    die "Can't fork" unless defined (my $pid = fork);
193

            
194
    if ($pid) {
195
        # Parent
196
        my $result = $dbi->select(table => 'table1');
197
        is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2});
198
    }
199
    else {
200
        # Child
201
        my $result = $dbi->select(table => 'table1');
202
        die "Not OK" unless $result->fetch_hash_first->{key1} == 1;
203
    }
204
}
205