2

I have code which handles a ConnectionError when Django cannot connect to the Cache, which I'd like to test.

I've hit an issue that without actually disabling the real cache, I can't simulate it within the tests.

I have tried using the Django settings override:

with self.settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}):

However the above only simulates a cache, and not that it isn't available.

Is there a way to do this?

Thanks in advance.

1 Answer 1

1

You could write your own cache backend that raises ConnectionError.

from django.core.cache.backends.base import BaseCache


class UnavailableCache(BaseCache):
    ...

    def get(self, *args, **kwargs)
        raise ConnectionError()
    ...

Then use this backend in self.settings:

with self.settings(CACHES={'default': {'BACKEND': 'path.to.UnavailableCache'}}):
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.