From: Andy Grundman Date: Tue, 30 Aug 2005 18:49:11 +0000 (+0000) Subject: Added tests for software-based limiting/paging, and a few related bug fixes X-Git-Tag: v0.03001~21 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9229f20afca715e8a1c7e852e46d9333dd392548;p=dbsrgits%2FDBIx-Class.git Added tests for software-based limiting/paging, and a few related bug fixes --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 28131b3..f2a0b37 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -20,7 +20,9 @@ sub new { count => undef, pager => undef, attrs => $attrs }; - return bless ($new, $it_class); + bless ($new, $it_class); + $new->pager if ($attrs->{page}); + return $new; } sub cursor { @@ -65,6 +67,7 @@ sub count { $self->{cond}, $attrs); } return 0 unless $self->{count}; + return $self->{pager}->entries_on_this_page if ($self->{pager}); return ( $attrs->{rows} && $attrs->{rows} < $self->{count} ) ? $attrs->{rows} : $self->{count}; diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 5a1509a..c24bdac 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -127,8 +127,9 @@ sub select { $order = $1 if $$condition =~ s/ORDER BY (.*)$//i; } my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order); - if ($self->sql_maker->_default_limit_syntax eq "GenericSubQ") { - $attrs->{software_limit} = 1; + if ($attrs->{software_limit} || + $self->sql_maker->_default_limit_syntax eq "GenericSubQ") { + $attrs->{software_limit} = 1; } else { push @args, $attrs->{rows}, $attrs->{offset}; } @@ -143,8 +144,9 @@ sub select_single { $order = $1 if $$condition =~ s/ORDER BY (.*)$//i; } my @args = ('select', $attrs->{bind}, $ident, $select, $condition, $order); - if ($self->sql_maker->_default_limit_syntax eq "GenericSubQ") { - $attrs->{software_limit} = 1; + if ($attrs->{software_limit} || + $self->sql_maker->_default_limit_syntax eq "GenericSubQ") { + $attrs->{software_limit} = 1; } else { push @args, 1, $attrs->{offset}; } diff --git a/t/07pager.t b/t/07pager.t index de8dbaf..64f33f9 100644 --- a/t/07pager.t +++ b/t/07pager.t @@ -1,6 +1,6 @@ use Test::More; -plan tests => 8; +plan tests => 13; use lib qw(t/lib); @@ -45,4 +45,22 @@ $it = DBICTest::CD->search( ); my $page = $it->page(2); +is( $page->count, 2, "standard resultset paged rs count ok" ); + is( $page->next->title, "Generic Manufactured Singles", "second page of standard resultset ok" ); + +# test software-based limit paging +$it = DBICTest::CD->search( + {}, + { order_by => 'title', + rows => 3, + page => 2, + software_limit => 1 } +); +is( $it->pager->entries_on_this_page, 2, "software entries_on_this_page ok" ); + +is( $it->pager->previous_page, 1, "software previous_page ok" ); + +is( $it->count, 2, "software count on paged rs ok" ); + +is( $it->next->title, "Generic Manufactured Singles", "software iterator->next ok" ); diff --git a/t/15limit.t b/t/15limit.t index edad740..98fca7a 100644 --- a/t/15limit.t +++ b/t/15limit.t @@ -3,7 +3,7 @@ use Test::More; BEGIN { eval "use DBD::SQLite"; - plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 6); + plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 10); } use lib qw(t/lib); @@ -29,6 +29,26 @@ my @cds = DBICTest::CD->search( {}, ); is( $cds[0]->title, "Spoonful of bees", "offset ok" ); +# test software-based limiting +$it = DBICTest::CD->search( {}, + { rows => 3, + software_limit => 1, + order_by => 'title' } +); +is( $it->count, 3, "software limit count ok" ); +is( $it->next->title, "Caterwaulin' Blues", "software iterator->next ok" ); +$it->next; +$it->next; +is( $it->next, undef, "software next past end of resultset ok" ); + +@cds = DBICTest::CD->search( {}, + { rows => 2, + offset => 2, + software_limit => 1, + order_by => 'year' } +); +is( $cds[0]->title, "Spoonful of bees", "software offset ok" ); + # based on a failing criteria submitted by waswas # requires SQL::Abstract >= 1.20 $it = DBICTest::CD->search(