Skip to content

Commit 7783ceb

Browse files
author
Karl Seguin
committed
batch_size is taken into account when limit is 0/not set
1 parent fbf7b75 commit 7783ceb

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/mongo/cursor.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,13 @@ def skip(number_to_skip=nil)
220220
# the server will determine the batch size.
221221
#
222222
# @return [Cursor]
223-
def batch_size(size=0)
223+
def batch_size(size=nil)
224+
return @batch_size unless size
224225
check_modifiable
225226
if size < 0 || size == 1
226227
raise ArgumentError, "Invalid value for batch_size #{size}; must be 0 or > 1."
227228
else
228-
@batch_size = size > @limit ? @limit : size
229+
@batch_size = @limit != 0 && size > @limit ? @limit : size
229230
end
230231

231232
self

test/unit/cursor_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ class CursorTest < Test::Unit::TestCase
7676
should "cache full collection name" do
7777
assert_equal "testing.items", @cursor.full_collection_name
7878
end
79+
80+
should "raise error when batch_size is 1" do
81+
e = assert_raise ArgumentError do
82+
@cursor.batch_size(1)
83+
end
84+
assert_equal "Invalid value for batch_size 1; must be 0 or > 1.", e.message
85+
end
86+
87+
should "use the limit for batch size when it's smaller than the specified batch_size" do
88+
@cursor.limit(99)
89+
@cursor.batch_size(100)
90+
assert_equal 99, @cursor.batch_size
91+
end
92+
93+
should "use the specified batch_size" do
94+
@cursor.batch_size(100)
95+
assert_equal 100, @cursor.batch_size
96+
end
7997
end
8098

8199
context "Query fields" do

0 commit comments

Comments
 (0)