collection options()
This commit is contained in:
parent
758d380080
commit
118e446e37
@ -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
|
||||
|
||||
@ -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()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user