add examples
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 | ||
- update_param_tag is DEPREC...
|
5 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
6 | ||
add examples
|
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...
|
14 |
require DBIx::Connector; |
15 | ||
add examples
|
16 |
# Function for test name |
cleanup
|
17 |
sub test { print "# $_[0]\n" } |
add examples
|
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...
|
37 |
my $result; |
add examples
|
38 | |
fixed test
|
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...
|
56 |
if (!$self->dsn) { |
fixed test
|
57 |
my $database = $self->database; |
58 |
my $host = $self->host; |
|
59 |
my $port = $self->port; |
|
data_source is DEPRECATED! I...
|
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
|
65 |
} |
66 |
|
|
67 |
return $self->SUPER::connect; |
|
68 |
} |
|
69 | ||
70 |
1; |
|
71 |
} |
|
add examples
|
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...
|
77 |
like($dbi->dsn, qr/dbi:mysql:database=.*;host=localhost;port=10000;/, "created data source"); |
cleanup
|
78 |
is(ref $dbi->dbh, 'DBI::db'); |
add examples
|
79 | |
80 |
test 'attributes'; |
|
81 |
$dbi = DBIx::Custom::MySQL->new; |
|
82 |
$dbi->host('a'); |
|
cleanup
|
83 |
is($dbi->host, 'a', "host"); |
add examples
|
84 |
$dbi->port('b'); |
cleanup
|
85 |
is($dbi->port, 'b', "port"); |
add examples
|
86 | |
87 |
test 'limit'; |
|
88 |
$dbi = DBIx::Custom->connect( |
|
data_source is DEPRECATED! I...
|
89 |
dsn => "dbi:mysql:database=$DATABASE", |
add examples
|
90 |
user => $USER, |
91 |
password => $PASSWORD |
|
92 |
); |
|
EXPERIMETAL fork safety impl...
|
93 |
$dbi->delete_all(table => 'table1'); |
add examples
|
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
|
114 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
add examples
|
115 |
$rows = $dbi->select( |
116 |
table => 'table1', |
|
117 |
where => {key1 => 1}, |
|
118 |
append => "order by key2 {limit 2 1}" |
|
119 |
)->fetch_hash_all; |
|
cleanup
|
120 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]); |
add examples
|
121 |
$rows = $dbi->select( |
122 |
table => 'table1', |
|
123 |
where => {key1 => 1}, |
|
124 |
append => "order by key2 {limit 1}" |
|
125 |
)->fetch_hash_all; |
|
cleanup
|
126 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
add examples
|
127 |
$dbi->delete_all(table => 'table1'); |
EXPERIMETAL fork safety impl...
|
128 | |
improved type_rule method
|
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 |
from => sub { |
|
145 |
my $date = shift; |
|
146 |
$date .= 'bbbbb'; |
|
147 |
return $date; |
|
148 |
} |
|
149 |
}, |
|
150 |
'DATETIME' => { |
|
151 |
into => sub { |
|
152 |
my $date = shift; |
|
153 |
$date =~ s/ccccc//g; |
|
154 |
return $date; |
|
155 |
|
|
156 |
}, |
|
157 |
}, |
|
158 |
'TIMESTAMP' => { |
|
159 |
from => sub { |
|
160 |
my $date = shift; |
|
161 |
$date .= 'ddddd'; |
|
162 |
return $date; |
|
163 |
} |
|
164 |
} |
|
165 |
); |
|
166 | ||
167 |
$dbi->insert( |
|
168 |
{ |
|
169 |
date => 'aaaaa2010-aaaaa11-12aaaaa', |
|
170 |
datetime => '2010-11ccccc-12 10:ccccc55:56' |
|
171 |
}, |
|
172 |
table => 'date_test' |
|
173 |
); |
|
174 |
is_deeply( |
|
175 |
$dbi->select(table => 'date_test')->fetch, |
|
176 |
['2010-11-12bbbbb', '2010-11-12 10:55:56ddddd'] |
|
177 |
); |
|
178 | ||
179 |
$dbi->execute("drop table date_test"); |
|
180 | ||
fixed dbh() method bug:wq
|
181 |
test 'dbh'; |
182 |
{ |
|
183 |
my $connector = DBIx::Connector->new( |
|
184 |
"dbi:mysql:database=$DATABASE", |
|
185 |
$USER, |
|
186 |
$PASSWORD, |
|
187 |
DBIx::Custom->new->default_dbi_option |
|
188 |
); |
|
189 | ||
190 |
my $dbi = DBIx::Custom->connect(connector => $connector); |
|
191 |
$dbi->delete_all(table => 'table1'); |
|
192 |
$dbi->do('insert into table1 (key1, key2) values (1, 2)'); |
|
193 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
|
194 |
|
|
195 |
$dbi = DBIx::Custom->new; |
|
196 |
$dbi->dbh('a'); |
|
197 |
is($dbi->{dbh}, 'a'); |
|
198 |
} |
|
199 | ||
200 |
test 'transaction'; |
|
201 |
test 'dbh'; |
|
202 |
{ |
|
203 |
my $connector = DBIx::Connector->new( |
|
204 |
"dbi:mysql:database=$DATABASE", |
|
205 |
$USER, |
|
206 |
$PASSWORD, |
|
207 |
DBIx::Custom->new->default_dbi_option |
|
208 |
); |
|
209 | ||
210 |
my $dbi = DBIx::Custom->connect(connector => $connector); |
|
211 |
$dbi->delete_all(table => 'table1'); |
|
212 |
|
|
213 |
$dbi->connector->txn(sub { |
|
214 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
215 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
216 |
}); |
|
217 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, |
|
218 |
[{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
219 | ||
220 |
$dbi->delete_all(table => 'table1'); |
|
221 |
eval { |
|
222 |
$dbi->connector->txn(sub { |
|
223 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
224 |
die "Error"; |
|
225 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
226 |
}); |
|
227 |
}; |
|
228 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, |
|
229 |
[]); |
|
230 |
} |
|
231 | ||
EXPERIMETAL fork safety impl...
|
232 |
test 'fork'; |
233 |
{ |
|
- removed EXPERIMENTAL Prefo...
|
234 |
my $connector = DBIx::Connector->new( |
235 |
"dbi:mysql:database=$DATABASE", |
|
236 |
$USER, |
|
237 |
$PASSWORD, |
|
238 |
DBIx::Custom->new->default_dbi_option |
|
EXPERIMETAL fork safety impl...
|
239 |
); |
- removed EXPERIMENTAL Prefo...
|
240 |
|
fixed dbh() method bug:wq
|
241 |
my $dbi = DBIx::Custom->new(connector => $connector); |
EXPERIMETAL fork safety impl...
|
242 |
$dbi->delete_all(table => 'table1'); |
243 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
244 |
die "Can't fork" unless defined (my $pid = fork); |
|
245 | ||
246 |
if ($pid) { |
|
247 |
# Parent |
|
248 |
my $result = $dbi->select(table => 'table1'); |
|
249 |
is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2}); |
|
250 |
} |
|
251 |
else { |
|
252 |
# Child |
|
253 |
my $result = $dbi->select(table => 'table1'); |
|
254 |
die "Not OK" unless $result->fetch_hash_first->{key1} == 1; |
|
255 |
} |
|
256 |
} |
|
257 |