minor: fixed sort api to allow a single key / direction pair to be specified as an array

This commit is contained in:
Kyle Banker 2009-12-28 13:43:20 -05:00
parent 23c8b9d45f
commit a698415fa5
2 changed files with 39 additions and 14 deletions

View File

@ -32,11 +32,19 @@ module Mongo #:nodoc:
# <tt>{ "field1" => 1, "field2" => -1}</tt>
def array_as_sort_parameters(value)
order_by = OrderedHash.new
value.each do |param|
if (param.class.name == "String")
order_by[param] = 1
if value.first.is_a? Array
value.each do |param|
if (param.class.name == "String")
order_by[param] = 1
else
order_by[param[0]] = sort_value(param[1]) unless param[1].nil?
end
end
else
if order_by.size == 1
order_by[value.first] = 1
else
order_by[param[0]] = sort_value(param[1]) unless param[1].nil?
order_by[value.first] = sort_value(value[1])
end
end
order_by

View File

@ -67,27 +67,30 @@ class CursorTest < Test::Unit::TestCase
def test_sort
@@coll.remove
5.times{|x| @@coll.insert({"a" => x}) }
5.times{|x| @@coll.insert({"age" => x}) }
assert_kind_of Cursor, @@coll.find().sort(:a, 1)
assert_kind_of Cursor, @@coll.find().sort(:age, 1)
assert_equal 0, @@coll.find().sort(:a, 1).next_document["a"]
assert_equal 4, @@coll.find().sort(:a, -1).next_document["a"]
assert_equal 0, @@coll.find().sort([["a", :asc]]).next_document["a"]
assert_equal 0, @@coll.find().sort(:age, 1).next_document["age"]
assert_equal 4, @@coll.find().sort(:age, -1).next_document["age"]
assert_equal 0, @@coll.find().sort([["age", :asc]]).next_document["age"]
assert_kind_of Cursor, @@coll.find().sort([[:a, -1], [:b, 1]])
assert_kind_of Cursor, @@coll.find().sort([[:age, -1], [:b, 1]])
assert_equal 4, @@coll.find().sort(:a, 1).sort(:a, -1).next_document["a"]
assert_equal 0, @@coll.find().sort(:a, -1).sort(:a, 1).next_document["a"]
assert_equal 4, @@coll.find().sort(:age, 1).sort(:age, -1).next_document["age"]
assert_equal 0, @@coll.find().sort(:age, -1).sort(:age, 1).next_document["age"]
assert_equal 4, @@coll.find().sort([:age, :asc]).sort(:age, -1).next_document["age"]
assert_equal 0, @@coll.find().sort([:age, :desc]).sort(:age, 1).next_document["age"]
cursor = @@coll.find()
cursor.next_document
assert_raise InvalidOperation do
cursor.sort(["a"])
cursor.sort(["age"])
end
assert_raise InvalidSortValueError do
@@coll.find().sort(:a, 25).next_document
@@coll.find().sort(:age, 25).next_document
end
assert_raise InvalidSortValueError do
@ -95,6 +98,20 @@ class CursorTest < Test::Unit::TestCase
end
end
def test_sort_date
@@coll.remove
5.times{|x| @@coll.insert({"created_at" => Time.utc(2000 + x)}) }
assert_equal 2000, @@coll.find().sort(:created_at, :asc).next_document["created_at"].year
assert_equal 2004, @@coll.find().sort(:created_at, :desc).next_document["created_at"].year
assert_equal 2000, @@coll.find().sort([:created_at, :asc]).next_document["created_at"].year
assert_equal 2004, @@coll.find().sort([:created_at, :desc]).next_document["created_at"].year
assert_equal 2000, @@coll.find().sort([[:created_at, :asc]]).next_document["created_at"].year
assert_equal 2004, @@coll.find().sort([[:created_at, :desc]]).next_document["created_at"].year
end
def test_limit
@@coll.remove