Skip to content

Commit 96e7f9e

Browse files
committed
Split prefetch related tests from 76joins.t
1 parent 331a564 commit 96e7f9e

File tree

2 files changed

+346
-313
lines changed

2 files changed

+346
-313
lines changed

t/76joins.t

Lines changed: 1 addition & 313 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ BEGIN {
1616
eval "use DBD::SQLite";
1717
plan $@
1818
? ( skip_all => 'needs DBD::SQLite for testing' )
19-
: ( tests => 64 );
19+
: ( tests => 16 );
2020
}
2121

2222
# figure out if we've got a version of sqlite that is older than 3.2.6, in
@@ -157,315 +157,3 @@ cmp_ok( $rs->count, '==', 1, "Single record in resultset");
157157

158158
is($rs->first->name, 'We Are Goth', 'Correct record returned');
159159

160-
# bug in 0.07000 caused attr (join/prefetch) to be modifed by search
161-
# so we check the search & attr arrays are not modified
162-
my $search = { 'artist.name' => 'Caterwauler McCrae' };
163-
my $attr = { prefetch => [ qw/artist liner_notes/ ],
164-
order_by => 'me.cdid' };
165-
my $search_str = Dumper($search);
166-
my $attr_str = Dumper($attr);
167-
168-
$rs = $schema->resultset("CD")->search($search, $attr);
169-
170-
is(Dumper($search), $search_str, 'Search hash untouched after search()');
171-
is(Dumper($attr), $attr_str, 'Attribute hash untouched after search()');
172-
cmp_ok($rs + 0, '==', 3, 'Correct number of records returned');
173-
174-
my $queries = 0;
175-
$schema->storage->debugcb(sub { $queries++; });
176-
$schema->storage->debug(1);
177-
178-
my @cd = $rs->all;
179-
180-
is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
181-
182-
ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
183-
184-
is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
185-
186-
is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
187-
188-
is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
189-
190-
is($queries, 1, 'prefetch ran only 1 select statement');
191-
192-
$schema->storage->debug($orig_debug);
193-
$schema->storage->debugobj->callback(undef);
194-
195-
# test for partial prefetch via columns attr
196-
my $cd = $schema->resultset('CD')->find(1,
197-
{
198-
columns => [qw/title artist.name/],
199-
join => { 'artist' => {} }
200-
}
201-
);
202-
ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
203-
204-
# start test for nested prefetch SELECT count
205-
$queries = 0;
206-
$schema->storage->debugcb(sub { $queries++ });
207-
$schema->storage->debug(1);
208-
209-
$rs = $schema->resultset('Tag')->search(
210-
{},
211-
{
212-
prefetch => { cd => 'artist' }
213-
}
214-
);
215-
216-
my $tag = $rs->first;
217-
218-
is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
219-
220-
is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
221-
222-
# count the SELECTs
223-
#$selects++ if /SELECT(?!.*WHERE 1=0.*)/;
224-
is($queries, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
225-
226-
$queries = 0;
227-
228-
is($tag->search_related('cd')->search_related('artist')->first->name,
229-
'Caterwauler McCrae',
230-
'chained belongs_to->belongs_to search_related ok');
231-
232-
is($queries, 0, 'chained search_related after belontgs_to->belongs_to prefetch ran no queries');
233-
234-
$queries = 0;
235-
236-
$cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
237-
238-
is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
239-
240-
is($queries, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
241-
242-
$queries = 0;
243-
244-
$schema->storage->debugcb(sub { $queries++; });
245-
246-
$cd = $schema->resultset('CD')->find(1, { prefetch => { cd_to_producer => 'producer' } });
247-
248-
is($cd->producers->first->name, 'Matt S Trout', 'many_to_many accessor ok');
249-
250-
is($queries, 1, 'many_to_many accessor with nested prefetch ran exactly 1 query');
251-
252-
$queries = 0;
253-
254-
my $producers = $cd->search_related('cd_to_producer')->search_related('producer');
255-
256-
is($producers->first->name, 'Matt S Trout', 'chained many_to_many search_related ok');
257-
258-
is($queries, 0, 'chained search_related after many_to_many prefetch ran no queries');
259-
260-
$schema->storage->debug($orig_debug);
261-
$schema->storage->debugobj->callback(undef);
262-
263-
$rs = $schema->resultset('Tag')->search(
264-
{},
265-
{
266-
join => { cd => 'artist' },
267-
prefetch => { cd => 'artist' }
268-
}
269-
);
270-
271-
cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
272-
273-
my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
274-
{ order_by => 'artistid DESC', join => 'cds' });
275-
276-
is($artist->name, 'Random Boy Band', "Join search by object ok");
277-
278-
my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
279-
{ join => 'liner_notes' });
280-
281-
cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
282-
283-
is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
284-
285-
my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
286-
{ join => { 'cds' => 'tags' } });
287-
288-
cmp_ok( @artists, '==', 2, "two-join search ok" );
289-
290-
$rs = $schema->resultset("CD")->search(
291-
{},
292-
{ group_by => [qw/ title me.cdid /] }
293-
);
294-
295-
SKIP: {
296-
skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
297-
if $is_broken_sqlite;
298-
cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
299-
}
300-
301-
cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
302-
303-
$rs = $schema->resultset("CD")->search(
304-
{},
305-
{ join => [qw/ artist /], group_by => [qw/ artist.name /] }
306-
);
307-
308-
SKIP: {
309-
skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
310-
if $is_broken_sqlite;
311-
cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
312-
}
313-
314-
$rs = $schema->resultset("Artist")->search(
315-
{},
316-
{ join => [qw/ cds /], group_by => [qw/ me.name /], having =>{ 'MAX(cds.cdid)'=> \'< 5' } }
317-
);
318-
319-
cmp_ok( $rs->all, '==', 2, "results ok after group_by on related column with a having" );
320-
321-
$rs = $rs->search( undef, { having =>{ 'count(*)'=> \'> 2' }});
322-
323-
cmp_ok( $rs->all, '==', 1, "count() ok after group_by on related column with a having" );
324-
325-
$rs = $schema->resultset("Artist")->search(
326-
{ 'cds.title' => 'Spoonful of bees',
327-
'cds_2.title' => 'Forkful of bees' },
328-
{ join => [ 'cds', 'cds' ] });
329-
330-
SKIP: {
331-
skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1
332-
if $is_broken_sqlite;
333-
cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
334-
}
335-
336-
is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
337-
338-
$cd = $schema->resultset('Artist')->first->create_related('cds',
339-
{
340-
title => 'Unproduced Single',
341-
year => 2007
342-
});
343-
344-
my $left_join = $schema->resultset('CD')->search(
345-
{ 'me.cdid' => $cd->cdid },
346-
{ prefetch => { cd_to_producer => 'producer' } }
347-
);
348-
349-
cmp_ok($left_join, '==', 1, 'prefetch with no join record present');
350-
351-
$queries = 0;
352-
$schema->storage->debugcb(sub { $queries++ });
353-
$schema->storage->debug(1);
354-
355-
my $tree_like =
356-
$schema->resultset('TreeLike')->find(4,
357-
{ join => { parent => { parent => 'parent' } },
358-
prefetch => { parent => { parent => 'parent' } } });
359-
360-
is($tree_like->name, 'quux', 'Bottom of tree ok');
361-
$tree_like = $tree_like->parent;
362-
is($tree_like->name, 'baz', 'First level up ok');
363-
$tree_like = $tree_like->parent;
364-
is($tree_like->name, 'bar', 'Second level up ok');
365-
$tree_like = $tree_like->parent;
366-
is($tree_like->name, 'foo', 'Third level up ok');
367-
368-
$schema->storage->debug($orig_debug);
369-
$schema->storage->debugobj->callback(undef);
370-
371-
cmp_ok($queries, '==', 1, 'Only one query run');
372-
373-
$tree_like = $schema->resultset('TreeLike')->search({'me.id' => 1});
374-
$tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first;
375-
is($tree_like->name, 'quux', 'Tree search_related ok');
376-
377-
$tree_like = $schema->resultset('TreeLike')->search_related('children',
378-
{ 'children.id' => 2, 'children_2.id' => 3 },
379-
{ prefetch => { children => 'children' } }
380-
)->first;
381-
is(eval { $tree_like->children->first->children->first->name }, 'quux',
382-
'Tree search_related with prefetch ok');
383-
384-
$tree_like = eval { $schema->resultset('TreeLike')->search(
385-
{ 'children.id' => 2, 'children_2.id' => 5 },
386-
{ join => [qw/children children/] }
387-
)->search_related('children', { 'children_4.id' => 6 }, { prefetch => 'children' }
388-
)->first->children->first; };
389-
is(eval { $tree_like->name }, 'fong', 'Tree with multiple has_many joins ok');
390-
391-
# test that collapsed joins don't get a _2 appended to the alias
392-
393-
my $sql = '';
394-
$schema->storage->debugcb(sub { $sql = $_[1] });
395-
$schema->storage->debug(1);
396-
397-
eval {
398-
my $row = $schema->resultset('Artist')->search_related('cds', undef, {
399-
join => 'tracks',
400-
prefetch => 'tracks',
401-
})->search_related('tracks')->first;
402-
};
403-
404-
like( $sql, qr/^SELECT tracks_2\.trackid/, "join not collapsed for search_related" );
405-
406-
$schema->storage->debug($orig_debug);
407-
$schema->storage->debugobj->callback(undef);
408-
409-
$rs = $schema->resultset('Artist');
410-
$rs->create({ artistid => 4, name => 'Unknown singer-songwriter' });
411-
$rs->create({ artistid => 5, name => 'Emo 4ever' });
412-
@artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' });
413-
is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok');
414-
415-
# -------------
416-
#
417-
# Tests for multilevel has_many prefetch
418-
419-
# artist resultsets - with and without prefetch
420-
my $art_rs = $schema->resultset('Artist');
421-
my $art_rs_pr = $art_rs->search(
422-
{},
423-
{
424-
join => [ { cds => ['tracks'] } ],
425-
prefetch => [ { cds => ['tracks'] } ],
426-
cache => 1 # last test needs this
427-
}
428-
);
429-
430-
# This test does the same operation twice - once on a
431-
# set of items fetched from the db with no prefetch of has_many rels
432-
# The second prefetches 2 levels of has_many
433-
# We check things are the same by comparing the name or title
434-
# we build everything into a hash structure and compare the one
435-
# from each rs to see what differs
436-
437-
sub make_hash_struc {
438-
my $rs = shift;
439-
440-
my $struc = {};
441-
foreach my $art ( $rs->all ) {
442-
foreach my $cd ( $art->cds ) {
443-
foreach my $track ( $cd->tracks ) {
444-
$struc->{ $art->name }{ $cd->title }{ $track->title }++;
445-
}
446-
}
447-
}
448-
return $struc;
449-
}
450-
451-
$queries = 0;
452-
$schema->storage->debugcb(sub { $queries++ });
453-
$schema->storage->debug(1);
454-
455-
my $prefetch_result = make_hash_struc($art_rs_pr);
456-
457-
is($queries, 1, 'nested prefetch across has_many->has_many ran exactly 1 query');
458-
459-
my $nonpre_result = make_hash_struc($art_rs);
460-
461-
is_deeply( $prefetch_result, $nonpre_result,
462-
'Compare 2 level prefetch result to non-prefetch result' );
463-
464-
$queries = 0;
465-
466-
is($art_rs_pr->search_related('cds')->search_related('tracks')->first->title,
467-
'Fowlin',
468-
'chained has_many->has_many search_related ok'
469-
);
470-
471-
is($queries, 0, 'chained search_related after has_many->has_many prefetch ran no queries');

0 commit comments

Comments
 (0)