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