-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switched imagekit caching from in-memory to redis (#1475)
- Loading branch information
1 parent
092adc0
commit f97b59f
Showing
3 changed files
with
117 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,87 @@ | ||
from django.core.cache.backends.base import BaseCache | ||
from dataclasses import dataclass | ||
|
||
import pytest | ||
from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache | ||
|
||
from main.cache.backends import FallbackCache | ||
|
||
|
||
def test_fallback_cache_get(mocker, settings): | ||
"""Test that get() on the fallback cache works correctly""" | ||
mock_cache_1 = mocker.Mock(spec=BaseCache) | ||
mock_cache_1.get.return_value = 12345 | ||
mock_cache_2 = mocker.Mock(spec=BaseCache) | ||
mock_cache_2.get.return_value = 67890 | ||
@dataclass | ||
class MockCaches: | ||
caches: list[BaseCache] | ||
cache_names: list[str] | ||
caches_by_name: dict[str, BaseCache] | ||
cache_results: list[str] | ||
|
||
mocker.patch.dict( | ||
"main.cache.backends.caches", {"dummy1": mock_cache_1, "dummy2": mock_cache_2} | ||
) | ||
|
||
cache = FallbackCache(["dummy1", "dummy2"], {}) | ||
@pytest.fixture(autouse=True) | ||
def mock_caches(mocker): | ||
"""Define mock caches""" | ||
cache_names = [] | ||
caches = [] | ||
caches_by_name = {} | ||
cache_results = [] | ||
|
||
for idx in range(3): | ||
name = f"dummy-{idx}" | ||
result = f"result-{idx}" | ||
mock_cache = mocker.Mock(spec=BaseCache) | ||
mock_cache.get.return_value = result | ||
|
||
assert cache.get("key", default="default", version=1) == 12345 | ||
cache_names.append(name) | ||
caches.append(mock_cache) | ||
caches_by_name[name] = mock_cache | ||
cache_results.append(result) | ||
|
||
mock_cache_1.get.return_value = None | ||
mocker.patch.dict("main.cache.backends.caches", caches_by_name) | ||
|
||
assert cache.get("key", default="default", version=1) == 67890 | ||
return MockCaches(caches, cache_names, caches_by_name, cache_results) | ||
|
||
mock_cache_2.get.return_value = None | ||
|
||
assert cache.get("key", default="default", version=1) is None | ||
@pytest.mark.parametrize("cache_hit_idx", range(4)) | ||
def test_fallback_cache_get(mock_caches: MockCaches, settings, cache_hit_idx): | ||
"""Test that get() on the fallback cache works correctly""" | ||
|
||
cache = FallbackCache(mock_caches.cache_names, {}) | ||
|
||
def test_fallback_cache_set(mocker, settings): | ||
"""Test that set() on the fallback cache works correctly""" | ||
mock_cache_1 = mocker.Mock(spec=BaseCache) | ||
mock_cache_2 = mocker.Mock(spec=BaseCache) | ||
cold_caches = mock_caches.caches[:cache_hit_idx] | ||
|
||
mocker.patch.dict( | ||
"main.cache.backends.caches", {"dummy1": mock_cache_1, "dummy2": mock_cache_2} | ||
for mock_cache in cold_caches: | ||
mock_cache.get.return_value = None | ||
|
||
caches_exhausted = cache_hit_idx >= len(mock_caches.caches) | ||
expected_value = ( | ||
"default" if caches_exhausted else mock_caches.cache_results[cache_hit_idx] | ||
) | ||
|
||
cache = FallbackCache(["dummy1", "dummy2"], {}) | ||
assert cache.get("key", default="default", version=1) == expected_value | ||
|
||
if not caches_exhausted: | ||
for mock_cache in cold_caches: | ||
mock_cache.set.assert_called_once_with( | ||
"key", expected_value, timeout=cache.get_backend_timeout(), version=1 | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("cache_timeout", [DEFAULT_TIMEOUT, None, 0, 1000]) | ||
@pytest.mark.parametrize( | ||
"kwargs", | ||
[ | ||
{}, | ||
{"timeout": 600}, | ||
{"version": 1}, | ||
{"timeout": 600, "version": 1}, | ||
], | ||
) | ||
def test_fallback_cache_set(mock_caches, settings, cache_timeout, kwargs): | ||
"""Test that set() on the fallback cache works correctly""" | ||
cache = FallbackCache(mock_caches.cache_names, {"TIMEOUT": cache_timeout}) | ||
cache.set("key", "value", **kwargs) | ||
|
||
cache.set("key", "value", timeout=600, version=1) | ||
expected_timeout = kwargs.get("timeout", cache_timeout) | ||
expected_version = kwargs.get("version", None) | ||
|
||
mock_cache_1.set.assert_called_once_with("key", "value", timeout=600, version=1) | ||
mock_cache_2.set.assert_called_once_with("key", "value", timeout=600, version=1) | ||
for mock_cache in mock_caches.caches: | ||
mock_cache.set.assert_called_once_with( | ||
"key", "value", timeout=expected_timeout, version=expected_version | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters