Skip to content

Commit

Permalink
Back to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Kahan committed Oct 11, 2016
1 parent b8d8e01 commit 260b36f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
5 changes: 5 additions & 0 deletions redis_cache/redis_cache.py
Expand Up @@ -54,6 +54,11 @@ def wrapper(*args, **kwargs):
return cache_inside

def invalidate_cache(self, cache_key):
"""
Creates the invalidator to be returned when requested to invalidate
the cache
:param cache_key: The cache key to invalidate
"""
self.redis_client.delete(cache_key)

def _default_signature_generator(*args, **kwargs):
Expand Down
14 changes: 10 additions & 4 deletions tests/test_redis_cache.py
Expand Up @@ -57,7 +57,7 @@ def test_cache_miss_kwargs(self, mock_client_object):
redis_cache = RedisCache(self.address, self.port)

# Create a function with the decorator
@redis_cache.cache()
@redis_cache.cache(invalidator=True)
def test_function(a, b, c=None, d=None):
return a

Expand All @@ -69,14 +69,19 @@ def cache_key_for(*args, **kwargs):
test_b = 'b'
test_c = True
test_d = False
function_response = test_function(test_a, test_b, c=test_c, d=test_d)
function_response, invalidator \
= test_function(test_a, test_b, c=test_c, d=test_d)

mock_client_object.assert_called_once_with(self.address, self.port)
expected_hash = cache_key_for(test_a, test_b, c=test_c, d=test_d)
mock_client.get.assert_called_once_with(expected_hash)
mock_client.setex.assert_called_once_with(
expected_hash, pickle.dumps(test_a), DEFAULT_EXPIRATION)

# Call the cache invalidator
invalidator()
mock_client.delete.assert_called_once_with(expected_hash)

@patch('redis_cache.redis_cache.RedisClient')
def test_cache_miss_expiration(self, mock_client_object):
"""
Expand Down Expand Up @@ -184,20 +189,21 @@ def test_redis_failure(self, mock_client_object):
redis_cache = RedisCache(self.address, self.port)

# Create a function with the decorator
@redis_cache.cache()
@redis_cache.cache(invalidator=True)
def test_function(a):
return a

def cache_key_for(*args, **kwargs):
return redis_cache._generate_cache_key(test_function, args, kwargs)

# Call that function
function_response = test_function(test_param)
function_response, invalidator = test_function(test_param)
expected_hash = cache_key_for(test_param)
mock_client_object.assert_called_once_with(self.address, self.port)
mock_client.get.assert_called_once_with(expected_hash)
self.assertEqual(mock_client.set.call_count, 0)
self.assertEqual(function_response, test_param)
self.assertIsNone(invalidator)

@patch('redis_cache.redis_cache.RedisClient')
def test_cache_custom_signature(self, mock_client_object):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_redis_client.py
Expand Up @@ -145,6 +145,29 @@ def socket_recv_side_effect(*args, **kwargs):
(len(key), key, len(value), value))
mock_socket.close.assert_called_once_with()

@patch('redis_cache.redis_client.socket')
def test_delete(self, mock_sock_lib):
"""
Tests DELETE
"""
key = 'something'
value = 'something_else'
mock_socket = Mock()
mock_sock_lib.socket.return_value = mock_socket

mock_socket.recv.return_value = ':%s\r\n' % value

cache_response = self.redis_client.delete(key)
self.assertEqual(cache_response, ':%s' % value)

mock_socket.recv.assert_called_with(self.redis_client.RECV_SIZE)
self.assertEqual(mock_socket.recv.call_count, 1)
mock_socket.connect.assert_called_once_with(
(self.address, self.port))
mock_socket.send.assert_called_once_with(
'*2\r\n$3\r\nDEL\r\n$9\r\n%s\r\n' % key)
mock_socket.close.assert_called_once_with()

@patch('redis_cache.redis_client.socket')
def test_setex(self, mock_sock_lib):
"""
Expand Down

0 comments on commit 260b36f

Please sign in to comment.