put last change in trunk
[dbsrgits/DBIx-Class.git] / t / run / 16joins.tl
index 0f59ab7..a3c9383 100644 (file)
@@ -7,7 +7,7 @@ BEGIN {
     eval "use DBD::SQLite";
     plan $@
         ? ( skip_all => 'needs DBD::SQLite for testing' )
-        : ( tests => 27 );
+        : ( tests => 41 );
 }
 
 # test the abstract join => SQL generator
@@ -132,6 +132,15 @@ $trace->close;
 unlink 't/var/dbic.trace';
 is($selects, 1, 'prefetch ran only 1 select statement');
 
+# test for partial prefetch via cols attr
+my $cd = $schema->resultset('CD')->find(1,
+    {
+      cols => [qw/title artist.name/], 
+      join => 'artist'
+    }
+);
+ok(eval { $cd->artist->name eq 'Caterwauler McCrae' }, 'single related column prefetched');
+
 # start test for nested prefetch SELECT count
 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
 DBI->trace(1, 't/var/dbic.trace');
@@ -151,8 +160,8 @@ is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch
 
 # count the SELECTs
 DBI->trace(0, undef);
-my $selects = 0;
-my $trace = IO::File->new('t/var/dbic.trace', '<') 
+$selects = 0;
+$trace = IO::File->new('t/var/dbic.trace', '<') 
     or die "Unable to read trace file";
 while (<$trace>) {
     $selects++ if /SELECT(?!.*WHERE 1=0.*)/;
@@ -161,6 +170,26 @@ $trace->close;
 unlink 't/var/dbic.trace';
 is($selects, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
 
+# start test for prefetch on find SELECT count
+unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
+DBI->trace(1, 't/var/dbic.trace');
+
+$cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
+
+is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
+
+# count the SELECTs
+DBI->trace(0, undef);
+$selects = 0;
+$trace = IO::File->new('t/var/dbic.trace', '<') 
+    or die "Unable to read trace file";
+while (<$trace>) {
+    $selects++ if /SELECT(?!.*WHERE 1=0.*)/;
+}
+$trace->close;
+unlink 't/var/dbic.trace';
+is($selects, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
+
 $rs = $schema->resultset('Tag')->search(
   {},
   {
@@ -188,6 +217,53 @@ my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
 
 cmp_ok( @artists, '==', 2, "two-join search ok" );
 
-}
+$rs = $schema->resultset("CD")->search(
+  {},
+  { group_by => [qw/ title me.cdid /] }
+);
+
+cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
+
+cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
+
+$rs = $schema->resultset("CD")->search(
+  {},
+  { join => [qw/ artist /], group_by => [qw/ artist.name /] }
+);
+
+cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
+
+cmp_ok( scalar $rs->all, '==', 3, "all() returns same count as count() after group_by on related column" );
+
+$rs = $schema->resultset("Artist")->search(
+        { 'cds.title' => 'Spoonful of bees',
+          'cds_2.title' => 'Forkful of bees' },
+        { join => [ 'cds', 'cds' ] });
+
+cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
+is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
+
+my $queries;
+$schema->storage->debugcb(sub { $queries++ });
+$schema->storage->debug(1);
+
+my $tree_like =
+     $schema->resultset('TreeLike')->find(4,
+       { join     => { parent => { parent => 'parent' } },
+         prefetch => { parent => { parent => 'parent' } } });
+
+is($tree_like->name, 'quux', 'Bottom of tree ok');
+$tree_like = $tree_like->parent;
+is($tree_like->name, 'baz', 'First level up ok');
+$tree_like = $tree_like->parent;
+is($tree_like->name, 'bar', 'Second level up ok');
+$tree_like = $tree_like->parent;
+is($tree_like->name, 'foo', 'Third level up ok');
+
+$schema->storage->debug(0);
+
+cmp_ok($queries, '==', 1, 'Only one query run');
+
+} # end run_tests
 
 1;