Fix tests failing due to unspecified resultset retrieval order
[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,
fb88ca2c 119 order_by => 'name',
27e0370d 120 columns => 'name', # only the owner name, still prefetch all the books
121 prefetch => 'books',
122 });
123
97e130fa 124 is_same_sql_bind(
125 $pref_rs->as_query,
126 '(
127 SELECT me.name, books.id, books.source, books.owner, books.title, books.price
128 FROM (
129 SELECT me.name, me.id
130 FROM owners me
fb88ca2c 131 ORDER BY name
97e130fa 132 LIMIT ?
133 OFFSET ?
134 ) me
135 LEFT JOIN books books
136 ON books.owner = me.id
fb88ca2c 137 ORDER BY name
97e130fa 138 )',
139 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
140 'Expected SQL on complex limited prefetch with non-selected join condition',
141 );
142
143 is_deeply (
144 $pref_rs->all_hri,
145 [ {
146 name => "Waltham",
147 books => [ {
148 id => 3,
149 owner => 2,
150 price => 65,
151 source => "Library",
152 title => "Best Recipe Cookbook",
153 } ],
154 } ],
155 'Expected result on complex limited prefetch with non-selected join condition'
156 );
157
158 my $empty_ordered_pref_rs = $pref_rs->search({}, {
159 columns => [], # nothing, we only prefetch the book data
160 order_by => 'me.name',
161 });
162 my $empty_ordered_pref_hri = [ {
163 books => [ {
164 id => 3,
165 owner => 2,
166 price => 65,
167 source => "Library",
168 title => "Best Recipe Cookbook",
169 } ],
170 } ];
171
172 is_same_sql_bind(
173 $empty_ordered_pref_rs->as_query,
174 '(
175 SELECT books.id, books.source, books.owner, books.title, books.price
176 FROM (
177 SELECT me.id, me.name
178 FROM owners me
179 ORDER BY me.name
180 LIMIT ?
181 OFFSET ?
182 ) me
183 LEFT JOIN books books
184 ON books.owner = me.id
185 ORDER BY me.name
186 )',
187 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
188 'Expected SQL on *ordered* complex limited prefetch with non-selected root data',
189 );
190
191 is_deeply (
192 $empty_ordered_pref_rs->all_hri,
193 $empty_ordered_pref_hri,
194 'Expected result on *ordered* complex limited prefetch with non-selected root data'
195 );
196
197 $empty_ordered_pref_rs = $empty_ordered_pref_rs->search({}, {
198 order_by => [ \ 'LENGTH(me.name)', \ 'RANDOM()' ],
199 });
200
201 is_same_sql_bind(
202 $empty_ordered_pref_rs->as_query,
203 '(
204 SELECT books.id, books.source, books.owner, books.title, books.price
205 FROM (
206 SELECT me.id, me.name
207 FROM owners me
208 ORDER BY LENGTH(me.name), RANDOM()
209 LIMIT ?
210 OFFSET ?
211 ) me
212 LEFT JOIN books books
213 ON books.owner = me.id
214 ORDER BY LENGTH(me.name), RANDOM()
215 )',
216 [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
217 'Expected SQL on *function-ordered* complex limited prefetch with non-selected root data',
218 );
219
220 is_deeply (
221 $empty_ordered_pref_rs->all_hri,
222 $empty_ordered_pref_hri,
223 'Expected result on *function-ordered* complex limited prefetch with non-selected root data'
224 );
27e0370d 225}
226
227
ce9f555e 228done_testing;