Introducing DBIx::Class::Schema::SanityChecker
[dbsrgits/DBIx-Class.git] / t / prefetch / multiple_hasmany_torture.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
9f6555d3 3use strict;
4use warnings;
5
6use Test::More;
52864fbd 7use Test::Deep;
9f6555d3 8use Test::Exception;
c0329273 9
9f6555d3 10use DBICTest;
b7dc8a5c 11use DBIx::Class::_Util 'sigwarn_silencer';
9f6555d3 12
13my $schema = DBICTest->init_schema();
14
9f6555d3 15$schema->resultset('Artist')->create(
3d3e99db 16 {
17 name => 'mo',
18 rank => '1337',
19 cds => [
20 {
21 title => 'Song of a Foo',
22 year => '1999',
23 tracks => [
24 { title => 'Foo Me Baby One More Time' },
25 { title => 'Foo Me Baby One More Time II' },
26 { title => 'Foo Me Baby One More Time III' },
27 { title => 'Foo Me Baby One More Time IV', cd_single => {
28 artist => 1, title => 'MO! Single', year => 2021, tracks => [
29 { title => 'singled out' },
30 { title => 'still alone' },
31 ]
32 } }
9f6555d3 33 ],
3d3e99db 34 cd_to_producer => [
35 { producer => { name => 'riba' } },
36 { producer => { name => 'sushi' } },
37 ]
38 },
39 {
40 title => 'Song of a Foo II',
41 year => '2002',
42 tracks => [
43 { title => 'Quit Playing Games With My Heart' },
44 { title => 'Bar Foo' },
45 { title => 'Foo Bar', cd_single => {
46 artist => 2, title => 'MO! Single', year => 2020, tracks => [
47 { title => 'singled out' },
48 { title => 'still alone' },
49 ]
50 } }
51 ],
52 cd_to_producer => [
53 { producer => { name => 'riba' } },
54 { producer => { name => 'sushi' } },
55 ],
56 }
57 ],
58 artwork_to_artist => [
fb88ca2c 59 { artwork => { cd_id => 1 } },
3d3e99db 60 { artwork => { cd_id => 2 } }
61 ]
62 }
9f6555d3 63);
64
3506ed68 65my $artist_with_extras = {
3d3e99db 66 artistid => 4, charfield => undef, name => 'mo', rank => 1337,
67 artwork_to_artist => [
68 { artist_id => 4, artwork_cd_id => 1, artwork => { cd_id => 1 } },
69 { artist_id => 4, artwork_cd_id => 2, artwork => { cd_id => 2 } },
70 ],
71 cds => [
9f6555d3 72 {
3d3e99db 73 artist => 4, cdid => 6, title => 'Song of a Foo', genreid => undef, year => 1999, single_track => undef,
74 cd_to_producer => [
75 { attribute => undef, cd => 6, producer => { name => 'riba', producerid => 4 } },
76 { attribute => undef, cd => 6, producer => { name => 'sushi', producerid => 5 } },
77 ],
78 tracks => [
79 { cd => 6, position => 1, trackid => 19, title => 'Foo Me Baby One More Time', last_updated_on => undef, last_updated_at => undef, cd_single => undef },
80 { 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 },
81 { 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 },
82 { cd => 6, position => 4, trackid => 22, title => 'Foo Me Baby One More Time IV', last_updated_on => undef, last_updated_at => undef, cd_single => {
83 single_track => 22, artist => 1, cdid => 7, title => 'MO! Single', genreid => undef, year => 2021, tracks => [
84 { cd => 7, position => 1, title => 'singled out', trackid => '23', last_updated_at => undef, last_updated_on => undef },
85 { cd => 7, position => 2, title => 'still alone', trackid => '24', last_updated_at => undef, last_updated_on => undef },
86 ],
87 } }
88 ],
89 },
90 {
91 artist => 4, cdid => 8, title => 'Song of a Foo II', genreid => undef, year => 2002, single_track => undef,
92 cd_to_producer => [
93 { attribute => undef, cd => 8, producer => { name => 'riba', producerid => 4 } },
94 { attribute => undef, cd => 8, producer => { name => 'sushi', producerid => 5 } },
95 ],
96 tracks => [
97 { cd => 8, position => 1, trackid => 25, title => 'Quit Playing Games With My Heart', last_updated_on => undef, last_updated_at => undef, cd_single => undef },
98 { cd => 8, position => 2, trackid => 26, title => 'Bar Foo', last_updated_on => undef, last_updated_at => undef, cd_single => undef },
99 { cd => 8, position => 3, trackid => 27, title => 'Foo Bar', last_updated_on => undef, last_updated_at => undef, cd_single => {
100 single_track => 27, artist => 2, cdid => 9, title => 'MO! Single', genreid => undef, year => 2020, tracks => [
101 { cd => 9, position => 1, title => 'singled out', trackid => '28', last_updated_at => undef, last_updated_on => undef },
102 { cd => 9, position => 2, title => 'still alone', trackid => '29', last_updated_at => undef, last_updated_on => undef },
103 ],
104 } }
105 ],
9f6555d3 106 }
3d3e99db 107 ],
3506ed68 108};
109
110my $art_rs = $schema->resultset('Artist')->search({ 'me.artistid' => 4 });
111
112
113my $art_rs_prefetch = $art_rs->search({}, {
114 order_by => [qw/tracks.position tracks.trackid producer.producerid tracks_2.trackid artwork.cd_id/],
115 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
116 prefetch => [
117 {
118 cds => [
119 { tracks => { cd_single => 'tracks' } },
120 { cd_to_producer => 'producer' }
121 ]
122 },
123 { artwork_to_artist => 'artwork' }
124 ],
3d3e99db 125});
9f6555d3 126
3506ed68 127cmp_deeply( $art_rs_prefetch->next, $artist_with_extras );
128
129
b7dc8a5c 130for my $order (
131 [ [qw( cds.cdid tracks.position )] ],
132
133 [ [qw( artistid tracks.cd tracks.position )],
134 'we need to proxy the knowledge from the collapser that tracks.cd is a stable sorter for CDs' ],
135) {
136
137 my $cds_rs_prefetch = $art_rs->related_resultset('cds')->search({}, {
138 order_by => [ $order->[0], qw(producer.name tracks_2.position) ],
139 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
140 prefetch => [
141 { tracks => { cd_single => 'tracks' } },
142 { cd_to_producer => 'producer' },
143 ],
144 });
145
146 local $SIG{__WARN__} = sigwarn_silencer(qr/Unable to properly collapse has_many results/) if $order->[1];
147
148 cmp_deeply( $cds_rs_prefetch->next, $artist_with_extras->{cds}[0], '1st cd structure matches' );
149 cmp_deeply( $cds_rs_prefetch->next, $artist_with_extras->{cds}[1], '2nd cd structure matches' );
150
151 # INTERNALS! (a.k.a boars, gore and whores) DO NOT CARGOCULT!!!
152 local $TODO = $order->[1] if $order->[1];
153 ok( $cds_rs_prefetch->_resolved_attrs->{_ordered_for_collapse}, 'ordered_for_collapse detected properly' );
154}
155
156
9f6555d3 157done_testing;