1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
12 my $schema = DBICTest->init_schema();
15 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
21 order_by => 'me.trackid',
26 my $use_prefetch = $no_prefetch->search(
33 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
35 scalar ($use_prefetch->all),
36 scalar ($no_prefetch->all),
37 "Amount of returned rows is right"
40 }, 'search_related prefetch with order_by works');
43 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
54 my $use_prefetch = $no_prefetch->search(
62 scalar ($use_prefetch->all),
63 scalar ($no_prefetch->all),
64 "Amount of returned rows is right"
66 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
68 }, 'search_related prefetch with condition referencing unqualified column of a joined table works');
70 # make sure chains off prefetched results still work
72 my $cd = $schema->resultset('CD')->search({}, { prefetch => 'cd_to_producer' })->find(1);
74 $schema->is_executed_querycount( sub {
75 is( $cd->cd_to_producer->count, 3 ,'Count of prefetched m2m links via accessor' );
76 is( scalar $cd->cd_to_producer->all, 3, 'Amount of prefetched m2m link objects via accessor' );
77 is( $cd->search_related('cd_to_producer')->count, 3, 'Count of prefetched m2m links via search_related' );
78 is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Amount of prefetched m2m links via search_related' );
79 }, 0, 'No queries ran so far');
81 is( scalar $cd->cd_to_producer->search_related('producer')->all, 3,
82 'Amount of objects via search_related off prefetched linker' );
83 is( $cd->cd_to_producer->search_related('producer')->count, 3,
84 'Count via search_related off prefetched linker' );
85 is( scalar $cd->search_related('cd_to_producer')->search_related('producer')->all, 3,
86 'Amount of objects via chained search_related off prefetched linker' );
87 is( $cd->search_related('cd_to_producer')->search_related('producer')->count, 3,
88 'Count via chained search_related off prefetched linker' );
89 is( scalar $cd->producers->all, 3,
90 'Amount of objects via m2m accessor' );
91 is( $cd->producers->count, 3,
92 'Count via m2m accessor' );
94 $schema->is_executed_querycount( sub {
95 is( $cd->cd_to_producer->count, 3 ,'Review count of prefetched m2m links via accessor' );
96 is( scalar $cd->cd_to_producer->all, 3, 'Review amount of prefetched m2m link objects via accessor' );
97 is( $cd->search_related('cd_to_producer')->count, 3, 'Review count of prefetched m2m links via search_related' );
98 is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Rreview amount of prefetched m2m links via search_related' );
99 }, 0, 'Still no queries on prefetched linker');
102 # tests with distinct => 1
104 my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
105 ->search_related('artwork_to_artist')->search_related('artist',
107 { prefetch => 'cds' },
109 is($rs->all, 0, 'prefetch without WHERE (objects)');
110 is($rs->count, 0, 'prefetch without WHERE (count)');
112 $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
113 ->search_related('artwork_to_artist')->search_related('artist',
114 { 'cds.title' => 'foo' },
115 { prefetch => 'cds' },
117 is($rs->all, 0, 'prefetch with WHERE (objects)');
118 is($rs->count, 0, 'prefetch with WHERE (count)');
121 # test where conditions at the root of the related chain
122 my $artist_rs = $schema->resultset("Artist")->search({artistid => 2});
123 my $artist = $artist_rs->next;
124 $artist->create_related ('cds', $_) for (
126 year => 1999, title => 'vague cd', genre => { name => 'vague genre' }
129 year => 1999, title => 'vague cd2', genre => { name => 'vague genre' }
133 $rs = $artist_rs->search_related('cds')->search_related('genre',
134 { 'genre.name' => 'vague genre' },
135 { prefetch => 'cds' },
137 is($rs->all, 1, 'base without distinct (objects)');
138 is($rs->count, 1, 'base without distinct (count)');
139 # artist -> 2 cds -> 2 genres -> 2 cds for each genre = 4
140 is($rs->search_related('cds')->all, 4, 'prefetch without distinct (objects)');
141 is($rs->search_related('cds')->count, 4, 'prefetch without distinct (count)');
144 $rs = $artist_rs->search_related('cds', {}, { distinct => 1})->search_related('genre',
145 { 'genre.name' => 'vague genre' },
147 is($rs->all, 2, 'distinct does not propagate over search_related (objects)');
148 is($rs->count, 2, 'distinct does not propagate over search_related (count)');
150 $rs = $rs->search ({}, { distinct => 1} );
151 is($rs->all, 1, 'distinct without prefetch (objects)');
152 is($rs->count, 1, 'distinct without prefetch (count)');
155 $rs = $artist_rs->search_related('cds')->search_related('genre',
156 { 'genre.name' => 'vague genre' },
157 { prefetch => 'cds', distinct => 1 },
159 is($rs->all, 1, 'distinct with prefetch (objects)');
160 is($rs->count, 1, 'distinct with prefetch (count)');
162 local $TODO = "This makes another 2 trips to the database, it can't be right";
163 $schema->is_executed_querycount( sub {
165 # the is() calls are not todoified
168 # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2
169 is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)');
170 is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)');
172 }, 0, 'No extra queries fired (prefetch survives search_related)');
174 }, 'distinct generally works with prefetch on deep search_related chains');
176 # pathological "user knows what they're doing" case
177 # lifted from production somewhere
179 $schema->resultset('CD')
180 ->search({ cdid => [1,2] })
181 ->search_related('tracks', { position => [3,1] })
184 my $rs = $schema->resultset('CD')->search_related('tracks', {}, {
185 group_by => 'me.title',
186 columns => { title => 'me.title', max_trk => \ 'MAX(tracks.position)' },
190 $rs->search({}, { order_by => 'me.title' })->all_hri,
192 { title => "Caterwaulin' Blues", max_trk => 3 },
193 { title => "Come Be Depressed With Us", max_trk => 3 },
194 { title => "Forkful of bees", max_trk => 1 },
195 { title => "Generic Manufactured Singles", max_trk => 3 },
196 { title => "Spoonful of bees", max_trk => 1 },