1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
11 use DBIx::Class::_Util 'sigwarn_silencer';
13 my $schema = DBICTest->init_schema();
15 $schema->resultset('Artist')->create(
21 title => 'Song of a Foo',
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' },
35 { producer => { name => 'riba' } },
36 { producer => { name => 'sushi' } },
40 title => 'Song of a Foo II',
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' },
53 { producer => { name => 'riba' } },
54 { producer => { name => 'sushi' } },
58 artwork_to_artist => [
59 { artwork => { cd_id => 1 } },
60 { artwork => { cd_id => 2 } }
65 my $artist_with_extras = {
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 } },
73 artist => 4, cdid => 6, title => 'Song of a Foo', genreid => undef, year => 1999, single_track => undef,
75 { attribute => undef, cd => 6, producer => { name => 'riba', producerid => 4 } },
76 { attribute => undef, cd => 6, producer => { name => 'sushi', producerid => 5 } },
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 },
91 artist => 4, cdid => 8, title => 'Song of a Foo II', genreid => undef, year => 2002, single_track => undef,
93 { attribute => undef, cd => 8, producer => { name => 'riba', producerid => 4 } },
94 { attribute => undef, cd => 8, producer => { name => 'sushi', producerid => 5 } },
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 },
110 my $art_rs = $schema->resultset('Artist')->search({ 'me.artistid' => 4 });
113 my $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',
119 { tracks => { cd_single => 'tracks' } },
120 { cd_to_producer => 'producer' }
123 { artwork_to_artist => 'artwork' }
127 cmp_deeply( $art_rs_prefetch->next, $artist_with_extras );
131 [ [qw( cds.cdid tracks.position )] ],
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' ],
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',
141 { tracks => { cd_single => 'tracks' } },
142 { cd_to_producer => 'producer' },
146 local $SIG{__WARN__} = sigwarn_silencer(qr/Unable to properly collapse has_many results/) if $order->[1];
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' );
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' );