Commit | Line | Data |
70350518 |
1 | use strict; |
68de9438 |
2 | use warnings; |
70350518 |
3 | |
4 | use Test::More; |
74de7c2c |
5 | use Test::Exception; |
722274d0 |
6 | use Test::Warn; |
da6a5860 |
7 | |
8 | use DBI::Const::GetInfoType; |
9 | use Scalar::Util qw/weaken/; |
199fbc45 |
10 | use DBIx::Class::Optional::Dependencies (); |
da6a5860 |
11 | |
70350518 |
12 | use lib qw(t/lib); |
13 | use DBICTest; |
0567538f |
14 | |
199fbc45 |
15 | plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mysql') |
16 | unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mysql'); |
17 | |
0567538f |
18 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}; |
19 | |
70350518 |
20 | plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test' |
0567538f |
21 | unless ($dsn && $user); |
22 | |
5f6bada6 |
23 | my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { quote_names => 1 }); |
0567538f |
24 | |
3ff5b740 |
25 | my $dbh = $schema->storage->dbh; |
0567538f |
26 | |
27 | $dbh->do("DROP TABLE IF EXISTS artist;"); |
28 | |
a0dd8679 |
29 | $dbh->do("CREATE TABLE artist (artistid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10));"); |
0567538f |
30 | |
74de7c2c |
31 | $dbh->do("DROP TABLE IF EXISTS cd;"); |
32 | |
e0d56e26 |
33 | $dbh->do("CREATE TABLE cd (cdid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, artist INTEGER, title TEXT, year DATE, genreid INTEGER, single_track INTEGER);"); |
74de7c2c |
34 | |
35 | $dbh->do("DROP TABLE IF EXISTS producer;"); |
36 | |
37 | $dbh->do("CREATE TABLE producer (producerid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name TEXT);"); |
38 | |
39 | $dbh->do("DROP TABLE IF EXISTS cd_to_producer;"); |
40 | |
41 | $dbh->do("CREATE TABLE cd_to_producer (cd INTEGER,producer INTEGER);"); |
42 | |
bed3a173 |
43 | $dbh->do("DROP TABLE IF EXISTS owners;"); |
44 | |
45 | $dbh->do("CREATE TABLE owners (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL);"); |
46 | |
47 | $dbh->do("DROP TABLE IF EXISTS books;"); |
48 | |
49 | $dbh->do("CREATE TABLE books (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, source VARCHAR(100) NOT NULL, owner integer NOT NULL, title varchar(100) NOT NULL, price integer);"); |
50 | |
0567538f |
51 | #'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', ''); |
52 | |
8273e845 |
53 | # make sure sqlt_type overrides work (::Storage::DBI::mysql does this) |
adbf0c89 |
54 | { |
55 | my $schema = DBICTest::Schema->connect($dsn, $user, $pass); |
56 | |
57 | ok (!$schema->storage->_dbh, 'definitely not connected'); |
58 | is ($schema->storage->sqlt_type, 'MySQL', 'sqlt_type correct pre-connection'); |
59 | } |
60 | |
3ff5b740 |
61 | # This is in Core now, but it's here just to test that it doesn't break |
62 | $schema->class('Artist')->load_components('PK::Auto'); |
0567538f |
63 | |
64 | # test primary key handling |
3ff5b740 |
65 | my $new = $schema->resultset('Artist')->create({ name => 'foo' }); |
0567538f |
66 | ok($new->artistid, "Auto-PK worked"); |
67 | |
68 | # test LIMIT support |
69 | for (1..6) { |
3ff5b740 |
70 | $schema->resultset('Artist')->create({ name => 'Artist ' . $_ }); |
0567538f |
71 | } |
3ff5b740 |
72 | my $it = $schema->resultset('Artist')->search( {}, |
0567538f |
73 | { rows => 3, |
74 | offset => 2, |
75 | order_by => 'artistid' } |
76 | ); |
fb4b58e8 |
77 | is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 7 artists |
0567538f |
78 | is( $it->next->name, "Artist 2", "iterator->next ok" ); |
79 | $it->next; |
80 | $it->next; |
81 | is( $it->next, undef, "next past end of resultset ok" ); |
82 | |
e5372da4 |
83 | # Limit with select-lock |
84 | lives_ok { |
85 | $schema->txn_do (sub { |
86 | isa_ok ( |
87 | $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}), |
88 | 'DBICTest::Schema::Artist', |
89 | ); |
90 | }); |
91 | } 'Limited FOR UPDATE select works'; |
92 | |
4e0a89e4 |
93 | # shared-lock |
94 | lives_ok { |
95 | $schema->txn_do (sub { |
96 | isa_ok ( |
97 | $schema->resultset('Artist')->find({artistid => 1}, {for => 'shared'}), |
98 | 'DBICTest::Schema::Artist', |
99 | ); |
100 | }); |
101 | } 'LOCK IN SHARE MODE select works'; |
102 | |
a953d8d9 |
103 | my $test_type_info = { |
104 | 'artistid' => { |
103e3e03 |
105 | 'data_type' => 'INT', |
106 | 'is_nullable' => 0, |
fc22fbac |
107 | 'size' => 11, |
108 | 'default_value' => undef, |
a953d8d9 |
109 | }, |
110 | 'name' => { |
103e3e03 |
111 | 'data_type' => 'VARCHAR', |
a953d8d9 |
112 | 'is_nullable' => 1, |
a0dd8679 |
113 | 'size' => 100, |
fc22fbac |
114 | 'default_value' => undef, |
103e3e03 |
115 | }, |
39da2a2b |
116 | 'rank' => { |
117 | 'data_type' => 'INT', |
118 | 'is_nullable' => 0, |
119 | 'size' => 11, |
120 | 'default_value' => 13, |
121 | }, |
103e3e03 |
122 | 'charfield' => { |
637219ab |
123 | 'data_type' => 'CHAR', |
103e3e03 |
124 | 'is_nullable' => 1, |
fc22fbac |
125 | 'size' => 10, |
126 | 'default_value' => undef, |
103e3e03 |
127 | }, |
a953d8d9 |
128 | }; |
129 | |
bed3a173 |
130 | $schema->populate ('Owners', [ |
131 | [qw/id name /], |
132 | [qw/1 wiggle/], |
133 | [qw/2 woggle/], |
134 | [qw/3 boggle/], |
135 | ]); |
136 | |
137 | $schema->populate ('BooksInLibrary', [ |
138 | [qw/source owner title /], |
13a2f031 |
139 | [qw/Library 1 secrets1/], |
140 | [qw/Eatery 1 secrets2/], |
141 | [qw/Library 2 secrets3/], |
bed3a173 |
142 | ]); |
143 | |
13a2f031 |
144 | # |
8273e845 |
145 | # try a distinct + prefetch on tables with identically named columns |
13a2f031 |
146 | # (mysql doesn't seem to like subqueries with equally named columns) |
147 | # |
bed3a173 |
148 | |
d758ec36 |
149 | { |
13a2f031 |
150 | # try a ->has_many direction (due to a 'multi' accessor the select/group_by group is collapsed) |
151 | my $owners = $schema->resultset ('Owners')->search ( |
152 | { 'books.id' => { '!=', undef }}, |
153 | { prefetch => 'books', distinct => 1 } |
154 | ); |
155 | my $owners2 = $schema->resultset ('Owners')->search ({ id => { -in => $owners->get_column ('me.id')->as_query }}); |
156 | for ($owners, $owners2) { |
b5963465 |
157 | is ($_->all, 2, 'Prefetched grouped search returns correct number of rows'); |
158 | is ($_->count, 2, 'Prefetched grouped search returns correct count'); |
13a2f031 |
159 | } |
160 | |
d758ec36 |
161 | # try a ->belongs_to direction (no select collapse) |
b5963465 |
162 | my $books = $schema->resultset ('BooksInLibrary')->search ( |
163 | { 'owner.name' => 'wiggle' }, |
164 | { prefetch => 'owner', distinct => 1 } |
165 | ); |
166 | my $books2 = $schema->resultset ('BooksInLibrary')->search ({ id => { -in => $books->get_column ('me.id')->as_query }}); |
167 | for ($books, $books2) { |
168 | is ($_->all, 1, 'Prefetched grouped search returns correct number of rows'); |
169 | is ($_->count, 1, 'Prefetched grouped search returns correct count'); |
13a2f031 |
170 | } |
171 | } |
bed3a173 |
172 | |
5db49b9a |
173 | SKIP: { |
af1f4f84 |
174 | my $norm_version = $schema->storage->_server_info->{normalized_dbms_version} |
175 | or skip "Cannot determine MySQL server version", 1; |
a953d8d9 |
176 | |
af1f4f84 |
177 | if ($norm_version < 5.000003_01) { |
5db49b9a |
178 | $test_type_info->{charfield}->{data_type} = 'VARCHAR'; |
179 | } |
a953d8d9 |
180 | |
3ff5b740 |
181 | my $type_info = $schema->storage->columns_info_for('artist'); |
5db49b9a |
182 | is_deeply($type_info, $test_type_info, 'columns_info_for - column data types'); |
183 | } |
a953d8d9 |
184 | |
6a999241 |
185 | my $cd = $schema->resultset ('CD')->create ({}); |
186 | my $producer = $schema->resultset ('Producer')->create ({}); |
187 | lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die'; |
188 | |
aa82ce29 |
189 | { |
190 | my $artist = $schema->resultset('Artist')->next; |
191 | my $cd = $schema->resultset('CD')->next; |
192 | $cd->set_from_related ('artist', $artist); |
193 | $cd->update; |
194 | |
195 | my $rs = $schema->resultset('CD')->search ({}, { prefetch => 'artist' }); |
196 | |
197 | lives_ok sub { |
198 | my $cd = $rs->next; |
199 | is ($cd->artist->name, $artist->name, 'Prefetched artist'); |
200 | }, 'join does not throw (mysql 3 test)'; |
aa82ce29 |
201 | } |
6a999241 |
202 | |
55cfbb70 |
203 | ## Can we properly deal with the null search problem? |
a7e65bb5 |
204 | ## |
205 | ## Only way is to do a SET SQL_AUTO_IS_NULL = 0; on connect |
206 | ## But I'm not sure if we should do this or not (Ash, 2008/06/03) |
0fde80d9 |
207 | # |
208 | # There is now a built-in function to do this, test that everything works |
209 | # with it (ribasushi, 2009/07/03) |
55cfbb70 |
210 | |
211 | NULLINSEARCH: { |
97a0a148 |
212 | my $ansi_schema = DBICTest::Schema->connect ($dsn, $user, $pass, { on_connect_call => 'set_strict_mode' }); |
8de9298c |
213 | |
214 | $ansi_schema->resultset('Artist')->create ({ name => 'last created artist' }); |
6a999241 |
215 | |
216 | ok my $artist1_rs = $ansi_schema->resultset('Artist')->search({artistid=>6666}) |
217 | => 'Created an artist resultset of 6666'; |
218 | |
55cfbb70 |
219 | is $artist1_rs->count, 0 |
6a999241 |
220 | => 'Got no returned rows'; |
374dd926 |
221 | |
6a999241 |
222 | ok my $artist2_rs = $ansi_schema->resultset('Artist')->search({artistid=>undef}) |
223 | => 'Created an artist resultset of undef'; |
74de7c2c |
224 | |
6a999241 |
225 | is $artist2_rs->count, 0 |
226 | => 'got no rows'; |
74de7c2c |
227 | |
6a999241 |
228 | my $artist = $artist2_rs->single; |
55cfbb70 |
229 | |
5083f7de |
230 | is $artist => undef, |
6a999241 |
231 | => 'Nothing Found!'; |
74de7c2c |
232 | } |
aa82ce29 |
233 | |
0ce4ab92 |
234 | # check for proper grouped counts |
235 | { |
9f775126 |
236 | my $ansi_schema = DBICTest::Schema->connect ($dsn, $user, $pass, { |
237 | on_connect_call => 'set_strict_mode', |
238 | quote_char => '`', |
9f775126 |
239 | }); |
0ce4ab92 |
240 | my $rs = $ansi_schema->resultset('CD'); |
241 | |
242 | my $years; |
243 | $years->{$_->year|| scalar keys %$years}++ for $rs->all; # NULL != NULL, thus the keys eval |
244 | |
245 | lives_ok ( sub { |
246 | is ( |
247 | $rs->search ({}, { group_by => 'year'})->count, |
248 | scalar keys %$years, |
249 | 'grouped count correct', |
250 | ); |
251 | }, 'Grouped count does not throw'); |
9f775126 |
252 | |
253 | lives_ok( sub { |
254 | $ansi_schema->resultset('Owners')->search({}, { |
255 | join => 'books', group_by => [ 'me.id', 'books.id' ] |
256 | })->count(); |
257 | }, 'count on grouped columns with the same name does not throw'); |
0ce4ab92 |
258 | } |
259 | |
45150bc4 |
260 | # a more contrived^Wcomplicated self-referential double-subquery test |
261 | { |
262 | my $rs = $schema->resultset('Artist')->search({ name => { -like => 'baby_%' } }); |
263 | |
264 | $rs->populate([map { [$_] } ('name', map { "baby_$_" } (1..10) ) ]); |
265 | |
266 | my ($count_sql, @count_bind) = @${$rs->count_rs->as_query}; |
267 | |
268 | my $complex_rs = $schema->resultset('Artist')->search( |
269 | { artistid => { |
270 | -in => $rs->get_column('artistid') |
271 | ->as_query |
272 | } }, |
273 | ); |
274 | |
275 | $complex_rs->update({ name => \[ "CONCAT( `name`, '_bell_out_of_', $count_sql )", @count_bind ] }); |
276 | |
277 | for (1..10) { |
278 | is ( |
279 | $schema->resultset('Artist')->search({ name => "baby_${_}_bell_out_of_10" })->count, |
280 | 1, |
281 | "Correctly updated babybell $_", |
282 | ); |
283 | } |
284 | |
f4fdfd69 |
285 | is ($rs->count, 10, '10 artists present'); |
45150bc4 |
286 | |
49eeb48d |
287 | $schema->is_executed_querycount( sub { |
288 | $complex_rs->delete; |
289 | }, 1, 'One delete query fired' ); |
f4fdfd69 |
290 | is ($rs->count, 0, '10 Artists correctly deleted'); |
291 | |
292 | $rs->create({ |
293 | name => 'baby_with_cd', |
294 | cds => [ { title => 'babeeeeee', year => 2013 } ], |
295 | }); |
296 | is ($rs->count, 1, 'Artist with cd created'); |
297 | |
f4fdfd69 |
298 | |
49eeb48d |
299 | $schema->is_executed_querycount( sub { |
300 | $schema->resultset('CD')->search_related('artist', |
301 | { 'artist.name' => { -like => 'baby_with_%' } } |
302 | )->delete; |
303 | }, 1, 'And one more delete query fired'); |
304 | is ($rs->count, 0, 'Artist with cd deleted'); |
45150bc4 |
305 | } |
306 | |
e0d56e26 |
307 | ZEROINSEARCH: { |
308 | my $cds_per_year = { |
309 | 2001 => 2, |
310 | 2002 => 1, |
311 | 2005 => 3, |
312 | }; |
313 | |
314 | my $rs = $schema->resultset ('CD'); |
315 | $rs->delete; |
316 | for my $y (keys %$cds_per_year) { |
317 | for my $c (1 .. $cds_per_year->{$y} ) { |
318 | $rs->create ({ title => "CD $y-$c", artist => 1, year => "$y-01-01" }); |
319 | } |
320 | } |
321 | |
322 | is ($rs->count, 6, 'CDs created successfully'); |
323 | |
324 | $rs = $rs->search ({}, { |
ca458871 |
325 | select => [ \ 'YEAR(year)' ], as => ['y'], distinct => 1, |
e0d56e26 |
326 | }); |
327 | |
722274d0 |
328 | my $y_rs = $rs->get_column ('y'); |
329 | |
330 | warnings_exist { is_deeply ( |
331 | [ sort ($y_rs->all) ], |
e0d56e26 |
332 | [ sort keys %$cds_per_year ], |
333 | 'Years group successfully', |
722274d0 |
334 | ) } qr/ |
335 | \QUse of distinct => 1 while selecting anything other than a column \E |
336 | \Qdeclared on the primary ResultSource is deprecated\E |
337 | /x, 'deprecation warning'; |
338 | |
e0d56e26 |
339 | |
340 | $rs->create ({ artist => 1, year => '0-1-1', title => 'Jesus Rap' }); |
341 | |
342 | is_deeply ( |
722274d0 |
343 | [ sort $y_rs->all ], |
e0d56e26 |
344 | [ 0, sort keys %$cds_per_year ], |
345 | 'Zero-year groups successfully', |
346 | ); |
347 | |
8273e845 |
348 | # convoluted search taken verbatim from list |
e0d56e26 |
349 | my $restrict_rs = $rs->search({ -and => [ |
350 | year => { '!=', 0 }, |
351 | year => { '!=', undef } |
352 | ]}); |
353 | |
722274d0 |
354 | warnings_exist { is_deeply ( |
e0d56e26 |
355 | [ $restrict_rs->get_column('y')->all ], |
722274d0 |
356 | [ $y_rs->all ], |
e0d56e26 |
357 | 'Zero year was correctly excluded from resultset', |
722274d0 |
358 | ) } qr/ |
359 | \QUse of distinct => 1 while selecting anything other than a column \E |
360 | \Qdeclared on the primary ResultSource is deprecated\E |
361 | /x, 'deprecation warning'; |
e0d56e26 |
362 | } |
10176e1f |
363 | |
f98120e4 |
364 | # make sure find hooks determine driver |
365 | { |
366 | my $schema = DBICTest::Schema->connect($dsn, $user, $pass); |
367 | $schema->resultset("Artist")->find(4); |
368 | isa_ok($schema->storage->sql_maker, 'DBIx::Class::SQLMaker::MySQL'); |
369 | } |
370 | |
371 | # make sure the mysql_auto_reconnect buggery is avoided |
372 | { |
373 | local $ENV{MOD_PERL} = 'boogiewoogie'; |
374 | my $schema = DBICTest::Schema->connect($dsn, $user, $pass); |
375 | ok (! $schema->storage->_get_dbh->{mysql_auto_reconnect}, 'mysql_auto_reconnect unset regardless of ENV' ); |
376 | |
da6a5860 |
377 | # Make sure hardcore forking action still works even if mysql_auto_reconnect |
378 | # is true (test inspired by ether) |
379 | |
380 | my $schema_autorecon = DBICTest::Schema->connect($dsn, $user, $pass, { mysql_auto_reconnect => 1 }); |
381 | my $orig_dbh = $schema_autorecon->storage->_get_dbh; |
382 | weaken $orig_dbh; |
383 | |
384 | ok ($orig_dbh, 'Got weak $dbh ref'); |
385 | ok ($orig_dbh->{mysql_auto_reconnect}, 'mysql_auto_reconnect is properly set if explicitly requested' ); |
386 | |
387 | my $rs = $schema_autorecon->resultset('Artist'); |
388 | |
7be5717e |
389 | my ($parent_in, $child_out); |
390 | pipe( $parent_in, $child_out ) or die "Pipe open failed: $!"; |
da6a5860 |
391 | my $pid = fork(); |
392 | if (! defined $pid ) { |
393 | die "fork() failed: $!" |
394 | } |
395 | elsif ($pid) { |
7be5717e |
396 | close $child_out; |
397 | |
da6a5860 |
398 | # sanity check |
399 | $schema_autorecon->storage->dbh_do(sub { |
400 | is ($_[1], $orig_dbh, 'Storage holds correct $dbh in parent'); |
401 | }); |
402 | |
403 | # kill our $dbh |
404 | $schema_autorecon->storage->_dbh(undef); |
e0b2dc74 |
405 | |
4ca1fd6f |
406 | { |
e0b2dc74 |
407 | local $TODO = "Perl $] is known to leak like a sieve" |
0d8817bc |
408 | if DBIx::Class::_ENV_::PEEPEENESS; |
e0b2dc74 |
409 | |
410 | ok (! defined $orig_dbh, 'Parent $dbh handle is gone'); |
411 | } |
da6a5860 |
412 | } |
413 | else { |
7be5717e |
414 | close $parent_in; |
da6a5860 |
415 | |
416 | #simulate a subtest to not confuse the parent TAP emission |
7be5717e |
417 | my $tb = Test::More->builder; |
418 | $tb->reset; |
419 | for (qw/output failure_output todo_output/) { |
420 | close $tb->$_; |
421 | open ($tb->$_, '>&', $child_out); |
422 | } |
423 | |
424 | # wait for parent to kill its $dbh |
425 | sleep 1; |
da6a5860 |
426 | |
da6a5860 |
427 | # try to do something dbic-esque |
428 | $rs->create({ name => "Hardcore Forker $$" }); |
429 | |
4ca1fd6f |
430 | { |
e0b2dc74 |
431 | local $TODO = "Perl $] is known to leak like a sieve" |
0d8817bc |
432 | if DBIx::Class::_ENV_::PEEPEENESS; |
e0b2dc74 |
433 | |
434 | ok (! defined $orig_dbh, 'DBIC operation triggered reconnect - old $dbh is gone'); |
435 | } |
da6a5860 |
436 | |
7be5717e |
437 | done_testing; |
da6a5860 |
438 | exit 0; |
439 | } |
440 | |
7be5717e |
441 | while (my $ln = <$parent_in>) { |
442 | print " $ln"; |
443 | } |
da6a5860 |
444 | wait; |
445 | ok(!$?, 'Child subtests passed'); |
446 | |
447 | ok ($rs->find({ name => "Hardcore Forker $pid" }), 'Expected row created'); |
f98120e4 |
448 | } |
10176e1f |
449 | |
aa82ce29 |
450 | done_testing; |