1623937df199459102ab57bcbdae87a3f2440748
[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 $schema->resultset('Artist')->create(
13   {
14     name => 'mo',
15     rank => '1337',
16     cds  => [
17       {
18         title  => 'Song of a Foo',
19         year   => '1999',
20         tracks => [
21           { title  => 'Foo Me Baby One More Time' },
22           { title  => 'Foo Me Baby One More Time II' },
23           { title  => 'Foo Me Baby One More Time III' },
24           { title  => 'Foo Me Baby One More Time IV', cd_single => {
25             artist => 1, title => 'MO! Single', year => 2021, tracks => [
26               { title => 'singled out' },
27               { title => 'still alone' },
28             ]
29           } }
30         ],
31         cd_to_producer => [
32           { producer => { name => 'riba' } },
33           { producer => { name => 'sushi' } },
34         ]
35       },
36       {
37         title  => 'Song of a Foo II',
38         year   => '2002',
39         tracks => [
40           { title  => 'Quit Playing Games With My Heart' },
41           { title  => 'Bar Foo' },
42           { title  => 'Foo Bar', cd_single => {
43             artist => 2, title => 'MO! Single', year => 2020, tracks => [
44               { title => 'singled out' },
45               { title => 'still alone' },
46             ]
47           } }
48         ],
49         cd_to_producer => [
50           { producer => { name => 'riba' } },
51           { producer => { name => 'sushi' } },
52         ],
53       }
54     ],
55     artwork_to_artist => [
56       { artwork => { cd_id => 1 } },
57       { artwork => { cd_id => 2 } }
58     ]
59   }
60 );
61
62 my $artist_with_extras = {
63   artistid => 4, charfield => undef, name => 'mo', rank => 1337,
64   artwork_to_artist => [
65     { artist_id => 4, artwork_cd_id => 1, artwork => { cd_id => 1 } },
66     { artist_id => 4, artwork_cd_id => 2, artwork => { cd_id => 2 } },
67   ],
68   cds => [
69     {
70       artist => 4, cdid => 6, title => 'Song of a Foo', genreid => undef, year => 1999, single_track => undef,
71       cd_to_producer => [
72         { attribute => undef, cd => 6, producer => { name => 'riba', producerid => 4 } },
73         { attribute => undef, cd => 6, producer => { name => 'sushi', producerid => 5 } },
74       ],
75       tracks => [
76         { cd => 6, position => 1, trackid => 19, title => 'Foo Me Baby One More Time', last_updated_on => undef, last_updated_at => undef, cd_single => undef },
77         { cd => 6, position => 2, trackid => 20, title => 'Foo Me Baby One More Time II', last_updated_on => undef, last_updated_at => undef, cd_single => undef },
78         { cd => 6, position => 3, trackid => 21, title => 'Foo Me Baby One More Time III', last_updated_on => undef, last_updated_at => undef, cd_single => undef },
79         { cd => 6, position => 4, trackid => 22, title => 'Foo Me Baby One More Time IV', last_updated_on => undef, last_updated_at => undef, cd_single => {
80           single_track => 22, artist => 1, cdid => 7, title => 'MO! Single', genreid => undef, year => 2021, tracks => [
81             { cd => 7, position => 1, title => 'singled out', trackid => '23', last_updated_at => undef, last_updated_on => undef },
82             { cd => 7, position => 2, title => 'still alone', trackid => '24', last_updated_at => undef, last_updated_on => undef },
83           ],
84         } }
85       ],
86     },
87     {
88       artist => 4, cdid => 8, title => 'Song of a Foo II', genreid => undef, year => 2002, single_track => undef,
89       cd_to_producer => [
90         { attribute => undef, cd => 8, producer => { name => 'riba', producerid => 4 } },
91         { attribute => undef, cd => 8, producer => { name => 'sushi', producerid => 5 } },
92       ],
93       tracks => [
94         { cd => 8, position => 1, trackid => 25, title => 'Quit Playing Games With My Heart', last_updated_on => undef, last_updated_at => undef, cd_single => undef },
95         { cd => 8, position => 2, trackid => 26, title => 'Bar Foo', last_updated_on => undef, last_updated_at => undef, cd_single => undef },
96         { cd => 8, position => 3, trackid => 27, title => 'Foo Bar', last_updated_on => undef, last_updated_at => undef, cd_single => {
97           single_track => 27, artist => 2, cdid => 9, title => 'MO! Single', genreid => undef, year => 2020, tracks => [
98             { cd => 9, position => 1, title => 'singled out', trackid => '28', last_updated_at => undef, last_updated_on => undef },
99             { cd => 9, position => 2, title => 'still alone', trackid => '29', last_updated_at => undef, last_updated_on => undef },
100           ],
101         } }
102       ],
103     }
104   ],
105 };
106
107 my $art_rs = $schema->resultset('Artist')->search({ 'me.artistid' => 4 });
108
109
110 my $art_rs_prefetch = $art_rs->search({}, {
111   order_by => [qw/tracks.position tracks.trackid producer.producerid tracks_2.trackid artwork.cd_id/],
112   result_class => 'DBIx::Class::ResultClass::HashRefInflator',
113   prefetch => [
114     {
115       cds => [
116         { tracks => { cd_single => 'tracks' } },
117         { cd_to_producer => 'producer' }
118       ]
119     },
120     { artwork_to_artist => 'artwork' }
121   ],
122 });
123
124 cmp_deeply( $art_rs_prefetch->next, $artist_with_extras );
125
126
127 done_testing;