Skip to content

Test cache isolation: FastAPICache.init() no-ops, so the cache leaks between tests #374

Description

@braddf

FastAPICache.init() starts with if cls._init: return. The first call in a pytest session wins and every later call is a silent no-op.

Two consequences:

  1. _create_server() re-inits the cache per test, but that does nothing after the first test — so the in-memory store is shared for the whole session and a cached route can serve a previous test's response. Hit this while adding coverage for GET /v0/solar/GB/status: the second test got the first one's payload. Existing tests hid it because their second case 404s, and exceptions aren't cached.

  2. The ~25 FastAPICache.init(InMemoryBackend(), prefix="test") / prefix="cold" calls in src/quartz_api/internal/service/v1/test_router.py are all no-ops. They neither set the prefix nor provide a fresh backend. The cold-cache 503 tests are not actually starting from a cold cache — they pass on session ordering rather than on the isolation they look like they're asserting.

Fix: autouse fixture in src/quartz_api/internal/service/conftest.py clearing the backend between tests, then drop the no-op init calls in test_router.py.

@pytest_asyncio.fixture(autouse=True)
async def clear_cache():
    if FastAPICache._init:
        await FastAPICache.clear()
    yield

Verified: with this in the shared conftest the full unit suite passes (223 passed) with no exceptions needed — including the v1 cold-cache tests, which get a genuinely cold cache rather than an accidental one. FastAPICache.reset() is also available if a test wants a full re-init.

Interim: test_status_router.py carries a local copy of this fixture. Remove it as part of the fix.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions