RUBY-233 show invalid key on invalid key exception

This commit is contained in:
Kyle Banker 2011-02-04 12:07:28 -05:00
parent 0c574b9975
commit 76730d4a7c
3 changed files with 45 additions and 39 deletions

View File

@ -231,12 +231,12 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
int i;
if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') {
buffer_free(buffer);
rb_raise(InvalidKeyName, "key must not start with '$'");
rb_raise(InvalidKeyName, "%s - key must not start with '$'", RSTRING_PTR(key));
}
for (i = 0; i < RSTRING_LEN(key); i++) {
if (RSTRING_PTR(key)[i] == '.') {
buffer_free(buffer);
rb_raise(InvalidKeyName, "key must not contain '.'");
rb_raise(InvalidKeyName, "%s - key must not contain '.'", RSTRING_PTR(key));
}
}
}

View File

@ -562,4 +562,47 @@ class BSONTest < Test::Unit::TestCase
end
end
def test_invalid_key_names
assert @encoder.serialize({"hello" => "world"}, true)
assert @encoder.serialize({"hello" => {"hello" => "world"}}, true)
assert @encoder.serialize({"he$llo" => "world"}, true)
assert @encoder.serialize({"hello" => {"hell$o" => "world"}}, true)
assert_raise BSON::InvalidDocument do
@encoder.serialize({"he\0llo" => "world"}, true)
end
assert_raise_error BSON::InvalidKeyName, "$hello" do
@encoder.serialize({"$hello" => "world"}, true)
end
assert_raise BSON::InvalidKeyName do
@encoder.serialize({"hello" => {"$hello" => "world"}}, true)
end
assert_raise_error BSON::InvalidKeyName, ".hello" do
@encoder.serialize({".hello" => "world"}, true)
end
assert_raise BSON::InvalidKeyName do
@encoder.serialize({"hello" => {".hello" => "world"}}, true)
end
assert_raise BSON::InvalidKeyName do
@encoder.serialize({"hello." => "world"}, true)
end
assert_raise BSON::InvalidKeyName do
@encoder.serialize({"hello" => {"hello." => "world"}}, true)
end
assert_raise BSON::InvalidKeyName do
@encoder.serialize({"hel.lo" => "world"}, true)
end
assert_raise BSON::InvalidKeyName do
@encoder.serialize({"hello" => {"hel.lo" => "world"}}, true)
end
end
end

View File

@ -621,43 +621,6 @@ class DBAPITest < Test::Unit::TestCase
assert_equal("mike", @@coll.find_one()["hello"])
end
def test_invalid_key_names
@@coll.remove
@@coll.insert({"hello" => "world"})
@@coll.insert({"hello" => {"hello" => "world"}})
assert_raise BSON::InvalidKeyName do
@@coll.insert({"$hello" => "world"})
end
assert_raise BSON::InvalidKeyName do
@@coll.insert({"hello" => {"$hello" => "world"}})
end
@@coll.insert({"he$llo" => "world"})
@@coll.insert({"hello" => {"hell$o" => "world"}})
assert_raise BSON::InvalidKeyName do
@@coll.insert({".hello" => "world"})
end
assert_raise BSON::InvalidKeyName do
@@coll.insert({"hello" => {".hello" => "world"}})
end
assert_raise BSON::InvalidKeyName do
@@coll.insert({"hello." => "world"})
end
assert_raise BSON::InvalidKeyName do
@@coll.insert({"hello" => {"hello." => "world"}})
end
assert_raise BSON::InvalidKeyName do
@@coll.insert({"hel.lo" => "world"})
end
assert_raise BSON::InvalidKeyName do
@@coll.insert({"hello" => {"hel.lo" => "world"}})
end
end
def test_collection_names
assert_raise TypeError do
@@db.collection(5)