collection options()

This commit is contained in:
Mike Dirolf 2009-01-21 13:33:39 -05:00
parent 758d380080
commit 118e446e37
2 changed files with 32 additions and 0 deletions

View File

@ -333,3 +333,23 @@ class Collection(object):
for index in raw:
info[index["name"]] = index["key"].items()
return info
def options(self):
"""Get the options set on this collection.
Returns a dictionary of options and their values - see
`Database.create_collection` for more information on the options
dictionary. Returns an empty dictionary if the collection has not been
created yet.
"""
result = self.__database.system.namespaces.find_one(
{"name": self.full_name()})
if not result:
return {}
options = result.get("options", {})
if "create" in options:
del options["create"]
return options

View File

@ -102,5 +102,17 @@ class TestCollection(unittest.TestCase):
"hello_-1_world_1": [("hello", DESCENDING),
("world", ASCENDING)]})
def test_options(self):
db = self.db
db.drop_collection("test")
db.test.save({})
self.assertEqual(db.test.options(), {})
self.assertEqual(db.test.doesnotexist.options(), {})
db.drop_collection("test")
options = {"capped": True}
db.create_collection("test", options)
self.assertEqual(db.test.options(), options)
if __name__ == "__main__":
unittest.main()