opensafely-core / opensafely-core/opencodelists

Spike: don't persist fixtures in test database between tests

Open
#2,523 0 comments 0 reactions 1 assignee View on GitHub

@mikerkelly is already working on this.

Since May 15, 2025.

initiative:codelist-builder
Dominant language
Python
Stars
60
Forks
16
Avg merge
4d 12h
Merged PRs (30d)
17

Description

Why are we doing this?

The persistence of the "universe" of potential fixture objects in the database throughout the session risks making tests non-independent. For example, a test requiring no instances of a certain model cannot run alongside a test using fixtures, and the Playwright tests (https://github.com/opensafely-core/opencodelists/issues/2408) need a workaround for the non-empty database, changing the fixtures to be function-scoped only for the functional tests.

Always instantiating the full universe of instances also means the dependencies between fixtures are somewhat implicit.

How will we know when it's done?

We have considered and prototyped approaches to the above issues, and raised one or more issues for future work.

What are we doing?

An alternative approach would be to store fixture instances as in-memory objects rather than in the database and save them to the database only when a test needs them. The database could then roll back to an empty state between tests. This might involve changing the scope of most of the fixtures to "function" from "session".

This approach would allow us to invert the hierarchy of the universe so that only required instances are present in the database, not all of them.

The testing coding system databases would likely remain unchanged.

Note that https://github.com/opensafely-core/opencodelists/pull/2430 relies on the universe fixture so we probably want to continue providing that in some form, but not have all tests rely on it. Consider the impact on https://github.com/opensafely-core/opencodelists/issues/2543 and updating that Issue.


Defining delivery tasks guidance

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.