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');
41 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
52 my $use_prefetch = $no_prefetch->search(
59 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
61 scalar ($use_prefetch->all),
62 scalar ($no_prefetch->all),
63 "Amount of returned rows is right"
66 }, 'search_related prefetch with condition referencing unqualified column of a joined table works');
70 my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
71 ->search_related('artwork_to_artist')->search_related('artist',
73 { prefetch => 'cds' },
75 is($rs->all, 0, 'prefetch without WHERE (objects)');
76 is($rs->count, 0, 'prefetch without WHERE (count)');
78 $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
79 ->search_related('artwork_to_artist')->search_related('artist',
80 { 'cds.title' => 'foo' },
81 { prefetch => 'cds' },
83 is($rs->all, 0, 'prefetch with WHERE (objects)');
84 is($rs->count, 0, 'prefetch with WHERE (count)');
87 # test where conditions at the root of the related chain
88 my $artist_rs = $schema->resultset("Artist")->search({artistid => 2});
89 my $artist = $artist_rs->next;
90 $artist->create_related ('cds', $_) for (
92 year => 1999, title => 'vague cd', genre => { name => 'vague genre' }
95 year => 1999, title => 'vague cd2', genre => { name => 'vague genre' }
99 $rs = $artist_rs->search_related('cds')->search_related('genre',
100 { 'genre.name' => 'vague genre' },
101 { prefetch => 'cds' },
103 is($rs->all, 1, 'base without distinct (objects)');
104 is($rs->count, 1, 'base without distinct (count)');
105 # artist -> 2 cds -> 2 genres -> 2 cds for each genre = 4
106 is($rs->search_related('cds')->all, 4, 'prefetch without distinct (objects)');
107 is($rs->search_related('cds')->count, 4, 'prefetch without distinct (count)');
110 $rs = $artist_rs->search(undef, {distinct => 1})
111 ->search_related('cds')->search_related('genre',
112 { 'genre.name' => 'vague genre' },
114 is($rs->all, 1, 'distinct without prefetch (objects)');
115 is($rs->count, 1, 'distinct without prefetch (count)');
118 $rs = $artist_rs->search({}, {distinct => 1})
119 ->search_related('cds')->search_related('genre',
120 { 'genre.name' => 'vague genre' },
121 { prefetch => 'cds' },
123 is($rs->all, 1, 'distinct with prefetch (objects)');
124 is($rs->count, 1, 'distinct with prefetch (count)');
125 # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2
126 is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)');
127 is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)');
131 }, 'distinct generally works with prefetch on deep search_related chains');