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