Private
Public Access
apps.core.context_processors.sidebar runs on every authenticated page
render and called unread_counts_by_feed, a join/group-by over the
user's whole article corpus, every single time — cost grew with the
archive's size regardless of which page was being viewed.
unread_counts_by_feed now caches its {feed_id: count} result per user
for 30 seconds (apps.core.cache_utils). The reader views that change
the current user's own read state (toggle_read, mark_all_read, viewing
an article) invalidate it immediately so the sidebar never visibly lags
behind the action that just caused it; other paths (rule-driven state
changes during background ingestion) aren't explicitly invalidated and
simply self-heal within the TTL, which is an acceptable tradeoff
already made elsewhere in this app (the stream's own "new articles"
banner polls on the same 30s cadence).
Also adds a global cache.clear() autouse fixture (conftest.py) — the
test CACHES backend (LocMemCache) lives in process memory and survives
Django's per-test DB rollback, so a cached count keyed by a user's pk
could otherwise leak into an unrelated later test reusing that pk.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
33 lines
1.3 KiB
Python
33 lines
1.3 KiB
Python
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
|