1use strict;
2use warnings;
3
4use Test::More;
5use Test::Deep;
6use lib qw(t/lib);
7use DBICTest;
8
9# More tests like this in t/prefetch/manual.t
10
11my $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
48for (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
64for 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
70cmp_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    { year => 1981, single_track => undef },
81    { year => 1976, single_track => undef },
82    { year => 1978, single_track => {
83      cd => {
84        artist => { name => "JMJ" }
85      },
86    }},
87    { year => 1977, single_track => undef },
88    { year => 1977, single_track => undef },
89
90  ],
91  'plain 1:1 descending chain ' . $rs->result_class
92;
93
94cmp_deeply
95  [ $rs->search({}, {
96    columns => {
97      'artist'                                  => 'me.artist',
98      'title'                                   => 'me.title',
99      'year'                                    => 'me.year',
100      'single_track.cd.artist.artistid'         => 'artist.artistid',
101      'single_track.cd.artist.cds.cdid'         => 'cds.cdid',
102      'single_track.cd.artist.cds.tracks.title' => 'tracks.title',
103    },
104    join => { single_track => { cd => { artist => { cds => 'tracks' } } } },
105    order_by => [qw/me.cdid artist.artistid cds.cdid tracks.trackid/],
106  })->all ],
107  [
108    {
109      artist => 1, title => "Magnetic Fields", year => 1981, single_track => undef,
110    },
111    {
112      artist => 1, title => "Oxygene", year => 1976, single_track => undef,
113    },
114    {
115      artist => 1, title => "Equinoxe", year => 1978, single_track => {
116        cd => {
117          artist => {
118            artistid => 1, cds => {
119              cdid => 1, tracks => {
120                title => "m1"
121              }
122            }
123          }
124        }
125      },
126    },
127    {
128      artist => 1, title => "Equinoxe", year => 1978, single_track => {
129        cd => {
130          artist => {
131            artistid => 1, cds => {
132              cdid => 1, tracks => {
133                title => "m2"
134              }
135            }
136          }
137        }
138      },
139    },
140    {
141      artist => 1, title => "Equinoxe", year => 1978, single_track => {
142        cd => {
143          artist => {
144            artistid => 1, cds => {
145              cdid => 1, tracks => {
146                title => "m3"
147              }
148            }
149          }
150        }
151      },
152    },
153    {
154      artist => 1, title => "Equinoxe", year => 1978, single_track => {
155        cd => {
156          artist => {
157            artistid => 1, cds => {
158              cdid => 1, tracks => {
159                title => "m4"
160              }
161            }
162          }
163        }
164      },
165    },
166    {
167      artist => 1, title => "Equinoxe", year => 1978, single_track => {
168        cd => {
169          artist => {
170            artistid => 1, cds => {
171              cdid => 2, tracks => {
172                title => "o2"
173              }
174            }
175          }
176        }
177      },
178    },
179    {
180      artist => 1, title => "Equinoxe", year => 1978, single_track => {
181        cd => {
182          artist => {
183            artistid => 1, cds => {
184              cdid => 2, tracks => {
185                title => "o1"
186              }
187            }
188          }
189        }
190      },
191    },
192    {
193      artist => 1, title => "Equinoxe", year => 1978, single_track => {
194        cd => {
195          artist => {
196            artistid => 1, cds => {
197              cdid => 3, tracks => {
198                title => "e1"
199              }
200            }
201          }
202        }
203      },
204    },
205    {
206      artist => 1, title => "Equinoxe", year => 1978, single_track => {
207        cd => {
208          artist => {
209            artistid => 1, cds => {
210              cdid => 3, tracks => {
211                title => "e2"
212              }
213            }
214          }
215        }
216      },
217    },
218    {
219      artist => 1, title => "Equinoxe", year => 1978, single_track => {
220        cd => {
221          artist => {
222            artistid => 1, cds => {
223              cdid => 3, tracks => {
224                title => "e3"
225              }
226            }
227          }
228        }
229      },
230    },
231    {
232      artist => 1, title => "Equinoxe", year => 1978, single_track => {
233        cd => {
234          artist => {
235            artistid => 1, cds => {
236              cdid => 4, tracks => undef
237            }
238          }
239        }
240      },
241    },
242    {
243      artist => 1, title => "Equinoxe", year => 1978, single_track => {
244        cd => {
245          artist => {
246            artistid => 1, cds => {
247              cdid => 5, tracks => undef
248            }
249          }
250        }
251      },
252    },
253    {
254      artist => 1, title => "fuzzy_1", year => 1977, single_track => undef,
255    },
256    {
257      artist => 1, title => "fuzzy_2", year => 1977, single_track => undef,
258    }
259  ],
260  'non-collapsing 1:1:1:M:M chain ' . $rs->result_class,
261;
262
263cmp_deeply
264  [ $rs->search({}, {
265    columns => {
266      'artist'                                  => 'me.artist',
267      'title'                                   => 'me.title',
268      'year'                                    => 'me.year',
269      'single_track.cd.artist.artistid'         => 'artist.artistid',
270      'single_track.cd.artist.cds.cdid'         => 'cds.cdid',
271      'single_track.cd.artist.cds.tracks.title' => 'tracks.title',
272    },
273    join => { single_track => { cd => { artist => { cds => 'tracks' } } } },
274    order_by => [qw/me.cdid artist.artistid cds.cdid tracks.trackid/],
275    collapse => 1,
276  })->all ],
277  [
278    {
279      artist => 1, title => "Magnetic Fields", year => 1981, single_track => undef,
280    },
281    {
282      artist => 1, title => "Oxygene", year => 1976, single_track => undef,
283    },
284    {
285      artist => 1, title => "Equinoxe", year => 1978, single_track => {
286        cd => {
287          artist => {
288            artistid => 1, cds => [
289              {
290                cdid => 1, tracks => [
291                  { title => "m1" },
292                  { title => "m2" },
293                  { title => "m3" },
294                  { title => "m4" },
295                ]
296              },
297              {
298                cdid => 2, tracks => [
299                  { title => "o2" },
300                  { title => "o1" },
301                ]
302              },
303              {
304                cdid => 3, tracks => [
305                  { title => "e1" },
306                  { title => "e2" },
307                  { title => "e3" },
308                ]
309              },
310              {
311                cdid => 4, tracks => [],
312              },
313              {
314                cdid => 5, tracks => [],
315              }
316            ]
317          }
318        }
319      },
320    },
321    {
322      artist => 1, title => "fuzzy_1", year => 1977, single_track => undef,
323    },
324    {
325      artist => 1, title => "fuzzy_2", year => 1977, single_track => undef,
326    }
327  ],
328  'collapsing 1:1:1:M:M chain ' . $rs->result_class,
329;
330
331}
332
333done_testing;
334