Skip to content

Commit

Permalink
Add more tests for CacheBackend; closes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Feb 28, 2021
1 parent 9de870c commit 685adf6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
2 changes: 1 addition & 1 deletion aiohttp_client_cache/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def is_cacheable(self, response: Union[AnyResponse, None]) -> bool:
if not response:
return False
cache_criteria = {
'allowed status': response.status in self.allowed_codes,
'allowed method': response.method in self.allowed_methods,
'allowed status': response.status in self.allowed_codes,
'not disabled': not self.disabled,
'not expired': not getattr(response, 'is_expired', False),
'not filtered': self.filter_fn(response),
Expand Down
87 changes: 69 additions & 18 deletions test/unit/backends/test_backend_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from sys import version_info
from unittest.mock import MagicMock, patch

from aiohttp_client_cache import CachedResponse
Expand All @@ -8,7 +7,7 @@
pytestmark = pytest.mark.asyncio


async def test_cache_backend__get_response__cache_response_hit():
async def test_get_response__cache_response_hit():
cache = CacheBackend()
mock_response = MagicMock(spec=CachedResponse, method='GET', status=200, is_expired=False)
await cache.responses.write('request-key', mock_response)
Expand All @@ -17,7 +16,7 @@ async def test_cache_backend__get_response__cache_response_hit():
assert response == mock_response


async def test_cache_backend__get_response__cache_redirect_hit():
async def test_get_response__cache_redirect_hit():
# Set up a cache with a couple cached items and a redirect
cache = CacheBackend()
mock_response = MagicMock(spec=CachedResponse, method='GET', status=200, is_expired=False)
Expand All @@ -28,7 +27,7 @@ async def test_cache_backend__get_response__cache_redirect_hit():
assert response == mock_response


async def test_cache_backend__get_response__cache_miss():
async def test_get_response__cache_miss():
cache = CacheBackend()
await cache.responses.write('invalid-response-key', MagicMock())

Expand All @@ -40,7 +39,7 @@ async def test_cache_backend__get_response__cache_miss():

@patch.object(CacheBackend, 'delete', return_value=False)
@patch.object(CacheBackend, 'is_cacheable', return_value=False)
async def test_cache_backend__get_response__cache_expired(mock_is_cacheable, mock_delete):
async def test_get_response__cache_expired(mock_is_cacheable, mock_delete):
cache = CacheBackend()
mock_response = MagicMock(spec=CachedResponse, method='GET', status=200, is_expired=True)
await cache.responses.write('request-key', mock_response)
Expand All @@ -50,37 +49,89 @@ async def test_cache_backend__get_response__cache_expired(mock_is_cacheable, moc
mock_delete.assert_called_with('request-key')


# TODO
@patch.object(CacheBackend, 'is_cacheable', return_value=True)
async def test_save_response(mock_is_cacheable):
cache = CacheBackend()
mock_response = MagicMock()
mock_response.history = [MagicMock(method='GET', url='test')]
redirect_key = cache.create_key('GET', 'test')

await cache.save_response('key', mock_response)
cached_response = await cache.responses.read('key')
assert cached_response and isinstance(cached_response, CachedResponse)
assert await cache.redirects.read(redirect_key) == 'key'


async def test_cache_backend__save_response():
@patch.object(CacheBackend, 'is_cacheable', return_value=False)
async def test_save_response__not_cacheable(mock_is_cacheable):
cache = CacheBackend()
pass
await cache.save_response('key', MagicMock())
assert 'key' not in cache.responses


async def test_cache_backend__clear():
pass
async def test_clear():
cache = CacheBackend()
await cache.responses.write('key', 'value')
await cache.redirects.write('key', 'value')
await cache.clear()

assert await cache.responses.size() == 0
assert await cache.redirects.size() == 0

async def test_cache_backend__delete():
pass

async def test_delete():
cache = CacheBackend()
mock_response = MagicMock()
mock_response.history = [MagicMock(method='GET', url='test')]
redirect_key = cache.create_key('GET', 'test')

await cache.responses.write('key', mock_response)
await cache.redirects.write(redirect_key, 'key')
await cache.redirects.write('some_other_redirect', 'key')

async def test_cache_backend__delete_url():
pass
await cache.delete('key')
assert await cache.responses.size() == 0
assert await cache.redirects.size() == 1


async def test_cache_backend__delete_expired_responses():
# TODO
async def test_delete_url():
pass


async def test_cache_backend__has_url():
# TODO
async def test_delete_expired_responses():
pass


async def test_cache_backend__create_key():
# TODO
async def test_has_url():
pass


async def test_cache_backend__is_cacheable():
# TODO
async def test_create_key():
pass


@pytest.mark.parametrize(
'method, status, disabled, expired, filter_return, expected_result',
[
('GET', 200, False, False, True, True),
('DELETE', 200, False, False, True, False),
('GET', 502, False, False, True, False),
('GET', 200, True, False, True, False),
('GET', 200, False, True, True, False),
('GET', 200, False, False, False, False),
],
)
async def test_is_cacheable(method, status, disabled, expired, filter_return, expected_result):
mock_response = MagicMock(
method=method,
status=status,
is_expired=expired,
)
cache = CacheBackend()
cache.filter_fn = lambda x: filter_return
cache.disabled = disabled
assert cache.is_cacheable(mock_response) is expected_result
3 changes: 1 addition & 2 deletions test/unit/test_session.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from sys import version_info
from unittest.mock import MagicMock, patch

from aiohttp_client_cache.backends import CacheBackend
Expand All @@ -11,7 +10,7 @@
try:
from unittest.mock import AsyncMock
except ImportError:
from asyncmock import AsyncMock
from asyncmock import AsyncMock # type: ignore


@patch.object(ClientSession, '_request')
Expand Down

0 comments on commit 685adf6

Please sign in to comment.