Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test helpers for dynamic database-based tests
See #2115 for full details. The problem this refactoring aims to solve is, in short, as quoted from @mikerkelly in #2115: > The [DatabaseOperationForbidden] errors occur because there's a mismatch > between what [these test cases](https://github.com/opensafely-core/opencodelists/blob/main/coding_systems/bnf/tests/test_import_data.py) > that import databases dynamically are > trying to do and how both the Django and `pytest-django` test case > frameworks work. In a multi-database context, the test frameworks want > to declare which databases are available to a test case at > test-case-definition-time, which then triggers those databases being > created, the test case wrapped in a transaction referencing those > databases, and the databases flushed in clean up. Since 5.1 it's > enforced that test cases only open connections to such databases, to > avoid accidental access. But the [coding system release import tests](https://github.com/opensafely-core/opencodelists/blob/main/coding_systems/bnf/tests/test_import_data.py) > do want to access a database that they do not want to exist at the start > of the test. That's in conflict. So maybe we shouldn't be using test cases > this way at all? Mike proposed several ideas to fix this that we looked at, and we found that: amending the test case attribute that defines the allowed databases seems reasonable, and, further, this was the only solution that we had working. It was decided that refactoring the existing tests was the solution for now, rather than rethinking them or the code under test entirely. This facilitates the upgrade to Django 5.1 without blocking us further. We may have to revisit this code and these tests in future, with a view to a more comprehensive overhaul. This single class facilitates getting most of the tests working by just moving them into a test class that uses this DynamicDatabaseTestCase class. Those tests have a generalisable pattern for accessing the databases. The BNF and base coding system tests are slightly more complicated, and the issues will be described in those respective subsequent commits. Co-authored-by: Mike Kelly <mike.kelly@thedatalab.org>
- Loading branch information