def test_drop_collection(self): """Ensure that the collection may be dropped from the database.""" collection_name = "person" self.Person(name="Test").save() assert collection_name in list_collection_names(self.db) self.Person.drop_collection() assert collection_name not in list_collection_names(self.db)
def test_drop_collection(self): """Ensure that the collection may be dropped from the database. """ collection_name = 'person' self.Person(name='Test').save() self.assertIn(collection_name, list_collection_names(self.db)) self.Person.drop_collection() self.assertNotIn(collection_name, list_collection_names(self.db))
def test_drop_collection(self): """Ensure that the collection may be dropped from the database. """ collection_name = 'person' self.Person(name='Test').save() self.assertIn(collection_name, list_collection_names(self.db)) self.Person.drop_collection() self.assertNotIn(collection_name, list_collection_names(self.db))
def _get_capped_collection(cls): """Create a new or get an existing capped PyMongo collection.""" db = cls._get_db() collection_name = cls._get_collection_name() # Get max document limit and max byte size from meta. max_size = cls._meta.get('max_size') or 10 * 2**20 # 10MB default max_documents = cls._meta.get('max_documents') # MongoDB will automatically raise the size to make it a multiple of # 256 bytes. We raise it here ourselves to be able to reliably compare # the options below. if max_size % 256: max_size = (max_size // 256 + 1) * 256 # If the collection already exists and has different options # (i.e. isn't capped or has different max/size), raise an error. if collection_name in list_collection_names( db, include_system_collections=True): collection = db[collection_name] options = collection.options() if (options.get('max') != max_documents or options.get('size') != max_size): raise InvalidCollectionError( 'Cannot create collection "{}" as a capped ' 'collection as it already exists'.format(cls._collection)) return collection # Create a new capped collection. opts = {'capped': True, 'size': max_size} if max_documents: opts['max'] = max_documents return db.create_collection(collection_name, **opts)
def test_custom_collection_name_operations(self): """Ensure that a collection with a specified name is used as expected.""" collection_name = "personCollTest" class Person(Document): name = StringField() meta = {"collection": collection_name} Person(name="Test User").save() assert collection_name in list_collection_names(self.db) user_obj = self.db[collection_name].find_one() assert user_obj["name"] == "Test User" user_obj = Person.objects[0] assert user_obj.name == "Test User" Person.drop_collection() assert collection_name not in list_collection_names(self.db)
def test_custom_collection_name_operations(self): """Ensure that a collection with a specified name is used as expected. """ collection_name = "personCollTest" class Person(Document): name = StringField() meta = {"collection": collection_name} Person(name="Test User").save() self.assertIn(collection_name, list_collection_names(self.db)) user_obj = self.db[collection_name].find_one() self.assertEqual(user_obj["name"], "Test User") user_obj = Person.objects[0] self.assertEqual(user_obj.name, "Test User") Person.drop_collection() self.assertNotIn(collection_name, list_collection_names(self.db))
def test_custom_collection_name_operations(self): """Ensure that a collection with a specified name is used as expected. """ collection_name = 'personCollTest' class Person(Document): name = StringField() meta = {'collection': collection_name} Person(name="Test User").save() self.assertIn(collection_name, list_collection_names(self.db)) user_obj = self.db[collection_name].find_one() self.assertEqual(user_obj['name'], "Test User") user_obj = Person.objects[0] self.assertEqual(user_obj.name, "Test User") Person.drop_collection() self.assertNotIn(collection_name, list_collection_names(self.db))
def _get_capped_collection(cls): """Create a new or get an existing capped PyMongo collection.""" db = cls._get_db() collection_name = cls._get_collection_name() # Get max document limit and max byte size from meta. max_size = cls._meta.get('max_size') or 10 * 2 ** 20 # 10MB default max_documents = cls._meta.get('max_documents') # MongoDB will automatically raise the size to make it a multiple of # 256 bytes. We raise it here ourselves to be able to reliably compare # the options below. if max_size % 256: max_size = (max_size // 256 + 1) * 256 # If the collection already exists and has different options # (i.e. isn't capped or has different max/size), raise an error. if collection_name in list_collection_names(db, include_system_collections=True): collection = db[collection_name] options = collection.options() if ( options.get('max') != max_documents or options.get('size') != max_size ): raise InvalidCollectionError( 'Cannot create collection "{}" as a capped ' 'collection as it already exists'.format(cls._collection) ) return collection # Create a new capped collection. opts = {'capped': True, 'size': max_size} if max_documents: opts['max'] = max_documents return db.create_collection(collection_name, **opts)
def tearDown(self): for collection in list_collection_names(self.db): self.db.drop_collection(collection)
def tearDown(self): for collection in list_collection_names(self.db): self.db.drop_collection(collection)