From 691e65f68410a155ca171cdf4519edfd700660c9 Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Thu, 6 Aug 2009 15:52:07 -0400 Subject: [PATCH] API CHANGE: fixing DB#collection_names() - only return collection name, not full name. only return names of valid collections on this database --- lib/mongo/db.rb | 13 ++++++------- tests/test_db.rb | 12 ++++++++++++ tests/test_db_api.rb | 10 +++++----- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 1fc4d4b..6931424 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -173,12 +173,11 @@ module XGen raise "error logging out: #{doc.inspect}" unless ok?(doc) end - # Returns an array of collection names. Each name is of the form - # "database_name.collection_name". + # Returns an array of collection names in this database. def collection_names names = collections_info.collect { |doc| doc['name'] || '' } - names.delete('') - names + names = names.delete_if {|name| name.index(@name).nil? || name.index('$')} + names.map {|name| name.sub(@name + '.', '')} end # Returns a cursor over query result hashes. Each hash contains a @@ -205,7 +204,7 @@ module XGen # :max :: Max number of records in a capped collection. Optional. def create_collection(name, options={}) # First check existence - if collection_names.include?(full_coll_name(name)) + if collection_names.include?(name) if strict? raise "Collection #{name} already exists. Currently in strict mode." else @@ -230,14 +229,14 @@ module XGen # new collection. If +strict+ is true, will raise an error if # collection +name+ does not already exists. def collection(name) - return Collection.new(self, name) if !strict? || collection_names.include?(full_coll_name(name)) + return Collection.new(self, name) if !strict? || collection_names.include?(name) raise "Collection #{name} doesn't exist. Currently in strict mode." end # Drop collection +name+. Returns +true+ on success or if the # collection does not exist, +false+ otherwise. def drop_collection(name) - return true unless collection_names.include?(full_coll_name(name)) + return true unless collection_names.include?(name) ok?(db_command(:drop => name)) end diff --git a/tests/test_db.rb b/tests/test_db.rb index 68a13f6..db2ed2c 100644 --- a/tests/test_db.rb +++ b/tests/test_db.rb @@ -51,6 +51,18 @@ class DBTest < Test::Unit::TestCase assert_equal 'ruby-mongo-test.test', @@db.full_coll_name(coll.name) end + def test_collection_names + @@db.collection("test").insert("foo" => 5) + @@db.collection("test.mike").insert("bar" => 0) + + colls = @@db.collection_names() + assert colls.include?("test") + assert colls.include?("test.mike") + colls.each { |name| + assert !name.include?("$") + } + end + def test_pair @@db.close @@users = nil diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index ac9d846..876445a 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -241,25 +241,25 @@ class DBAPITest < Test::Unit::TestCase def test_drop_collection assert @@db.drop_collection(@@coll.name), "drop of collection #{@@coll.name} failed" - assert !@@db.collection_names.include?(@@coll_full_name) + assert !@@db.collection_names.include?(@@coll.name) end def test_other_drop - assert @@db.collection_names.include?(@@coll_full_name) + assert @@db.collection_names.include?(@@coll.name) @@coll.drop - assert !@@db.collection_names.include?(@@coll_full_name) + assert !@@db.collection_names.include?(@@coll.name) end def test_collection_names names = @@db.collection_names assert names.length >= 1 - assert names.include?(@@coll_full_name) + assert names.include?(@@coll.name) coll2 = @@db.collection('test2') coll2.insert('a' => 1) # collection not created until it's used names = @@db.collection_names assert names.length >= 2 - assert names.include?(@@coll_full_name) + assert names.include?(@@coll.name) assert names.include?('ruby-mongo-test.test2') ensure @@db.drop_collection('test2')