openclimatefix / openclimatefix/quartz-api

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

Open Beginner friendly
#374 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
10
Forks
32
Avg merge
1d 3h
Merged PRs (30d)
13

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the shared fixture in src/quartz_api/internal/service/conftest.py, then inspect the FastAPICache.init calls and local fixture in src/quartz_api/internal/service/v1/test_router.py and test_status_router.py. Remove the redundant setup as described, run the full unit suite, and confirm the v1 cold-cache tests use isolated caches and all 223 tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
testing
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
84/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.