Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / prefetch / incomplete.t
index 8840c1d..114ccfb 100644 (file)
@@ -1,11 +1,13 @@
+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 DBICTest ':DiffSQL';
 
 my $schema = DBICTest->init_schema();
 
@@ -54,6 +56,7 @@ lives_ok ( sub {
     for my $tr ($cd->tracks->all) {
       push @{$data->{tracks}}, { $tr->get_columns };
     }
+    @{$data->{tracks}} = sort { $a->{trackid} <=> $b->{trackid} } @{$data->{tracks}};
     push @cds_and_tracks, $data;
   }
 
@@ -65,6 +68,7 @@ lives_ok ( sub {
     for my $tr ($cd->tracks->all) {
       push @{$data->{tracks}}, { $tr->get_columns };
     }
+    @{$data->{tracks}} = sort { $a->{trackid} <=> $b->{trackid} } @{$data->{tracks}};
     push @pref_cds_and_tracks, $data;
   }
 
@@ -75,7 +79,7 @@ lives_ok ( sub {
   );
 
   cmp_deeply (
-    [ $pref_rs->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' })->all ],
+    $pref_rs->search ({}, { order_by => [ { -desc => 'me.year' }, 'trackid' ] })->all_hri,
     \@cds_and_tracks,
     'Correct HRI collapsing on non-unique primary object'
   );
@@ -115,13 +119,112 @@ throws_ok(
   my $pref_rs = $schema->resultset('Owners')->search({}, {
     rows => 3,
     offset => 1,
+    order_by => 'name',
     columns => 'name',  # only the owner name, still prefetch all the books
     prefetch => 'books',
   });
 
-  lives_ok {
-    is ($pref_rs->all, 1, 'Expected count of objects on limtied prefetch')
-  } "Complex limited prefetch works with non-selected join condition";
+  is_same_sql_bind(
+    $pref_rs->as_query,
+    '(
+      SELECT me.name, books.id, books.source, books.owner, books.title, books.price
+        FROM (
+          SELECT me.name, me.id
+            FROM owners me
+          ORDER BY name
+          LIMIT ?
+          OFFSET ?
+        ) me
+        LEFT JOIN books books
+          ON books.owner = me.id
+      ORDER BY name
+    )',
+    [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
+    'Expected SQL on complex limited prefetch with non-selected join condition',
+  );
+
+  is_deeply (
+    $pref_rs->all_hri,
+    [ {
+      name => "Waltham",
+      books => [ {
+        id => 3,
+        owner => 2,
+        price => 65,
+        source => "Library",
+        title => "Best Recipe Cookbook",
+      } ],
+    } ],
+    'Expected result on complex limited prefetch with non-selected join condition'
+  );
+
+  my $empty_ordered_pref_rs = $pref_rs->search({}, {
+    columns => [],  # nothing, we only prefetch the book data
+    order_by => 'me.name',
+  });
+  my $empty_ordered_pref_hri = [ {
+    books => [ {
+      id => 3,
+      owner => 2,
+      price => 65,
+      source => "Library",
+      title => "Best Recipe Cookbook",
+    } ],
+  } ];
+
+  is_same_sql_bind(
+    $empty_ordered_pref_rs->as_query,
+    '(
+      SELECT books.id, books.source, books.owner, books.title, books.price
+        FROM (
+          SELECT me.id, me.name
+            FROM owners me
+          ORDER BY me.name
+          LIMIT ?
+          OFFSET ?
+        ) me
+        LEFT JOIN books books
+          ON books.owner = me.id
+      ORDER BY me.name
+    )',
+    [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
+    'Expected SQL on *ordered* complex limited prefetch with non-selected root data',
+  );
+
+  is_deeply (
+    $empty_ordered_pref_rs->all_hri,
+    $empty_ordered_pref_hri,
+    'Expected result on *ordered* complex limited prefetch with non-selected root data'
+  );
+
+  $empty_ordered_pref_rs = $empty_ordered_pref_rs->search({}, {
+    order_by => [ \ 'LENGTH(me.name)', \ 'RANDOM()' ],
+  });
+
+  is_same_sql_bind(
+    $empty_ordered_pref_rs->as_query,
+    '(
+      SELECT books.id, books.source, books.owner, books.title, books.price
+        FROM (
+          SELECT me.id, me.name
+            FROM owners me
+          ORDER BY LENGTH(me.name), RANDOM()
+          LIMIT ?
+          OFFSET ?
+        ) me
+        LEFT JOIN books books
+          ON books.owner = me.id
+      ORDER BY LENGTH(me.name), RANDOM()
+    )',
+    [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
+    'Expected SQL on *function-ordered* complex limited prefetch with non-selected root data',
+  );
+
+  is_deeply (
+    $empty_ordered_pref_rs->all_hri,
+    $empty_ordered_pref_hri,
+    'Expected result on *function-ordered* complex limited prefetch with non-selected root data'
+  );
 }