Skip to content

Commit

Permalink
minor: tweaks and some more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Dirolf committed Sep 17, 2009
1 parent 1b98a19 commit f41b48a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
6 changes: 3 additions & 3 deletions lib/mongo/cursor.rb
Expand Up @@ -84,7 +84,7 @@ def count
# This method overrides any sort order specified in the Collection#find
# method, and only the last sort applied has an effect
def sort(order)
raise InvalidOperation, "can't call Cursor#sort on a used cursor" if @query_run
check_modifiable
@query.order_by = order
self
end
Expand All @@ -100,7 +100,7 @@ def limit(number_to_return)
raise ArgumentError, "limit requires an integer" unless number_to_return.is_a? Integer

@query.number_to_return = number_to_return
return self
self
end

# Skips the first +number_to_skip+ results of this cursor.
Expand All @@ -114,7 +114,7 @@ def skip(number_to_skip)
raise ArgumentError, "skip requires an integer" unless number_to_skip.is_a? Integer

@query.number_to_skip = number_to_skip
return self
self
end

# Iterate over each document in this cursor, yielding it to the given
Expand Down
25 changes: 15 additions & 10 deletions test/test_cursor.rb
Expand Up @@ -57,22 +57,27 @@ def test_count

assert_equal 0, @@db['acollectionthatdoesn'].count()
end

def test_sort
@@coll.clear
5.times{|x| @@coll.insert({"a" => x, "b" => 5-x}) }
assert_kind_of Cursor, @@coll.find().sort({:a=>1})
5.times{|x| @@coll.insert({"a" => x}) }

assert_kind_of Cursor, @@coll.find().sort({:a => 1})

assert_equal 0, @@coll.find().sort({:a => 1}).next_object["a"]
assert_equal 4, @@coll.find().sort({:a => -1}).next_object["a"]

assert_equal 1, @@coll.find().sort({:a => -1, :b => 1}).next_object["b"]
assert_equal 5, @@coll.find().sort({:a => 1, :b => -1}).next_object["b"]
assert_equal 0, @@coll.find().sort(["a"]).next_object["a"]

assert_kind_of Cursor, @@coll.find().sort({:a => -1, :b => 1})

assert_equal 4, @@coll.find().sort({:a => 1}).sort({:a => -1}).next_object["a"]
assert_equal 0, @@coll.find().sort({:a => -1}).sort({:a => 1}).next_object["a"]


cursor = @@coll.find()
cursor.next_object()
assert_raise InvalidOperation do
cursor.sort(["a"])
end
end

def test_limit
Expand Down

0 comments on commit f41b48a

Please sign in to comment.