Skip to content

Commit

Permalink
Add a couple more CacheBackend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Feb 23, 2021
1 parent a0b776e commit 33dae9e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
44 changes: 29 additions & 15 deletions test/backends/test_backend_base.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import pytest
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch

from aiohttp_client_cache import CachedResponse
from aiohttp_client_cache.backends.base import BaseCache, CacheBackend, DictCache # noqa


@pytest.mark.asyncio
async def test_cache_backend__get_response__cache_hit():
async def test_cache_backend__get_response__cache_response_hit():
cache = CacheBackend()
mock_response = MagicMock(spec=CachedResponse)
await cache.responses.write('request-key', mock_response)

response = await cache.get_response('request-key')
assert response == mock_response


@pytest.mark.asyncio
async def test_cache_backend__get_response__cache_redirect_hit():
# Set up a cache with a couple cached items and a redirect
cache = CacheBackend()
mock_response_1 = MagicMock(spec=CachedResponse)
mock_response_2 = MagicMock(spec=CachedResponse)
await cache.responses.write('request-key-1', mock_response_1)
await cache.responses.write('request-key-2', mock_response_2)
await cache.redirects.write('redirect-key', 'request-key-2')

response = await cache.get_response('request-key-1')
assert response == mock_response_1
mock_response = MagicMock(spec=CachedResponse)
await cache.responses.write('request-key', mock_response)
await cache.redirects.write('redirect-key', 'request-key')

response = await cache.get_response('redirect-key')
assert response == mock_response_2
assert response == mock_response


@pytest.mark.asyncio
Expand All @@ -32,16 +38,24 @@ async def test_cache_backend__get_response__cache_miss():
assert response is None


# TODO
@pytest.mark.asyncio
@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):
cache = CacheBackend()
await cache.responses.write('request-key', MagicMock(spec=CachedResponse))

response = await cache.get_response('request-key')
assert response.is_expired is True
mock_delete.assert_called_with('request-key')

@pytest.mark.asyncio
async def test_cache_backend__get_response__cache_expired():
pass

# TODO


@pytest.mark.asyncio
async def test_cache_backend__save_response():
cache = CacheBackend()
pass


Expand Down
2 changes: 1 addition & 1 deletion test/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from unittest.mock import AsyncMock
except ImportError:
pass
pytestmark = pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python 3.8 or higher")
pytestmark = pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python 3.8+")


@pytest.mark.asyncio
Expand Down

0 comments on commit 33dae9e

Please sign in to comment.