From: Peter Rabbitson <ribasushi@cpan.org>
Date: Sat, 16 May 2009 08:40:44 +0000 (+0000)
Subject: Test and fixed for paged grouped count
X-Git-Tag: v0.08103~64^2~6
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=db326a590fb76893c9e57dea0fca59be67ba25de;p=dbsrgits%2FDBIx-Class.git

Test and fixed for paged grouped count
---

diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm
index 079e0e4..932d16f 100644
--- a/lib/DBIx/Class/ResultSet.pm
+++ b/lib/DBIx/Class/ResultSet.pm
@@ -1712,8 +1712,15 @@ sub pager {
   $self->throw_exception("Can't create pager for non-paged rs")
     unless $self->{attrs}{page};
   $attrs->{rows} ||= 10;
+
+  # throw away the paging flags and re-run the count (possibly 
+  # with a subselect) to get the real total count
+  my $count_attrs = { %$attrs };
+  delete $count_attrs->{$_} for qw/rows offset page pager/;
+  my $total_count = (ref $self)->new($self->result_source, $count_attrs);
+
   return $self->{pager} ||= Data::Page->new(
-    $self->__count, $attrs->{rows}, $self->{attrs}{page});
+    $total_count, $attrs->{rows}, $self->{attrs}{page});
 }
 
 =head2 page
diff --git a/t/count/grouped_pager.t b/t/count/grouped_pager.t
new file mode 100644
index 0000000..cfe29af
--- /dev/null
+++ b/t/count/grouped_pager.t
@@ -0,0 +1,44 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+use lib qw(t/lib);
+
+use DBICTest;
+
+plan tests => 7;
+
+my $schema = DBICTest->init_schema();
+
+use Data::Dumper;
+
+# add 2 extra artists
+$schema->populate ('Artist', [
+    [qw/name/],
+    [qw/ar_1/],
+    [qw/ar_2/],
+]);
+
+# add 3 extra cds to every artist
+for my $ar ($schema->resultset ('Artist')->all) {
+  for my $cdnum (1 .. 3) {
+    $ar->create_related ('cds', {
+      title => "bogon $cdnum",
+      year => 2000 + $cdnum,
+    });
+  }
+}
+
+my $cds = $schema->resultset ('CD')->search ({}, { group_by => 'artist' } );
+is ($cds->count, 5, 'Resultset collapses to 5 groups');
+
+my ($pg1, $pg2, $pg3) = map { $cds->search_rs ({}, {rows => 2, page => $_}) } (1..3);
+
+for ($pg1, $pg2, $pg3) {
+  is ($_->pager->total_entries, 5, 'Total count via pager correct');
+}
+
+is ($pg1->count, 2, 'First page has 2 groups');
+is ($pg2->count, 2, 'Second page has 2 groups');
+is ($pg3->count, 1, 'Third page has one group remaining');