fix attrs on find
[dbsrgits/DBIx-Class.git] / t / run / 16joins.tl
index 1b3ff15..3ea71f1 100644 (file)
@@ -7,7 +7,7 @@ BEGIN {
     eval "use DBD::SQLite";
     plan $@
         ? ( skip_all => 'needs DBD::SQLite for testing' )
-        : ( tests => 27 );
+        : ( tests => 33 );
 }
 
 # test the abstract join => SQL generator
@@ -161,6 +161,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');
+
+my $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 +208,23 @@ 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" );
 }
 
 1;