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

            
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
5
use DBIx::Connector;
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

            
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

            
37
# Constant varialbes for test
38
use DBIx::Custom::MySQL;
39

            
40
test 'connect';
41
$dbi = DBIx::Custom::MySQL->new(user => $USER, password => $PASSWORD,
42
                    database => $DATABASE, host => 'localhost', port => '10000');
43
$dbi->connect;
cleanup
Yuki Kimoto authored on 2011-01-23
44
like($dbi->data_source, qr/dbi:mysql:database=.*;host=localhost;port=10000;/, "created data source");
45
is(ref $dbi->dbh, 'DBI::db');
add examples
Yuki Kimoto authored on 2011-01-07
46

            
47
test 'attributes';
48
$dbi = DBIx::Custom::MySQL->new;
49
$dbi->host('a');
cleanup
Yuki Kimoto authored on 2011-01-23
50
is($dbi->host, 'a', "host");
add examples
Yuki Kimoto authored on 2011-01-07
51
$dbi->port('b');
cleanup
Yuki Kimoto authored on 2011-01-23
52
is($dbi->port, 'b', "port");
add examples
Yuki Kimoto authored on 2011-01-07
53

            
54
test 'limit';
55
$dbi = DBIx::Custom->connect(
56
    data_source => "dbi:mysql:database=$DATABASE",
57
    user => $USER,
58
    password => $PASSWORD
59
);
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
60
$dbi->delete_all(table => 'table1');
add examples
Yuki Kimoto authored on 2011-01-07
61
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
62
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
63
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
64
$dbi->query_builder->register_tag_processor(
65
    limit => sub {
66
        my ($count, $offset) = @_;
67
        
68
        my $s = '';
69
        $offset = 0 unless defined $offset;
70
        $s .= "limit $offset";
71
        $s .= ", $count";
72
        
73
        return [$s, []];
74
    }
75
);
76
$rows = $dbi->select(
77
  table => 'table1',
78
  where => {key1 => 1},
79
  append => "order by key2 {limit 1 0}"
80
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
81
is_deeply($rows, [{key1 => 1, key2 => 2}]);
add examples
Yuki Kimoto authored on 2011-01-07
82
$rows = $dbi->select(
83
  table => 'table1',
84
  where => {key1 => 1},
85
  append => "order by key2 {limit 2 1}"
86
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
87
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
add examples
Yuki Kimoto authored on 2011-01-07
88
$rows = $dbi->select(
89
  table => 'table1',
90
  where => {key1 => 1},
91
  append => "order by key2 {limit 1}"
92
)->fetch_hash_all;
cleanup
Yuki Kimoto authored on 2011-01-23
93
is_deeply($rows, [{key1 => 1, key2 => 2}]);
add examples
Yuki Kimoto authored on 2011-01-07
94
$dbi->delete_all(table => 'table1');
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
95

            
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
96
test 'dbh';
97
{
98
    my $connector = DBIx::Connector->new(
99
        "dbi:mysql:database=$DATABASE",
100
        $USER,
101
        $PASSWORD,
102
        DBIx::Custom->new->default_dbi_option
103
    );
104

            
105
    my $dbi = DBIx::Custom->connect(connector => $connector);
106
    $dbi->delete_all(table => 'table1');
107
    $dbi->do('insert into table1 (key1, key2) values (1, 2)');
108
    is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
109
    
110
    $dbi = DBIx::Custom->new;
111
    $dbi->dbh('a');
112
    is($dbi->{dbh}, 'a');
113
}
114

            
115
test 'transaction';
116
test 'dbh';
117
{
118
    my $connector = DBIx::Connector->new(
119
        "dbi:mysql:database=$DATABASE",
120
        $USER,
121
        $PASSWORD,
122
        DBIx::Custom->new->default_dbi_option
123
    );
124

            
125
    my $dbi = DBIx::Custom->connect(connector => $connector);
126
    $dbi->delete_all(table => 'table1');
127
    
128
    $dbi->connector->txn(sub {
129
        $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
130
        $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
131
    });
132
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
133
              [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
134

            
135
    $dbi->delete_all(table => 'table1');
136
    eval {
137
        $dbi->connector->txn(sub {
138
            $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
139
            die "Error";
140
            $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
141
        });
142
    };
143
    is_deeply($dbi->select(table => 'table1')->fetch_hash_all,
144
              []);
145
}
146

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
147
test 'fork';
148
{
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
149
    my $connector = DBIx::Connector->new(
150
        "dbi:mysql:database=$DATABASE",
151
        $USER,
152
        $PASSWORD,
153
        DBIx::Custom->new->default_dbi_option
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
154
    );
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
155
    
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
156
    my $dbi = DBIx::Custom->new(connector => $connector);
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
157
    $dbi->delete_all(table => 'table1');
158
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
159
    die "Can't fork" unless defined (my $pid = fork);
160

            
161
    if ($pid) {
162
        # Parent
163
        my $result = $dbi->select(table => 'table1');
164
        is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2});
165
    }
166
    else {
167
        # Child
168
        my $result = $dbi->select(table => 'table1');
169
        die "Not OK" unless $result->fetch_hash_first->{key1} == 1;
170
    }
171
}
172