async def test_store_should_propagate_redis_error_to_caller(self): cache = redis_cache.RedisCache(REDIS_HOST, REDIS_PORT) self.mock_redis.setex.side_effect = redis.RedisError() with self.assertRaises(redis.RedisError): await cache.add_cache_value(ODS_CODE, INTERACTION_ID, VALUE_DICTIONARY)
async def test_should_store_value_as_json(self): cache = redis_cache.RedisCache(REDIS_HOST, REDIS_PORT) await cache.add_cache_value(ODS_CODE, INTERACTION_ID, VALUE_DICTIONARY) self.mock_redis.setex.assert_called_with(CACHE_KEY, FIFTEEN_MINUTES_IN_SECONDS, VALUE_DICTIONARY_JSON)
async def test_should_return_none_if_value_does_not_exist(self): cache = redis_cache.RedisCache(REDIS_HOST, REDIS_PORT) self.mock_redis.get.return_value = None value = await cache.retrieve_mhs_attributes_value( ODS_CODE, INTERACTION_ID) self.assertIsNone(value)
async def test_should_retrieve_value_from_store_if_exists(self): cache = redis_cache.RedisCache(REDIS_HOST, REDIS_PORT) self.mock_redis.get.return_value = VALUE_DICTIONARY_JSON.encode() value = await cache.retrieve_mhs_attributes_value( ODS_CODE, INTERACTION_ID) self.assertEqual(value, VALUE_DICTIONARY) self.mock_redis.get.assert_called_with(CACHE_KEY)
async def test_store_should_use_custom_expiry_time_if_specified(self): custom_expiry_time = 27 cache = redis_cache.RedisCache(REDIS_HOST, REDIS_PORT, expiry_time=custom_expiry_time) await cache.add_cache_value(ODS_CODE, INTERACTION_ID, VALUE_DICTIONARY) self.mock_redis.setex.assert_called_with(CACHE_KEY, custom_expiry_time, VALUE_DICTIONARY_JSON)
def load_cache_implementation(): cache_expiry_time = int(config.get_config("SDS_CACHE_EXPIRY_TIME", cache_adaptor.FIFTEEN_MINUTES_IN_SECONDS)) redis_host = config.get_config("SDS_REDIS_CACHE_HOST") redis_port = int(config.get_config("SDS_REDIS_CACHE_PORT", "6379")) disable_tls_flag = config.get_config("SDS_REDIS_DISABLE_TLS", None) use_tls = disable_tls_flag != "True" logger.info('Using the Redis cache with {redis_host}, {redis_port}, {cache_expiry_time}, {use_tls}', fparams={ 'redis_host': redis_host, 'redis_port': redis_port, 'cache_expiry_time': cache_expiry_time, 'use_tls': use_tls }) return redis_cache.RedisCache(redis_host, redis_port, cache_expiry_time, use_tls)
async def test_should_raise_exception_if_fails_to_retrieve_value(self): cache = redis_cache.RedisCache(REDIS_HOST, REDIS_PORT) self.mock_redis.get.side_effect = redis.RedisError with (self.assertRaises(redis.RedisError)): await cache.retrieve_mhs_attributes_value(ODS_CODE, INTERACTION_ID)
def test_tls_is_enabled_by_default(self): redis_cache.RedisCache(REDIS_HOST, REDIS_PORT) self.mock_redis_constructor.assert_called_with(host=REDIS_HOST, port=REDIS_PORT, ssl=True)
def test_redis_params_are_passed(self): redis_cache.RedisCache(REDIS_HOST, REDIS_PORT, use_tls=USE_TLS) self.mock_redis_constructor.assert_called_with(host=REDIS_HOST, port=REDIS_PORT, ssl=USE_TLS)
async def test_should_only_accept_positive_expiry_times(self): with self.assertRaises(ValueError): redis_cache.RedisCache(REDIS_HOST, REDIS_PORT, -1)