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

            
5
# user password database
6
our ($USER, $PASSWORD, $DATABASE) = connect_info();
7

            
8
plan skip_all => 'private MySQL test' unless $USER;
9

            
10
plan 'no_plan';
11

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

            
add examples
Yuki Kimoto authored on 2011-01-07
14
# Function for test name
cleanup
Yuki Kimoto authored on 2011-01-23
15
sub test { print "# $_[0]\n" }
add examples
Yuki Kimoto authored on 2011-01-07
16

            
17
# Functions for tests
18
sub connect_info {
19
    my $file = 'password.tmp';
20
    open my $fh, '<', $file
21
      or return;
22
    
23
    my ($user, $password, $database) = split(/\s/, (<$fh>)[0]);
24
    
25
    close $fh;
26
    
27
    return ($user, $password, $database);
28
}
29

            
30

            
31
# Varialbes for tests
32
my $dbi;
33
my $dbname;
34
my $rows;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
35
my $result;
add examples
Yuki Kimoto authored on 2011-01-07
36

            
fixed test
Yuki Kimoto authored on 2011-04-11
37
{
38
    package DBIx::Custom::MySQL;
39

            
40
    use strict;
41
    use warnings;
42

            
43
    use base 'DBIx::Custom';
44

            
45
    __PACKAGE__->attr([qw/database host port/]);
46

            
47
    sub connect {
48
        my $proto = shift;
49
        
50
        # Create a new object
51
        my $self = ref $proto ? $proto : $proto->new(@_);
52
        
53
        # Data source
54
        if (!$self->data_source) {
55
            my $database = $self->database;
56
            my $host     = $self->host;
57
            my $port     = $self->port;
58
            my $data_source = "dbi:mysql:";
59
            $data_source .= "database=$database;" if $database;
60
            $data_source .= "host=$host;"         if $host;
61
            $data_source .= "port=$port;"         if $port;
62
            $self->data_source($data_source);
63
        }
64
        
65
        return $self->SUPER::connect;
66
    }
67

            
68
    1;
69
}
add examples
Yuki Kimoto authored on 2011-01-07
70

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

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

            
85
test 'limit';
86
$dbi = DBIx::Custom->connect(
87
    data_source => "dbi:mysql:database=$DATABASE",
88
    user => $USER,
89
    password => $PASSWORD
90
);
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
91
$dbi->delete_all(table => 'table1');
add examples
Yuki Kimoto authored on 2011-01-07
92
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
93
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
94
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
95
$dbi->query_builder->register_tag_processor(
96
    limit => sub {
97
        my ($count, $offset) = @_;
98
        
99
        my $s = '';
100
        $offset = 0 unless defined $offset;
101
        $s .= "limit $offset";
102
        $s .= ", $count";
103
        
104
        return [$s, []];
105
    }
106
);
107
$rows = $dbi->select(
108
  table => 'table1',
109
  where => {key1 => 1},
110
  append => "order by key2 {limit 1 0}"
111
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
112
is_deeply($rows, [{key1 => 1, key2 => 2}]);
add examples
Yuki Kimoto authored on 2011-01-07
113
$rows = $dbi->select(
114
  table => 'table1',
115
  where => {key1 => 1},
116
  append => "order by key2 {limit 2 1}"
117
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
118
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
add examples
Yuki Kimoto authored on 2011-01-07
119
$rows = $dbi->select(
120
  table => 'table1',
121
  where => {key1 => 1},
122
  append => "order by key2 {limit 1}"
123
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
124
is_deeply($rows, [{key1 => 1, key2 => 2}]);
add examples
Yuki Kimoto authored on 2011-01-07
125
$dbi->delete_all(table => 'table1');
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
126

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

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

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

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

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

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

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