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