04237f92ab9dddcbacb17f4b3d6676f26ccdf5d9
[dbsrgits/DBIx-Class.git] / t / inflate / hri_torture.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 # More tests like this in t/prefetch/manual.t
9
10 my $schema = DBICTest->init_schema(no_populate => 1, quote_names => 1);
11 $schema->resultset('Artist')->create({ name => 'JMJ', cds => [{
12   title => 'Magnetic Fields',
13   year => 1981,
14   genre => { name => 'electro' },
15   tracks => [
16     { title => 'm1' },
17     { title => 'm2' },
18     { title => 'm3' },
19     { title => 'm4' },
20   ],
21 } ] });
22
23
24 $schema->resultset('CD')->create({
25   title => 'Equinoxe',
26   year => 1978,
27   artist => { name => 'JMJ' },
28   genre => { name => 'electro' },
29   tracks => [
30     { title => 'e1' },
31     { title => 'e2' },
32     { title => 'e3' },
33   ],
34   single_track => {
35     title => 'o1',
36     cd => {
37       title => 'Oxygene',
38       year => 1976,
39       artist => { name => 'JMJ' },
40       tracks => [
41         { title => 'o2', position => 2},  # the position should not be needed here, bug in MC
42       ],
43     },
44   },
45 });
46
47 for (1,2) {
48   $schema->resultset('CD')->create({ artist => 1, year => 1977, title => "fuzzy_$_" });
49 }
50
51 my $rs = $schema->resultset('CD');
52
53 is_deeply
54   $rs->search({}, {
55     columns => {
56       year                          => 'me.year',
57       'single_track.cd.artist.name' => 'artist.name',
58     },
59     join => { single_track => { cd => 'artist' } },
60     order_by => [qw/me.cdid artist.artistid/],
61   })->all_hri,
62   [
63     {
64       single_track => undef,
65       year => 1981
66     },
67     {
68       single_track => undef,
69       year => 1976
70     },
71     {
72       single_track => {
73         cd => {
74           artist => {
75             name => "JMJ"
76           }
77         }
78       },
79       year => 1978
80     },
81     {
82       single_track => undef,
83       year => 1977
84     },
85     {
86       single_track => undef,
87       year => 1977
88     },
89   ],
90   'plain 1:1 descending chain'
91 ;
92
93 is_deeply
94   $rs->search({}, {
95     columns => {
96       'artist'                                  => 'me.artist',
97       'title'                                   => 'me.title',
98       'year'                                    => 'me.year',
99       'single_track.cd.artist.artistid'         => 'artist.artistid',
100       'single_track.cd.artist.cds.cdid'         => 'cds.cdid',
101       'single_track.cd.artist.cds.tracks.title' => 'tracks.title',
102     },
103     join => { single_track => { cd => { artist => { cds => 'tracks' } } } },
104     order_by => [qw/me.cdid artist.artistid cds.cdid tracks.trackid/],
105   })->all_hri,
106   [
107     {
108       artist => 1,
109       single_track => undef,
110       title => "Magnetic Fields",
111       year => 1981
112     },
113     {
114       artist => 1,
115       single_track => undef,
116       title => "Oxygene",
117       year => 1976
118     },
119     {
120       artist => 1,
121       single_track => {
122         cd => {
123           artist => {
124             artistid => 1,
125             cds => {
126               cdid => 1,
127               tracks => {
128                 title => "m1"
129               }
130             }
131           }
132         }
133       },
134       title => "Equinoxe",
135       year => 1978
136     },
137     {
138       artist => 1,
139       single_track => {
140         cd => {
141           artist => {
142             artistid => 1,
143             cds => {
144               cdid => 1,
145               tracks => {
146                 title => "m2"
147               }
148             }
149           }
150         }
151       },
152       title => "Equinoxe",
153       year => 1978
154     },
155     {
156       artist => 1,
157       single_track => {
158         cd => {
159           artist => {
160             artistid => 1,
161             cds => {
162               cdid => 1,
163               tracks => {
164                 title => "m3"
165               }
166             }
167           }
168         }
169       },
170       title => "Equinoxe",
171       year => 1978
172     },
173     {
174       artist => 1,
175       single_track => {
176         cd => {
177           artist => {
178             artistid => 1,
179             cds => {
180               cdid => 1,
181               tracks => {
182                 title => "m4"
183               }
184             }
185           }
186         }
187       },
188       title => "Equinoxe",
189       year => 1978
190     },
191     {
192       artist => 1,
193       single_track => {
194         cd => {
195           artist => {
196             artistid => 1,
197             cds => {
198               cdid => 2,
199               tracks => {
200                 title => "o2"
201               }
202             }
203           }
204         }
205       },
206       title => "Equinoxe",
207       year => 1978
208     },
209     {
210       artist => 1,
211       single_track => {
212         cd => {
213           artist => {
214             artistid => 1,
215             cds => {
216               cdid => 2,
217               tracks => {
218                 title => "o1"
219               }
220             }
221           }
222         }
223       },
224       title => "Equinoxe",
225       year => 1978
226     },
227     {
228       artist => 1,
229       single_track => {
230         cd => {
231           artist => {
232             artistid => 1,
233             cds => {
234               cdid => 3,
235               tracks => {
236                 title => "e1"
237               }
238             }
239           }
240         }
241       },
242       title => "Equinoxe",
243       year => 1978
244     },
245     {
246       artist => 1,
247       single_track => {
248         cd => {
249           artist => {
250             artistid => 1,
251             cds => {
252               cdid => 3,
253               tracks => {
254                 title => "e2"
255               }
256             }
257           }
258         }
259       },
260       title => "Equinoxe",
261       year => 1978
262     },
263     {
264       artist => 1,
265       single_track => {
266         cd => {
267           artist => {
268             artistid => 1,
269             cds => {
270               cdid => 3,
271               tracks => {
272                 title => "e3"
273               }
274             }
275           }
276         }
277       },
278       title => "Equinoxe",
279       year => 1978
280     },
281     {
282       artist => 1,
283       single_track => {
284         cd => {
285           artist => {
286             artistid => 1,
287             cds => {
288               cdid => 4,
289               tracks => undef
290             }
291           }
292         }
293       },
294       title => "Equinoxe",
295       year => 1978
296     },
297     {
298       artist => 1,
299       single_track => {
300         cd => {
301           artist => {
302             artistid => 1,
303             cds => {
304               cdid => 5,
305               tracks => undef
306             }
307           }
308         }
309       },
310       title => "Equinoxe",
311       year => 1978
312     },
313     {
314       artist => 1,
315       single_track => undef,
316       title => "fuzzy_1",
317       year => 1977
318     },
319     {
320       artist => 1,
321       single_track => undef,
322       title => "fuzzy_2",
323       year => 1977
324     }
325   ],
326   'non-collapsing 1:1:1:M:M chain',
327 ;
328
329 is_deeply
330   $rs->search({}, {
331     columns => {
332       'artist'                                  => 'me.artist',
333       'title'                                   => 'me.title',
334       'year'                                    => 'me.year',
335       'single_track.cd.artist.artistid'         => 'artist.artistid',
336       'single_track.cd.artist.cds.cdid'         => 'cds.cdid',
337       'single_track.cd.artist.cds.tracks.title' => 'tracks.title',
338     },
339     join => { single_track => { cd => { artist => { cds => 'tracks' } } } },
340     order_by => [qw/me.cdid artist.artistid cds.cdid tracks.trackid/],
341     collapse => {}, #hashref to keep older DBIC versions happy (doesn't actually work)
342   })->all_hri,
343   [
344     {
345       artist => 1,
346       single_track => undef,
347       title => "Magnetic Fields",
348       year => 1981
349     },
350     {
351       artist => 1,
352       single_track => undef,
353       title => "Oxygene",
354       year => 1976
355     },
356     {
357       artist => 1,
358       single_track => {
359         cd => {
360           artist => {
361             artistid => 1,
362             cds => [
363               {
364                 cdid => 1,
365                 tracks => [
366                   {
367                     title => "m1"
368                   },
369                   {
370                     title => "m2"
371                   },
372                   {
373                     title => "m3"
374                   },
375                   {
376                     title => "m4"
377                   }
378                 ]
379               },
380               {
381                 cdid => 2,
382                 tracks => [
383                   {
384                     title => "o2"
385                   },
386                   {
387                     title => "o1"
388                   }
389                 ]
390               },
391               {
392                 cdid => 3,
393                 tracks => [
394                   {
395                     title => "e1"
396                   },
397                   {
398                     title => "e2"
399                   },
400                   {
401                     title => "e3"
402                   }
403                 ]
404               },
405               {
406                 cdid => 4,
407                 tracks => []
408               },
409               {
410                 cdid => 5,
411                 tracks => []
412               }
413             ]
414           }
415         }
416       },
417       title => "Equinoxe",
418       year => 1978
419     },
420     {
421       artist => 1,
422       single_track => undef,
423       title => "fuzzy_1",
424       year => 1977
425     },
426     {
427       artist => 1,
428       single_track => undef,
429       title => "fuzzy_2",
430       year => 1977
431     }
432   ],
433   'collapsing 1:1:1:M:M chain',
434 ;
435
436 done_testing;