From 0051b9446a0944c6d1ef9f3e80fdd455c8cf310e Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Mon, 9 May 2011 12:28:07 -0400 Subject: [PATCH] RUBY-260 methods using DB#collection_names should account for symbols. --- lib/mongo/collection.rb | 5 +++-- lib/mongo/db.rb | 34 +++++++++++++++++++--------------- test/db_test.rb | 25 +++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index d922aa8..cd1b51f 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -91,7 +91,7 @@ module Mongo # Return a sub-collection of this collection by name. If 'users' is a collection, then # 'users.comments' is a sub-collection of users. # - # @param [String] name + # @param [String, Symbol] name # the collection to return # # @raise [Mongo::InvalidNSName] @@ -101,7 +101,8 @@ module Mongo # the specified sub-collection def [](name) name = "#{self.name}.#{name}" - return Collection.new(name, db) if !db.strict? || db.collection_names.include?(name) + return Collection.new(name, db) if !db.strict? || + db.collection_names.include?(name.to_s) raise "Collection #{name} doesn't exist. Currently in strict mode." end diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 7f7b443..6fe6496 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -251,26 +251,28 @@ module Mongo # new collection. If +strict+ is true, will raise an error if # collection +name+ already exists. # - # @param [String] name the name of the new collection. + # @param [String, Symbol] name the name of the new collection. # # @option opts [Boolean] :capped (False) created a capped collection. # - # @option opts [Integer] :size (Nil) If +capped+ is +true+, specifies the maximum number of - # bytes for the capped collection. If +false+, specifies the number of bytes allocated + # @option opts [Integer] :size (Nil) If +capped+ is +true+, + # specifies the maximum number of bytes for the capped collection. + # If +false+, specifies the number of bytes allocated # for the initial extent of the collection. # - # @option opts [Integer] :max (Nil) If +capped+ is +true+, indicates the maximum number of records - # in a capped collection. + # @option opts [Integer] :max (Nil) If +capped+ is +true+, indicates + # the maximum number of records in a capped collection. # - # @raise [MongoDBError] raised under two conditions: either we're in +strict+ mode and the collection + # @raise [MongoDBError] raised under two conditions: + # either we're in +strict+ mode and the collection # already exists or collection creation fails on the server. # # @return [Mongo::Collection] def create_collection(name, opts={}) - # Does the collection already exist? - if collection_names.include?(name) + if collection_names.include?(name.to_s) if strict? - raise MongoDBError, "Collection #{name} already exists. Currently in strict mode." + raise MongoDBError, "Collection #{name} already exists. " + + "Currently in strict mode." else return Collection.new(name, self, opts) end @@ -286,15 +288,17 @@ module Mongo # Get a collection by name. # - # @param [String] name the collection name. + # @param [String, Symbol] name the collection name. # @param [Hash] opts any valid options that can be passed to Collection#new. # - # @raise [MongoDBError] if collection does not already exist and we're in +strict+ mode. + # @raise [MongoDBError] if collection does not already exist and we're in + # +strict+ mode. # # @return [Mongo::Collection] def collection(name, opts={}) - if strict? && !collection_names.include?(name) - raise Mongo::MongoDBError, "Collection #{name} doesn't exist. Currently in strict mode." + if strict? && !collection_names.include?(name.to_s) + raise Mongo::MongoDBError, "Collection #{name} doesn't exist. " + + "Currently in strict mode." else opts = opts.dup opts[:safe] = opts.fetch(:safe, @safe) @@ -306,11 +310,11 @@ module Mongo # Drop a collection by +name+. # - # @param [String] name + # @param [String, Symbol] name # # @return [Boolean] +true+ on success or +false+ if the collection name doesn't exist. def drop_collection(name) - return true unless collection_names.include?(name) + return true unless collection_names.include?(name.to_s) ok?(command(:drop => name)) end diff --git a/test/db_test.rb b/test/db_test.rb index 3001001..0154592 100644 --- a/test/db_test.rb +++ b/test/db_test.rb @@ -32,14 +32,35 @@ class DBTest < Test::Unit::TestCase @@users = @@db.collection('system.users') end end - + + def test_create_collection + col = @@db.create_collection('foo') + assert_equal @@db['foo'].name, col.name + + col = @@db.create_collection(:foo) + assert_equal @@db['foo'].name, col.name + + @@db.drop_collection('foo') + end + + def test_get_and_drop_collection + db = @@conn.db(MONGO_TEST_DB, :strict => true) + db.create_collection('foo') + assert db.collection('foo') + assert db.drop_collection('foo') + + db.create_collection(:foo) + assert db.collection(:foo) + assert db.drop_collection(:foo) + end + def test_logger output = StringIO.new logger = Logger.new(output) logger.level = Logger::DEBUG conn = standard_connection(:logger => logger) assert_equal logger, conn.logger - + conn.logger.debug 'testing' assert output.string.include?('testing') end