Initial full test pass - all fetches are eager for now
[dbsrgits/DBIx-Class.git] / t / resultset / inflate_result_api.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema(no_populate => 1);
9
10 $schema->resultset('CD')->create({
11   title => 'Equinoxe',
12   year => 1978,
13   artist => { name => 'JMJ' },
14   genre => { name => 'electro' },
15   tracks => [
16     { title => 'e1' },
17     { title => 'e2' },
18     { title => 'e3' },
19   ],
20   single_track => {
21     title => 'o1',
22     cd => {
23       title => 'Oxygene',
24       year => 1976,
25       artist => {
26         name => 'JMJ',
27         cds => [
28           {
29             title => 'Magnetic Fields',
30             year => 1981,
31             genre => { name => 'electro' },
32             tracks => [
33               { title => 'm1' },
34               { title => 'm2' },
35               { title => 'm3' },
36               { title => 'm4' },
37             ],
38           },
39         ],
40       },
41       tracks => [
42         { title => 'o2', position => 2},  # the position should not be needed here, bug in MC
43       ],
44     },
45   },
46 });
47
48 {
49   package DBICTest::_IRCapture;
50   sub inflate_result { [@_[2,3]] };
51 }
52
53 is_deeply(
54   ([$schema->resultset ('CD')->search ({}, {
55     result_class => 'DBICTest::_IRCapture',
56     prefetch => { single_track => { cd => 'artist' } },
57     order_by => 'me.cdid',
58   })->all]),
59   [
60     [
61       { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
62       { single_track => [
63         { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
64         {  cd => [
65           { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
66           {
67             artist => [
68               { artistid => undef, name => undef, charfield => undef, rank => undef }
69             ]
70           }
71         ] }
72       ] }
73     ],
74     [
75       { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
76       { single_track => [
77         { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
78         {  cd => [
79           { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
80           {
81             artist => [
82               { artistid => undef, name => undef, charfield => undef, rank => undef }
83             ]
84           }
85         ] }
86       ] }
87     ],
88     [
89       { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
90       { single_track => [
91         { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef },
92         {  cd => [
93           { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
94           {
95             artist => [
96               { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 }
97             ]
98           }
99         ] }
100       ] }
101     ],
102   ],
103   'Simple 1:1 descend with classic prefetch ok'
104 );
105
106 is_deeply(
107   [$schema->resultset ('CD')->search ({}, {
108     result_class => 'DBICTest::_IRCapture',
109     join => { single_track => { cd => 'artist' } },
110     columns => [
111       { 'year'                                    => 'me.year' },
112       { 'genreid'                                 => 'me.genreid' },
113       { 'single_track.cd.artist.artistid'         => 'artist.artistid' },
114       { 'title'                                   => 'me.title' },
115       { 'artist'                                  => 'me.artist' },
116     ],
117     order_by => 'me.cdid',
118   })->all],
119   [
120     [
121       { artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
122       { single_track => [
123         undef,
124         {  cd => [
125           undef,
126           {
127             artist => [
128               { artistid => undef }
129             ]
130           }
131         ] }
132       ] }
133     ],
134     [
135       { artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
136       { single_track => [
137         undef,
138         {  cd => [
139           undef,
140           {
141             artist => [
142               { artistid => undef }
143             ]
144           }
145         ] }
146       ] }
147     ],
148     [
149       { artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
150       { single_track => [
151         undef,
152         {  cd => [
153           undef,
154           {
155             artist => [
156               { artistid => 1 }
157             ]
158           }
159         ] }
160       ] }
161     ],
162   ],
163   'Simple 1:1 descend with missing selectors ok'
164 );
165
166 is_deeply(
167   ([$schema->resultset ('CD')->search ({}, {
168     result_class => 'DBICTest::_IRCapture',
169     prefetch => [ { single_track => { cd => { artist => { cds => 'tracks' } } } } ],
170     order_by => [qw/me.cdid tracks.trackid/],
171   })->all]),
172   [
173     [
174       { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
175       { single_track => [
176         { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
177         {  cd => [
178           { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
179           {
180             artist => [
181               { artistid => undef, name => undef, charfield => undef, rank => undef },
182               { cds => [ [
183                 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
184                 { tracks => [ [
185                   { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
186                 ] ] },
187               ]]},
188             ],
189           },
190         ] },
191       ] },
192     ],
193     [
194       { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
195       { single_track => [
196         { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
197         {  cd => [
198           { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
199           {
200             artist => [
201               { artistid => undef, name => undef, charfield => undef, rank => undef },
202               { cds => [ [
203                 { cdid => undef, single_track => undef, artist => undef, genreid => undef, year => undef, title => undef },
204                 { tracks => [ [
205                   { trackid => undef, title => undef, position => undef, cd => undef, last_updated_at => undef, last_updated_on => undef },
206                 ] ] },
207               ]]},
208             ]
209           }
210         ] }
211       ] }
212     ],
213     [
214       { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
215       { single_track => [
216         { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef },
217         {  cd => [
218           { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
219           {
220             artist => [
221               { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
222               { cds => [
223                 [
224                   { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
225                   { tracks => [
226                     [ { trackid => 1, title => 'm1', position => 1, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
227                     [ { trackid => 2, title => 'm2', position => 2, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
228                     [ { trackid => 3, title => 'm3', position => 3, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
229                     [ { trackid => 4, title => 'm4', position => 4, cd => 1, last_updated_at => undef, last_updated_on => undef } ],
230                   ]},
231                 ],
232                 [
233                   { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
234                   { tracks => [
235                     [ { trackid => 5, title => 'o2', position => 2, cd => 2, last_updated_at => undef, last_updated_on => undef } ],
236                     [ { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef } ],
237                   ]},
238                 ],
239                 [
240                   { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
241                   { tracks => [
242                     [ { trackid => 7, title => 'e1', position => 1, cd => 3, last_updated_at => undef, last_updated_on => undef } ],
243                     [ { trackid => 8, title => 'e2', position => 2, cd => 3, last_updated_at => undef, last_updated_on => undef } ],
244                     [ { trackid => 9, title => 'e3', position => 3, cd => 3, last_updated_at => undef, last_updated_on => undef } ],
245                   ]},
246                 ],
247               ]},
248             ]
249           }
250         ] }
251       ] }
252     ],
253   ],
254   'Collapsing 1:1 ending in chained has_many with classic prefetch ok'
255 );
256
257 is_deeply (
258   ([$schema->resultset ('Artist')->search ({}, {
259     result_class => 'DBICTest::_IRCapture',
260     join => { cds => 'tracks' },
261     '+columns' => [
262       (map { "cds.$_" } $schema->source('CD')->columns),
263       (map { +{ "cds.tracks.$_" => "tracks.$_" } } $schema->source('Track')->columns),
264     ],
265     order_by => [qw/cds.cdid tracks.trackid/],
266   })->all]),
267   [
268     [
269       { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
270       { cds => [
271         { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
272         { tracks => [
273           { trackid => 1, title => 'm1', position => 1, cd => 1, last_updated_at => undef, last_updated_on => undef },
274         ]},
275       ]},
276     ],
277     [
278       { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
279       { cds => [
280         { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
281         { tracks => [
282           { trackid => 2, title => 'm2', position => 2, cd => 1, last_updated_at => undef, last_updated_on => undef },
283         ]},
284       ]},
285     ],
286     [
287       { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
288       { cds => [
289         { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
290         { tracks => [
291           { trackid => 3, title => 'm3', position => 3, cd => 1, last_updated_at => undef, last_updated_on => undef },
292         ]},
293       ]},
294     ],
295     [
296       { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
297       { cds => [
298         { cdid => 1, single_track => undef, artist => 1, genreid => 1, year => 1981, title => "Magnetic Fields" },
299         { tracks => [
300           { trackid => 4, title => 'm4', position => 4, cd => 1, last_updated_at => undef, last_updated_on => undef },
301         ]},
302       ]},
303     ],
304     [
305       { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
306       { cds => [
307         { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
308         { tracks => [
309           { trackid => 5, title => 'o2', position => 2, cd => 2, last_updated_at => undef, last_updated_on => undef },
310         ]},
311       ]},
312     ],
313     [
314       { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
315       { cds => [
316         { cdid => 2, single_track => undef, artist => 1, genreid => undef, year => 1976, title => "Oxygene" },
317         { tracks => [
318           { trackid => 6, title => 'o1', position => 1, cd => 2, last_updated_at => undef, last_updated_on => undef },
319         ]},
320       ]},
321     ],
322     [
323       { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
324       { cds => [
325         { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
326         { tracks => [
327           { trackid => 7, title => 'e1', position => 1, cd => 3, last_updated_at => undef, last_updated_on => undef },
328         ]},
329       ]},
330     ],
331     [
332       { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
333       { cds => [
334         { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
335         { tracks => [
336           { trackid => 8, title => 'e2', position => 2, cd => 3, last_updated_at => undef, last_updated_on => undef },
337         ]},
338       ]},
339     ],
340     [
341       { artistid => 1, name => 'JMJ', charfield => undef, rank => 13 },
342       { cds => [
343         { cdid => 3, single_track => 6, artist => 1, genreid => 1, year => 1978, title => "Equinoxe" },
344         { tracks => [
345           { trackid => 9, title => 'e3', position => 3, cd => 3, last_updated_at => undef, last_updated_on => undef },
346         ]},
347       ]},
348     ],
349   ],
350   'Non-Collapsing chained has_many ok'
351 );
352
353 done_testing;