Resetting skip and limit when trying to get total count

This commit is contained in:
Sam Lown 2011-04-15 16:24:52 +02:00
parent 00a4cc7f3b
commit cacc320235
2 changed files with 10 additions and 2 deletions

View file

@ -113,7 +113,7 @@ module CouchRest
def count def count
raise "View#count cannot be used with group options" if query[:group] raise "View#count cannot be used with group options" if query[:group]
if can_reduce? if can_reduce?
row = reduce.rows.first row = reduce.skip(0).limit(1).rows.first
row.nil? ? 0 : row.value row.nil? ? 0 : row.value
else else
limit(0).total_rows limit(0).total_rows

View file

@ -173,7 +173,7 @@ describe "Design View" do
describe "#count" do describe "#count" do
it "should raise an error if view prepared for group" do it "should raise an error if view prepared for group" do
@obj.should_receive(:query).and_return({:group => true}) @obj.should_receive(:query).and_return({:group => true})
lambda { @obj.count }.should raise_error lambda { @obj.count }.should raise_error(/group/)
end end
it "should return first row value if reduce possible" do it "should return first row value if reduce possible" do
@ -181,6 +181,8 @@ describe "Design View" do
row = mock("Row") row = mock("Row")
@obj.should_receive(:can_reduce?).and_return(true) @obj.should_receive(:can_reduce?).and_return(true)
@obj.should_receive(:reduce).and_return(view) @obj.should_receive(:reduce).and_return(view)
view.should_receive(:skip).with(0).and_return(view)
view.should_receive(:limit).with(1).and_return(view)
view.should_receive(:rows).and_return([row]) view.should_receive(:rows).and_return([row])
row.should_receive(:value).and_return(2) row.should_receive(:value).and_return(2)
@obj.count.should eql(2) @obj.count.should eql(2)
@ -189,6 +191,8 @@ describe "Design View" do
view = mock("SubView") view = mock("SubView")
@obj.should_receive(:can_reduce?).and_return(true) @obj.should_receive(:can_reduce?).and_return(true)
@obj.should_receive(:reduce).and_return(view) @obj.should_receive(:reduce).and_return(view)
view.should_receive(:skip).with(0).and_return(view)
view.should_receive(:limit).with(1).and_return(view)
view.should_receive(:rows).and_return([]) view.should_receive(:rows).and_return([])
@obj.count.should eql(0) @obj.count.should eql(0)
end end
@ -770,6 +774,10 @@ describe "Design View" do
docs[0].name.should eql("Judith") docs[0].name.should eql("Judith")
docs[1].name.should eql("Peter") docs[1].name.should eql("Peter")
end end
it "should provide count even if limit or skip set" do
docs = DesignViewModel.by_name.limit(20).skip(2)
docs.count.should eql(5)
end
end end
describe "pagination" do describe "pagination" do