tests + fix for deserializing pre-epoch dates w/ pure Ruby BSON deserializer - thanks chendo

This commit is contained in:
Mike Dirolf 2009-09-16 12:53:46 -04:00
parent b6901673bb
commit d95971b20b
3 changed files with 17 additions and 2 deletions

View File

@ -248,8 +248,10 @@ class BSON
end
def deserialize_date_data(buf)
millisecs = buf.get_long()
Time.at(millisecs.to_f / 1000.0).utc # at() takes fractional seconds
unsigned = buf.get_long()
# see note for deserialize_number_long_data below
milliseconds = unsigned >= 2 ** 64 / 2 ? unsigned - 2**64 : unsigned
Time.at(milliseconds.to_f / 1000.0).utc # at() takes fractional seconds
end
def deserialize_boolean_data(buf)

View File

@ -124,6 +124,14 @@ class BSONTest < Test::Unit::TestCase
assert doc2['date'].utc?
end
def test_date_before_epoch
doc = {'date' => Time.utc(1600)}
@b.serialize(doc)
doc2 = @b.deserialize
# Mongo only stores up to the millisecond
assert_in_delta doc['date'], doc2['date'], 0.001
end
def test_dbref
oid = ObjectID.new
doc = {}

View File

@ -182,6 +182,11 @@ class TestCollection < Test::Unit::TestCase
assert c.closed?
end
def test_saving_dates_pre_epoch
@@test.save({'date' => Time.utc(1600)})
assert_in_delta Time.utc(1600), @@test.find_one()["date"], 0.001
end
def test_save_symbol_find_string
@@test.save(:foo => :mike)