More sorting tests.

This commit is contained in:
Adrian Madrid 2009-01-06 18:38:04 -07:00
parent c8072f2f4a
commit 0413e9b159
1 changed files with 41 additions and 12 deletions

View File

@ -105,38 +105,67 @@ class DBAPITest < Test::Unit::TestCase
end end
def test_find_sorting def test_find_sorting
@coll.insert('a' => 2) @coll.clear
@coll.insert('b' => 3) @coll.insert('a' => 1, 'b' => 2)
@coll.insert('a' => 2, 'b' => 1)
@coll.insert('a' => 3, 'b' => 2)
@coll.insert('a' => 4, 'b' => 1)
# Sorting (ascending) # Sorting (ascending)
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => [{'a' => 1}]).map{ |x| x } docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => [{'a' => 1}]).map{ |x| x }
assert_equal 2, docs.size assert_equal 4, docs.size
assert_equal 1, docs[0]['a'] assert_equal 1, docs[0]['a']
assert_equal 2, docs[1]['a'] assert_equal 2, docs[1]['a']
assert_equal 3, docs[2]['a']
assert_equal 4, docs[3]['a']
# Sorting (descending) # Sorting (descending)
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => [{'a' => -1}]).map{ |x| x } docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => [{'a' => -1}]).map{ |x| x }
assert_equal 2, docs.size assert_equal 4, docs.size
assert_equal 2, docs[0]['a'] assert_equal 4, docs[0]['a']
assert_equal 1, docs[1]['a'] assert_equal 3, docs[1]['a']
assert_equal 2, docs[2]['a']
assert_equal 1, docs[3]['a']
# Sorting using array of names; assumes ascending order. # Sorting using array of names; assumes ascending order.
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => ['a']).map{ |x| x } docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => ['a']).map{ |x| x }
assert_equal 2, docs.size assert_equal 4, docs.size
assert_equal 1, docs.first['a'] assert_equal 1, docs[0]['a']
assert_equal 2, docs[1]['a']
assert_equal 3, docs[2]['a']
assert_equal 4, docs[3]['a']
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => ['b', 'a']).map{ |x| x }
assert_equal 4, docs.size
assert_equal 2, docs[0]['a']
assert_equal 4, docs[1]['a']
assert_equal 1, docs[2]['a']
assert_equal 3, docs[3]['a']
# Sorting using empty array; no order guarantee but should not blow up. # Sorting using empty array; no order guarantee but should not blow up.
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => []).map{ |x| x } docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => []).map{ |x| x }
assert_equal 2, docs.size assert_equal 4, docs.size
# Sorting using ordered hash. You can use an unordered one, but then the # Sorting using ordered hash. You can use an unordered one, but then the
# order of the keys won't be guaranteed thus your sort won't make sense. # order of the keys won't be guaranteed thus your sort won't make sense.
oh = OrderedHash.new oh = OrderedHash.new
oh['a'] = -1 oh['a'] = -1
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => oh).map{ |x| x } docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => oh).map{ |x| x }
assert_equal 2, docs.size assert_equal 4, docs.size
assert_equal 2, docs[0]['a'] assert_equal 4, docs[0]['a']
assert_equal 1, docs[1]['a'] assert_equal 3, docs[1]['a']
assert_equal 2, docs[2]['a']
assert_equal 1, docs[3]['a']
oh = OrderedHash.new
oh['b'] = -1
oh['a'] = 1
docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => oh).map{ |x| x }
assert_equal 4, docs.size
assert_equal 1, docs[0]['a']
assert_equal 3, docs[1]['a']
assert_equal 2, docs[2]['a']
assert_equal 4, docs[3]['a']
end end
def test_find_limits def test_find_limits