import pytest from django.core.cache import cache from apps.accounts.middleware import SetupWizardMiddleware @pytest.fixture(autouse=True) def _reset_django_cache(): """config.settings.test overrides CACHES to LocMemCache, which — like SetupWizardMiddleware's own cache below — lives in process memory and survives Django's per-test DB rollback. Without this, a cached sidebar unread count (apps.core.cache_utils) keyed by a user's pk could leak into a later, unrelated test that happens to create a user with the same auto-incrementing pk.""" cache.clear() yield cache.clear() @pytest.fixture(autouse=True) def _reset_setup_wizard_cache(): """SetupWizardMiddleware caches "does any User exist" on the class itself (not per-request), so it survives across tests in the same pytest process regardless of Django's per-test DB rollback. Autouse and global (not scoped to apps/accounts/tests.py) so no test file can leak a stale True into another — a test exercising "no user exists yet" behavior must actually start from that state, not from whatever an earlier, unrelated test happened to leave behind. """ SetupWizardMiddleware._a_user_exists = False yield SetupWizardMiddleware._a_user_exists = False