test/test_bson.rb in mongo-0.18.2 vs test/test_bson.rb in mongo-0.18.3
- old
+ new
@@ -1,8 +1,18 @@
# encoding:utf-8
require 'test/test_helper'
+require 'complex'
+require 'bigdecimal'
+require 'rational'
+# Need to simulating this class
+# without actually loading it.
+module ActiveSupport
+ class TimeWithZone
+ end
+end
+
class BSONTest < Test::Unit::TestCase
include Mongo
def test_string
@@ -179,10 +189,23 @@
# TODO figure out how that will work if somebady has saved data
# w/ early dates already and is just querying for it.
end
end
+ def test_exeption_on_using_unsupported_date_class
+ [DateTime.now, Date.today, ActiveSupport::TimeWithZone.new].each do |invalid_date|
+ doc = {:date => invalid_date}
+ begin
+ bson = BSON.serialize(doc)
+ rescue => e
+ ensure
+ assert_equal InvalidDocument, e.class
+ assert_match /UTC Time/, e.message
+ end
+ end
+ end
+
def test_dbref
oid = ObjectID.new
doc = {}
doc['dbref'] = DBRef.new('namespace', oid)
bson = BSON.serialize(doc)
@@ -291,10 +314,23 @@
assert_raise RangeError do
bson = BSON.serialize(doc)
end
end
+ def test_invalid_numeric_types
+ [BigDecimal.new("1.0"), Complex(0, 1), Rational(2, 3)].each do |type|
+ doc = {"x" => type}
+ begin
+ BSON.serialize(doc)
+ rescue => e
+ ensure
+ assert_equal InvalidDocument, e.class
+ assert_match /Cannot serialize/, e.message
+ end
+ end
+ end
+
def test_do_not_change_original_object
val = OrderedHash.new
val['not_id'] = 1
val['_id'] = 2
assert val.keys.include?('_id')
@@ -331,9 +367,21 @@
end
assert_raise InvalidDocument do
BSON.serialize({"a" => (Regexp.compile "ab\x00c")})
end
+ end
+
+ def test_max_key
+ doc = {"a" => MaxKey.new}
+
+ assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
+ end
+
+ def test_min_key
+ doc = {"a" => MinKey.new}
+
+ assert_equal doc, BSON.deserialize(BSON.serialize(doc).to_a)
end
def test_invalid_object
o = Object.new
assert_raise InvalidDocument do