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 eb69de3 commit 4cc3be8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
46 changes: 31 additions & 15 deletions test/backends/test_backend_base.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import pytest
from unittest.mock import MagicMock
from sys import version_info
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 +39,25 @@ async def test_cache_backend__get_response__cache_miss():
assert response is None


# TODO
@pytest.mark.skipif(version_info < (3, 8), reason="AsyncMock requires python 3.8+")
@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
4 changes: 2 additions & 2 deletions test/test_session.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
import sys
from sys import version_info
from unittest.mock import MagicMock, patch

from aiohttp_client_cache.backends import CacheBackend
Expand All @@ -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(version_info < (3, 8), reason="AsyncMock requires python 3.8+")


@pytest.mark.asyncio
Expand Down

0 comments on commit 4cc3be8

Please sign in to comment.