added common test executing ...
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
cleanup
|
4 |
use utf8; |
added common test executing ...
|
5 | |
6 |
use FindBin; |
|
cleanup test
|
7 |
use DBIx::Custom; |
added common test executing ...
|
8 | |
cleanup test
|
9 |
my $dbi; |
cleanup
|
10 |
my $dsn; |
11 |
my $args; |
|
12 |
my $user = 'dbix_custom'; |
|
13 |
my $password = 'dbix_custom'; |
|
14 |
my $database = 'dbix_custom'; |
|
cleanup test
|
15 | |
cleanup
|
16 |
$dsn = "dbi:mysql:database=$database"; |
17 |
$args = {dsn => $dsn, user => $user, password => $password,}; |
|
18 | ||
19 |
plan skip_all => 'mysql private test' unless -f "$FindBin::Bin/run/mysql2.run" |
|
20 |
&& eval { $dbi = DBIx::Custom->connect($args); 1 }; |
|
added common test executing ...
|
21 |
plan 'no_plan'; |
22 | ||
23 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
|
24 | ||
25 |
require DBIx::Connector; |
|
26 | ||
27 |
# Function for test name |
|
28 |
sub test { print "# $_[0]\n" } |
|
29 | ||
30 |
# Varialbes for tests |
|
31 |
my $dbname; |
|
micro optimization
|
32 |
my $row; |
added common test executing ...
|
33 |
my $rows; |
34 |
my $result; |
|
fixed update_or_insert metho...
|
35 |
my $model; |
added common test executing ...
|
36 | |
37 |
test 'connect'; |
|
38 |
eval { |
|
cleanup
|
39 |
$dbi = DBIx::Custom->connect( |
40 |
dsn => "dbi:mysql:database=$database;host=localhost;port=10000", |
|
41 |
user => $user, |
|
42 |
password => $password |
|
43 |
); |
|
added common test executing ...
|
44 |
}; |
45 |
ok(!$@); |
|
46 | ||
- removed DEPRECATED status ...
|
47 |
eval { $dbi->do('drop table table1') }; |
48 |
$dbi->do('create table table1 (key1 varchar(255), key2 varchar(255)) engine=InnoDB'); |
|
49 | ||
added EXPERIMENTAL bulk_inse...
|
50 |
test 'bulk_insert'; |
51 |
$dbi->delete_all(table => 'table1'); |
|
52 |
$dbi->insert( |
|
cleanup
|
53 |
[{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], |
54 |
table => 'table1', |
|
55 |
bulk_insert => 1 |
|
added EXPERIMENTAL bulk_inse...
|
56 |
); |
57 |
like($dbi->last_sql, qr/(\?.+){4}/); |
|
58 |
$rows = $dbi->select(table => 'table1')->all; |
|
59 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
60 | ||
61 |
$dbi->delete_all(table => 'table1'); |
|
62 |
$dbi->insert( |
|
cleanup
|
63 |
[{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], |
64 |
table => 'table1', |
|
65 |
bulk_insert => 1, |
|
66 |
filter => {key1 => sub { $_[0] * 2 }} |
|
added EXPERIMENTAL bulk_inse...
|
67 |
); |
68 |
like($dbi->last_sql, qr/(\?.+){4}/); |
|
69 |
$rows = $dbi->select(table => 'table1')->all; |
|
70 |
is_deeply($rows, [{key1 => 2, key2 => 2}, {key1 => 6, key2 => 4}]); |
|
71 | ||
- removed DEPRECATED status ...
|
72 |
test 'update_or_insert'; |
73 |
$dbi->delete_all(table => 'table1'); |
|
74 |
$dbi->update_or_insert( |
|
cleanup
|
75 |
{key2 => 2}, |
76 |
table => 'table1', |
|
77 |
id => 1, |
|
78 |
primary_key => 'key1', |
|
79 |
option => { |
|
80 |
select => {append => 'for update'}, |
|
81 |
insert => {append => ' #'}, |
|
82 |
update => {append => ' #'} |
|
83 |
} |
|
- removed DEPRECATED status ...
|
84 |
); |
85 | ||
micro optimization
|
86 |
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one; |
- removed DEPRECATED status ...
|
87 |
is_deeply($row, {key1 => 1, key2 => 2}, "basic"); |
88 | ||
- EXPERIMENTAL update_or_ins...
|
89 |
$dbi->update_or_insert( |
cleanup
|
90 |
{key2 => 3}, |
91 |
table => 'table1', |
|
92 |
id => 1, |
|
93 |
primary_key => 'key1', |
|
94 |
option => { |
|
95 |
select => {append => 'for update'}, |
|
96 |
insert => {append => ' #'}, |
|
97 |
update => {append => ' #'} |
|
98 |
} |
|
- EXPERIMENTAL update_or_ins...
|
99 |
); |
100 | ||
micro optimization
|
101 |
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one; |
- EXPERIMENTAL update_or_ins...
|
102 |
is_deeply($row, {key1 => 1, key2 => 3}, "basic"); |
103 | ||
fixed update_or_insert metho...
|
104 |
$dbi->delete_all(table => 'table1'); |
105 |
$model = $dbi->create_model( |
|
cleanup
|
106 |
table => 'table1', |
107 |
primary_key => 'key1', |
|
fixed update_or_insert metho...
|
108 |
); |
109 |
$model->update_or_insert( |
|
cleanup
|
110 |
{key2 => 2}, |
111 |
id => 1, |
|
112 |
option => { |
|
113 |
select => {append => 'for update'}, |
|
114 |
insert => {append => ' #'}, |
|
115 |
update => {append => ' #'} |
|
116 |
} |
|
fixed update_or_insert metho...
|
117 |
); |
118 |
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one; |
|
119 |
is_deeply($row, {key1 => 1, key2 => 2}, "basic"); |
|
120 |
$model->update_or_insert( |
|
cleanup
|
121 |
{key2 => 3}, |
122 |
id => 1, |
|
123 |
option => { |
|
124 |
select => {append => 'for update'}, |
|
125 |
insert => {append => ' #'}, |
|
126 |
update => {append => ' #'} |
|
127 |
} |
|
fixed update_or_insert metho...
|
128 |
); |
129 |
$row = $dbi->select(id => 1, table => 'table1', primary_key => 'key1')->one; |
|
130 |
is_deeply($row, {key1 => 1, key2 => 3}, "basic"); |
|
131 | ||
added memory leak check test
|
132 |
# Test memory leaks |
133 |
for (1 .. 200) { |
|
cleanup
|
134 |
$dbi = DBIx::Custom->connect( |
135 |
dsn => "dbi:mysql:database=$database;host=localhost;port=10000", |
|
136 |
user => $user, |
|
137 |
password => $password |
|
138 |
); |
|
139 |
$dbi->query_builder; |
|
140 |
$dbi->create_model(table => 'table1'); |
|
141 |
$dbi->create_model(table => 'table2'); |
|
added memory leak check test
|
142 |
} |
143 | ||
added common test executing ...
|
144 |
test 'limit'; |
145 |
$dbi = DBIx::Custom->connect( |
|
cleanup
|
146 |
dsn => "dbi:mysql:database=$database", |
147 |
user => $user, |
|
148 |
password => $password |
|
added common test executing ...
|
149 |
); |
150 |
$dbi->delete_all(table => 'table1'); |
|
151 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
152 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}); |
|
153 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6}); |
|
test cleanup
|
154 |
$dbi->register_tag( |
cleanup
|
155 |
limit => sub { |
156 |
my ($count, $offset) = @_; |
|
157 |
|
|
158 |
my $s = ''; |
|
159 |
$offset = 0 unless defined $offset; |
|
160 |
$s .= "limit $offset"; |
|
161 |
$s .= ", $count"; |
|
162 |
|
|
163 |
return [$s, []]; |
|
164 |
} |
|
added common test executing ...
|
165 |
); |
166 |
$rows = $dbi->select( |
|
cleanup
|
167 |
table => 'table1', |
168 |
where => {key1 => 1}, |
|
169 |
append => "order by key2 {limit 1 0}" |
|
added common test executing ...
|
170 |
)->fetch_hash_all; |
171 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
172 |
$rows = $dbi->select( |
|
cleanup
|
173 |
table => 'table1', |
174 |
where => {key1 => 1}, |
|
175 |
append => "order by key2 {limit 2 1}" |
|
added common test executing ...
|
176 |
)->fetch_hash_all; |
177 |
is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]); |
|
178 |
$rows = $dbi->select( |
|
cleanup
|
179 |
table => 'table1', |
180 |
where => {key1 => 1}, |
|
181 |
append => "order by key2 {limit 1}" |
|
added common test executing ...
|
182 |
)->fetch_hash_all; |
183 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
184 | ||
185 |
$dbi->dbh->disconnect; |
|
186 |
$dbi = undef; |
|
187 |
$dbi = DBIx::Custom->connect( |
|
cleanup
|
188 |
dsn => "dbi:mysql:database=$database", |
189 |
user => $user, |
|
190 |
password => $password |
|
added common test executing ...
|
191 |
); |
192 |
$rows = $dbi->select( |
|
cleanup
|
193 |
table => 'table1', |
194 |
where => {key1 => 1, key2 => 4}, |
|
195 |
append => "order by key2 limit 0, 1" |
|
added common test executing ...
|
196 |
)->fetch_hash_all; |
197 |
is_deeply($rows, [{key1 => 1, key2 => 4}]); |
|
198 |
$dbi->delete_all(table => 'table1'); |
|
199 | ||
200 |
test 'dbh'; |
|
201 |
{ |
|
cleanup
|
202 |
my $connector = DBIx::Connector->new( |
203 |
"dbi:mysql:database=$database", |
|
204 |
$user, |
|
205 |
$password, |
|
206 |
DBIx::Custom->new->default_option |
|
207 |
); |
|
added common test executing ...
|
208 | |
cleanup
|
209 |
my $dbi = DBIx::Custom->connect(connector => $connector); |
210 |
$dbi->delete_all(table => 'table1'); |
|
211 |
$dbi->do('insert into table1 (key1, key2) values (1, 2)'); |
|
212 |
is($dbi->select(table => 'table1')->fetch_hash_one->{key1}, 1); |
|
213 |
|
|
214 |
$dbi = DBIx::Custom->new; |
|
215 |
$dbi->dbh('a'); |
|
216 |
is($dbi->{dbh}, 'a'); |
|
added common test executing ...
|
217 |
} |
218 | ||
219 |
test 'transaction'; |
|
220 |
test 'dbh'; |
|
221 |
{ |
|
cleanup
|
222 |
my $connector = DBIx::Connector->new( |
223 |
"dbi:mysql:database=$database", |
|
224 |
$user, |
|
225 |
$password, |
|
226 |
DBIx::Custom->new->default_dbi_option |
|
227 |
); |
|
added common test executing ...
|
228 | |
cleanup
|
229 |
my $dbi = DBIx::Custom->connect(connector => $connector); |
230 |
$dbi->delete_all(table => 'table1'); |
|
231 |
|
|
232 |
$dbi->connector->txn(sub { |
|
233 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
234 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
235 |
}); |
|
236 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, |
|
237 |
[{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
238 | ||
239 |
$dbi->delete_all(table => 'table1'); |
|
240 |
eval { |
|
added common test executing ...
|
241 |
$dbi->connector->txn(sub { |
cleanup
|
242 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
243 |
die "Error"; |
|
244 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
added common test executing ...
|
245 |
}); |
cleanup
|
246 |
}; |
247 |
is_deeply($dbi->select(table => 'table1')->fetch_hash_all, |
|
248 |
[]); |
|
added common test executing ...
|
249 |
} |
250 | ||
251 |
use DBIx::Custom; |
|
252 |
use Scalar::Util 'blessed'; |
|
253 |
{ |
|
cleanup
|
254 |
my $dbi = DBIx::Custom->connect( |
255 |
user => $user, |
|
256 |
password => $password, |
|
257 |
dsn => "dbi:mysql:dbname=$database" |
|
258 |
); |
|
259 |
$dbi->connect; |
|
260 |
|
|
261 |
ok(blessed $dbi->dbh); |
|
262 |
can_ok($dbi->dbh, qw/prepare/); |
|
263 |
ok($dbi->dbh->{AutoCommit}); |
|
264 |
ok(!$dbi->dbh->{mysql_enable_utf8}); |
|
added common test executing ...
|
265 |
} |
266 | ||
267 |
{ |
|
cleanup
|
268 |
my $dbi = DBIx::Custom->connect( |
269 |
user => $user, |
|
270 |
password => $password, |
|
271 |
dsn => "dbi:mysql:dbname=$database", |
|
272 |
option => {AutoCommit => 0, mysql_enable_utf8 => 1} |
|
273 |
); |
|
274 |
$dbi->connect; |
|
275 |
ok(!$dbi->dbh->{AutoCommit}); |
|
276 |
#ok($dbi->dbh->{mysql_enable_utf8}); |
|
added common test executing ...
|
277 |
} |
278 | ||
279 |
test 'fork'; |
|
280 |
{ |
|
cleanup
|
281 |
my $connector = DBIx::Connector->new( |
282 |
"dbi:mysql:database=$database", |
|
283 |
$user, |
|
284 |
$password, |
|
285 |
DBIx::Custom->new->default_option |
|
286 |
); |
|
287 |
|
|
288 |
my $dbi = DBIx::Custom->new(connector => $connector); |
|
289 |
$dbi->delete_all(table => 'table1'); |
|
290 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
291 |
die "Can't fork" unless defined (my $pid = fork); |
|
added common test executing ...
|
292 | |
cleanup
|
293 |
if ($pid) { |
294 |
# Parent |
|
295 |
my $result = $dbi->select(table => 'table1'); |
|
296 |
is_deeply($result->fetch_hash_one, {key1 => 1, key2 => 2}); |
|
297 |
} |
|
298 |
else { |
|
299 |
# Child |
|
300 |
my $result = $dbi->select(table => 'table1'); |
|
301 |
die "Not OK" unless $result->fetch_hash_one->{key1} == 1; |
|
302 |
} |
|
added common test executing ...
|
303 |
} |
304 |