Skip to content

Commit

Permalink
add InvalidName exception and use that where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Dirolf committed Aug 13, 2009
1 parent a41a88d commit 431039a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 33 deletions.
7 changes: 5 additions & 2 deletions ext/cbson/cbson.c
Expand Up @@ -49,6 +49,7 @@ static VALUE DBRef;
static VALUE Code;
static VALUE RegexpOfHolding;
static VALUE OrderedHash;
static VALUE InvalidName;

// this sucks. but for some reason these moved around between 1.8 and 1.9
#ifdef ONIGURUMA_H
Expand Down Expand Up @@ -183,11 +184,11 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
if (check_keys == Qtrue) {
int i;
if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') {
rb_raise(rb_eRuntimeError, "key must not start with '$'");
rb_raise(InvalidName, "key must not start with '$'");
}
for (i = 0; i < RSTRING_LEN(key); i++) {
if (RSTRING_PTR(key)[i] == '.') {
rb_raise(rb_eRuntimeError, "key must not contain '.'");
rb_raise(InvalidName, "key must not contain '.'");
}
}
}
Expand Down Expand Up @@ -755,6 +756,8 @@ void Init_cbson() {
Code = rb_const_get(driver, rb_intern("Code"));
rb_require("mongo/types/regexp_of_holding");
RegexpOfHolding = rb_const_get(driver, rb_intern("RegexpOfHolding"));
rb_require("mongo/errors");
InvalidName = rb_const_get(driver, rb_intern("InvalidName"));
rb_require("mongo/util/ordered_hash");
OrderedHash = rb_const_get(rb_cObject, rb_intern("OrderedHash"));

Expand Down
18 changes: 9 additions & 9 deletions lib/mongo/collection.rb
Expand Up @@ -29,19 +29,19 @@ def initialize(db, name)
case name
when Symbol, String
else
raise RuntimeError, "new_name must be a string or symbol"
raise TypeError, "new_name must be a string or symbol"
end

name = name.to_s

if name.empty? or name.include? ".."
raise RuntimeError, "collection names cannot be empty"
raise InvalidName, "collection names cannot be empty"
end
if name.include? "$" and not name.match(/^\$cmd/)
raise RuntimeError, "collection names must not contain '$'"
raise InvalidName, "collection names must not contain '$'"
end
if name.match(/^\./) or name.match(/\.$/)
raise RuntimeError, "collection names must not start or end with '.'"
raise InvalidName, "collection names must not start or end with '.'"
end

@db, @name = db, name
Expand Down Expand Up @@ -256,27 +256,27 @@ def group(keys, condition, initial, reduce)
# Rename this collection.
#
# If operating in auth mode, client must be authorized as an admin to
# perform this operation. Raises an error if +new_name+ is an invalid
# perform this operation. Raises +InvalidName+ if +new_name+ is an invalid
# collection name.
#
# :new_name :: new name for this collection
def rename(new_name)
case new_name
when Symbol, String
else
raise RuntimeError, "new_name must be a string or symbol"
raise TypeError, "new_name must be a string or symbol"
end

new_name = new_name.to_s

if new_name.empty? or new_name.include? ".."
raise RuntimeError, "collection names cannot be empty"
raise InvalidName, "collection names cannot be empty"
end
if new_name.include? "$"
raise RuntimeError, "collection names must not contain '$'"
raise InvalidName, "collection names must not contain '$'"
end
if new_name.match(/^\./) or new_name.match(/\.$/)
raise RuntimeError, "collection names must not start or end with '.'"
raise InvalidName, "collection names must not start or end with '.'"
end

@db.rename_collection(@name, new_name)
Expand Down
3 changes: 3 additions & 0 deletions lib/mongo/errors.rb
Expand Up @@ -19,6 +19,9 @@ module Mongo
module Driver
# Raised when a database operation fails.
class OperationFailure < RuntimeError; end

# Raised when an invalid name is used.
class InvalidName < RuntimeError; end
end
end
end
4 changes: 2 additions & 2 deletions lib/mongo/util/bson.rb
Expand Up @@ -104,10 +104,10 @@ def serialize_key_value(k, v, check_keys)
k = k.to_s
if check_keys
if k[0] == ?$
raise RuntimeError.new("key #{k} must not start with '$'")
raise InvalidName.new("key #{k} must not start with '$'")
end
if k.include? ?.
raise RuntimeError.new("key #{k} must not contain '.'")
raise InvalidName.new("key #{k} must not contain '.'")
end
end
type = bson_type(v)
Expand Down
40 changes: 20 additions & 20 deletions tests/test_db_api.rb
Expand Up @@ -690,55 +690,55 @@ def test_invalid_key_names
@@coll.insert({"hello" => "world"})
@@coll.insert({"hello" => {"hello" => "world"}})

assert_raise RuntimeError do
assert_raise InvalidName do
@@coll.insert({"$hello" => "world"})
end
assert_raise RuntimeError do
assert_raise InvalidName do
@@coll.insert({"hello" => {"$hello" => "world"}})
end

@@coll.insert({"he$llo" => "world"})
@@coll.insert({"hello" => {"hell$o" => "world"}})

assert_raise RuntimeError do
assert_raise InvalidName do
@@coll.insert({".hello" => "world"})
end
assert_raise RuntimeError do
assert_raise InvalidName do
@@coll.insert({"hello" => {".hello" => "world"}})
end
assert_raise RuntimeError do
assert_raise InvalidName do
@@coll.insert({"hello." => "world"})
end
assert_raise RuntimeError do
assert_raise InvalidName do
@@coll.insert({"hello" => {"hello." => "world"}})
end
assert_raise RuntimeError do
assert_raise InvalidName do
@@coll.insert({"hel.lo" => "world"})
end
assert_raise RuntimeError do
assert_raise InvalidName do
@@coll.insert({"hello" => {"hel.lo" => "world"}})
end

@@coll.modify({"hello" => "world"}, {"$inc" => "hello"})
end

def test_collection_names
assert_raise RuntimeError do
assert_raise TypeError do
@@db.collection(5)
end
assert_raise RuntimeError do
assert_raise InvalidName do
@@db.collection("")
end
assert_raise RuntimeError do
assert_raise InvalidName do
@@db.collection("te$t")
end
assert_raise RuntimeError do
assert_raise InvalidName do
@@db.collection(".test")
end
assert_raise RuntimeError do
assert_raise InvalidName do
@@db.collection("test.")
end
assert_raise RuntimeError do
assert_raise InvalidName do
@@db.collection("tes..t")
end
end
Expand All @@ -749,22 +749,22 @@ def test_rename_collection
a = @@db.collection("foo")
b = @@db.collection("bar")

assert_raise RuntimeError do
assert_raise TypeError do
a.rename(5)
end
assert_raise RuntimeError do
assert_raise InvalidName do
a.rename("")
end
assert_raise RuntimeError do
assert_raise InvalidName do
a.rename("te$t")
end
assert_raise RuntimeError do
assert_raise InvalidName do
a.rename(".test")
end
assert_raise RuntimeError do
assert_raise InvalidName do
a.rename("test.")
end
assert_raise RuntimeError do
assert_raise InvalidName do
a.rename("tes..t")
end

Expand Down

0 comments on commit 431039a

Please sign in to comment.