Commit | Line | Data |
0567538f |
1 | sub run_tests { |
1edaf6fe |
2 | my $schema = shift; |
0567538f |
3 | |
4 | use IO::File; |
5 | |
6 | BEGIN { |
7 | eval "use DBD::SQLite"; |
8 | plan $@ |
9 | ? ( skip_all => 'needs DBD::SQLite for testing' ) |
0823196c |
10 | : ( tests => 44 ); |
0567538f |
11 | } |
12 | |
58ff4acf |
13 | # figure out if we've got a version of sqlite that is older than 3.2.6, in |
14 | # which case COUNT(DISTINCT()) doesn't work |
15 | my $is_broken_sqlite = 0; |
16 | my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) = |
17 | split /\./, $schema->storage->dbh->get_info(18); |
18 | if( $schema->storage->dbh->get_info(17) eq 'SQLite' && |
19 | ( ($sqlite_major_ver < 3) || |
20 | ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) || |
21 | ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) { |
22 | $is_broken_sqlite = 1; |
23 | } |
485f6e10 |
24 | |
0567538f |
25 | # test the abstract join => SQL generator |
26 | my $sa = new DBIC::SQL::Abstract; |
27 | |
28 | my @j = ( |
29 | { child => 'person' }, |
30 | [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ], |
31 | [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ], |
32 | ); |
33 | my $match = 'person child JOIN person father ON ( father.person_id = ' |
34 | . 'child.father_id ) JOIN person mother ON ( mother.person_id ' |
35 | . '= child.mother_id )' |
36 | ; |
37 | is( $sa->_recurse_from(@j), $match, 'join 1 ok' ); |
38 | |
39 | my @j2 = ( |
40 | { mother => 'person' }, |
41 | [ [ { child => 'person' }, |
42 | [ { father => 'person' }, |
43 | { 'father.person_id' => 'child.father_id' } |
44 | ] |
45 | ], |
46 | { 'mother.person_id' => 'child.mother_id' } |
47 | ], |
48 | ); |
49 | $match = 'person mother JOIN (person child JOIN person father ON (' |
50 | . ' father.person_id = child.father_id )) ON ( mother.person_id = ' |
51 | . 'child.mother_id )' |
52 | ; |
53 | is( $sa->_recurse_from(@j2), $match, 'join 2 ok' ); |
54 | |
55 | my @j3 = ( |
56 | { child => 'person' }, |
57 | [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ], |
58 | [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ], |
59 | ); |
60 | $match = 'person child INNER JOIN person father ON ( father.person_id = ' |
61 | . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id ' |
62 | . '= child.mother_id )' |
63 | ; |
64 | |
65 | is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok'); |
66 | |
f9db5527 |
67 | my $rs = $schema->resultset("CD")->search( |
0567538f |
68 | { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, |
69 | { from => [ { 'me' => 'cd' }, |
70 | [ |
71 | { artist => 'artist' }, |
72 | { 'me.artist' => 'artist.artistid' } |
73 | ] ] } |
74 | ); |
75 | |
ebaefbc2 |
76 | cmp_ok( $rs + 0, '==', 1, "Single record in resultset"); |
0567538f |
77 | |
78 | is($rs->first->title, 'Forkful of bees', 'Correct record returned'); |
79 | |
f9db5527 |
80 | $rs = $schema->resultset("CD")->search( |
0567538f |
81 | { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, |
82 | { join => 'artist' }); |
83 | |
ebaefbc2 |
84 | cmp_ok( $rs + 0, '==', 1, "Single record in resultset"); |
0567538f |
85 | |
86 | is($rs->first->title, 'Forkful of bees', 'Correct record returned'); |
87 | |
f9db5527 |
88 | $rs = $schema->resultset("CD")->search( |
0567538f |
89 | { 'artist.name' => 'We Are Goth', |
90 | 'liner_notes.notes' => 'Kill Yourself!' }, |
91 | { join => [ qw/artist liner_notes/ ] }); |
92 | |
ebaefbc2 |
93 | cmp_ok( $rs + 0, '==', 1, "Single record in resultset"); |
0567538f |
94 | |
95 | is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned'); |
96 | |
8fe164b9 |
97 | # when using join attribute, make sure slice()ing all objects has same count as all() |
f9db5527 |
98 | $rs = $schema->resultset("CD")->search( |
8fe164b9 |
99 | { 'artist' => 1 }, |
100 | { join => [qw/artist/], order_by => 'artist.name' } |
101 | ); |
102 | cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' ); |
103 | |
0823196c |
104 | eval { $rs->search(undef, { rows => 0, offset => 3 })->all; }; |
105 | |
106 | ok($@, "rows => 0 errors: $@"); |
107 | |
f9db5527 |
108 | $rs = $schema->resultset("Artist")->search( |
0567538f |
109 | { 'liner_notes.notes' => 'Kill Yourself!' }, |
110 | { join => { 'cds' => 'liner_notes' } }); |
111 | |
112 | cmp_ok( $rs->count, '==', 1, "Single record in resultset"); |
113 | |
114 | is($rs->first->name, 'We Are Goth', 'Correct record returned'); |
115 | |
f9db5527 |
116 | $rs = $schema->resultset("CD")->search( |
0567538f |
117 | { 'artist.name' => 'Caterwauler McCrae' }, |
118 | { prefetch => [ qw/artist liner_notes/ ], |
119 | order_by => 'me.cdid' }); |
120 | |
ebaefbc2 |
121 | cmp_ok($rs + 0, '==', 3, 'Correct number of records returned'); |
0567538f |
122 | |
d52170d4 |
123 | my $queries = 0; |
124 | $schema->storage->debugcb(sub { $queries++ }); |
125 | |
126 | $queries = 0; |
127 | $schema->storage->debug(1); |
0567538f |
128 | |
129 | my @cd = $rs->all; |
130 | |
131 | is($cd[0]->title, 'Spoonful of bees', 'First record returned ok'); |
132 | |
7cd300ea |
133 | ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join'); |
0567538f |
134 | |
135 | is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN'); |
136 | |
dd417d06 |
137 | is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class'); |
138 | |
0567538f |
139 | is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok'); |
140 | |
d52170d4 |
141 | is($queries, 1, 'prefetch ran only 1 select statement'); |
142 | |
143 | $schema->storage->debug(0); |
0567538f |
144 | |
9b465d00 |
145 | # test for partial prefetch via columns attr |
d20bb28e |
146 | my $cd = $schema->resultset('CD')->find(1, |
147 | { |
9b465d00 |
148 | columns => [qw/title artist.name/], |
0e35aa05 |
149 | join => { 'artist' => {} } |
d20bb28e |
150 | } |
151 | ); |
152 | ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched'); |
153 | |
b3e8ac9b |
154 | # start test for nested prefetch SELECT count |
d52170d4 |
155 | $queries = 0; |
156 | $schema->storage->debug(1); |
b3e8ac9b |
157 | |
158 | $rs = $schema->resultset('Tag')->search( |
159 | {}, |
160 | { |
161 | prefetch => { cd => 'artist' } |
162 | } |
163 | ); |
164 | |
165 | my $tag = $rs->first; |
166 | |
167 | is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' ); |
168 | |
169 | is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch'); |
170 | |
171 | # count the SELECTs |
d52170d4 |
172 | #$selects++ if /SELECT(?!.*WHERE 1=0.*)/; |
173 | is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)'); |
b3e8ac9b |
174 | |
d52170d4 |
175 | $queries = 0; |
c5b7d799 |
176 | |
d20bb28e |
177 | $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' }); |
c5b7d799 |
178 | |
179 | is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find'); |
180 | |
d52170d4 |
181 | is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)'); |
182 | |
183 | $schema->storage->debug(0); |
c5b7d799 |
184 | |
b3e8ac9b |
185 | $rs = $schema->resultset('Tag')->search( |
186 | {}, |
187 | { |
188 | join => { cd => 'artist' }, |
189 | prefetch => { cd => 'artist' } |
190 | } |
191 | ); |
192 | |
193 | cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' ); |
194 | |
f9db5527 |
195 | my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 }, |
0567538f |
196 | { order_by => 'artistid DESC', join => 'cds' }); |
197 | |
198 | is($artist->name, 'Random Boy Band', "Join search by object ok"); |
199 | |
f9db5527 |
200 | my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' }, |
0567538f |
201 | { join => 'liner_notes' }); |
202 | |
203 | cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have"); |
204 | |
205 | is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved"); |
206 | |
f9db5527 |
207 | my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' }, |
0567538f |
208 | { join => { 'cds' => 'tags' } }); |
209 | |
210 | cmp_ok( @artists, '==', 2, "two-join search ok" ); |
211 | |
15c382be |
212 | $rs = $schema->resultset("CD")->search( |
213 | {}, |
214 | { group_by => [qw/ title me.cdid /] } |
215 | ); |
216 | |
485f6e10 |
217 | SKIP: { |
218 | skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1 |
219 | if $is_broken_sqlite; |
220 | cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" ); |
221 | } |
15c382be |
222 | |
223 | cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" ); |
224 | |
225 | $rs = $schema->resultset("CD")->search( |
226 | {}, |
227 | { join => [qw/ artist /], group_by => [qw/ artist.name /] } |
228 | ); |
229 | |
485f6e10 |
230 | SKIP: { |
231 | skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1 |
232 | if $is_broken_sqlite; |
233 | cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" ); |
234 | } |
15c382be |
235 | |
8839560b |
236 | $rs = $schema->resultset("Artist")->search( |
6991aba3 |
237 | {}, |
01c73d7b |
238 | { join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } } |
6991aba3 |
239 | ); |
240 | |
8839560b |
241 | cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" ); |
6991aba3 |
242 | |
01c73d7b |
243 | $rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }}); |
6991aba3 |
244 | |
8839560b |
245 | cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" ); |
489709af |
246 | |
247 | $rs = $schema->resultset("Artist")->search( |
248 | { 'cds.title' => 'Spoonful of bees', |
249 | 'cds_2.title' => 'Forkful of bees' }, |
250 | { join => [ 'cds', 'cds' ] }); |
251 | |
485f6e10 |
252 | SKIP: { |
253 | skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1 |
254 | if $is_broken_sqlite; |
255 | cmp_ok($rs->count, '==', 1, "single artist returned from multi-join"); |
256 | } |
257 | |
489709af |
258 | is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned"); |
259 | |
d52170d4 |
260 | $queries = 0; |
887ce227 |
261 | $schema->storage->debug(1); |
262 | |
263 | my $tree_like = |
264 | $schema->resultset('TreeLike')->find(4, |
265 | { join => { parent => { parent => 'parent' } }, |
266 | prefetch => { parent => { parent => 'parent' } } }); |
267 | |
268 | is($tree_like->name, 'quux', 'Bottom of tree ok'); |
269 | $tree_like = $tree_like->parent; |
270 | is($tree_like->name, 'baz', 'First level up ok'); |
271 | $tree_like = $tree_like->parent; |
272 | is($tree_like->name, 'bar', 'Second level up ok'); |
273 | $tree_like = $tree_like->parent; |
274 | is($tree_like->name, 'foo', 'Third level up ok'); |
275 | |
276 | $schema->storage->debug(0); |
277 | |
278 | cmp_ok($queries, '==', 1, 'Only one query run'); |
279 | |
0823196c |
280 | # has_many resulting in an additional select if no records available despite prefetch |
281 | my $track = $schema->resultset("Artist")->create( { |
282 | artistid => 4, |
283 | name => 'Artist without CDs', |
284 | } ); |
285 | |
286 | $queries = 0; |
287 | $schema->storage->debug(1); |
288 | |
289 | my $artist_without_cds = $schema->resultset("Artist")->find(4, { |
290 | join => [qw/ cds /], |
291 | prefetch => [qw/ cds /], |
292 | }); |
293 | my @no_cds = $artist_without_cds->cds; |
294 | |
295 | is($queries, 1, 'prefetch ran only 1 sql statement'); |
296 | |
297 | $schema->storage->debug(0); |
298 | |
489709af |
299 | } # end run_tests |
0567538f |
300 | |
301 | 1; |