cleanup test
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 | ||
test cleanup
|
5 |
use FindBin; |
cleanup test
|
6 | |
test cleanup
|
7 |
plan skip_all => 'private test' unless -f "$FindBin::Bin/private-mysql-run.tmp"; |
cleanup test
|
8 | |
9 |
plan 'no_plan'; |
|
10 | ||
test cleanup
|
11 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
12 | ||
13 |
# user password database |
|
14 |
our ($USER, $PASSWORD, $DATABASE) = qw/appuser 123456 usertest/; |
|
15 | ||
cleanup test
|
16 |
require DBIx::Connector; |
17 | ||
18 |
# Function for test name |
|
19 |
sub test { print "# $_[0]\n" } |
|
20 | ||
21 |
# Functions for tests |
|
22 |
sub connect_info { |
|
23 |
my $file = 'password.tmp'; |
|
24 |
open my $fh, '<', $file |
|
25 |
or return; |
|
26 |
|
|
27 |
my ($user, $password, $database) = split(/\s/, (<$fh>)[0]); |
|
28 |
|
|
29 |
close $fh; |
|
30 |
|
|
31 |
return ($user, $password, $database); |
|
32 |
} |
|
33 | ||
34 | ||
35 |
# Varialbes for tests |
|
36 |
my $dbi; |
|
37 |
my $dbname; |
|
38 |
my $rows; |
|
39 |
my $result; |
|
40 | ||
41 |
{ |
|
42 |
package DBIx::Custom::MySQL; |
|
43 | ||
44 |
use strict; |
|
45 |
use warnings; |
|
46 | ||
47 |
use base 'DBIx::Custom'; |
|
48 | ||
49 |
__PACKAGE__->attr([qw/database host port/]); |
|
50 | ||
51 |
sub connect { |
|
52 |
my $proto = shift; |
|
53 |
|
|
54 |
# Create a new object |
|
55 |
my $self = ref $proto ? $proto : $proto->new(@_); |
|
56 |
|
|
57 |
# Data source |
|
58 |
if (!$self->dsn) { |
|
59 |
my $database = $self->database; |
|
60 |
my $host = $self->host; |
|
61 |
my $port = $self->port; |
|
62 |
my $dsn = "dbi:mysql:"; |
|
63 |
$dsn .= "database=$database;" if $database; |
|
64 |
$dsn .= "host=$host;" if $host; |
|
65 |
$dsn .= "port=$port;" if $port; |
|
66 |
$self->dsn($dsn); |
|
67 |
} |
|
68 |
|
|
69 |
return $self->SUPER::connect; |
|
70 |
} |
|
71 | ||
72 |
1; |
|
73 |
} |
|
74 | ||
75 |
test 'connect'; |
|
76 |
$dbi = DBIx::Custom::MySQL->new(user => $USER, password => $PASSWORD, |
|
77 |
database => $DATABASE, host => 'localhost', port => '10000'); |
|
78 |
$dbi->connect; |
|
79 |
like($dbi->dsn, qr/dbi:mysql:database=.*;host=localhost;port=10000;/, "created data source"); |
|
80 |
is(ref $dbi->dbh, 'DBI::db'); |
|
81 | ||
82 |
test 'attributes'; |
|
83 |
$dbi = DBIx::Custom::MySQL->new; |
|
84 |
$dbi->host('a'); |
|
85 |
is($dbi->host, 'a', "host"); |
|
86 |
$dbi->port('b'); |
|
87 |
is($dbi->port, 'b', "port"); |
|
88 | ||
89 |
test 'limit'; |
|
90 |
$dbi = DBIx::Custom->connect( |
|
91 |
dsn => "dbi:mysql:database=$DATABASE", |
|
92 |
user => $USER, |
|
93 |
password => $PASSWORD |
|
94 |
); |
|
95 |
$dbi->delete_all(table => 'table1'); |
|
96 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
97 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}); |
|
98 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6}); |
|
99 |
$dbi->query_builder->register_tag_processor( |
|
100 |
limit => sub { |
|
101 |
my ($count, $offset) = @_; |
|
102 |
|
|
103 |
my $s = ''; |
|
104 |
$offset = 0 unless defined $offset; |
|
105 |
$s .= "limit $offset"; |
|
106 |
$s .= ", $count"; |
|
107 |
|
|
108 |
return [$s, []]; |
|
109 |
} |
|
110 |
); |
|
111 |
$rows = $dbi->select( |
|
112 |
table => 'table1', |
|
113 |
where => {key1 => 1}, |
|
114 |
append => "order by key2 {limit 1 0}" |
|
115 |
)->fetch_hash_all; |
|
116 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
117 |
$rows = $dbi->select( |
|
118 |
table => 'table1', |
|
119 |
where => {key1 => 1}, |
|
120 |
append => "order by key2 {limit 2 1}" |
|
121 |
)->fetch_hash_all; |
|
122 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]); |
|
123 |
$rows = $dbi->select( |
|
124 |
table => 'table1', |
|
125 |
where => {key1 => 1}, |
|
126 |
append => "order by key2 {limit 1}" |
|
127 |
)->fetch_hash_all; |
|
128 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
129 | ||
130 |
$dbi->dbh->disconnect; |
|
131 |
$dbi = undef; |
|
132 |
$dbi = DBIx::Custom->connect( |
|
133 |
dsn => "dbi:mysql:database=$DATABASE", |
|
134 |
user => $USER, |
|
135 |
password => $PASSWORD |
|
136 |
); |
|
137 |
$rows = $dbi->select( |
|
138 |
table => 'table1', |
|
139 |
where => {key1 => 1, key2 => 4}, |
|
140 |
append => "order by key2 limit 0, 1" |
|
141 |
)->fetch_hash_all; |
|
142 |
is_deeply($rows, [{key1 => 1, key2 => 4}]); |
|
143 |
$dbi->delete_all(table => 'table1'); |
|
144 | ||
145 |
test 'type_rule'; |
|
146 |
$dbi = DBIx::Custom->connect( |
|
147 |
dsn => "dbi:mysql:database=$DATABASE", |
|
148 |
user => $USER, |
|
149 |
password => $PASSWORD |
|
150 |
); |
|
151 |
eval{$dbi->execute("create table date_test (date DATE, datetime DATETIME)")}; |
|
152 |
$dbi->each_column( |
|
153 |
sub { |
|
154 |
my ($self, $table, $column, $column_info) = @_; |
|
155 |
} |
|
156 |
); |
|
157 | ||
158 |
$dbi->type_rule( |
|
159 |
into1 => { |
|
160 |
date=> sub { |
|
161 |
my $date = shift; |
|
162 |
$date =~ s/aaaaa//g; |
|
163 |
return $date; |
|
164 |
}, |
|
165 |
datetime => sub { |
|
166 |
my $date = shift; |
|
167 |
$date =~ s/ccccc//g; |
|
168 |
return $date; |
|
169 |
}, |
|
170 |
}, |
|
171 |
from1 => { |
|
172 |
# DATE |
|
173 |
9 => sub { |
|
174 |
my $date = shift; |
|
175 |
$date .= 'bbbbb'; |
|
176 |
return $date; |
|
177 |
}, |
|
178 |
# DATETIME or TIMPESTANM |
|
179 |
11 => sub { |
|
180 |
my $date = shift; |
|
181 |
$date .= 'ddddd'; |
|
182 |
return $date; |
|
183 |
} |
|
184 |
} |
|
185 |
); |
|
186 | ||
187 |
$dbi->insert( |
|
188 |
{ |
|
189 |
date => 'aaaaa2010-aaaaa11-12aaaaa', |
|
190 |
datetime => '2010-11ccccc-12 10:ccccc55:56' |
|
191 |
}, |
|
192 |
table => 'date_test' |
|
193 |
); |
|
194 |
is_deeply( |
|
195 |
$dbi->select(table => 'date_test')->fetch, |
|
196 |
['2010-11-12bbbbb', '2010-11-12 10:55:56ddddd'] |
|
197 |
); |
|
198 | ||
199 |
$dbi->execute("drop table date_test"); |
|
200 | ||
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 |
$dbi->do('insert into table1 (key1, key2) values (1, 2)'); |
|
213 |
is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1); |
|
214 |
|
|
215 |
$dbi = DBIx::Custom->new; |
|
216 |
$dbi->dbh('a'); |
|
217 |
is($dbi->{dbh}, 'a'); |
|
218 |
} |
|
219 | ||
220 |
test 'transaction'; |
|
221 |
test 'dbh'; |
|
222 |
{ |
|
223 |
my $connector = DBIx::Connector->new( |
|
224 |
"dbi:mysql:database=$DATABASE", |
|
225 |
$USER, |
|
226 |
$PASSWORD, |
|
227 |
DBIx::Custom->new->default_dbi_option |
|
228 |
); |
|
229 | ||
230 |
my $dbi = DBIx::Custom->connect(connector => $connector); |
|
231 |
$dbi->delete_all(table => 'table1'); |
|
232 |
|
|
233 |
$dbi->connector->txn(sub { |
|
234 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
235 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
236 |
}); |
|
237 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, |
|
238 |
[{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
239 | ||
240 |
$dbi->delete_all(table => 'table1'); |
|
241 |
eval { |
|
242 |
$dbi->connector->txn(sub { |
|
243 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
244 |
die "Error"; |
|
245 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
246 |
}); |
|
247 |
}; |
|
248 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, |
|
249 |
[]); |
|
250 |
} |
|
251 | ||
cleanup test
|
252 |
use DBIx::Custom; |
253 |
use Scalar::Util 'blessed'; |
|
254 |
{ |
|
255 |
my $dbi = DBIx::Custom->connect( |
|
256 |
user => $USER, |
|
257 |
password => $PASSWORD, |
|
258 |
dsn => "dbi:mysql:dbname=$DATABASE" |
|
259 |
); |
|
260 |
$dbi->connect; |
|
261 |
|
|
262 |
ok(blessed $dbi->dbh); |
|
263 |
can_ok($dbi->dbh, qw/prepare/); |
|
264 |
ok($dbi->dbh->{AutoCommit}); |
|
265 |
ok(!$dbi->dbh->{mysql_enable_utf8}); |
|
266 |
} |
|
267 | ||
268 |
{ |
|
269 |
my $dbi = DBIx::Custom->connect( |
|
270 |
user => $USER, |
|
271 |
password => $PASSWORD, |
|
272 |
dsn => "dbi:mysql:dbname=$DATABASE", |
|
273 |
dbi_options => {AutoCommit => 0, mysql_enable_utf8 => 1} |
|
274 |
); |
|
275 |
$dbi->connect; |
|
276 |
ok(!$dbi->dbh->{AutoCommit}); |
|
277 |
#ok($dbi->dbh->{mysql_enable_utf8}); |
|
278 |
} |
|
279 | ||
cleanup test
|
280 |
test 'fork'; |
281 |
{ |
|
282 |
my $connector = DBIx::Connector->new( |
|
283 |
"dbi:mysql:database=$DATABASE", |
|
284 |
$USER, |
|
285 |
$PASSWORD, |
|
286 |
DBIx::Custom->new->default_dbi_option |
|
287 |
); |
|
288 |
|
|
289 |
my $dbi = DBIx::Custom->new(connector => $connector); |
|
290 |
$dbi->delete_all(table => 'table1'); |
|
291 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
292 |
die "Can't fork" unless defined (my $pid = fork); |
|
293 | ||
294 |
if ($pid) { |
|
295 |
# Parent |
|
296 |
my $result = $dbi->select(table => 'table1'); |
|
297 |
is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2}); |
|
298 |
} |
|
299 |
else { |
|
300 |
# Child |
|
301 |
my $result = $dbi->select(table => 'table1'); |
|
302 |
die "Not OK" unless $result->fetch_hash_first->{key1} == 1; |
|
303 |
} |
|
304 |
} |
|
305 |