Example #1
0
 def test_get_object_id(self):
     "Test get_object_id function"
     api = 'test'
     params = {'foo': 'bar'}
     expected = ObjectId('09f51db4a7ebda7328d11bd4')
     result = get_object_id(str(api) + str(params))
     self.assertEqual(result, expected)
Example #2
0
 def insert_cache(self, coll, api, params=dict(), data=dict()):
     """
     Insert data into collection
     Collection should be the service it is caching data for
     Use update to have the possibility to force cache update
     """
     result = list()
     db_coll = self.db[coll]
     object_id = get_object_id(str(api)+str(params))
     data['_id'] = object_id
     data['datetime'] = datetime_day(datetime.datetime.utcnow())
     for i in range(2):
         try:
             result = db_coll.replace_one({'_id':object_id}, data, upsert=True)
         except DocumentTooLarge:
             self.logger.warning('DocumentTooLarge error for %s api %s', coll, api)
             break
         except AutoReconnect:
             call(["start_mongodb", self.OPT_PATH])
             continue
         else:
             if (result.modified_count == 0) and (not result.upserted_id):
                 self.logger.debug('Failed to insert %s cache for api %s\n\tData: %s', coll, api, str(data))
             break
     else:
         self.logger.error("Couldn't establish connection to mongodb server %s", self.URI)
     return result
Example #3
0
 def test_get_object_id(self):
     "Test get_object_id function"
     api = 'test'
     params = {'foo':'bar'}
     expected = ObjectId('09f51db4a7ebda7328d11bd4')
     result = get_object_id(str(api)+str(params))
     self.assertEqual(result, expected)
Example #4
0
 def get_cache(self, coll, api, params=dict()):
     """
     Fetch cached data for a service
     Collection should be name of service
     """
     data = dict()
     db_coll = self.db[coll]
     object_id = get_object_id(str(api)+str(params))
     for i in range(2):
         try:
             data = db_coll.find_one({'_id':object_id}, {'_id':0})
         except AutoReconnect:
             call(["start_mongodb", self.OPT_PATH])
             continue
         else:
             break
     else:
         self.logger.error("Couldn't establish connection to mongodb server %s", self.URI)
     return data