Consider unselected order_by during complex subqueried prefetch
[dbsrgits/DBIx-Class.git] / t / prefetch / incomplete.t
CommitLineData
5aa25b93 1use strict;
8273e845 2use warnings;
5aa25b93 3
4use Test::More;
52864fbd 5use Test::Deep;
5aa25b93 6use Test::Exception;
7use lib qw(t/lib);
8use DBICTest;
97e130fa 9use DBIC::SqlMakerTest;
5aa25b93 10
376b7a93 11my $schema = DBICTest->init_schema();
5aa25b93 12
376b7a93 13lives_ok(sub {
fcf32d04 14 # while cds.* will be selected anyway (prefetch implies it)
15 # only the requested me.name column will be fetched.
5aa25b93 16
376b7a93 17 # reference sql with select => [...]
fcf32d04 18 # SELECT me.name, cds.title, cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track FROM ...
5aa25b93 19
376b7a93 20 my $rs = $schema->resultset('Artist')->search(
21 { 'cds.title' => { '!=', 'Generic Manufactured Singles' } },
22 {
23 prefetch => [ qw/ cds / ],
24 order_by => [ { -desc => 'me.name' }, 'cds.title' ],
fcf32d04 25 select => [qw/ me.name cds.title / ],
69ab63d4 26 },
376b7a93 27 );
5aa25b93 28
376b7a93 29 is ($rs->count, 2, 'Correct number of collapsed artists');
69e99ee6 30 my ($we_are_goth) = $rs->all;
376b7a93 31 is ($we_are_goth->name, 'We Are Goth', 'Correct first artist');
32 is ($we_are_goth->cds->count, 1, 'Correct number of CDs for first artist');
33 is ($we_are_goth->cds->first->title, 'Come Be Depressed With Us', 'Correct cd for artist');
951b7581 34}, 'explicit prefetch on a keyless object works');
35
69ab63d4 36lives_ok ( sub {
37
38 my $rs = $schema->resultset('CD')->search(
39 {},
40 {
41 order_by => [ { -desc => 'me.year' } ],
42 }
43 );
44 my $years = [qw/ 2001 2001 1999 1998 1997/];
45
52864fbd 46 cmp_deeply (
69ab63d4 47 [ $rs->search->get_column('me.year')->all ],
48 $years,
49 'Expected years (at least one duplicate)',
50 );
51
52 my @cds_and_tracks;
53 for my $cd ($rs->all) {
908aa1bb 54 my $data = { year => $cd->year, cdid => $cd->cdid };
69ab63d4 55 for my $tr ($cd->tracks->all) {
56 push @{$data->{tracks}}, { $tr->get_columns };
57 }
58 push @cds_and_tracks, $data;
59 }
60
908aa1bb 61 my $pref_rs = $rs->search ({}, { columns => [qw/year cdid/], prefetch => 'tracks' });
69ab63d4 62
63 my @pref_cds_and_tracks;
64 for my $cd ($pref_rs->all) {
65 my $data = { $cd->get_columns };
66 for my $tr ($cd->tracks->all) {
67 push @{$data->{tracks}}, { $tr->get_columns };
68 }
69 push @pref_cds_and_tracks, $data;
70 }
71
52864fbd 72 cmp_deeply (
69ab63d4 73 \@pref_cds_and_tracks,
74 \@cds_and_tracks,
75 'Correct collapsing on non-unique primary object'
76 );
77
52864fbd 78 cmp_deeply (
69ab63d4 79 [ $pref_rs->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' })->all ],
80 \@cds_and_tracks,
81 'Correct HRI collapsing on non-unique primary object'
82 );
83
84}, 'weird collapse lives');
85
86
951b7581 87lives_ok(sub {
88 # test implicit prefetch as well
89
90 my $rs = $schema->resultset('CD')->search(
91 { title => 'Generic Manufactured Singles' },
92 {
93 join=> 'artist',
94 select => [qw/ me.title artist.name / ],
95 }
96 );
97
98 my $cd = $rs->next;
99 is ($cd->title, 'Generic Manufactured Singles', 'CD title prefetched correctly');
100 isa_ok ($cd->artist, 'DBICTest::Artist');
101 is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name');
5aa25b93 102
951b7581 103}, 'implicit keyless prefetch works');
ce9f555e 104
105# sane error
106throws_ok(
107 sub {
108 $schema->resultset('Track')->search({}, { join => { cd => 'artist' }, '+columns' => 'artist.name' } )->next;
109 },
95e41036 110 qr|\QInflation into non-existent relationship 'artist' of 'Track' requested, check the inflation specification (columns/as) ending in '...artist.name'|,
ce9f555e 111 'Sensible error message on mis-specified "as"',
112);
113
27e0370d 114# check complex limiting prefetch without the join-able columns
115{
116 my $pref_rs = $schema->resultset('Owners')->search({}, {
117 rows => 3,
118 offset => 1,
119 columns => 'name', # only the owner name, still prefetch all the books
120 prefetch => 'books',
121 });
122
97e130fa 123 is_same_sql_bind(
124 $pref_rs->as_query,
125 '(
126 SELECT me.name, books.id, books.source, books.owner, books.title, books.price
127 FROM (
128 SELECT me.name, me.id
129 FROM owners me
130 LIMIT ?
131 OFFSET ?
132 ) me
133 LEFT JOIN books books
134 ON books.owner = me.id
135 )',
136 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
137 'Expected SQL on complex limited prefetch with non-selected join condition',
138 );
139
140 is_deeply (
141 $pref_rs->all_hri,
142 [ {
143 name => "Waltham",
144 books => [ {
145 id => 3,
146 owner => 2,
147 price => 65,
148 source => "Library",
149 title => "Best Recipe Cookbook",
150 } ],
151 } ],
152 'Expected result on complex limited prefetch with non-selected join condition'
153 );
154
155 my $empty_ordered_pref_rs = $pref_rs->search({}, {
156 columns => [], # nothing, we only prefetch the book data
157 order_by => 'me.name',
158 });
159 my $empty_ordered_pref_hri = [ {
160 books => [ {
161 id => 3,
162 owner => 2,
163 price => 65,
164 source => "Library",
165 title => "Best Recipe Cookbook",
166 } ],
167 } ];
168
169 is_same_sql_bind(
170 $empty_ordered_pref_rs->as_query,
171 '(
172 SELECT books.id, books.source, books.owner, books.title, books.price
173 FROM (
174 SELECT me.id, me.name
175 FROM owners me
176 ORDER BY me.name
177 LIMIT ?
178 OFFSET ?
179 ) me
180 LEFT JOIN books books
181 ON books.owner = me.id
182 ORDER BY me.name
183 )',
184 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
185 'Expected SQL on *ordered* complex limited prefetch with non-selected root data',
186 );
187
188 is_deeply (
189 $empty_ordered_pref_rs->all_hri,
190 $empty_ordered_pref_hri,
191 'Expected result on *ordered* complex limited prefetch with non-selected root data'
192 );
193
194 $empty_ordered_pref_rs = $empty_ordered_pref_rs->search({}, {
195 order_by => [ \ 'LENGTH(me.name)', \ 'RANDOM()' ],
196 });
197
198 is_same_sql_bind(
199 $empty_ordered_pref_rs->as_query,
200 '(
201 SELECT books.id, books.source, books.owner, books.title, books.price
202 FROM (
203 SELECT me.id, me.name
204 FROM owners me
205 ORDER BY LENGTH(me.name), RANDOM()
206 LIMIT ?
207 OFFSET ?
208 ) me
209 LEFT JOIN books books
210 ON books.owner = me.id
211 ORDER BY LENGTH(me.name), RANDOM()
212 )',
213 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
214 'Expected SQL on *function-ordered* complex limited prefetch with non-selected root data',
215 );
216
217 is_deeply (
218 $empty_ordered_pref_rs->all_hri,
219 $empty_ordered_pref_hri,
220 'Expected result on *function-ordered* complex limited prefetch with non-selected root data'
221 );
27e0370d 222}
223
224
ce9f555e 225done_testing;