warn if using Date or DateTime instead of Time

This commit is contained in:
Kyle Banker 2010-01-06 14:30:58 -05:00
parent 77df695bca
commit 42b835243e
4 changed files with 22 additions and 1 deletions

View File

@ -369,6 +369,11 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
SAFE_WRITE_AT_POS(buffer, length_location, (const char*)&obj_length, 4);
break;
}
if (strcmp(cls, "DateTime") == 0 || strcmp(cls, "Date") == 0) {
buffer_free(buffer);
rb_raise(InvalidDocument, "Trying to use Date or DateTime; the driver currently supports Time objects only.",
TYPE(value));
}
buffer_free(buffer);
rb_raise(InvalidDocument, "Unsupported type for BSON (%d)", TYPE(value));
break;

View File

@ -39,7 +39,8 @@ module Mongo
# Raised on failures in connection to the database server.
class ConnectionTimeoutError < MongoRubyError; end
# Raised when trying to insert a document that exceeds the 4MB limit.
# Raised when trying to insert a document that exceeds the 4MB limit or
# when the document contains objects that can't be serialized as BSON.
class InvalidDocument < MongoDBError; end
# Raised when a database operation fails.

View File

@ -553,6 +553,8 @@ class BSON_RUBY
OBJECT
when Symbol
SYMBOL
when Date, DateTime
raise InvalidDocument, "Trying to use Date or DateTime; the driver currently supports Time objects only."
else
raise InvalidDocument, "Unknown type of object: #{o.class.name}"
end

View File

@ -181,6 +181,19 @@ class BSONTest < Test::Unit::TestCase
end
end
def test_exeption_on_using_date
[DateTime.now, Date.today].each do |invalid_date|
doc = {:date => invalid_date}
begin
bson = BSON.serialize(doc)
rescue => e
ensure
assert_equal InvalidDocument, e.class
assert_match /Time/, e.message
end
end
end
def test_dbref
oid = ObjectID.new
doc = {}