Skip to content

Commit

Permalink
fix: deletion of items that don't implement truthiness (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeppeKlitgaard committed Oct 15, 2020
1 parent 628a575 commit a966f84
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion aiocache/backends/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async def _redlock_release(self, key, value):

@classmethod
def __delete(cls, key):
if cls._cache.pop(key, None):
if cls._cache.pop(key, None) is not None:
handle = cls._handlers.pop(key, None)
if handle:
handle.cancel()
Expand Down
14 changes: 14 additions & 0 deletions tests/ut/backends/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ async def test_delete_missing(self, memory):
await memory._delete(pytest.KEY)
SimpleMemoryBackend._cache.pop.assert_called_with(pytest.KEY, None)

@pytest.mark.asyncio
async def test_delete_non_truthy(self, memory):
non_truthy = MagicMock()
non_truthy.__bool__.side_effect = ValueError("Does not implement truthiness")

with pytest.raises(ValueError):
bool(non_truthy)

SimpleMemoryBackend._cache.pop.return_value = non_truthy
await memory._delete(pytest.KEY)

assert non_truthy.__bool__.call_count == 1
SimpleMemoryBackend._cache.pop.assert_called_with(pytest.KEY, None)

@pytest.mark.asyncio
async def test_clear_namespace(self, memory):
SimpleMemoryBackend._cache.__iter__.return_value = iter(["nma", "nmb", "no"])
Expand Down

0 comments on commit a966f84

Please sign in to comment.