fix for pure ruby encoder when _id is a falsey value. more tests

This commit is contained in:
Mike Dirolf 2009-05-12 14:21:17 -04:00
parent 124409ee03
commit a9aa5e5271
3 changed files with 15 additions and 8 deletions

View File

@ -86,13 +86,11 @@ class BSON
# Write key/value pairs. Always write _id first if it exists.
if obj.has_key? '_id'
oid = obj['_id']
serialize_key_value('_id', obj['_id'])
elsif obj.has_key? :_id
oid = obj[:_id]
else
oid = false
serialize_key_value('_id', obj[:_id])
end
serialize_key_value('_id', oid) if oid
obj.each {|k, v| serialize_key_value(k, v) unless k == '_id' || k == :_id }
serialize_eoo_element(@buf)

View File

@ -181,6 +181,11 @@ class BSONTest < Test::Unit::TestCase
assert_equal '_id', roundtrip.keys.first
end
def test_nil_id
doc = {"_id" => nil}
assert_equal doc, @b.deserialize(@b.serialize(doc).to_a)
end
def test_timestamp
val = {"test" => [4, 20]}
assert_equal val, @b.deserialize([0x13, 0x00, 0x00, 0x00,

View File

@ -547,14 +547,18 @@ class DBAPITest < Test::Unit::TestCase
@@coll.clear
assert_equal nil, @@db.dereference(DBRef.new("test", ObjectID.new))
obj = {"x" => true}
key = @@coll.insert(obj)["_id"]
assert_equal true, @@db.dereference(DBRef.new("test", key))["x"]
@@coll.insert({"x" => "hello"})
key = @@coll.find_first()["_id"]
assert_equal "hello", @@db.dereference(DBRef.new("test", key))["x"]
assert_equal nil, @@db.dereference(DBRef.new("test", 4))
obj = {"_id" => 4}
@@coll.insert(obj)
assert_equal obj, @@db.dereference(DBRef.new("test", 4))
@@coll.clear
@@coll.insert({"x" => "hello"})
assert_equal nil, @@db.dereference(DBRef.new("test", nil))
end
# TODO this test fails with error message "Undefed Before end of object"