10 my $schema = DBICTest->init_schema();
13 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
19 order_by => 'me.trackid',
24 my $use_prefetch = $no_prefetch->search(
31 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
33 scalar ($use_prefetch->all),
34 scalar ($no_prefetch->all),
35 "Amount of returned rows is right"
38 }, 'search_related prefetch with order_by works');
42 my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
43 ->search_related('artwork_to_artist')->search_related('artist',
45 { prefetch => 'cds' },
47 is($rs->all, 0, 'prefetch without WHERE (objects)');
48 is($rs->count, 0, 'prefetch without WHERE (count)');
50 $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
51 ->search_related('artwork_to_artist')->search_related('artist',
52 { 'cds.title' => 'foo' },
53 { prefetch => 'cds' },
55 is($rs->all, 0, 'prefetch with WHERE (objects)');
56 is($rs->count, 0, 'prefetch with WHERE (count)');
59 # test where conditions at the root of the related chain
60 my $artist_rs = $schema->resultset("Artist")->search({artistid => 2});
61 my $artist = $artist_rs->next;
62 $artist->create_related ('cds', $_) for (
64 year => 1999, title => 'vague cd', genre => { name => 'vague genre' }
67 year => 1999, title => 'vague cd2', genre => { name => 'vague genre' }
71 $rs = $artist_rs->search_related('cds')->search_related('genre',
72 { 'genre.name' => 'vague genre' },
73 { prefetch => 'cds' },
75 is($rs->all, 1, 'base without distinct (objects)');
76 is($rs->count, 1, 'base without distinct (count)');
77 # artist -> 2 cds -> 2 genres -> 2 cds for each genre = 4
78 is($rs->search_related('cds')->all, 4, 'prefetch without distinct (objects)');
79 is($rs->search_related('cds')->count, 4, 'prefetch without distinct (count)');
82 $rs = $artist_rs->search(undef, {distinct => 1})
83 ->search_related('cds')->search_related('genre',
84 { 'genre.name' => 'vague genre' },
86 is($rs->all, 1, 'distinct without prefetch (objects)');
87 is($rs->count, 1, 'distinct without prefetch (count)');
90 $rs = $artist_rs->search({}, {distinct => 1})
91 ->search_related('cds')->search_related('genre',
92 { 'genre.name' => 'vague genre' },
93 { prefetch => 'cds' },
95 is($rs->all, 1, 'distinct with prefetch (objects)');
96 is($rs->count, 1, 'distinct with prefetch (count)');
97 # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2
98 is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)');
99 is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)');
103 }, 'distinct generally works with prefetch on deep search_related chains');