def get_collection_size(res_id, collection_name): coll = get_internal_coll_name(res_id, collection_name) try: return get_db().command({'collstats': coll})['size'] except OperationFailure as e: if 'ns not found' in e.message: return 0 else: raise MWSServerError(500, e.message)
def test_removes_old_sessions_and_associated_collections(self, datetime_mock): dates = [ datetime(2012, 7, 2), datetime(2012, 7, 3), datetime(2012, 7, 4), datetime(2012, 7, 5), ] for session_res_id, date in enumerate(dates): collections = [] for collection in xrange(3): collections.append(collection) collection_name = get_internal_coll_name(session_res_id, collection) self.db[collection_name].insert({'foo': 'barr'}) self.db.clients.insert({ 'session_id': session_res_id, 'res_id': session_res_id, 'collections': collections, 'timestamp': date, }) # Want to get rid of everything before 2012/7/4 delta = timedelta(seconds=crontab.EXPIRE_SESSION_DURATION) datetime_mock.now.return_value = datetime(2012, 7, 4) - delta crontab.expire_sessions(self.real_app) # Should grab all remaining records we inserted res = self.db.clients.find({ 'timestamp': {'$lt': self.test_before_date} }) self.assertEqual(res.count(), 2) actual_dates = [r['timestamp'] for r in res] expected_dates = dates[-2:] # Only the last two should survive self.assertItemsEqual(actual_dates, expected_dates) # Make sure collections were dropped coll_names = self.db.collection_names() self.assertNotIn('00', coll_names) self.assertNotIn('01', coll_names) self.assertNotIn('02', coll_names) self.assertNotIn('10', coll_names) self.assertNotIn('11', coll_names) self.assertNotIn('12', coll_names) self.assertIn('20', coll_names) self.assertIn('21', coll_names) self.assertIn('22', coll_names) self.assertIn('30', coll_names) self.assertIn('31', coll_names) self.assertIn('32', coll_names) for name in ['20', '21', '22', '30', '31', '32']: self.db[name].drop()
def setUp(self): super(DBCollectionTestCase, self).setUp() self.coll_name = 'test_collection' self.internal_coll_name = get_internal_coll_name( self.res_id, self.coll_name) self.db = get_db() self.db_collection = self.db[self.internal_coll_name] self.make_request_url = '/mws/%s/db/%s/%%s' % \ (self.res_id, self.coll_name)
def setUp(self): super(DBCollectionTestCase, self).setUp() self.coll_name = 'test_collection' self.internal_coll_name = get_internal_coll_name(self.res_id, self.coll_name) self.db = get_db() self.db_collection = self.db[self.internal_coll_name] self.make_request_url = '/mws/%s/db/%s/%%s' % \ (self.res_id, self.coll_name)
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), [])