Skip to content

Commit

Permalink
RUBY-233 show invalid key on invalid key exception
Browse files Browse the repository at this point in the history
  • Loading branch information
banker committed Feb 4, 2011
1 parent 0c574b9 commit 76730d4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
4 changes: 2 additions & 2 deletions ext/cbson/cbson.c
Expand Up @@ -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));
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions test/bson/bson_test.rb
Expand Up @@ -562,4 +562,47 @@ def test_move_id_with_nested_doc
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
37 changes: 0 additions & 37 deletions test/db_api_test.rb
Expand Up @@ -621,43 +621,6 @@ def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection
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)
Expand Down

0 comments on commit 76730d4

Please sign in to comment.