grouped prefetch fix
[dbsrgits/DBIx-Class.git] / t / prefetch / grouped.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 use lib qw(t/lib);
6 use DBICTest;
7 use DBIC::SqlMakerTest;
8
9 #plan tests => 6;
10 plan 'no_plan';
11
12 my $schema = DBICTest->init_schema();
13 my $sdebug = $schema->storage->debug;
14
15 my $cd_rs = $schema->resultset('CD')->search (
16   { 'tracks.cd' => { '!=', undef } },
17   { prefetch => 'tracks' },
18 );
19
20 # Database sanity check
21 is($cd_rs->count, 5, 'CDs with tracks count');
22 for ($cd_rs->all) {
23   is ($_->tracks->count, 3, '3 tracks for CD' . $_->id );
24 }
25
26 # Test a belongs_to prefetch of a has_many
27 {
28   my $track_rs = $schema->resultset ('Track')->search (
29     { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } },
30     {
31       select => [
32         'me.cd',
33         { count => 'me.trackid' },
34       ],
35       as => [qw/
36         cd
37         track_count
38       /],
39       group_by => [qw/me.cd/],
40       prefetch => 'cd',
41     },
42   );
43
44   # this used to fuck up ->all, do not remove!
45   ok ($track_rs->first, 'There is stuff in the rs');
46
47   is($track_rs->count, 5, 'Prefetched count with groupby');
48   is($track_rs->all, 5, 'Prefetched objects with groupby');
49
50   {
51     my $query_cnt = 0;
52     $schema->storage->debugcb ( sub { $query_cnt++ } );
53     $schema->storage->debug (1);
54
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" );
59     }
60
61     is ($query_cnt, 1, 'Single query on prefetched titles');
62     $schema->storage->debugcb (undef);
63     $schema->storage->debug ($sdebug);
64   }
65
66   # Test sql by hand, as the sqlite db will simply paper over
67   # improper group/select combinations
68   #
69   is_same_sql_bind (
70     $track_rs->count_rs->as_query,
71     '(
72       SELECT COUNT( * )
73         FROM (
74           SELECT me.cd
75             FROM track me
76             JOIN cd cd ON cd.cdid = me.cd
77           WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
78           GROUP BY me.cd
79         )
80       count_subq
81     )',
82     [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ],
83     'count() query generated expected SQL',
84   );
85
86   is_same_sql_bind (
87     $track_rs->as_query,
88     '(
89       SELECT me.cd, me.track_count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
90         FROM (
91           SELECT me.cd, COUNT (me.trackid) AS track_count,
92             FROM track me
93             JOIN cd cd ON cd.cdid = me.cd
94           WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
95           GROUP BY me.cd
96           ) as me
97         JOIN cd cd ON cd.cdid = me.cd
98       WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
99     )',
100     [ map { [ 'me.cd' => $_] } ( ($cd_rs->get_column ('cdid')->all) x 2 ) ],
101     'next() query generated expected SQL',
102   );
103
104
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',
110   });
111
112   my $top_cd_collapsed_track = $track_rs->search ({}, {
113     rows => 2,
114     order_by => [
115       { -desc => 'track_count' },
116     ],
117   });
118
119   is ($top_cd_collapsed_track->count, 2);
120
121   is (
122     $top_cd->title,
123     $top_cd_collapsed_track->first->cd->title,
124     'Correct collapsed track with prefetched CD returned on top'
125   );
126 }
127
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
130 {
131   my $most_tracks_rs = $schema->resultset ('CD')->search (
132     {
133       'me.cdid' => { '!=' => undef },  # duh - this is just to test WHERE
134     },
135     {
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' },
141       rows => 2,
142     }
143   );
144
145   is_same_sql_bind (
146     $most_tracks_rs->count_rs->as_query,
147     '(
148       SELECT COUNT( * )
149         FROM (
150           SELECT me.cdid
151             FROM cd me
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 )
155           GROUP BY me.cdid
156           LIMIT 2
157         ) count_subq
158     )',
159     [],
160     'count() query generated expected SQL',
161   );
162
163   is_same_sql_bind (
164     $most_tracks_rs->as_query,
165     '(
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
167         FROM (
168           SELECT me.cdid, COUNT( tracks.trackid ) AS track_count
169             FROM cd me
170             LEFT JOIN track tracks ON tracks.cd = me.cdid
171           WHERE ( me.cdid IS NOT NULL )
172           GROUP BY me.cdid
173           ORDER BY track_count DESC
174           LIMIT 2
175         ) me
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
180     )',
181     [],
182     'next() query generated expected SQL',
183   );
184
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
188
189   my $query_cnt = 0;
190   $schema->storage->debugcb ( sub { $query_cnt++ } );
191   $schema->storage->debug (1);
192
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');
196   is (
197     $top_cd->liner_notes->notes,
198     'Buy Whiskey!',
199     'Correct liner pre-fetched with top cd',
200   );
201
202   is ($query_cnt, 0, 'No queries executed during prefetched data access');
203   $schema->storage->debugcb (undef);
204   $schema->storage->debug ($sdebug);
205 }