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