Support one more convoluted case of data-poor collapse
[dbsrgits/DBIx-Class.git] / t / resultset / as_subselect_rs.t
index 8644079..edfcae7 100644 (file)
@@ -1,12 +1,13 @@
+BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
+
 use strict;
 use warnings;
 
 use Test::More;
 use Test::Exception;
 
-use lib qw(t/lib);
-use DBICTest;
-use DBIC::SqlMakerTest;
+
+use DBICTest ':DiffSQL';
 
 my $schema = DBICTest->init_schema();
 
@@ -40,4 +41,32 @@ is_same_sql_bind (
   'Resultset-class attributes do not seep outside of the subselect',
 );
 
+is_same_sql_bind(
+  $schema->resultset('CD')->search ({}, {
+    rows => 2,
+    join => [ 'genre', { artist => 'cds' } ],
+    distinct => 1,
+    columns => {
+      title => 'me.title',
+      artist__name => 'artist.name',
+      genre__name => 'genre.name',
+      cds_for_artist => \ '(SELECT COUNT(*) FROM cds WHERE cd.artist = artist.id)',
+    },
+    order_by => { -desc => 'me.year' },
+  })->count_rs->as_query,
+  '(
+    SELECT COUNT( * )
+      FROM (
+        SELECT artist.name AS artist__name, (SELECT COUNT(*) FROM cds WHERE cd.artist = artist.id), genre.name AS genre__name, me.title, me.year
+          FROM cd me
+          LEFT JOIN genre genre
+            ON genre.genreid = me.genreid
+          JOIN artist artist ON artist.artistid = me.artist
+        GROUP BY artist.name, (SELECT COUNT(*) FROM cds WHERE cd.artist = artist.id), genre.name, me.title, me.year
+        LIMIT ?
+      ) me
+  )',
+  [ [{ sqlt_datatype => 'integer' } => 2 ] ],
+);
+
 done_testing;