noripyt / django-cachalot

No effort, no worry, maximum performance.
http://django-cachalot.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.23k stars 151 forks source link

using override_settings(CACHALOT_ENABLED=False) has no effect #223

Closed treyhunner closed 1 year ago

treyhunner commented 1 year ago

What happened?

I'm using freezegun, so I'm affected by #126.

When I set CACHALOT_ENABLED= False in my settings, my tests pass.

I'd prefer to use cachalot for most of my tests and only disable it for the tests where freezegun is used. When I use override_settings(CACHALOT_ENABLED=False), the setting does change but the queries with the test are still cached.

What should've happened instead?

Using the override_settings decorator to disable CACHALOT_ENABLED should have disabled caching during the decorated test.

Steps to reproduce

Use the @override_settings(CACHALOT_ENABLED=False) decorator on a test that uses freezegun.freeze_time for a long past date. When calling the refresh_from_db() method on a model instance, the instance will not refresh even if it has been changed.

I'm using Docker with Postgres and on Django 3.2.7.

Andrew-Chen-Wang commented 1 year ago

Settings typically don't change during production but yes this is a good point to override. Cachalot patches the Django ORM on startup. Something you can use instead is cachalot.api.cachalot_disabled

This is less so of a bug, but semantically a feature to be implemented I suppose... However, I believe utilizing the context manager/decorator is more suitable, so closing.

max-wittig commented 1 year ago

I solved it by triggering a reload in a fixture:

from django.test.utils import override_settings
from cachalot.settings import cachalot_settings
import pytest

@pytest.fixture
@override_settings(CACHALOT_ENABLED=False)
def disable_caching() -> None:
    cachalot_settings.reload()

def test_something(disable_caching: None):
    pass
    ....