Fix tests failing due to unspecified resultset retrieval order
[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 tracks_2.trackid artwork.cd_id/],
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           { title  => 'Foo Me Baby One More Time' },
41           { title  => 'Foo Me Baby One More Time II' },
42           { title  => 'Foo Me Baby One More Time III' },
43           { title  => 'Foo Me Baby One More Time IV', cd_single => {
44             artist => 1, title => 'MO! Single', year => 2021, tracks => [
45               { title => 'singled out' },
46               { title => 'still alone' },
47             ]
48           } }
49         ],
50         cd_to_producer => [
51           { producer => { name => 'riba' } },
52           { producer => { name => 'sushi' } },
53         ]
54       },
55       {
56         title  => 'Song of a Foo II',
57         year   => '2002',
58         tracks => [
59           { title  => 'Quit Playing Games With My Heart' },
60           { title  => 'Bar Foo' },
61           { title  => 'Foo Bar', cd_single => {
62             artist => 2, title => 'MO! Single', year => 2020, tracks => [
63               { title => 'singled out' },
64               { title => 'still alone' },
65             ]
66           } }
67         ],
68         cd_to_producer => [
69           { producer => { name => 'riba' } },
70           { producer => { name => 'sushi' } },
71         ],
72       }
73     ],
74     artwork_to_artist => [
75       { artwork => { cd_id => 1 } },
76       { artwork => { cd_id => 2 } }
77     ]
78   }
79 );
80
81 my $mo = $mo_rs->next;
82
83 is( @{$mo->{cds}}, 2, 'two CDs' );
84
85 cmp_deeply( $mo, {
86   artistid => 4, charfield => undef, name => 'mo', rank => 1337,
87   artwork_to_artist => [
88     { artist_id => 4, artwork_cd_id => 1, artwork => { cd_id => 1 } },
89     { artist_id => 4, artwork_cd_id => 2, artwork => { cd_id => 2 } },
90   ],
91   cds => [
92     {
93       artist => 4, cdid => 6, title => 'Song of a Foo', genreid => undef, year => 1999, single_track => undef,
94       cd_to_producer => [
95         { attribute => undef, cd => 6, producer => { name => 'riba', producerid => 4 } },
96         { attribute => undef, cd => 6, producer => { name => 'sushi', producerid => 5 } },
97       ],
98       tracks => [
99         { cd => 6, position => 1, trackid => 19, title => 'Foo Me Baby One More Time', last_updated_on => undef, last_updated_at => undef, cd_single => undef },
100         { 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 },
101         { 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 },
102         { cd => 6, position => 4, trackid => 22, title => 'Foo Me Baby One More Time IV', last_updated_on => undef, last_updated_at => undef, cd_single => {
103           single_track => 22, artist => 1, cdid => 7, title => 'MO! Single', genreid => undef, year => 2021, tracks => [
104             { cd => 7, position => 1, title => 'singled out', trackid => '23', last_updated_at => undef, last_updated_on => undef },
105             { cd => 7, position => 2, title => 'still alone', trackid => '24', last_updated_at => undef, last_updated_on => undef },
106           ],
107         } }
108       ],
109     },
110     {
111       artist => 4, cdid => 8, title => 'Song of a Foo II', genreid => undef, year => 2002, single_track => undef,
112       cd_to_producer => [
113         { attribute => undef, cd => 8, producer => { name => 'riba', producerid => 4 } },
114         { attribute => undef, cd => 8, producer => { name => 'sushi', producerid => 5 } },
115       ],
116       tracks => [
117         { cd => 8, position => 1, trackid => 25, title => 'Quit Playing Games With My Heart', last_updated_on => undef, last_updated_at => undef, cd_single => undef },
118         { cd => 8, position => 2, trackid => 26, title => 'Bar Foo', last_updated_on => undef, last_updated_at => undef, cd_single => undef },
119         { cd => 8, position => 3, trackid => 27, title => 'Foo Bar', last_updated_on => undef, last_updated_at => undef, cd_single => {
120           single_track => 27, artist => 2, cdid => 9, title => 'MO! Single', genreid => undef, year => 2020, tracks => [
121             { cd => 9, position => 1, title => 'singled out', trackid => '28', last_updated_at => undef, last_updated_on => undef },
122             { cd => 9, position => 2, title => 'still alone', trackid => '29', last_updated_at => undef, last_updated_on => undef },
123           ],
124         } }
125       ],
126     }
127   ],
128 });
129
130 done_testing;