django-commons/django-cookie-consent

Cache instance is resolved at import time rather than runtime

Closed this issue · 3 comments

The cache to be used by django-cookie-consent is frozen at import time and makes it annoying to swap out cache backends during tests using django.test.override_settings.

Our default settings use the real cache backend, but in testing we're replacing this with the no-op cache backend. Except, because the cache instance is resolved at instance time, using override_settings has no effect. This means that during testing, test isolation is broken because there's now cache entries that span multiple tests.

https://github.com/bmihelac/django-cookie-consent/blob/510887228a5209d6355a99fca62bb6683d84b4d3/cookie_consent/cache.py#L6

I can resolve this for now using unittest.mock.patch, however, the get_cache call should really happen inside the functions exposed in the module rather than at import time.

@sergei-maertens thanks for report. I agree. Would you like to make a PR?

I would like to, but I'm afraid I have other priorities at the moment so if anyone else wants to pick it up, feelf ree!

As an unrelated item - I've recently sent an e-mail as well to the e-mail address in your Github profile for something that's not suited to be reported publicly, could you give that a look too?

@sergei-maertens I don't really know what you mean so it is hard for me and maybe some others to help. Can you provide some more code or even your console output to show us what you are talking about or can you explain it in an easier way? I am confused by runtime vs import vs instance time and what you mean by frozen. What is the no-op cache backend? It seems to be that you cannot switch to another cache backend like redis/memcache or others so easily? This only happens in testing but not production? If you can make a PR that would be great because as of right now I am having trouble understanding.

Are you referring to this?
https://github.com/bmihelac/django-cookie-consent/blob/master/tests/core/tests/test_cache.py