Fixed BSON (de)serialization of object ids. New test that saves/restores oid to the database.

This commit is contained in:
Jim Menard 2008-12-09 13:48:38 -05:00
parent 9627908bc1
commit 6c481c617d
2 changed files with 16 additions and 9 deletions

View File

@ -184,10 +184,7 @@ class BSON
end
def deserialize_oid_data(buf)
high_bytes = buf.get_long
low_bytes = buf.get_int
hexval = (high_bytes << 32) + low_bytes
XGen::Mongo::Driver::ObjectID.new('%012x' % hexval)
XGen::Mongo::Driver::ObjectID.new(buf.get(12))
end
def serialize_eoo_element(buf)
@ -254,11 +251,7 @@ class BSON
buf.put(OID)
self.class.serialize_cstr(buf, key)
hexval = val.to_s.hex
high_bytes = hexval >> 32
low_bytes = hexval && 0xffffffff
buf.put_long(high_bytes)
buf.put_int(low_bytes)
buf.put_array(val.to_a)
end
def serialize_string_element(buf, key, val, type)

View File

@ -51,4 +51,18 @@ class ObjectIDTest < Test::Unit::TestCase
assert_equal 24, $1.length
end
def test_save_and_restore
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
db = XGen::Mongo::Driver::Mongo.new(host, port).db('ruby-mongo-test')
coll = db.collection('test')
coll.clear
coll << {'a' => 1, '_id' => @o}
row = coll.find().collect.first
assert_equal 1, row['a']
assert_equal @o, row['_id']
end
end