7 use DBIC::SqlMakerTest;
12 my $schema = DBICTest->init_schema();
14 my $cd_rs = $schema->resultset('CD')->search (
15 { 'tracks.cd' => { '!=', undef } },
16 { prefetch => 'tracks' },
19 # Database sanity check
20 is($cd_rs->count, 5, 'CDs with tracks count');
22 is ($_->tracks->count, 3, '3 tracks for CD' . $_->id );
25 # Test a belongs_to prefetch of a has_many
27 my $track_rs = $schema->resultset ('Track')->search (
28 { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } },
30 # the select/as is deliberately silly to test both funcs and refs below
33 \ 'COUNT (me.trackid) AS track_count',
34 { count => 'me.trackid' },
41 group_by => [qw/me.cd/],
46 is($track_rs->count, 5, 'Prefetched count with groupby');
47 is($track_rs->all, 5, 'Prefetched objects with groupby');
51 $schema->storage->debugcb ( sub { $query_cnt++ } );
54 while (my $collapsed_track = $track_rs->next) {
56 my $cdid = $collapsed_track->get_column('cd');
57 is($collapsed_track->get_column('track_count'), 3, "Correct count of tracks for CD $cdid" );
58 ok($collapsed_track->cd->title, "Prefetched title for CD $cdid" );
61 is ($query_cnt, 0, 'No queries on prefetched titles');
62 $schema->storage->debugcb (undef);
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',
87 # the double-count is deliberate
91 SELECT me.cd, me.track_count, me.track_count_2, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
93 SELECT me.cd, COUNT (me.trackid) AS track_count, COUNT (me.trackid) AS track_count_2
95 WHERE ( cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
98 JOIN cd cd ON cd.cdid = me.cd
99 WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
101 [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ],
102 'next() query generated expected SQL',
106 # add an extra track to one of the cds, and then make sure we can get it on top
107 # (check if limit works)
108 my $top_cd = $cd_rs->slice (1,1)->next;
109 $top_cd->create_related ('tracks', {
110 title => 'over the top',
113 my $top_cd_collapsed_track = $track_rs->search ({}, {
116 { -desc => 'track_count' },
120 is ($top_cd_collapsed_track->count, 2);
124 $top_cd_collapsed_track->first->cd->title,
125 'Correct collapsed track with prefetched CD returned on top'
129 # test a has_many/might_have prefetch at the same level
130 # double-count for now, until we figure out how to do this cleanly
132 # Note that one of the CDs now has 4 tracks instead of 3
134 my $most_tracks_rs = $cd_rs->search ({}, {
135 prefetch => 'liner_notes', # tracks are alredy prefetched
136 select => ['me.cdid', { count => 'tracks.trackid' } ],
137 as => [qw/cdid track_count/],
138 group_by => 'me.cdid',
139 order_by => { -desc => \ 'COUNT (tracks.trackid)' },
144 $most_tracks_rs->count_rs->as_query,
150 LEFT JOIN track tracks ON tracks.cd = me.cdid
151 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
152 WHERE ( tracks.cd IS NOT NULL )
158 'count() query generated expected SQL',
162 $most_tracks_rs->as_query,
164 SELECT me.cdid, 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
166 SELECT me.cdid, COUNT( tracks.trackid ) AS track_count
168 LEFT JOIN track tracks ON tracks.cd = me.cdid
169 WHERE ( tracks.cd IS NOT NULL )
171 ORDER BY COUNT (tracks.trackid) DESC,
174 LEFT JOIN track tracks ON tracks.cd = me.cdid
175 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
176 WHERE ( tracks.cd IS NOT NULL )
177 ORDER BY COUNT (tracks.trackid) DESC, tracks.cd
180 'next() query generated expected SQL',
183 is ($most_tracks_rs->count, 2, 'Limit works');
184 my $top_cd = $most_tracks_rs->first;
185 is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) above
188 $schema->storage->debugcb ( sub { $query_cnt++ } );
190 is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly');
191 is ($top_cd->tracks->count, 4, 'Count of prefetched tracks rs still correct');
192 is ($top_cd->tracks->all, 4, 'Number of prefetched track objects still correct');
194 $top_cd->liner_notes->notes,
196 'Correct liner pre-fetched with top cd',
199 is ($query_cnt, 0, 'No queries executed during prefetched data access');
200 $schema->storage->debugcb (undef);