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