[Bast-commits] r7186 - in DBIx-Class/0.08/branches/prefetch_redux/t: count prefetch

ribasushi at dev.catalyst.perl.org ribasushi at dev.catalyst.perl.org
Tue Aug 4 10:34:59 GMT 2009


Author: ribasushi
Date: 2009-08-04 10:34:58 +0000 (Tue, 04 Aug 2009)
New Revision: 7186

Modified:
   DBIx-Class/0.08/branches/prefetch_redux/t/count/prefetch.t
   DBIx-Class/0.08/branches/prefetch_redux/t/prefetch/grouped.t
Log:
Add an extra test specifically for distinct/prefetch
Remove duplicate test in count/prefetch

Switch to as_query instead of debug overloading

Modified: DBIx-Class/0.08/branches/prefetch_redux/t/count/prefetch.t
===================================================================
--- DBIx-Class/0.08/branches/prefetch_redux/t/count/prefetch.t	2009-08-03 15:26:17 UTC (rev 7185)
+++ DBIx-Class/0.08/branches/prefetch_redux/t/count/prefetch.t	2009-08-04 10:34:58 UTC (rev 7186)
@@ -6,9 +6,8 @@
 use Test::More;
 use DBICTest;
 use DBIC::SqlMakerTest;
-use DBIC::DebugObj;
 
-plan tests => 17;
+plan tests => 14;
 
 my $schema = DBICTest->init_schema();
 
@@ -20,72 +19,58 @@
                 { prefetch => [qw/tracks artist/] },
             );
   is ($rs->all, 5, 'Correct number of objects');
-
-
-  my ($sql, @bind);
-  $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
-  $schema->storage->debug(1);
-
-
   is ($rs->count, 5, 'Correct count');
 
   is_same_sql_bind (
-    $sql,
-    \@bind,
-    'SELECT COUNT( * ) FROM (SELECT cds.cdid FROM artist me JOIN cd cds ON cds.artist = me.artistid LEFT JOIN track tracks ON tracks.cd = cds.cdid JOIN artist artist ON artist.artistid = cds.artist WHERE tracks.position = ? OR tracks.position = ? GROUP BY cds.cdid) count_subq',
-    [ qw/'1' '2'/ ],
+    $rs->count_rs->as_query,
+    '(
+      SELECT COUNT( * )
+        FROM (
+          SELECT cds.cdid
+            FROM artist me
+            JOIN cd cds ON cds.artist = me.artistid
+            LEFT JOIN track tracks ON tracks.cd = cds.cdid
+            JOIN artist artist ON artist.artistid = cds.artist
+          WHERE tracks.position = ? OR tracks.position = ?
+          GROUP BY cds.cdid
+        ) count_subq
+    )',
+    [ map { [ 'tracks.position' => $_ ] } (1, 2) ],
   );
 }
 
-# Added test by mo per http://scsys.co.uk:8001/31870
-TODO: {
-  todo_skip "This breaks stuff", 3;
-  my $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
-            ->search_related('cds')->search_related('genre',
-                { 'genre.name' => 'foo' },
-                { prefetch => q(cds) },
-            );
-  is ($rs->all, 5, 'Correct number of objects');
-
-
-  my ($sql, @bind);
-  $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
-  $schema->storage->debug(1);
-
-
-  is ($rs->count, 5, 'Correct count');
-
-  is_same_sql_bind (
-    $sql,
-    \@bind,
-    'SELECT COUNT( * ) FROM (SELECT cds.cdid FROM artist me JOIN cd cds ON cds.artist = me.artistid LEFT JOIN track tracks ON tracks.cd = cds.cdid JOIN artist artist ON artist.artistid = cds.artist WHERE tracks.position = ? OR tracks.position = ? GROUP BY cds.cdid) count_subq',
-    [ qw/'1' '2'/ ],
-  );
-}
-
 # collapsing prefetch with distinct
-TODO: {
-  todo_skip "This is busted", 3;
+{
+  my $first_cd = $schema->resultset('Artist')->first->cds->first;
+  $first_cd->update ({
+    genreid => $first_cd->create_related (
+      genre => ({ name => 'vague genre' })
+    )->id
+  });
+
   my $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
             ->search_related('cds')->search_related('genre',
-                { 'genre.name' => 'foo' },
+                { 'genre.name' => { '!=', 'foo' } },
                 { prefetch => q(cds) },
             );
-  is ($rs->all, 5, 'Correct number of objects');
+  is ($rs->all, 1, 'Correct number of objects');
+  is ($rs->count, 1, 'Correct count');
 
-
-  my ($sql, @bind);
-  $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
-  $schema->storage->debug(1);
-
-
-  is ($rs->count, 5, 'Correct count');
-
   is_same_sql_bind (
-    $sql,
-    \@bind,
-    'SELECT COUNT( * ) FROM (SELECT cds.cdid FROM artist me JOIN cd cds ON cds.artist = me.artistid LEFT JOIN track tracks ON tracks.cd = cds.cdid JOIN artist artist ON artist.artistid = cds.artist WHERE tracks.position = ? OR tracks.position = ? GROUP BY cds.cdid) count_subq',
-    [ qw/'1' '2'/ ],
+    $rs->count_rs->as_query,
+    '(
+      SELECT COUNT( * )
+        FROM (
+          SELECT genre.genreid
+            FROM artist me
+            JOIN cd cds ON cds.artist = me.artistid
+            JOIN genre genre ON genre.genreid = cds.genreid
+            LEFT JOIN cd cds_2 ON cds_2.genreid = genre.genreid
+          WHERE ( genre.name != ? )
+          GROUP BY genre.genreid
+        ) count_subq
+    )',
+    [ [ 'genre.name' => 'foo' ] ],
   );
 }
 
@@ -99,24 +84,23 @@
   is ($rs->all, 10, 'Correct number of objects');
 
 
-  my ($sql, @bind);
-  $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
-  $schema->storage->debug(1);
-
-
   is ($rs->count, 10, 'Correct count');
 
   is_same_sql_bind (
-    $sql,
-    \@bind,
-    'SELECT COUNT( * ) FROM cd me JOIN track tracks ON tracks.cd = me.cdid JOIN cd disc ON disc.cdid = tracks.cd LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid WHERE ( ( position = ? OR position = ? ) )',
-    [ qw/'1' '2'/ ],
+    $rs->count_rs->as_query,
+    '(
+      SELECT COUNT( * )
+        FROM cd me
+        JOIN track tracks ON tracks.cd = me.cdid
+        JOIN cd disc ON disc.cdid = tracks.cd
+        LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid
+      WHERE position = ? OR position = ?
+    )',
+    [ map { [ position => $_ ] } (1, 2) ],
   );
 }
 
-# Added test by mo per http://scsys.co.uk:8001/31873
-TODO: {
-    todo_skip "This breaks stuff", 5;
+{
     my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
               ->search_related('artwork_to_artist')->search_related('artist',
                  undef,
@@ -130,16 +114,16 @@
                   { prefetch => q(cds) },
               );
     is($rs->all, 0, 'success with WHERE');
-    
+
+
     # different case
-    
     $rs = $schema->resultset("Artist")->search(undef)#, {distinct => 1})
                 ->search_related('cds')->search_related('genre',
                     { 'genre.name' => 'foo' },
                     { prefetch => q(cds) },
                  );
     is($rs->all, 0, 'success without distinct');
-    
+
     $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
                 ->search_related('cds')->search_related('genre',
                     { 'genre.name' => 'foo' },

Modified: DBIx-Class/0.08/branches/prefetch_redux/t/prefetch/grouped.t
===================================================================
--- DBIx-Class/0.08/branches/prefetch_redux/t/prefetch/grouped.t	2009-08-03 15:26:17 UTC (rev 7185)
+++ DBIx-Class/0.08/branches/prefetch_redux/t/prefetch/grouped.t	2009-08-04 10:34:58 UTC (rev 7186)
@@ -6,7 +6,6 @@
 use DBICTest;
 use DBIC::SqlMakerTest;
 
-#plan tests => 6;
 plan 'no_plan';
 
 my $schema = DBICTest->init_schema();
@@ -203,3 +202,33 @@
   $schema->storage->debugcb (undef);
   $schema->storage->debug ($sdebug);
 }
+
+# make sure that distinct still works
+{
+  my $rs = $schema->resultset("CD")->search({}, {
+    prefetch => 'tags',
+    order_by => 'cdid',
+    distinct => 1,
+  });
+
+  is_same_sql_bind (
+    $rs->as_query,
+    '(
+      SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
+             tags.tagid, tags.cd, tags.tag 
+        FROM (
+          SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
+            FROM cd me
+          GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
+          ORDER BY cdid
+        ) me
+        LEFT JOIN tags tags ON tags.cd = me.cdid
+      ORDER BY cdid, tags.cd, tags.tag
+    )',
+    [],
+    'Prefetch + distinct resulted in correct group_by',
+  );
+
+  is ($rs->all, 5, 'Correct number of CD objects');
+  is ($rs->count, 5, 'Correct count of CDs');
+}




More information about the Bast-commits mailing list