From 0413e9b1591284baaa35cc175739f7f2d140c38a Mon Sep 17 00:00:00 2001 From: Adrian Madrid Date: Tue, 6 Jan 2009 18:38:04 -0700 Subject: [PATCH] More sorting tests. --- tests/test_db_api.rb | 53 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 3e2f931..e27afb5 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -105,38 +105,67 @@ class DBAPITest < Test::Unit::TestCase end def test_find_sorting - @coll.insert('a' => 2) - @coll.insert('b' => 3) + @coll.clear + @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) 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 2, docs[1]['a'] + assert_equal 3, docs[2]['a'] + assert_equal 4, docs[3]['a'] # Sorting (descending) docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => [{'a' => -1}]).map{ |x| x } - assert_equal 2, docs.size - assert_equal 2, docs[0]['a'] - assert_equal 1, docs[1]['a'] + assert_equal 4, docs.size + assert_equal 4, docs[0]['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. docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => ['a']).map{ |x| x } - assert_equal 2, docs.size - assert_equal 1, docs.first['a'] + assert_equal 4, docs.size + 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. 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 # order of the keys won't be guaranteed thus your sort won't make sense. oh = OrderedHash.new oh['a'] = -1 docs = @coll.find({'a' => { '$lt' => 10 }}, :sort => oh).map{ |x| x } - assert_equal 2, docs.size - assert_equal 2, docs[0]['a'] - assert_equal 1, docs[1]['a'] + assert_equal 4, docs.size + assert_equal 4, docs[0]['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 def test_find_limits