From 7783cebfdfda7d396babf2e31492dfb47737ca6d Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Thu, 16 Jun 2011 22:21:40 +0800 Subject: [PATCH] batch_size is taken into account when limit is 0/not set --- lib/mongo/cursor.rb | 5 +++-- test/unit/cursor_test.rb | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 652d7af..4f567b2 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -220,12 +220,13 @@ module Mongo # the server will determine the batch size. # # @return [Cursor] - def batch_size(size=0) + def batch_size(size=nil) + return @batch_size unless size check_modifiable if size < 0 || size == 1 raise ArgumentError, "Invalid value for batch_size #{size}; must be 0 or > 1." else - @batch_size = size > @limit ? @limit : size + @batch_size = @limit != 0 && size > @limit ? @limit : size end self diff --git a/test/unit/cursor_test.rb b/test/unit/cursor_test.rb index 14f36b5..d971dad 100644 --- a/test/unit/cursor_test.rb +++ b/test/unit/cursor_test.rb @@ -76,6 +76,24 @@ class CursorTest < Test::Unit::TestCase should "cache full collection name" do assert_equal "testing.items", @cursor.full_collection_name end + + should "raise error when batch_size is 1" do + e = assert_raise ArgumentError do + @cursor.batch_size(1) + end + assert_equal "Invalid value for batch_size 1; must be 0 or > 1.", e.message + end + + should "use the limit for batch size when it's smaller than the specified batch_size" do + @cursor.limit(99) + @cursor.batch_size(100) + assert_equal 99, @cursor.batch_size + end + + should "use the specified batch_size" do + @cursor.batch_size(100) + assert_equal 100, @cursor.batch_size + end end context "Query fields" do