def test_updates_collection_list(self): with self.real_app.app_context(): db = get_db() res_id = 'myresid.' # Setup resource id record clients_collection = db[CLIENTS_COLLECTION] clients_collection.remove({'res_id': res_id}) clients_collection.insert({'res_id': res_id, 'collections': []}) with UseResId(res_id) as db: self.assertItemsEqual(get_collection_names(res_id), []) db.foo.insert({'message': 'test'}) self.assertItemsEqual(get_collection_names(res_id), ['foo']) self.assertItemsEqual(list(db.foo.find({}, {'_id': 0})), [{ 'message': 'test' }]) db.bar.update({}, {'message': 'test'}, upsert=True) self.assertItemsEqual(get_collection_names(res_id), ['foo', 'bar']) self.assertItemsEqual(list(db.bar.find({}, {'_id': 0})), [{ 'message': 'test' }]) db.foo.drop() self.assertItemsEqual(get_collection_names(res_id), ['bar']) self.assertNotIn(res_id + 'foo', get_collection_names(res_id))
def test_updates_collection_list(self): with self.real_app.app_context(): db = get_db() res_id = 'myresid.' # Setup resource id record clients_collection = db[CLIENTS_COLLECTION] clients_collection.remove({'res_id': res_id}) clients_collection.insert({ 'res_id': res_id, 'collections': [] }) with UseResId(res_id) as db: self.assertItemsEqual(get_collection_names(res_id), []) db.foo.insert({'message': 'test'}) self.assertItemsEqual(get_collection_names(res_id), ['foo']) self.assertItemsEqual(list(db.foo.find({}, {'_id': 0})), [{'message': 'test'}]) db.bar.update({}, {'message': 'test'}, upsert=True) self.assertItemsEqual(get_collection_names(res_id), ['foo', 'bar']) self.assertItemsEqual(list(db.bar.find({}, {'_id': 0})), [{'message': 'test'}]) db.foo.drop() self.assertItemsEqual(get_collection_names(res_id), ['bar']) self.assertNotIn(res_id + 'foo', get_collection_names(res_id))
def db_drop(res_id): DB = get_db() collections = get_collection_names(res_id) with UseResId(res_id): for c in collections: DB.drop_collection(c) return empty_success()
def setUp(self): super(QuotaCollectionsTestCase, self).setUp() self.old_quota = self.real_app.config['QUOTA_NUM_COLLECTIONS'] self.res_id = 'myresid.' with self.real_app.app_context(): collections = get_collection_names(self.res_id) with UseResId(self.res_id) as db: for c in collections: db.drop_collection(c)
def test_drop_db(self): testdoc = {'name': 'Mongo'} colls = ['a', 'b', 'c'] update = {'$addToSet': {'collections': {'$each': colls}}} self.db[CLIENTS_COLLECTION].update({'res_id': self.res_id}, update) colls = [to_coll_name(self.res_id, c) for c in colls] for col in colls: self.db[col].insert(testdoc) actual_colls = self.db.collection_names() for col in colls: self.assertIn(col, actual_colls) self.make_db_drop_request() actual_colls = self.db.collection_names() for col in colls: self.assertNotIn(col, actual_colls) self.assertItemsEqual(get_collection_names(self.res_id), [])
def test_drop_db(self): testdoc = {"name": "Mongo"} colls = ["a", "b", "c"] update = {"$addToSet": {"collections": {"$each": colls}}} self.db[CLIENTS_COLLECTION].update({"res_id": self.res_id}, update) colls = [get_internal_coll_name(self.res_id, c) for c in colls] for c in colls: self.db[c].insert(testdoc) actual_colls = self.db.collection_names() for c in colls: self.assertIn(c, actual_colls) self.make_db_drop_request() actual_colls = self.db.collection_names() for c in colls: self.assertNotIn(c, actual_colls) self.assertItemsEqual(get_collection_names(self.res_id), [])
def db_get_collection_names(res_id): return to_json({'result': get_collection_names(res_id)})
def cleanup_collections(res_id): db = get_db() for coll in get_collection_names(res_id): with UseResId(res_id): _logger.info('dropping %s', coll) db.drop_collection(coll)