Restore ability to handle underdefined root (t/prefetch/incomplete.t)
[dbsrgits/DBIx-Class.git] / t / prefetch / incomplete.t
index 000a386..f8e89de 100644 (file)
@@ -1,17 +1,15 @@
 use strict;
-use warnings;  
+use warnings;
 
 use Test::More;
 use Test::Exception;
 use lib qw(t/lib);
 use DBICTest;
 
-plan tests => 9;
-
 my $schema = DBICTest->init_schema();
 
 lives_ok(sub {
-  # while cds.* will be selected anyway (prefetch currently forces the result of _resolve_prefetch)
+  # while cds.* will be selected anyway (prefetch implies it)
   # only the requested me.name column will be fetched.
 
   # reference sql with select => [...]
@@ -22,7 +20,7 @@ lives_ok(sub {
     {
       prefetch => [ qw/ cds / ],
       order_by => [ { -desc => 'me.name' }, 'cds.title' ],
-      select => [qw/ me.name  cds.title / ],
+      select => [qw/ me.name cds.title / ],
     },
   );
 
@@ -33,7 +31,6 @@ lives_ok(sub {
   is ($we_are_goth->cds->first->title, 'Come Be Depressed With Us', 'Correct cd for artist');
 }, 'explicit prefetch on a keyless object works');
 
-
 lives_ok ( sub {
 
   my $rs = $schema->resultset('CD')->search(
@@ -52,14 +49,14 @@ lives_ok ( sub {
 
   my @cds_and_tracks;
   for my $cd ($rs->all) {
-    my $data->{year} = $cd->year;
+    my $data = { year => $cd->year, cdid => $cd->cdid };
     for my $tr ($cd->tracks->all) {
       push @{$data->{tracks}}, { $tr->get_columns };
     }
     push @cds_and_tracks, $data;
   }
 
-  my $pref_rs = $rs->search ({}, { columns => ['year'], prefetch => 'tracks' });
+  my $pref_rs = $rs->search ({}, { columns => [qw/year cdid/], prefetch => 'tracks' });
 
   my @pref_cds_and_tracks;
   for my $cd ($pref_rs->all) {
@@ -102,3 +99,29 @@ lives_ok(sub {
   is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name');
 
 }, 'implicit keyless prefetch works');
+
+# sane error
+throws_ok(
+  sub {
+    $schema->resultset('Track')->search({}, { join => { cd => 'artist' }, '+columns' => 'artist.name' } )->next;
+  },
+  qr|\QCan't inflate prefetch into non-existent relationship 'artist' from 'Track', check the inflation specification (columns/as) ending in '...artist.name'|,
+  'Sensible error message on mis-specified "as"',
+);
+
+# check complex limiting prefetch without the join-able columns
+{
+  my $pref_rs = $schema->resultset('Owners')->search({}, {
+    rows => 3,
+    offset => 1,
+    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";
+}
+
+
+done_testing;