diff --git a/django_cache_mock/backends/memcached.py b/django_cache_mock/backends/memcached.py index f956818..e48a4db 100644 --- a/django_cache_mock/backends/memcached.py +++ b/django_cache_mock/backends/memcached.py @@ -3,51 +3,67 @@ from django.core.cache.backends.memcached import BaseMemcachedCache, PyMemcacheCache +from django_cache_mock.exceptions import LazyLibImportError + logger = logging.getLogger(__name__) -class MockcacheCache(BaseMemcachedCache): - _dbs = {} +try: + import mockcache + + class MockcacheCache(BaseMemcachedCache): + _dbs = {} + + def __init__(self, server, params): + super().__init__( + server, + params, + library=mockcache, + value_not_found_exception=ValueError, + ) + self.location = server + + def get(self, key, default=None, version=None): + # Override method because library don't support a default value. + key = self.make_key(key, version=version) + self.validate_key(key) + val = self._cache.get(key) + if val is None: + return default + return val + + @cached_property + def _cache(self): + client = super()._cache + client.dictionary = self._dbs.setdefault(self.location, {}) + return client - def __init__(self, server, params): - import mockcache +except ImportError as _import_error: + logger.debug("mockcache is not installed.") - super().__init__( - server, - params, - library=mockcache, - value_not_found_exception=ValueError, - ) - self.location = server + class MockcacheCache(LazyLibImportError): + parent_exception = _import_error - def get(self, key, default=None, version=None): - # Override method because library don't support a default value. - key = self.make_key(key, version=version) - self.validate_key(key) - val = self._cache.get(key) - if val is None: - return default - return val - @cached_property - def _cache(self): - client = super()._cache - client.dictionary = self._dbs.setdefault(self.location, {}) - return client +try: + from pymemcache.test.utils import MockMemcacheClient + class PyMemcacheMockMemcacheCache(PyMemcacheCache): + _dbs = {} -class PyMemcacheMockMemcacheCache(PyMemcacheCache): - _dbs = {} + def __init__(self, server, params): + super().__init__(server, params) + self._class = MockMemcacheClient + self.location = server - def __init__(self, server, params): - from pymemcache.test.utils import MockMemcacheClient + @cached_property + def _cache(self): + client = super()._cache + client._contents = self._dbs.setdefault(self.location, {}) + return client - super().__init__(server, params) - self._class = MockMemcacheClient - self.location = server +except ImportError as _import_error: + logger.debug("pymemcache is not installed.") - @cached_property - def _cache(self): - client = super()._cache - client._contents = self._dbs.setdefault(self.location, {}) - return client + class PyMemcacheMockMemcacheCache(LazyLibImportError): + parent_exception = _import_error diff --git a/tests/conftest.py b/tests/conftest.py index 04bb2d1..c8cfd81 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -56,22 +56,6 @@ def cache_alias_not_installed(cache_alias): return cache_alias -@pytest.fixture -def memcached_cache_alias_not_installed(cache_alias_not_installed): - cache_alias = cache_alias_not_installed - if "memcached" not in CACHES[cache_alias]["BACKEND"]: - pytest.skip(f"Module {cache_alias} is not a memcached backend.") - return cache_alias - - -@pytest.fixture -def redis_cache_alias_not_installed(cache_alias_not_installed): - cache_alias = cache_alias_not_installed - if "redis" not in CACHES[cache_alias]["BACKEND"]: - pytest.skip(f"Module {cache_alias} is not a redis backend.") - return cache_alias - - @pytest.fixture(params=["fakeredis", "redislite"]) def redis_backend(request): return request.param diff --git a/tests/test_backends.py b/tests/test_backends.py index 6e994da..6854838 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -59,21 +59,7 @@ class MyError(LazyLibImportError): assert isinstance(exception_info.value.exception, ZeroDivisionError) -def test_redis_import_error(redis_cache_alias_not_installed): - cache_alias = redis_cache_alias_not_installed - try: +def test_redis_import_error(cache_alias_not_installed): + cache_alias = cache_alias_not_installed + with pytest.raises(LazyLibImportError): caches[cache_alias] - except LazyLibImportError: - pass - else: # pragma: no cover - pytest.fail("Cache unexpectedly worked.") - - -def test_memcached_import_error(memcached_cache_alias_not_installed): - cache_alias = memcached_cache_alias_not_installed - try: - caches[cache_alias] - except ImportError: - pass - else: # pragma: no cover - pytest.fail("Cache unexpectedly worked.")