Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / prefetch / multiple_hasmany_torture.t
index b524aa9..cd503dd 100644 (file)
@@ -1,33 +1,17 @@
+BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
+
 use strict;
 use warnings;
 
 use Test::More;
 use Test::Deep;
 use Test::Exception;
-use lib qw(t/lib);
+
 use DBICTest;
+use DBIx::Class::_Util 'sigwarn_silencer';
 
 my $schema = DBICTest->init_schema();
 
-my $mo_rs = $schema->resultset('Artist')->search(
-  { 'me.artistid' => 4 },
-  {
-    prefetch   => [
-      {
-        cds => [
-          { tracks     => { cd_single => 'tracks' } },
-          { cd_to_producer => 'producer' }
-        ]
-      },
-      { artwork_to_artist => 'artwork' }
-    ],
-
-    result_class => 'DBIx::Class::ResultClass::HashRefInflator',
-
-    order_by => [qw/tracks.position tracks.trackid producer.producerid tracks_2.trackid artwork.cd_id/],
-  }
-);
-
 $schema->resultset('Artist')->create(
   {
     name => 'mo',
@@ -78,11 +62,7 @@ $schema->resultset('Artist')->create(
   }
 );
 
-my $mo = $mo_rs->next;
-
-is( @{$mo->{cds}}, 2, 'two CDs' );
-
-cmp_deeply( $mo, {
+my $artist_with_extras = {
   artistid => 4, charfield => undef, name => 'mo', rank => 1337,
   artwork_to_artist => [
     { artist_id => 4, artwork_cd_id => 1, artwork => { cd_id => 1 } },
@@ -125,6 +105,53 @@ cmp_deeply( $mo, {
       ],
     }
   ],
+};
+
+my $art_rs = $schema->resultset('Artist')->search({ 'me.artistid' => 4 });
+
+
+my $art_rs_prefetch = $art_rs->search({}, {
+  order_by => [qw/tracks.position tracks.trackid producer.producerid tracks_2.trackid artwork.cd_id/],
+  result_class => 'DBIx::Class::ResultClass::HashRefInflator',
+  prefetch => [
+    {
+      cds => [
+        { tracks => { cd_single => 'tracks' } },
+        { cd_to_producer => 'producer' }
+      ]
+    },
+    { artwork_to_artist => 'artwork' }
+  ],
 });
 
+cmp_deeply( $art_rs_prefetch->next, $artist_with_extras );
+
+
+for my $order (
+  [ [qw( cds.cdid tracks.position )] ],
+
+  [ [qw( artistid tracks.cd tracks.position )],
+    'we need to proxy the knowledge from the collapser that tracks.cd is a stable sorter for CDs' ],
+) {
+
+  my $cds_rs_prefetch = $art_rs->related_resultset('cds')->search({}, {
+    order_by => [ $order->[0], qw(producer.name tracks_2.position) ],
+    result_class => 'DBIx::Class::ResultClass::HashRefInflator',
+    prefetch => [
+      { tracks => { cd_single => 'tracks' } },
+      { cd_to_producer => 'producer' },
+    ],
+  });
+
+  local $SIG{__WARN__} = sigwarn_silencer(qr/Unable to properly collapse has_many results/) if $order->[1];
+
+  cmp_deeply( $cds_rs_prefetch->next, $artist_with_extras->{cds}[0], '1st cd structure matches' );
+  cmp_deeply( $cds_rs_prefetch->next, $artist_with_extras->{cds}[1], '2nd cd structure matches' );
+
+  # INTERNALS! (a.k.a boars, gore and whores) DO NOT CARGOCULT!!!
+  local $TODO = $order->[1] if $order->[1];
+  ok( $cds_rs_prefetch->_resolved_attrs->{_ordered_for_collapse}, 'ordered_for_collapse detected properly' );
+}
+
+
 done_testing;