Commit | Line | Data |
70350518 |
1 | use strict; |
68de9438 |
2 | use warnings; |
70350518 |
3 | |
4 | use Test::More; |
74de7c2c |
5 | use Test::Exception; |
70350518 |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
5db49b9a |
8 | use DBI::Const::GetInfoType; |
aa82ce29 |
9 | use DBIC::SqlMakerTest; |
0567538f |
10 | |
11 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}; |
12 | |
13 | #warn "$dsn $user $pass"; |
14 | |
70350518 |
15 | plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test' |
0567538f |
16 | unless ($dsn && $user); |
17 | |
3ff5b740 |
18 | my $schema = DBICTest::Schema->connect($dsn, $user, $pass); |
0567538f |
19 | |
3ff5b740 |
20 | my $dbh = $schema->storage->dbh; |
0567538f |
21 | |
22 | $dbh->do("DROP TABLE IF EXISTS artist;"); |
23 | |
a0dd8679 |
24 | $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 |
25 | |
74de7c2c |
26 | $dbh->do("DROP TABLE IF EXISTS cd;"); |
27 | |
e0d56e26 |
28 | $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 |
29 | |
30 | $dbh->do("DROP TABLE IF EXISTS producer;"); |
31 | |
32 | $dbh->do("CREATE TABLE producer (producerid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name TEXT);"); |
33 | |
34 | $dbh->do("DROP TABLE IF EXISTS cd_to_producer;"); |
35 | |
36 | $dbh->do("CREATE TABLE cd_to_producer (cd INTEGER,producer INTEGER);"); |
37 | |
bed3a173 |
38 | $dbh->do("DROP TABLE IF EXISTS owners;"); |
39 | |
40 | $dbh->do("CREATE TABLE owners (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL);"); |
41 | |
42 | $dbh->do("DROP TABLE IF EXISTS books;"); |
43 | |
44 | $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);"); |
45 | |
0567538f |
46 | #'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', ''); |
47 | |
adbf0c89 |
48 | # make sure sqlt_type overrides work (::Storage::DBI::mysql does this) |
49 | { |
50 | my $schema = DBICTest::Schema->connect($dsn, $user, $pass); |
51 | |
52 | ok (!$schema->storage->_dbh, 'definitely not connected'); |
53 | is ($schema->storage->sqlt_type, 'MySQL', 'sqlt_type correct pre-connection'); |
54 | } |
55 | |
3ff5b740 |
56 | # This is in Core now, but it's here just to test that it doesn't break |
57 | $schema->class('Artist')->load_components('PK::Auto'); |
0567538f |
58 | |
59 | # test primary key handling |
3ff5b740 |
60 | my $new = $schema->resultset('Artist')->create({ name => 'foo' }); |
0567538f |
61 | ok($new->artistid, "Auto-PK worked"); |
62 | |
63 | # test LIMIT support |
64 | for (1..6) { |
3ff5b740 |
65 | $schema->resultset('Artist')->create({ name => 'Artist ' . $_ }); |
0567538f |
66 | } |
3ff5b740 |
67 | my $it = $schema->resultset('Artist')->search( {}, |
0567538f |
68 | { rows => 3, |
69 | offset => 2, |
70 | order_by => 'artistid' } |
71 | ); |
fb4b58e8 |
72 | is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 7 artists |
0567538f |
73 | is( $it->next->name, "Artist 2", "iterator->next ok" ); |
74 | $it->next; |
75 | $it->next; |
76 | is( $it->next, undef, "next past end of resultset ok" ); |
77 | |
e5372da4 |
78 | # Limit with select-lock |
79 | lives_ok { |
80 | $schema->txn_do (sub { |
81 | isa_ok ( |
82 | $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}), |
83 | 'DBICTest::Schema::Artist', |
84 | ); |
85 | }); |
86 | } 'Limited FOR UPDATE select works'; |
87 | |
4e0a89e4 |
88 | # shared-lock |
89 | lives_ok { |
90 | $schema->txn_do (sub { |
91 | isa_ok ( |
92 | $schema->resultset('Artist')->find({artistid => 1}, {for => 'shared'}), |
93 | 'DBICTest::Schema::Artist', |
94 | ); |
95 | }); |
96 | } 'LOCK IN SHARE MODE select works'; |
97 | |
a953d8d9 |
98 | my $test_type_info = { |
99 | 'artistid' => { |
103e3e03 |
100 | 'data_type' => 'INT', |
101 | 'is_nullable' => 0, |
fc22fbac |
102 | 'size' => 11, |
103 | 'default_value' => undef, |
a953d8d9 |
104 | }, |
105 | 'name' => { |
103e3e03 |
106 | 'data_type' => 'VARCHAR', |
a953d8d9 |
107 | 'is_nullable' => 1, |
a0dd8679 |
108 | 'size' => 100, |
fc22fbac |
109 | 'default_value' => undef, |
103e3e03 |
110 | }, |
39da2a2b |
111 | 'rank' => { |
112 | 'data_type' => 'INT', |
113 | 'is_nullable' => 0, |
114 | 'size' => 11, |
115 | 'default_value' => 13, |
116 | }, |
103e3e03 |
117 | 'charfield' => { |
637219ab |
118 | 'data_type' => 'CHAR', |
103e3e03 |
119 | 'is_nullable' => 1, |
fc22fbac |
120 | 'size' => 10, |
121 | 'default_value' => undef, |
103e3e03 |
122 | }, |
a953d8d9 |
123 | }; |
124 | |
bed3a173 |
125 | $schema->populate ('Owners', [ |
126 | [qw/id name /], |
127 | [qw/1 wiggle/], |
128 | [qw/2 woggle/], |
129 | [qw/3 boggle/], |
130 | ]); |
131 | |
132 | $schema->populate ('BooksInLibrary', [ |
133 | [qw/source owner title /], |
13a2f031 |
134 | [qw/Library 1 secrets1/], |
135 | [qw/Eatery 1 secrets2/], |
136 | [qw/Library 2 secrets3/], |
bed3a173 |
137 | ]); |
138 | |
13a2f031 |
139 | # |
140 | # try a distinct + prefetch on tables with identically named columns |
141 | # (mysql doesn't seem to like subqueries with equally named columns) |
142 | # |
bed3a173 |
143 | |
d758ec36 |
144 | { |
13a2f031 |
145 | # try a ->has_many direction (due to a 'multi' accessor the select/group_by group is collapsed) |
146 | my $owners = $schema->resultset ('Owners')->search ( |
147 | { 'books.id' => { '!=', undef }}, |
148 | { prefetch => 'books', distinct => 1 } |
149 | ); |
150 | my $owners2 = $schema->resultset ('Owners')->search ({ id => { -in => $owners->get_column ('me.id')->as_query }}); |
151 | for ($owners, $owners2) { |
b5963465 |
152 | is ($_->all, 2, 'Prefetched grouped search returns correct number of rows'); |
153 | is ($_->count, 2, 'Prefetched grouped search returns correct count'); |
13a2f031 |
154 | } |
155 | |
d758ec36 |
156 | # try a ->belongs_to direction (no select collapse) |
b5963465 |
157 | my $books = $schema->resultset ('BooksInLibrary')->search ( |
158 | { 'owner.name' => 'wiggle' }, |
159 | { prefetch => 'owner', distinct => 1 } |
160 | ); |
161 | my $books2 = $schema->resultset ('BooksInLibrary')->search ({ id => { -in => $books->get_column ('me.id')->as_query }}); |
162 | for ($books, $books2) { |
163 | is ($_->all, 1, 'Prefetched grouped search returns correct number of rows'); |
164 | is ($_->count, 1, 'Prefetched grouped search returns correct count'); |
13a2f031 |
165 | } |
166 | } |
bed3a173 |
167 | |
5db49b9a |
168 | SKIP: { |
169 | my $mysql_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ); |
5db49b9a |
170 | skip "Cannot determine MySQL server version", 1 if !$mysql_version; |
a953d8d9 |
171 | |
f750163c |
172 | my ($v1, $v2, $v3) = $mysql_version =~ /^(\d+)\.(\d+)(?:\.(\d+))?/; |
173 | skip "Cannot determine MySQL server version", 1 if !$v1 || !defined($v2); |
174 | |
175 | $v3 ||= 0; |
176 | |
5db49b9a |
177 | if( ($v1 < 5) || ($v1 == 5 && $v2 == 0 && $v3 <= 3) ) { |
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)'; |
201 | |
202 | # induce a jointype override, make sure it works even if we don't have mysql3 |
de5f71ef |
203 | local $schema->storage->sql_maker->{_default_jointype} = 'inner'; |
aa82ce29 |
204 | is_same_sql_bind ( |
205 | $rs->as_query, |
206 | '( |
207 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, |
208 | artist.artistid, artist.name, artist.rank, artist.charfield |
209 | FROM cd me |
210 | INNER JOIN artist artist ON artist.artistid = me.artist |
211 | )', |
212 | [], |
213 | 'overriden default join type works', |
214 | ); |
215 | } |
6a999241 |
216 | |
b8391c87 |
217 | { |
218 | # Test support for straight joins |
219 | my $cdsrc = $schema->source('CD'); |
220 | my $artrel_info = $cdsrc->relationship_info ('artist'); |
221 | $cdsrc->add_relationship( |
222 | 'straight_artist', |
223 | $artrel_info->{class}, |
224 | $artrel_info->{cond}, |
225 | { %{$artrel_info->{attrs}}, join_type => 'straight' }, |
226 | ); |
227 | is_same_sql_bind ( |
228 | $cdsrc->resultset->search({}, { prefetch => 'straight_artist' })->as_query, |
229 | '( |
230 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, |
231 | straight_artist.artistid, straight_artist.name, straight_artist.rank, straight_artist.charfield |
232 | FROM cd me |
233 | STRAIGHT_JOIN artist straight_artist ON straight_artist.artistid = me.artist |
234 | )', |
235 | [], |
236 | 'straight joins correctly supported for mysql' |
237 | ); |
238 | } |
239 | |
55cfbb70 |
240 | ## Can we properly deal with the null search problem? |
a7e65bb5 |
241 | ## |
242 | ## Only way is to do a SET SQL_AUTO_IS_NULL = 0; on connect |
243 | ## But I'm not sure if we should do this or not (Ash, 2008/06/03) |
0fde80d9 |
244 | # |
245 | # There is now a built-in function to do this, test that everything works |
246 | # with it (ribasushi, 2009/07/03) |
55cfbb70 |
247 | |
248 | NULLINSEARCH: { |
97a0a148 |
249 | my $ansi_schema = DBICTest::Schema->connect ($dsn, $user, $pass, { on_connect_call => 'set_strict_mode' }); |
8de9298c |
250 | |
251 | $ansi_schema->resultset('Artist')->create ({ name => 'last created artist' }); |
6a999241 |
252 | |
253 | ok my $artist1_rs = $ansi_schema->resultset('Artist')->search({artistid=>6666}) |
254 | => 'Created an artist resultset of 6666'; |
255 | |
55cfbb70 |
256 | is $artist1_rs->count, 0 |
6a999241 |
257 | => 'Got no returned rows'; |
374dd926 |
258 | |
6a999241 |
259 | ok my $artist2_rs = $ansi_schema->resultset('Artist')->search({artistid=>undef}) |
260 | => 'Created an artist resultset of undef'; |
74de7c2c |
261 | |
6a999241 |
262 | is $artist2_rs->count, 0 |
263 | => 'got no rows'; |
74de7c2c |
264 | |
6a999241 |
265 | my $artist = $artist2_rs->single; |
55cfbb70 |
266 | |
6a999241 |
267 | is $artist => undef |
268 | => 'Nothing Found!'; |
74de7c2c |
269 | } |
aa82ce29 |
270 | |
0ce4ab92 |
271 | # check for proper grouped counts |
272 | { |
9f775126 |
273 | my $ansi_schema = DBICTest::Schema->connect ($dsn, $user, $pass, { |
274 | on_connect_call => 'set_strict_mode', |
275 | quote_char => '`', |
9f775126 |
276 | }); |
0ce4ab92 |
277 | my $rs = $ansi_schema->resultset('CD'); |
278 | |
279 | my $years; |
280 | $years->{$_->year|| scalar keys %$years}++ for $rs->all; # NULL != NULL, thus the keys eval |
281 | |
282 | lives_ok ( sub { |
283 | is ( |
284 | $rs->search ({}, { group_by => 'year'})->count, |
285 | scalar keys %$years, |
286 | 'grouped count correct', |
287 | ); |
288 | }, 'Grouped count does not throw'); |
9f775126 |
289 | |
290 | lives_ok( sub { |
291 | $ansi_schema->resultset('Owners')->search({}, { |
292 | join => 'books', group_by => [ 'me.id', 'books.id' ] |
293 | })->count(); |
294 | }, 'count on grouped columns with the same name does not throw'); |
295 | |
296 | |
0ce4ab92 |
297 | } |
298 | |
e0d56e26 |
299 | ZEROINSEARCH: { |
300 | my $cds_per_year = { |
301 | 2001 => 2, |
302 | 2002 => 1, |
303 | 2005 => 3, |
304 | }; |
305 | |
306 | my $rs = $schema->resultset ('CD'); |
307 | $rs->delete; |
308 | for my $y (keys %$cds_per_year) { |
309 | for my $c (1 .. $cds_per_year->{$y} ) { |
310 | $rs->create ({ title => "CD $y-$c", artist => 1, year => "$y-01-01" }); |
311 | } |
312 | } |
313 | |
314 | is ($rs->count, 6, 'CDs created successfully'); |
315 | |
316 | $rs = $rs->search ({}, { |
ca458871 |
317 | select => [ \ 'YEAR(year)' ], as => ['y'], distinct => 1, |
e0d56e26 |
318 | }); |
319 | |
320 | is_deeply ( |
ca458871 |
321 | [ sort ($rs->get_column ('y')->all) ], |
e0d56e26 |
322 | [ sort keys %$cds_per_year ], |
323 | 'Years group successfully', |
324 | ); |
325 | |
326 | $rs->create ({ artist => 1, year => '0-1-1', title => 'Jesus Rap' }); |
327 | |
328 | is_deeply ( |
ca458871 |
329 | [ sort $rs->get_column ('y')->all ], |
e0d56e26 |
330 | [ 0, sort keys %$cds_per_year ], |
331 | 'Zero-year groups successfully', |
332 | ); |
333 | |
334 | # convoluted search taken verbatim from list |
335 | my $restrict_rs = $rs->search({ -and => [ |
336 | year => { '!=', 0 }, |
337 | year => { '!=', undef } |
338 | ]}); |
339 | |
340 | is_deeply ( |
341 | [ $restrict_rs->get_column('y')->all ], |
342 | [ $rs->get_column ('y')->all ], |
343 | 'Zero year was correctly excluded from resultset', |
344 | ); |
345 | } |
10176e1f |
346 | |
347 | ## If find() is the first query after connect() |
348 | ## DBI::Storage::sql_maker() will be called before |
349 | ## _determine_driver() and so the ::SQLHacks class for MySQL |
350 | ## will not be used |
351 | |
352 | my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass); |
353 | $schema2->resultset("Artist")->find(4); |
d5dedbd6 |
354 | isa_ok($schema2->storage->sql_maker, 'DBIx::Class::SQLMaker::MySQL'); |
10176e1f |
355 | |
aa82ce29 |
356 | done_testing; |