Test cleanups
[dbsrgits/DBIx-Class-Historic.git] / t / prefetch / grouped.t
CommitLineData
0c5ea449 1use strict;
2use warnings;
3use Test::More;
4
5use lib qw(t/lib);
6use DBICTest;
7use DBIC::SqlMakerTest;
8
9#plan tests => 6;
10plan 'no_plan';
11
12my $schema = DBICTest->init_schema();
a2287768 13my $sdebug = $schema->storage->debug;
0c5ea449 14
15my $cd_rs = $schema->resultset('CD')->search (
16 { 'tracks.cd' => { '!=', undef } },
17 { prefetch => 'tracks' },
18);
19
20# Database sanity check
21is($cd_rs->count, 5, 'CDs with tracks count');
22for ($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 # the select/as is deliberately silly to test both funcs and refs below
32 select => [
33 'me.cd',
0c5ea449 34 { count => 'me.trackid' },
35 ],
36 as => [qw/
37 cd
38 track_count
0c5ea449 39 /],
40 group_by => [qw/me.cd/],
41 prefetch => 'cd',
42 },
43 );
44
45 is($track_rs->count, 5, 'Prefetched count with groupby');
46 is($track_rs->all, 5, 'Prefetched objects with groupby');
47
48 {
49 my $query_cnt = 0;
50 $schema->storage->debugcb ( sub { $query_cnt++ } );
a2287768 51 $schema->storage->debug (1);
0c5ea449 52
53 $track_rs->reset;
54 while (my $collapsed_track = $track_rs->next) {
0c5ea449 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" );
58 }
59
60 is ($query_cnt, 0, 'No queries on prefetched titles');
61 $schema->storage->debugcb (undef);
a2287768 62 $schema->storage->debug ($sdebug);
0c5ea449 63 }
64
65 # Test sql by hand, as the sqlite db will simply paper over
66 # improper group/select combinations
67 #
68 # the exploded IN needs fixing below, coming in another branch
69 #
70 is_same_sql_bind (
71 $track_rs->count_rs->as_query,
72 '(
73 SELECT COUNT( * )
74 FROM (
75 SELECT me.cd
76 FROM track me
77 JOIN cd cd ON cd.cdid = me.cd
78 WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
79 GROUP BY me.cd
80 )
81 count_subq
82 )',
83 [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ],
84 'count() query generated expected SQL',
85 );
86
0c5ea449 87 is_same_sql_bind (
88 $track_rs->as_query,
89 '(
5b45001f 90 SELECT me.cd, me.track_count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
0c5ea449 91 FROM (
5b45001f 92 SELECT me.cd, COUNT (me.trackid) AS track_count,
0c5ea449 93 FROM track me
1c1937b7 94 JOIN cd cd ON cd.cdid = me.cd
95 WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
0c5ea449 96 GROUP BY me.cd
97 ) as me
98 JOIN cd cd ON cd.cdid = me.cd
99 WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
100 )',
1c1937b7 101 [ map
102 { [ 'me.cd' => $_] }
103 ( $cd_rs->get_column ('cdid')->all, $cd_rs->get_column ('cdid')->all )
104 ],
0c5ea449 105 'next() query generated expected SQL',
106 );
107
108
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',
114 });
115
116 my $top_cd_collapsed_track = $track_rs->search ({}, {
117 rows => 2,
118 order_by => [
119 { -desc => 'track_count' },
120 ],
121 });
122
123 is ($top_cd_collapsed_track->count, 2);
124
125 is (
126 $top_cd->title,
127 $top_cd_collapsed_track->first->cd->title,
128 'Correct collapsed track with prefetched CD returned on top'
129 );
130}
131
132# test a has_many/might_have prefetch at the same level
0c5ea449 133# Note that one of the CDs now has 4 tracks instead of 3
134{
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',
5b45001f 140 order_by => { -desc => 'track_count' },
0c5ea449 141 rows => 2,
142 });
143
144 is_same_sql_bind (
145 $most_tracks_rs->count_rs->as_query,
146 '(
147 SELECT COUNT( * )
148 FROM (
149 SELECT me.cdid
150 FROM cd me
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 )
154 GROUP BY me.cdid
155 LIMIT 2
156 ) count_subq
157 )',
158 [],
159 'count() query generated expected SQL',
160 );
161
162 is_same_sql_bind (
163 $most_tracks_rs->as_query,
164 '(
1c1937b7 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
0c5ea449 166 FROM (
a57827b6 167 SELECT me.cdid, COUNT( tracks.trackid ) AS track_count
0c5ea449 168 FROM cd me
169 LEFT JOIN track tracks ON tracks.cd = me.cdid
170 WHERE ( tracks.cd IS NOT NULL )
171 GROUP BY me.cdid
1c1937b7 172 ORDER BY track_count DESC
0c5ea449 173 LIMIT 2
174 ) me
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 )
1c1937b7 178 ORDER BY track_count DESC, tracks.cd
0c5ea449 179 )',
180 [],
181 'next() query generated expected SQL',
182 );
183
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
187
188 my $query_cnt = 0;
189 $schema->storage->debugcb ( sub { $query_cnt++ } );
a2287768 190 $schema->storage->debug (1);
0c5ea449 191
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');
195 is (
196 $top_cd->liner_notes->notes,
197 'Buy Whiskey!',
198 'Correct liner pre-fetched with top cd',
199 );
200
201 is ($query_cnt, 0, 'No queries executed during prefetched data access');
202 $schema->storage->debugcb (undef);
a2287768 203 $schema->storage->debug ($sdebug);
0c5ea449 204}