|
| 1 | +from collections.abc import Generator |
| 2 | +from unittest.mock import Mock, MagicMock |
| 3 | + |
| 4 | +import pytest |
| 5 | +from pytest_mock import MockFixture |
| 6 | +from requests import HTTPError |
| 7 | + |
| 8 | +from country_workspace.contrib.kobo.api.common import DataGetter |
| 9 | + |
| 10 | + |
| 11 | +URL = "https://test.org" |
| 12 | +CACHE_TTL = 42 |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def cache_mock(mocker: MockFixture) -> Mock: |
| 17 | + return mocker.patch("country_workspace.contrib.kobo.api.common.cache") |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def session_mock() -> Generator[Mock, None, None]: |
| 22 | + return MagicMock(name="Session()") |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def cached_response_class_mock(mocker: MockFixture) -> Mock: |
| 27 | + return mocker.patch("country_workspace.contrib.kobo.api.common.CachedResponse") |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def data_getter_cache_key_mock(mocker: MockFixture) -> Mock: |
| 32 | + return mocker.patch("country_workspace.contrib.kobo.api.common.data_getter_cache_key") |
| 33 | + |
| 34 | + |
| 35 | +def test_cache_can_be_skipped(cache_mock: Mock, session_mock: Mock) -> None: |
| 36 | + function = MagicMock() |
| 37 | + function.return_value = True |
| 38 | + |
| 39 | + data_getter = DataGetter( |
| 40 | + session=session_mock, |
| 41 | + cache_ttl=CACHE_TTL, |
| 42 | + do_not_use_cache_if=function, |
| 43 | + ) |
| 44 | + response = data_getter(URL) |
| 45 | + |
| 46 | + assert response == session_mock.get.return_value |
| 47 | + session_mock.get.assert_called_with(URL, headers=None) |
| 48 | + function.assert_called_once_with(URL) |
| 49 | + cache_mock.assert_not_called() |
| 50 | + |
| 51 | + |
| 52 | +def test_cached_value_is_returned( |
| 53 | + cache_mock: Mock, session_mock: Mock, cached_response_class_mock: Mock, data_getter_cache_key_mock: Mock |
| 54 | +) -> None: |
| 55 | + data_getter = DataGetter( |
| 56 | + session=session_mock, |
| 57 | + cache_ttl=CACHE_TTL, |
| 58 | + ) |
| 59 | + response = data_getter(URL) |
| 60 | + |
| 61 | + assert response == cached_response_class_mock.return_value |
| 62 | + cached_response_class_mock.assert_called_once_with(cache_mock.get.return_value) |
| 63 | + session_mock.get.assert_not_called() |
| 64 | + cache_mock.get.assert_called_once_with(data_getter_cache_key_mock.return_value) |
| 65 | + data_getter_cache_key_mock.assert_called_once_with(URL) |
| 66 | + |
| 67 | + |
| 68 | +def test_failing_response_is_not_cached(cache_mock: Mock, session_mock: Mock) -> None: |
| 69 | + cache_mock.get.return_value = None |
| 70 | + session_mock.get.return_value.raise_for_status.side_effect = HTTPError() |
| 71 | + |
| 72 | + data_getter = DataGetter( |
| 73 | + session=session_mock, |
| 74 | + cache_ttl=CACHE_TTL, |
| 75 | + ) |
| 76 | + response = data_getter(URL) |
| 77 | + |
| 78 | + assert response == session_mock.get.return_value |
| 79 | + cache_mock.set.assert_not_called() |
| 80 | + |
| 81 | + |
| 82 | +def test_response_is_cached( |
| 83 | + cache_mock: Mock, session_mock: Mock, cached_response_class_mock: Mock, data_getter_cache_key_mock: Mock |
| 84 | +) -> None: |
| 85 | + cache_mock.get.return_value = None |
| 86 | + |
| 87 | + data_getter = DataGetter( |
| 88 | + session=session_mock, |
| 89 | + cache_ttl=CACHE_TTL, |
| 90 | + ) |
| 91 | + response = data_getter(URL) |
| 92 | + |
| 93 | + assert response == session_mock.get.return_value |
| 94 | + cache_mock.set.assert_called_once_with( |
| 95 | + data_getter_cache_key_mock.return_value, |
| 96 | + { |
| 97 | + "json": session_mock.get.return_value.json.return_value, |
| 98 | + "status_code": session_mock.get.return_value.status_code, |
| 99 | + }, |
| 100 | + CACHE_TTL, |
| 101 | + ) |
0 commit comments