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