Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix sort and add a test

  • Loading branch information...
commit 16b44931b6016a233372ea2e97581363b8510680 1 parent 411c971
@gdb authored
View
12 lib/embedded-mongo/backend/collection.rb
@@ -90,15 +90,15 @@ def insert(doc)
def sort_cmp(sort, x, y)
sort.each do |field, direction|
- x_val = x[field]
- y_val = y[field]
+ x_val = x[field.to_s]
+ y_val = y[field.to_s]
if direction.to_s == 'ascending' or direction.to_s == 'asc'
if x_val.kind_of?(Numeric) and y_val.kind_of?(Numeric)
cmp = x_val <=> y_val
elsif x_val.kind_of?(Numeric)
- cmp = -1
- elsif y_val.kind_of?(Numeric)
cmp = 1
+ elsif y_val.kind_of?(Numeric)
+ cmp = -1
else
cmp = 0
end
@@ -107,9 +107,9 @@ def sort_cmp(sort, x, y)
if x_val.kind_of?(Numeric) and y_val.kind_of?(Numeric)
cmp = y_val <=> x_val
elsif x_val.kind_of?(Numeric)
- cmp = 1
- elsif y_val.kind_of?(Numeric)
cmp = -1
+ elsif y_val.kind_of?(Numeric)
+ cmp = 1
else
cmp = 0
end
View
19 test/functional/test_interface.rb
@@ -11,6 +11,7 @@ def setup
@test_db = @conn['test']
@foo_collection = @test_db['foo']
+ @foo_collection.remove
end
def test_insert_and_find
@@ -52,4 +53,22 @@ def test_remove
assert_equal(1, cursor.count)
assert_equal({ '_id' => id2, 'lemon' => 'limit' }, cursor.first)
end
+
+ def test_sort
+ @foo_collection.insert({ 'a' => 10 })
+ @foo_collection.insert({ 'a' => 20 })
+ @foo_collection.insert({ 'b' => 'foo' })
+
+ cursor1 = @foo_collection.find.sort([['a', 'asc']])
+ res1 = cursor1.to_a
+ assert_equal(nil, res1[0]['a'])
+ assert_equal(10, res1[1]['a'])
+ assert_equal(20, res1[2]['a'])
+
+ cursor2 = @foo_collection.find.sort([['a', 'desc']])
+ res2 = cursor2.to_a
+ assert_equal(20, res2[0]['a'])
+ assert_equal(10, res2[1]['a'])
+ assert_equal(nil, res2[2]['a'])
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.