Introduce GOVERNANCE document and empty RESOLUTIONS file.
[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       artwork => {},
44       tracks => [
45         { title => 'o2', position => 2},  # the position should not be needed here, bug in MC
46       ],
47     },
48   },
49 });
50
51 for (1,2) {
52   $schema->resultset('CD')->create({ artist => 1, year => 1977, title => "fuzzy_$_" });
53 }
54
55 {
56   package DBICTest::HRI::Subclass;
57   use base 'DBIx::Class::ResultClass::HashRefInflator';
58 }
59
60 {
61   package DBICTest::HRI::Around;
62   use base 'DBIx::Class::ResultClass::HashRefInflator';
63
64   sub inflate_result { shift->next::method(@_) }
65 }
66
67 for my $rs (
68   $schema->resultset('CD')->search_rs({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' }),
69   $schema->resultset('CD')->search_rs({}, { result_class => 'DBICTest::HRI::Subclass' }),
70   $schema->resultset('CD')->search_rs({}, { result_class => 'DBICTest::HRI::Around' }),
71 ) {
72
73 cmp_deeply
74   [ $rs->search({}, {
75     columns => {
76       year                          => 'me.year',
77       'single_track.cd.artist.name' => 'artist.name',
78     },
79     join => { single_track => { cd => 'artist' } },
80     order_by => [qw/me.cdid artist.artistid/],
81   })->all ],
82   [
83     { year => 1981, single_track => undef },
84     { year => 1976, single_track => undef },
85     { year => 1978, single_track => {
86       cd => {
87         artist => { name => "JMJ" }
88       },
89     }},
90     { year => 1977, single_track => undef },
91     { year => 1977, single_track => undef },
92
93   ],
94   'plain 1:1 descending chain ' . $rs->result_class
95 ;
96
97 cmp_deeply
98   [ $rs->search({}, {
99     columns => {
100       'artist'                                  => 'me.artist',
101       'title'                                   => 'me.title',
102       'year'                                    => 'me.year',
103       'single_track.cd.artist.artistid'         => 'artist.artistid',
104       'single_track.cd.artist.cds.cdid'         => 'cds.cdid',
105       'single_track.cd.artist.cds.tracks.title' => 'tracks.title',
106     },
107     join => { single_track => { cd => { artist => { cds => 'tracks' } } } },
108     order_by => [qw/me.cdid artist.artistid cds.cdid tracks.trackid/],
109   })->all ],
110   [
111     {
112       artist => 1, title => "Magnetic Fields", year => 1981, single_track => undef,
113     },
114     {
115       artist => 1, title => "Oxygene", year => 1976, single_track => undef,
116     },
117     {
118       artist => 1, title => "Equinoxe", year => 1978, single_track => {
119         cd => {
120           artist => {
121             artistid => 1, cds => {
122               cdid => 1, tracks => {
123                 title => "m1"
124               }
125             }
126           }
127         }
128       },
129     },
130     {
131       artist => 1, title => "Equinoxe", year => 1978, single_track => {
132         cd => {
133           artist => {
134             artistid => 1, cds => {
135               cdid => 1, tracks => {
136                 title => "m2"
137               }
138             }
139           }
140         }
141       },
142     },
143     {
144       artist => 1, title => "Equinoxe", year => 1978, single_track => {
145         cd => {
146           artist => {
147             artistid => 1, cds => {
148               cdid => 1, tracks => {
149                 title => "m3"
150               }
151             }
152           }
153         }
154       },
155     },
156     {
157       artist => 1, title => "Equinoxe", year => 1978, single_track => {
158         cd => {
159           artist => {
160             artistid => 1, cds => {
161               cdid => 1, tracks => {
162                 title => "m4"
163               }
164             }
165           }
166         }
167       },
168     },
169     {
170       artist => 1, title => "Equinoxe", year => 1978, single_track => {
171         cd => {
172           artist => {
173             artistid => 1, cds => {
174               cdid => 2, tracks => {
175                 title => "o2"
176               }
177             }
178           }
179         }
180       },
181     },
182     {
183       artist => 1, title => "Equinoxe", year => 1978, single_track => {
184         cd => {
185           artist => {
186             artistid => 1, cds => {
187               cdid => 2, tracks => {
188                 title => "o1"
189               }
190             }
191           }
192         }
193       },
194     },
195     {
196       artist => 1, title => "Equinoxe", year => 1978, single_track => {
197         cd => {
198           artist => {
199             artistid => 1, cds => {
200               cdid => 3, tracks => {
201                 title => "e1"
202               }
203             }
204           }
205         }
206       },
207     },
208     {
209       artist => 1, title => "Equinoxe", year => 1978, single_track => {
210         cd => {
211           artist => {
212             artistid => 1, cds => {
213               cdid => 3, tracks => {
214                 title => "e2"
215               }
216             }
217           }
218         }
219       },
220     },
221     {
222       artist => 1, title => "Equinoxe", year => 1978, single_track => {
223         cd => {
224           artist => {
225             artistid => 1, cds => {
226               cdid => 3, tracks => {
227                 title => "e3"
228               }
229             }
230           }
231         }
232       },
233     },
234     {
235       artist => 1, title => "Equinoxe", year => 1978, single_track => {
236         cd => {
237           artist => {
238             artistid => 1, cds => {
239               cdid => 4, tracks => undef
240             }
241           }
242         }
243       },
244     },
245     {
246       artist => 1, title => "Equinoxe", year => 1978, single_track => {
247         cd => {
248           artist => {
249             artistid => 1, cds => {
250               cdid => 5, tracks => undef
251             }
252           }
253         }
254       },
255     },
256     {
257       artist => 1, title => "fuzzy_1", year => 1977, single_track => undef,
258     },
259     {
260       artist => 1, title => "fuzzy_2", year => 1977, single_track => undef,
261     }
262   ],
263   'non-collapsing 1:1:1:M:M chain ' . $rs->result_class,
264 ;
265
266 cmp_deeply
267   [ $rs->search({}, {
268     columns => {
269       'artist'                                  => 'me.artist',
270       'title'                                   => 'me.title',
271       'year'                                    => 'me.year',
272       'single_track.cd.artist.artistid'         => 'artist.artistid',
273       'single_track.cd.artist.cds.cdid'         => 'cds.cdid',
274       'single_track.cd.artist.cds.tracks.title' => 'tracks.title',
275     },
276     join => { single_track => { cd => { artist => { cds => 'tracks' } } } },
277     order_by => [qw/me.cdid artist.artistid cds.cdid tracks.trackid/],
278     collapse => 1,
279   })->all ],
280   [
281     {
282       artist => 1, title => "Magnetic Fields", year => 1981, single_track => undef,
283     },
284     {
285       artist => 1, title => "Oxygene", year => 1976, single_track => undef,
286     },
287     {
288       artist => 1, title => "Equinoxe", year => 1978, single_track => {
289         cd => {
290           artist => {
291             artistid => 1, cds => [
292               {
293                 cdid => 1, tracks => [
294                   { title => "m1" },
295                   { title => "m2" },
296                   { title => "m3" },
297                   { title => "m4" },
298                 ]
299               },
300               {
301                 cdid => 2, tracks => [
302                   { title => "o2" },
303                   { title => "o1" },
304                 ]
305               },
306               {
307                 cdid => 3, tracks => [
308                   { title => "e1" },
309                   { title => "e2" },
310                   { title => "e3" },
311                 ]
312               },
313               {
314                 cdid => 4, tracks => [],
315               },
316               {
317                 cdid => 5, tracks => [],
318               }
319             ]
320           }
321         }
322       },
323     },
324     {
325       artist => 1, title => "fuzzy_1", year => 1977, single_track => undef,
326     },
327     {
328       artist => 1, title => "fuzzy_2", year => 1977, single_track => undef,
329     }
330   ],
331   'collapsing 1:1:1:M:M chain ' . $rs->result_class,
332 ;
333
334 cmp_deeply
335   [ $rs->search_rs (
336     {
337       'tracks.title' => 'e2',
338       'cds.title' => 'Oxygene',
339     },
340     {
341       collapse => 1,
342       join => [
343         'tracks',
344         { single_track => { cd => 'mandatory_artwork' } },
345         { artist => { cds => 'mandatory_artwork'} },
346       ],
347       columns => {
348         cdid                                      => 'cdid',
349         'single_track.cd.mandatory_artwork.cd_id' => 'mandatory_artwork.cd_id',
350         'artist.cds.mandatory_artwork.cd_id'      => 'mandatory_artwork_2.cd_id',
351       },
352     },
353   )->all ],
354   [
355     {
356       cdid => 3,
357       single_track => {
358         cd => {
359           mandatory_artwork => { cd_id => 2 },
360         },
361       },
362       artist => {
363         cds => [
364           { mandatory_artwork => { cd_id => 2 } }
365         ]
366       },
367     },
368   ],
369 ;
370
371 }
372
373 done_testing;