async def test_create_key(mock_create_key): """Actual logic is in cache_keys module; just test to make sure it gets called correctly""" headers = {'key': 'value'} ignored_params = ['ignored'] cache = CacheBackend(include_headers=True, ignored_params=ignored_params) cache.create_key('GET', 'https://test.com', headers=headers) mock_create_key.assert_called_with( 'GET', 'https://test.com', include_headers=True, ignored_params=set(ignored_params), headers=headers, )
async def test_has_url(): cache = CacheBackend() mock_response = await CachedResponse.from_client_response( get_mock_response()) cache_key = cache.create_key('GET', TEST_URL, params={'param': 'value'}) await cache.responses.write(cache_key, mock_response) assert await cache.has_url(TEST_URL, params={'param': 'value'}) assert not await cache.has_url('https://test.com/some_other_path')
async def test_delete_url(): cache = CacheBackend() mock_response = await CachedResponse.from_client_response( get_mock_response()) cache_key = cache.create_key('GET', TEST_URL, params={'param': 'value'}) await cache.responses.write(cache_key, mock_response) assert await cache.responses.size() == 1 await cache.delete_url(TEST_URL, params={'param': 'value'}) assert await cache.responses.size() == 0
async def test_save_response(mock_is_cacheable): cache = CacheBackend() mock_response = get_mock_response() mock_response.history = [MagicMock(method='GET', url='test')] redirect_key = cache.create_key('GET', 'test') await cache.save_response(mock_response, CacheActions(key='key')) 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_delete(): cache = CacheBackend() mock_response = get_mock_response() 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') await cache.delete('key') assert await cache.responses.size() == 0 assert await cache.redirects.size() == 1