|
1 | | -from django.core.cache.backends.base import BaseCache |
| 1 | +from dataclasses import dataclass |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache |
2 | 5 |
|
3 | 6 | from main.cache.backends import FallbackCache |
4 | 7 |
|
5 | 8 |
|
6 | | -def test_fallback_cache_get(mocker, settings): |
7 | | - """Test that get() on the fallback cache works correctly""" |
8 | | - mock_cache_1 = mocker.Mock(spec=BaseCache) |
9 | | - mock_cache_1.get.return_value = 12345 |
10 | | - mock_cache_2 = mocker.Mock(spec=BaseCache) |
11 | | - mock_cache_2.get.return_value = 67890 |
| 9 | +@dataclass |
| 10 | +class MockCaches: |
| 11 | + caches: list[BaseCache] |
| 12 | + cache_names: list[str] |
| 13 | + caches_by_name: dict[str, BaseCache] |
| 14 | + cache_results: list[str] |
12 | 15 |
|
13 | | - mocker.patch.dict( |
14 | | - "main.cache.backends.caches", {"dummy1": mock_cache_1, "dummy2": mock_cache_2} |
15 | | - ) |
16 | 16 |
|
17 | | - cache = FallbackCache(["dummy1", "dummy2"], {}) |
| 17 | +@pytest.fixture(autouse=True) |
| 18 | +def mock_caches(mocker): |
| 19 | + """Define mock caches""" |
| 20 | + cache_names = [] |
| 21 | + caches = [] |
| 22 | + caches_by_name = {} |
| 23 | + cache_results = [] |
| 24 | + |
| 25 | + for idx in range(3): |
| 26 | + name = f"dummy-{idx}" |
| 27 | + result = f"result-{idx}" |
| 28 | + mock_cache = mocker.Mock(spec=BaseCache) |
| 29 | + mock_cache.get.return_value = result |
18 | 30 |
|
19 | | - assert cache.get("key", default="default", version=1) == 12345 |
| 31 | + cache_names.append(name) |
| 32 | + caches.append(mock_cache) |
| 33 | + caches_by_name[name] = mock_cache |
| 34 | + cache_results.append(result) |
20 | 35 |
|
21 | | - mock_cache_1.get.return_value = None |
| 36 | + mocker.patch.dict("main.cache.backends.caches", caches_by_name) |
22 | 37 |
|
23 | | - assert cache.get("key", default="default", version=1) == 67890 |
| 38 | + return MockCaches(caches, cache_names, caches_by_name, cache_results) |
24 | 39 |
|
25 | | - mock_cache_2.get.return_value = None |
26 | 40 |
|
27 | | - assert cache.get("key", default="default", version=1) is None |
| 41 | +@pytest.mark.parametrize("cache_hit_idx", range(4)) |
| 42 | +def test_fallback_cache_get(mock_caches: MockCaches, settings, cache_hit_idx): |
| 43 | + """Test that get() on the fallback cache works correctly""" |
28 | 44 |
|
| 45 | + cache = FallbackCache(mock_caches.cache_names, {}) |
29 | 46 |
|
30 | | -def test_fallback_cache_set(mocker, settings): |
31 | | - """Test that set() on the fallback cache works correctly""" |
32 | | - mock_cache_1 = mocker.Mock(spec=BaseCache) |
33 | | - mock_cache_2 = mocker.Mock(spec=BaseCache) |
| 47 | + cold_caches = mock_caches.caches[:cache_hit_idx] |
34 | 48 |
|
35 | | - mocker.patch.dict( |
36 | | - "main.cache.backends.caches", {"dummy1": mock_cache_1, "dummy2": mock_cache_2} |
| 49 | + for mock_cache in cold_caches: |
| 50 | + mock_cache.get.return_value = None |
| 51 | + |
| 52 | + caches_exhausted = cache_hit_idx >= len(mock_caches.caches) |
| 53 | + expected_value = ( |
| 54 | + "default" if caches_exhausted else mock_caches.cache_results[cache_hit_idx] |
37 | 55 | ) |
38 | 56 |
|
39 | | - cache = FallbackCache(["dummy1", "dummy2"], {}) |
| 57 | + assert cache.get("key", default="default", version=1) == expected_value |
| 58 | + |
| 59 | + if not caches_exhausted: |
| 60 | + for mock_cache in cold_caches: |
| 61 | + mock_cache.set.assert_called_once_with( |
| 62 | + "key", expected_value, timeout=cache.get_backend_timeout(), version=1 |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize("cache_timeout", [DEFAULT_TIMEOUT, None, 0, 1000]) |
| 67 | +@pytest.mark.parametrize( |
| 68 | + "kwargs", |
| 69 | + [ |
| 70 | + {}, |
| 71 | + {"timeout": 600}, |
| 72 | + {"version": 1}, |
| 73 | + {"timeout": 600, "version": 1}, |
| 74 | + ], |
| 75 | +) |
| 76 | +def test_fallback_cache_set(mock_caches, settings, cache_timeout, kwargs): |
| 77 | + """Test that set() on the fallback cache works correctly""" |
| 78 | + cache = FallbackCache(mock_caches.cache_names, {"TIMEOUT": cache_timeout}) |
| 79 | + cache.set("key", "value", **kwargs) |
40 | 80 |
|
41 | | - cache.set("key", "value", timeout=600, version=1) |
| 81 | + expected_timeout = kwargs.get("timeout", cache_timeout) |
| 82 | + expected_version = kwargs.get("version", None) |
42 | 83 |
|
43 | | - mock_cache_1.set.assert_called_once_with("key", "value", timeout=600, version=1) |
44 | | - mock_cache_2.set.assert_called_once_with("key", "value", timeout=600, version=1) |
| 84 | + for mock_cache in mock_caches.caches: |
| 85 | + mock_cache.set.assert_called_once_with( |
| 86 | + "key", "value", timeout=expected_timeout, version=expected_version |
| 87 | + ) |
0 commit comments