diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index fce1be3..790065a 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -26,6 +26,24 @@ module XGen attr_reader :db, :name, :hint def initialize(db, name) + case name + when Symbol, String + else + raise RuntimeError, "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" + end + if name.include? "$" and not name.match(/^\$cmd/) + raise RuntimeError, "collection names must not contain '$'" + end + if name.match(/^\./) or name.match(/\.$/) + raise RuntimeError, "collection names must not start or end with '.'" + end + @db, @name = db, name @hint = nil end diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 0089fea..ac9d846 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -713,6 +713,27 @@ class DBAPITest < Test::Unit::TestCase @@coll.modify({"hello" => "world"}, {"$inc" => "hello"}) end + def test_collection_names + assert_raise RuntimeError do + @@db.collection(5) + end + assert_raise RuntimeError do + @@db.collection("") + end + assert_raise RuntimeError do + @@db.collection("te$t") + end + assert_raise RuntimeError do + @@db.collection(".test") + end + assert_raise RuntimeError do + @@db.collection("test.") + end + assert_raise RuntimeError do + @@db.collection("tes..t") + end + end + def test_rename_collection @@db.drop_collection("foo") @@db.drop_collection("bar")