e0b73b5ada3f617c4977c817cdd54f2c423526e5
[dbsrgits/DBIx-Class.git] / t / prefetch / multiple_hasmany_torture.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Deep;
6 use Test::Exception;
7 use lib qw(t/lib);
8 use DBICTest;
9
10 my $schema = DBICTest->init_schema();
11
12 my $mo_rs = $schema->resultset('Artist')->search(
13     { 'me.artistid' => 4 },
14     {
15         prefetch     => [
16             {
17                 cds => [
18                     { tracks         => { cd_single => 'tracks' } },
19                     { cd_to_producer => 'producer' }
20                 ]
21             },
22             { artwork_to_artist => 'artwork' }
23         ],
24
25         result_class => 'DBIx::Class::ResultClass::HashRefInflator',
26
27         order_by => [qw/tracks.position tracks.trackid producer.producerid/],
28     }
29 );
30
31 $schema->resultset('Artist')->create(
32     {
33         name => 'mo',
34         rank => '1337',
35         cds  => [
36             {
37                 title  => 'Song of a Foo',
38                 year   => '1999',
39                 tracks => [
40                     {
41                         title    => 'Foo Me Baby One More Time',
42                     },
43                     {
44                         title    => 'Foo Me Baby One More Time II',
45                     },
46                     {
47                         title    => 'Foo Me Baby One More Time III',
48                     },
49                     {
50                         title    => 'Foo Me Baby One More Time IV',
51                         cd_single =>
52                           { artist => 1, title => 'MO! Single', year => 2021, tracks => [
53                             { title => 'singled out' }, { title => 'still alone' },
54                           ] },
55                     }
56                 ],
57                 cd_to_producer => [
58                     { producer => { name => 'riba' } },
59                     { producer => { name => 'sushi' } },
60                 ]
61             },
62             {
63                 title  => 'Song of a Foo II',
64                 year   => '2002',
65                 tracks => [
66                     {
67                         title    => 'Quit Playing Games With My Heart',
68                     },
69                     {
70                         title    => 'Bar Foo',
71                     },
72                     {
73                         title    => 'Foo Bar',
74                         cd_single =>
75                           { artist => 2, title => 'MO! Single', year => 2020, tracks => [
76                             { title => 'singled out' }, { title => 'still alone' },
77                           ] },
78                     }
79                 ],
80                 cd_to_producer => [
81                   { producer => { name => 'riba' } },
82                   { producer => { name => 'sushi' } },
83                 ],
84             }
85         ],
86         artwork_to_artist =>
87           [ { artwork => { cd_id => 1 } }, { artwork => { cd_id => 2 } } ]
88     }
89 );
90
91 my $mo = $mo_rs->next;
92
93 is( @{$mo->{cds}}, 2, 'two CDs' );
94
95 cmp_deeply(
96     $mo,
97     {
98         'cds' => [
99             {
100                 'single_track' => undef,
101                 'tracks'       => [
102                     {
103                         'cd'        => '6',
104                         'position'  => '1',
105                         'trackid'   => '19',
106                         'title'     => 'Foo Me Baby One More Time',
107                         'cd_single' => undef,
108                         'last_updated_on' => undef,
109                         'last_updated_at' => undef
110                     },
111                     {
112                         'cd'              => '6',
113                         'position'        => '2',
114                         'trackid'         => '20',
115                         'title'           => 'Foo Me Baby One More Time II',
116                         'cd_single'       => undef,
117                         'last_updated_on' => undef,
118                         'last_updated_at' => undef
119                     },
120                     {
121                         'cd'              => '6',
122                         'position'        => '3',
123                         'trackid'         => '21',
124                         'title'           => 'Foo Me Baby One More Time III',
125                         'cd_single'       => undef,
126                         'last_updated_on' => undef,
127                         'last_updated_at' => undef
128                     },
129                     {
130                         'cd'              => '6',
131                         'position'        => '4',
132                         'trackid'         => '22',
133                         'title'           => 'Foo Me Baby One More Time IV',
134                         'last_updated_on' => undef,
135                         'last_updated_at' => undef,
136                         'cd_single' => {
137                             'single_track' => '22',
138                             'artist'       => '1',
139                             'cdid'         => '7',
140                             'title'        => 'MO! Single',
141                             'genreid'      => undef,
142                             'year'         => '2021',
143                             'tracks'       => [
144                                 {
145                                     'cd' => '7',
146                                     'position' => '1',
147                                     'title' => 'singled out',
148                                     'trackid' => '23',
149                                     'last_updated_at' => undef,
150                                     'last_updated_on' => undef
151                                 },
152                                 {
153                                     'cd' => '7',
154                                     'position' => '2',
155                                     'title' => 'still alone',
156                                     'trackid' => '24',
157                                     'last_updated_at' => undef,
158                                     'last_updated_on' => undef
159                                 },
160                             ],
161                         },
162                     }
163                 ],
164                 'artist'         => '4',
165                 'cdid'           => '6',
166                 'cd_to_producer' => [
167                     {
168                         'attribute' => undef,
169                         'cd'        => '6',
170                         'producer'  => {
171                             'name'       => 'riba',
172                             'producerid' => '4'
173                         }
174                     },
175                     {
176                         'attribute' => undef,
177                         'cd'        => '6',
178                         'producer'  => {
179                             'name'       => 'sushi',
180                             'producerid' => '5'
181                         }
182                     }
183                 ],
184                 'title'   => 'Song of a Foo',
185                 'genreid' => undef,
186                 'year'    => '1999'
187             },
188             {
189                 'single_track' => undef,
190                 'tracks'       => [
191                     {
192                         'cd'        => '8',
193                         'position'  => '1',
194                         'trackid'   => '25',
195                         'title'     => 'Quit Playing Games With My Heart',
196                         'last_updated_on' => undef,
197                         'last_updated_at' => undef,
198                         'cd_single'       => undef,
199                     },
200                     {
201                         'cd'              => '8',
202                         'position'        => '2',
203                         'trackid'         => '26',
204                         'title'           => 'Bar Foo',
205                         'cd_single'       => undef,
206                         'last_updated_on' => undef,
207                         'last_updated_at' => undef
208                     },
209                     {
210                         'cd'              => '8',
211                         'position'        => '3',
212                         'trackid'         => '27',
213                         'title'           => 'Foo Bar',
214                         'last_updated_on' => undef,
215                         'last_updated_at' => undef,
216                         'cd_single' => {
217                             'single_track' => '27',
218                             'artist'       => '2',
219                             'cdid'         => '9',
220                             'title'        => 'MO! Single',
221                             'genreid'      => undef,
222                             'year'         => '2020',
223                             'tracks'       => [
224                                 {
225                                     'cd' => '9',
226                                     'position' => '1',
227                                     'title' => 'singled out',
228                                     'trackid' => '28',
229                                     'last_updated_at' => undef,
230                                     'last_updated_on' => undef
231                                 },
232                                 {
233                                     'cd' => '9',
234                                     'position' => '2',
235                                     'title' => 'still alone',
236                                     'trackid' => '29',
237                                     'last_updated_at' => undef,
238                                     'last_updated_on' => undef
239                                 },
240                             ],
241
242                           },
243                       },
244                 ],
245                 'artist'         => '4',
246                 'cdid'           => '8',
247                 'cd_to_producer' => [
248                     {
249                         'attribute' => undef,
250                         'cd'        => '8',
251                         'producer'  => {
252                             'name'       => 'riba',
253                             'producerid' => '4'
254                         }
255                     },
256                     {
257                         'attribute' => undef,
258                         'cd'        => '8',
259                         'producer'  => {
260                             'name'       => 'sushi',
261                             'producerid' => '5'
262                         }
263                     }
264                 ],
265                 'title'   => 'Song of a Foo II',
266                 'genreid' => undef,
267                 'year'    => '2002'
268             }
269         ],
270         'artistid'          => '4',
271         'charfield'         => undef,
272         'name'              => 'mo',
273         'artwork_to_artist' => [
274             {
275                 'artwork'       => { 'cd_id' => '1' },
276                 'artist_id'     => '4',
277                 'artwork_cd_id' => '1'
278             },
279             {
280                 'artwork'       => { 'cd_id' => '2' },
281                 'artist_id'     => '4',
282                 'artwork_cd_id' => '2'
283             }
284         ],
285         'rank' => '1337'
286     }
287 );
288
289 done_testing;