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 ] } },
33 { count => 'me.trackid' },
39 group_by => [qw/me.cd/],
44 # this used to fuck up ->all, do not remove!
45 ok ($track_rs->first, 'There is stuff in the rs');
47 is($track_rs->count, 5, 'Prefetched count with groupby');
48 is($track_rs->all, 5, 'Prefetched objects with groupby');
52 $schema->storage->debugcb ( sub { $query_cnt++ } );
53 $schema->storage->debug (1);
55 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, 1, 'Single query on prefetched titles');
62 $schema->storage->debugcb (undef);
63 $schema->storage->debug ($sdebug);
66 # Test sql by hand, as the sqlite db will simply paper over
67 # improper group/select combinations
70 $track_rs->count_rs->as_query,
76 JOIN cd cd ON cd.cdid = me.cd
77 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
82 [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ],
83 'count() query generated expected SQL',
89 SELECT me.cd, me.track_count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
91 SELECT me.cd, COUNT (me.trackid) AS track_count,
93 JOIN cd cd ON cd.cdid = me.cd
94 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
97 JOIN cd cd ON cd.cdid = me.cd
98 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
100 [ map { [ 'me.cd' => $_] } ( ($cd_rs->get_column ('cdid')->all) x 2 ) ],
101 'next() query generated expected SQL',
105 # add an extra track to one of the cds, and then make sure we can get it on top
106 # (check if limit works)
107 my $top_cd = $cd_rs->slice (1,1)->next;
108 $top_cd->create_related ('tracks', {
109 title => 'over the top',
112 my $top_cd_collapsed_track = $track_rs->search ({}, {
115 { -desc => 'track_count' },
119 is ($top_cd_collapsed_track->count, 2);
123 $top_cd_collapsed_track->first->cd->title,
124 'Correct collapsed track with prefetched CD returned on top'
128 # test a has_many/might_have prefetch at the same level
129 # Note that one of the CDs now has 4 tracks instead of 3
131 my $most_tracks_rs = $schema->resultset ('CD')->search (
133 'me.cdid' => { '!=' => undef }, # duh - this is just to test WHERE
136 prefetch => [qw/tracks liner_notes/],
137 select => ['me.cdid', { count => 'tracks.trackid' } ],
138 as => [qw/cdid track_count/],
139 group_by => 'me.cdid',
140 order_by => { -desc => 'track_count' },
146 $most_tracks_rs->count_rs->as_query,
152 LEFT JOIN track tracks ON tracks.cd = me.cdid
153 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
154 WHERE ( me.cdid IS NOT NULL )
160 'count() query generated expected SQL',
164 $most_tracks_rs->as_query,
166 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
168 SELECT me.cdid, COUNT( tracks.trackid ) AS track_count
170 LEFT JOIN track tracks ON tracks.cd = me.cdid
171 WHERE ( me.cdid IS NOT NULL )
173 ORDER BY track_count DESC
176 LEFT JOIN track tracks ON tracks.cd = me.cdid
177 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
178 WHERE ( me.cdid IS NOT NULL )
179 ORDER BY track_count DESC, tracks.cd
182 'next() query generated expected SQL',
185 is ($most_tracks_rs->count, 2, 'Limit works');
186 my $top_cd = $most_tracks_rs->first;
187 is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier
190 $schema->storage->debugcb ( sub { $query_cnt++ } );
191 $schema->storage->debug (1);
193 is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly');
194 is ($top_cd->tracks->count, 4, 'Count of prefetched tracks rs still correct');
195 is ($top_cd->tracks->all, 4, 'Number of prefetched track objects still correct');
197 $top_cd->liner_notes->notes,
199 'Correct liner pre-fetched with top cd',
202 is ($query_cnt, 0, 'No queries executed during prefetched data access');
203 $schema->storage->debugcb (undef);
204 $schema->storage->debug ($sdebug);