Do not artificially order the internals of a has_many prefetch subquery
[dbsrgits/DBIx-Class.git] / t / prefetch / with_limit.t
1 # Test to ensure we get a consistent result set wether or not we use the
2 # prefetch option in combination rows (LIMIT).
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8 use lib qw(t/lib);
9 use DBICTest;
10 use DBIC::SqlMakerTest;
11 use DBIx::Class::SQLMaker::LimitDialects;
12
13 my $ROWS = DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype;
14
15 my $schema = DBICTest->init_schema();
16
17
18 my $no_prefetch = $schema->resultset('Artist')->search(
19   [   # search deliberately contrived
20     { 'artwork.cd_id' => undef },
21     { 'tracks.title' => { '!=' => 'blah-blah-1234568' }}
22   ],
23   { rows => 3, join => { cds => [qw/artwork tracks/] },
24  }
25 );
26
27 my $use_prefetch = $no_prefetch->search(
28   {},
29   {
30     select => ['me.artistid', 'me.name'],
31     as => ['artistid', 'name'],
32     prefetch => 'cds',
33     order_by => { -desc => 'name' },
34   }
35 );
36
37 # add an extra +select to make sure it does not throw things off
38 # we also expect it to appear in both selectors, as we can not know
39 # for sure which part of the query it applies to (may be order_by,
40 # maybe something else)
41 #
42 # we use a reference to the same array in bind vals, because
43 # is_deeply picks up this difference too (not sure if bug or
44 # feature)
45 $use_prefetch = $use_prefetch->search({}, {
46   '+columns' => { monkeywrench => \[ 'me.artistid + ?', [ \ 'inTEger' => 1 ] ] },
47 });
48
49 my $bind_int_resolved = sub { [ { sqlt_datatype => 'inTEger' } => 1 ] };
50 my $bind_vc_resolved = sub { [
51   { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'tracks.title' }
52     => 'blah-blah-1234568'
53 ] };
54 is_same_sql_bind (
55   $use_prefetch->as_query,
56   '(
57     SELECT  me.artistid + ?,
58             me.artistid, me.name,
59             cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track
60       FROM (
61         SELECT me.artistid + ?,
62                me.artistid, me.name
63           FROM artist me
64           LEFT JOIN cd cds
65             ON cds.artist = me.artistid
66           LEFT JOIN cd_artwork artwork
67             ON artwork.cd_id = cds.cdid
68           LEFT JOIN track tracks
69             ON tracks.cd = cds.cdid
70         WHERE   artwork.cd_id IS NULL
71              OR tracks.title != ?
72         GROUP BY me.artistid + ?, me.artistid, me.name
73         ORDER BY name DESC LIMIT ?
74       ) me
75       LEFT JOIN cd cds
76         ON cds.artist = me.artistid
77       LEFT JOIN cd_artwork artwork
78         ON artwork.cd_id = cds.cdid
79       LEFT JOIN track tracks
80         ON tracks.cd = cds.cdid
81     WHERE artwork.cd_id IS NULL
82        OR tracks.title != ?
83     GROUP BY me.artistid + ?, me.artistid, me.name, cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track
84     ORDER BY name DESC
85   )',
86   [
87     $bind_int_resolved->(),  # outer select
88     $bind_int_resolved->(),  # inner select
89     $bind_vc_resolved->(), # inner where
90     $bind_int_resolved->(),  # inner group_by
91     [ $ROWS => 3 ],
92     $bind_vc_resolved->(), # outer where
93     $bind_int_resolved->(),  # outer group_by
94   ],
95   'Expected SQL on complex limited prefetch'
96 );
97
98 is($no_prefetch->count, $use_prefetch->count, '$no_prefetch->count == $use_prefetch->count');
99 is(
100   scalar ($no_prefetch->all),
101   scalar ($use_prefetch->all),
102   "Amount of returned rows is right"
103 );
104
105 my $artist_many_cds = $schema->resultset('Artist')->search ( {}, {
106   join => 'cds',
107   group_by => 'me.artistid',
108   having => \ 'count(cds.cdid) > 1',
109 })->first;
110
111
112 $no_prefetch = $schema->resultset('Artist')->search(
113   { artistid => $artist_many_cds->id },
114   { rows => 1 }
115 );
116
117 $use_prefetch = $no_prefetch->search ({}, { prefetch => 'cds' });
118
119 my $normal_artist = $no_prefetch->single;
120 my $prefetch_artist = $use_prefetch->find({ name => $artist_many_cds->name });
121 my $prefetch2_artist = $use_prefetch->first;
122
123 is(
124   $prefetch_artist->cds->count,
125   $normal_artist->cds->count,
126   "Count of child rel with prefetch + rows => 1 is right (find)"
127 );
128 is(
129   $prefetch2_artist->cds->count,
130   $normal_artist->cds->count,
131   "Count of child rel with prefetch + rows => 1 is right (first)"
132 );
133
134 is (
135   scalar ($prefetch_artist->cds->all),
136   scalar ($normal_artist->cds->all),
137   "Amount of child rel rows with prefetch + rows => 1 is right (find)"
138 );
139 is (
140   scalar ($prefetch2_artist->cds->all),
141   scalar ($normal_artist->cds->all),
142   "Amount of child rel rows with prefetch + rows => 1 is right (first)"
143 );
144
145 throws_ok (
146   sub { $use_prefetch->single },
147   qr/resultsets prefetching has_many/,
148   'single() with multiprefetch is illegal',
149 );
150
151 throws_ok (
152   sub {
153     $use_prefetch->search(
154       {'tracks.title' => { '!=' => 'foo' }},
155       { order_by => \ 'some oddball literal sql', join => { cds => 'tracks' } }
156     )->next
157   }, qr/A required group_by clause could not be constructed automatically/,
158 );
159
160 my $artist = $use_prefetch->search({'cds.title' => $artist_many_cds->cds->first->title })->next;
161 is($artist->cds->count, 1, "count on search limiting prefetched has_many");
162
163 # try with double limit
164 my $artist2 = $use_prefetch->search({'cds.title' => { '!=' => $artist_many_cds->cds->first->title } })->slice (0,0)->next;
165 is($artist2->cds->count, 2, "count on search limiting prefetched has_many");
166
167 # make sure 1:1 joins do not force a subquery (no point to exercise the optimizer, if at all available)
168 # get cd's that have any tracks and their artists
169 my $single_prefetch_rs = $schema->resultset ('CD')->search (
170   { 'me.year' => 2010, 'artist.name' => 'foo' },
171   { prefetch => ['tracks', 'artist'], rows => 15 },
172 );
173 is_same_sql_bind (
174   $single_prefetch_rs->as_query,
175   '(
176     SELECT
177         me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
178         tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at,
179         artist.artistid, artist.name, artist.rank, artist.charfield
180       FROM (
181         SELECT
182             me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
183           FROM cd me
184           JOIN artist artist ON artist.artistid = me.artist
185         WHERE ( ( artist.name = ? AND me.year = ? ) )
186         LIMIT ?
187       ) me
188       LEFT JOIN track tracks
189         ON tracks.cd = me.cdid
190       JOIN artist artist
191         ON artist.artistid = me.artist
192     WHERE ( ( artist.name = ? AND me.year = ? ) )
193     ORDER BY me.cdid
194   )',
195   [
196     [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'artist.name' } => 'foo' ],
197     [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' } => 2010 ],
198     [ $ROWS         => 15    ],
199     [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'artist.name' } => 'foo' ],
200     [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' } => 2010 ],
201   ],
202   'No grouping of non-multiplying resultsets',
203 );
204
205 done_testing;