7 use DBIC::SqlMakerTest;
12 my $schema = DBICTest->init_schema();
13 my $sdebug = $schema->storage->debug;
15 my $cd_rs = $schema->resultset('CD')->search (
16 { 'tracks.cd' => { '!=', undef } },
17 { prefetch => 'tracks' },
20 # Database sanity check
21 is($cd_rs->count, 5, 'CDs with tracks count');
23 is ($_->tracks->count, 3, '3 tracks for CD' . $_->id );
26 # Test a belongs_to prefetch of a has_many
28 my $track_rs = $schema->resultset ('Track')->search (
29 { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } },
31 # the select/as is deliberately silly to test both funcs and refs below
34 { count => 'me.trackid' },
40 group_by => [qw/me.cd/],
45 is($track_rs->count, 5, 'Prefetched count with groupby');
46 is($track_rs->all, 5, 'Prefetched objects with groupby');
50 $schema->storage->debugcb ( sub { $query_cnt++ } );
51 $schema->storage->debug (1);
54 while (my $collapsed_track = $track_rs->next) {
55 my $cdid = $collapsed_track->get_column('cd');
56 is($collapsed_track->get_column('track_count'), 3, "Correct count of tracks for CD $cdid" );
57 ok($collapsed_track->cd->title, "Prefetched title for CD $cdid" );
60 is ($query_cnt, 0, 'No queries on prefetched titles');
61 $schema->storage->debugcb (undef);
62 $schema->storage->debug ($sdebug);
65 # Test sql by hand, as the sqlite db will simply paper over
66 # improper group/select combinations
68 # the exploded IN needs fixing below, coming in another branch
71 $track_rs->count_rs->as_query,
77 JOIN cd cd ON cd.cdid = me.cd
78 WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
83 [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ],
84 'count() query generated expected SQL',
90 SELECT me.cd, me.track_count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
92 SELECT me.cd, COUNT (me.trackid) AS track_count,
94 JOIN cd cd ON cd.cdid = me.cd
95 WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
98 JOIN cd cd ON cd.cdid = me.cd
99 WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
103 ( $cd_rs->get_column ('cdid')->all, $cd_rs->get_column ('cdid')->all )
105 'next() query generated expected SQL',
109 # add an extra track to one of the cds, and then make sure we can get it on top
110 # (check if limit works)
111 my $top_cd = $cd_rs->slice (1,1)->next;
112 $top_cd->create_related ('tracks', {
113 title => 'over the top',
116 my $top_cd_collapsed_track = $track_rs->search ({}, {
119 { -desc => 'track_count' },
123 is ($top_cd_collapsed_track->count, 2);
127 $top_cd_collapsed_track->first->cd->title,
128 'Correct collapsed track with prefetched CD returned on top'
132 # test a has_many/might_have prefetch at the same level
133 # Note that one of the CDs now has 4 tracks instead of 3
135 my $most_tracks_rs = $cd_rs->search ({}, {
136 prefetch => 'liner_notes', # tracks are alredy prefetched
137 select => ['me.cdid', { count => 'tracks.trackid' } ],
138 as => [qw/cdid track_count/],
139 group_by => 'me.cdid',
140 order_by => { -desc => 'track_count' },
145 $most_tracks_rs->count_rs->as_query,
151 LEFT JOIN track tracks ON tracks.cd = me.cdid
152 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
153 WHERE ( tracks.cd IS NOT NULL )
159 'count() query generated expected SQL',
163 $most_tracks_rs->as_query,
165 SELECT me.cdid, me.track_count, tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, liner_notes.liner_id, liner_notes.notes
167 SELECT me.cdid, COUNT( tracks.trackid ) AS track_count
169 LEFT JOIN track tracks ON tracks.cd = me.cdid
170 WHERE ( tracks.cd IS NOT NULL )
172 ORDER BY track_count DESC
175 LEFT JOIN track tracks ON tracks.cd = me.cdid
176 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
177 WHERE ( tracks.cd IS NOT NULL )
178 ORDER BY track_count DESC, tracks.cd
181 'next() query generated expected SQL',
184 is ($most_tracks_rs->count, 2, 'Limit works');
185 my $top_cd = $most_tracks_rs->first;
186 is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) above
189 $schema->storage->debugcb ( sub { $query_cnt++ } );
190 $schema->storage->debug (1);
192 is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly');
193 is ($top_cd->tracks->count, 4, 'Count of prefetched tracks rs still correct');
194 is ($top_cd->tracks->all, 4, 'Number of prefetched track objects still correct');
196 $top_cd->liner_notes->notes,
198 'Correct liner pre-fetched with top cd',
201 is ($query_cnt, 0, 'No queries executed during prefetched data access');
202 $schema->storage->debugcb (undef);
203 $schema->storage->debug ($sdebug);