Commit | Line | Data |
5aa25b93 |
1 | use strict; |
8273e845 |
2 | use warnings; |
5aa25b93 |
3 | |
4 | use Test::More; |
52864fbd |
5 | use Test::Deep; |
5aa25b93 |
6 | use Test::Exception; |
7 | use lib qw(t/lib); |
8 | use DBICTest; |
97e130fa |
9 | use DBIC::SqlMakerTest; |
5aa25b93 |
10 | |
376b7a93 |
11 | my $schema = DBICTest->init_schema(); |
5aa25b93 |
12 | |
376b7a93 |
13 | lives_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 |
36 | lives_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 | } |
44976045 |
58 | @{$data->{tracks}} = sort { $a->{trackid} <=> $b->{trackid} } @{$data->{tracks}}; |
69ab63d4 |
59 | push @cds_and_tracks, $data; |
60 | } |
61 | |
908aa1bb |
62 | my $pref_rs = $rs->search ({}, { columns => [qw/year cdid/], prefetch => 'tracks' }); |
69ab63d4 |
63 | |
64 | my @pref_cds_and_tracks; |
65 | for my $cd ($pref_rs->all) { |
66 | my $data = { $cd->get_columns }; |
67 | for my $tr ($cd->tracks->all) { |
68 | push @{$data->{tracks}}, { $tr->get_columns }; |
69 | } |
44976045 |
70 | @{$data->{tracks}} = sort { $a->{trackid} <=> $b->{trackid} } @{$data->{tracks}}; |
69ab63d4 |
71 | push @pref_cds_and_tracks, $data; |
72 | } |
73 | |
52864fbd |
74 | cmp_deeply ( |
69ab63d4 |
75 | \@pref_cds_and_tracks, |
76 | \@cds_and_tracks, |
77 | 'Correct collapsing on non-unique primary object' |
78 | ); |
79 | |
52864fbd |
80 | cmp_deeply ( |
44976045 |
81 | $pref_rs->search ({}, { order_by => [ { -desc => 'me.year' }, 'trackid' ] })->all_hri, |
69ab63d4 |
82 | \@cds_and_tracks, |
83 | 'Correct HRI collapsing on non-unique primary object' |
84 | ); |
85 | |
86 | }, 'weird collapse lives'); |
87 | |
88 | |
951b7581 |
89 | lives_ok(sub { |
90 | # test implicit prefetch as well |
91 | |
92 | my $rs = $schema->resultset('CD')->search( |
93 | { title => 'Generic Manufactured Singles' }, |
94 | { |
95 | join=> 'artist', |
96 | select => [qw/ me.title artist.name / ], |
97 | } |
98 | ); |
99 | |
100 | my $cd = $rs->next; |
101 | is ($cd->title, 'Generic Manufactured Singles', 'CD title prefetched correctly'); |
102 | isa_ok ($cd->artist, 'DBICTest::Artist'); |
103 | is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name'); |
5aa25b93 |
104 | |
951b7581 |
105 | }, 'implicit keyless prefetch works'); |
ce9f555e |
106 | |
107 | # sane error |
108 | throws_ok( |
109 | sub { |
110 | $schema->resultset('Track')->search({}, { join => { cd => 'artist' }, '+columns' => 'artist.name' } )->next; |
111 | }, |
95e41036 |
112 | qr|\QInflation into non-existent relationship 'artist' of 'Track' requested, check the inflation specification (columns/as) ending in '...artist.name'|, |
ce9f555e |
113 | 'Sensible error message on mis-specified "as"', |
114 | ); |
115 | |
27e0370d |
116 | # check complex limiting prefetch without the join-able columns |
117 | { |
118 | my $pref_rs = $schema->resultset('Owners')->search({}, { |
119 | rows => 3, |
120 | offset => 1, |
fb88ca2c |
121 | order_by => 'name', |
27e0370d |
122 | columns => 'name', # only the owner name, still prefetch all the books |
123 | prefetch => 'books', |
124 | }); |
125 | |
97e130fa |
126 | is_same_sql_bind( |
127 | $pref_rs->as_query, |
128 | '( |
129 | SELECT me.name, books.id, books.source, books.owner, books.title, books.price |
130 | FROM ( |
131 | SELECT me.name, me.id |
132 | FROM owners me |
fb88ca2c |
133 | ORDER BY name |
97e130fa |
134 | LIMIT ? |
135 | OFFSET ? |
136 | ) me |
137 | LEFT JOIN books books |
138 | ON books.owner = me.id |
fb88ca2c |
139 | ORDER BY name |
97e130fa |
140 | )', |
141 | [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ], |
142 | 'Expected SQL on complex limited prefetch with non-selected join condition', |
143 | ); |
144 | |
145 | is_deeply ( |
146 | $pref_rs->all_hri, |
147 | [ { |
148 | name => "Waltham", |
149 | books => [ { |
150 | id => 3, |
151 | owner => 2, |
152 | price => 65, |
153 | source => "Library", |
154 | title => "Best Recipe Cookbook", |
155 | } ], |
156 | } ], |
157 | 'Expected result on complex limited prefetch with non-selected join condition' |
158 | ); |
159 | |
160 | my $empty_ordered_pref_rs = $pref_rs->search({}, { |
161 | columns => [], # nothing, we only prefetch the book data |
162 | order_by => 'me.name', |
163 | }); |
164 | my $empty_ordered_pref_hri = [ { |
165 | books => [ { |
166 | id => 3, |
167 | owner => 2, |
168 | price => 65, |
169 | source => "Library", |
170 | title => "Best Recipe Cookbook", |
171 | } ], |
172 | } ]; |
173 | |
174 | is_same_sql_bind( |
175 | $empty_ordered_pref_rs->as_query, |
176 | '( |
177 | SELECT books.id, books.source, books.owner, books.title, books.price |
178 | FROM ( |
179 | SELECT me.id, me.name |
180 | FROM owners me |
181 | ORDER BY me.name |
182 | LIMIT ? |
183 | OFFSET ? |
184 | ) me |
185 | LEFT JOIN books books |
186 | ON books.owner = me.id |
187 | ORDER BY me.name |
188 | )', |
189 | [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ], |
190 | 'Expected SQL on *ordered* complex limited prefetch with non-selected root data', |
191 | ); |
192 | |
193 | is_deeply ( |
194 | $empty_ordered_pref_rs->all_hri, |
195 | $empty_ordered_pref_hri, |
196 | 'Expected result on *ordered* complex limited prefetch with non-selected root data' |
197 | ); |
198 | |
199 | $empty_ordered_pref_rs = $empty_ordered_pref_rs->search({}, { |
200 | order_by => [ \ 'LENGTH(me.name)', \ 'RANDOM()' ], |
201 | }); |
202 | |
203 | is_same_sql_bind( |
204 | $empty_ordered_pref_rs->as_query, |
205 | '( |
206 | SELECT books.id, books.source, books.owner, books.title, books.price |
207 | FROM ( |
208 | SELECT me.id, me.name |
209 | FROM owners me |
210 | ORDER BY LENGTH(me.name), RANDOM() |
211 | LIMIT ? |
212 | OFFSET ? |
213 | ) me |
214 | LEFT JOIN books books |
215 | ON books.owner = me.id |
216 | ORDER BY LENGTH(me.name), RANDOM() |
217 | )', |
218 | [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ], |
219 | 'Expected SQL on *function-ordered* complex limited prefetch with non-selected root data', |
220 | ); |
221 | |
222 | is_deeply ( |
223 | $empty_ordered_pref_rs->all_hri, |
224 | $empty_ordered_pref_hri, |
225 | 'Expected result on *function-ordered* complex limited prefetch with non-selected root data' |
226 | ); |
27e0370d |
227 | } |
228 | |
229 | |
ce9f555e |
230 | done_testing; |