コード例 #1
0
async def test_get_urls():
    cache = CacheBackend()
    for i in range(7):
        mock_response = get_mock_response(url=f'https://test.com/{i}')
        await cache.responses.write(f'request-key-{i}', mock_response)

    urls = {url async for url in cache.get_urls()}
    assert urls == {f'https://test.com/{i}' for i in range(7)}
コード例 #2
0
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')
コード例 #3
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'
コード例 #4
0
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
コード例 #5
0
async def test_is_cacheable(method, status, disabled, expired, filter_return,
                            expected_result):
    mock_response = get_mock_response(
        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
コード例 #6
0
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
コード例 #7
0
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,
    )
コード例 #8
0
async def test_get_response__cache_response_hit():
    cache = CacheBackend()
    mock_response = get_mock_response()
    await cache.responses.write('request-key', mock_response)

    response = await cache.get_response('request-key')
    assert response == mock_response
コード例 #9
0
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
コード例 #10
0
async def test_get_response__cache_expired(mock_is_cacheable, mock_delete):
    cache = CacheBackend()
    mock_response = get_mock_response(is_expired=True)
    await cache.responses.write('request-key', mock_response)

    response = await cache.get_response('request-key')
    assert response is None
    mock_delete.assert_called_with('request-key')
コード例 #11
0
async def test_get_response__cache_redirect_hit():
    # Set up a cache with a couple cached items and a redirect
    cache = CacheBackend()
    mock_response = get_mock_response()
    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
コード例 #12
0
async def test_get_response__cache_invalid(mock_read, mock_delete, error_type):
    cache = CacheBackend()
    mock_read.side_effect = error_type
    mock_response = get_mock_response()
    await cache.responses.write('request-key', mock_response)

    response = await cache.get_response('request-key')
    assert response is None
    mock_delete.assert_not_called()
コード例 #13
0
async def test_delete_expired_responses():
    cache = CacheBackend()
    await cache.responses.write('request-key-1',
                                get_mock_response(is_expired=False))
    await cache.responses.write('request-key-2',
                                get_mock_response(is_expired=True))

    assert await cache.responses.size() == 2
    await cache.delete_expired_responses()
    assert await cache.responses.size() == 1
コード例 #14
0
    def __init__(
        self,
        base_url: Optional[StrOrURL] = None,
        *,
        cache: CacheBackend = None,
        **kwargs,
    ):
        self.cache = cache or CacheBackend()

        # Pass along any valid kwargs for ClientSession (or custom session superclass)
        session_kwargs = get_valid_kwargs(super().__init__, {
            **kwargs, 'base_url': base_url
        })
        super().__init__(**session_kwargs)
コード例 #15
0
async def test_save_response__not_cacheable(mock_is_cacheable):
    cache = CacheBackend()
    await cache.save_response('key', MagicMock())
    assert 'key' not in cache.responses
コード例 #16
0
 def __init__(self, *, cache: CacheBackend = None, **kwargs):
     super().__init__(**kwargs)  # type: ignore
     self.cache = cache or CacheBackend()
コード例 #17
0
async def test_get_response__cache_miss(mock_delete):
    cache = CacheBackend()

    response_1 = await cache.get_response('nonexistent-key')
    assert response_1 is None
    mock_delete.assert_not_called()