API CHANGE: fixing DB#collection_names() - only return collection name, not full name. only return names of valid collections on this database

This commit is contained in:
Mike Dirolf 2009-08-06 15:52:07 -04:00
parent fe7d7745c7
commit 691e65f684
3 changed files with 23 additions and 12 deletions

View File

@ -173,12 +173,11 @@ module XGen
raise "error logging out: #{doc.inspect}" unless ok?(doc) raise "error logging out: #{doc.inspect}" unless ok?(doc)
end end
# Returns an array of collection names. Each name is of the form # Returns an array of collection names in this database.
# "database_name.collection_name".
def collection_names def collection_names
names = collections_info.collect { |doc| doc['name'] || '' } names = collections_info.collect { |doc| doc['name'] || '' }
names.delete('') names = names.delete_if {|name| name.index(@name).nil? || name.index('$')}
names names.map {|name| name.sub(@name + '.', '')}
end end
# Returns a cursor over query result hashes. Each hash contains a # 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. # :max :: Max number of records in a capped collection. Optional.
def create_collection(name, options={}) def create_collection(name, options={})
# First check existence # First check existence
if collection_names.include?(full_coll_name(name)) if collection_names.include?(name)
if strict? if strict?
raise "Collection #{name} already exists. Currently in strict mode." raise "Collection #{name} already exists. Currently in strict mode."
else else
@ -230,14 +229,14 @@ module XGen
# new collection. If +strict+ is true, will raise an error if # new collection. If +strict+ is true, will raise an error if
# collection +name+ does not already exists. # collection +name+ does not already exists.
def collection(name) 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." raise "Collection #{name} doesn't exist. Currently in strict mode."
end end
# Drop collection +name+. Returns +true+ on success or if the # Drop collection +name+. Returns +true+ on success or if the
# collection does not exist, +false+ otherwise. # collection does not exist, +false+ otherwise.
def drop_collection(name) 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)) ok?(db_command(:drop => name))
end end

View File

@ -51,6 +51,18 @@ class DBTest < Test::Unit::TestCase
assert_equal 'ruby-mongo-test.test', @@db.full_coll_name(coll.name) assert_equal 'ruby-mongo-test.test', @@db.full_coll_name(coll.name)
end 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 def test_pair
@@db.close @@db.close
@@users = nil @@users = nil

View File

@ -241,25 +241,25 @@ class DBAPITest < Test::Unit::TestCase
def test_drop_collection def test_drop_collection
assert @@db.drop_collection(@@coll.name), "drop of collection #{@@coll.name} failed" 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 end
def test_other_drop def test_other_drop
assert @@db.collection_names.include?(@@coll_full_name) assert @@db.collection_names.include?(@@coll.name)
@@coll.drop @@coll.drop
assert !@@db.collection_names.include?(@@coll_full_name) assert !@@db.collection_names.include?(@@coll.name)
end end
def test_collection_names def test_collection_names
names = @@db.collection_names names = @@db.collection_names
assert names.length >= 1 assert names.length >= 1
assert names.include?(@@coll_full_name) assert names.include?(@@coll.name)
coll2 = @@db.collection('test2') coll2 = @@db.collection('test2')
coll2.insert('a' => 1) # collection not created until it's used coll2.insert('a' => 1) # collection not created until it's used
names = @@db.collection_names names = @@db.collection_names
assert names.length >= 2 assert names.length >= 2
assert names.include?(@@coll_full_name) assert names.include?(@@coll.name)
assert names.include?('ruby-mongo-test.test2') assert names.include?('ruby-mongo-test.test2')
ensure ensure
@@db.drop_collection('test2') @@db.drop_collection('test2')