pytest-dev / pytest-dev/pytest-django
Mutl databases (multi db) support
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 367
- PR merge metrics
- No merged PRs in 30d
Description
This issue replaces some historical issues: #76, #342, #423, #461, #828, #838, #839 (probably a partial list).
Background
Django supports multi databases. This means defining multiple entries in the DATABASE setting, which then allows directly certain queries to certain databases.
One case is when an extra database is entirely independent, has its own migrations, setups etc.
Second case is when an extra database is readonly, only used for read queries, not managed by Django.
Third case is a readonly replica, for this Django provides the MIRROR setting
Django allows configuring the order in which test databases are set up.
Django's multi-db testing support
pytest-django mostly relies on Django's underlying TransactionTestCase and TestCase classes for dealing with DB setups and such. Each pytest-django test gets run in a dynamically-generated TestCase/TransactionTestCase.
The main setting for mutli-db support is TransactionTestCase.databases. This tells Django which databases to consider for the test case. By default it's only default. It's possible to specify __all__ to include all databases.
Historical note: The TransactionTestCase.databases attribute was added in Django 2.2. Before that a multi_db attribute was used. pytest-django only supports Django>=2.2 so we happily don't need to concern ourselves with that.
Previous attempts
#397 - Adds multi_db=True argument to pytest.mark.django_db(), adds django_multi_db fixture. Problem: uses the old multi_db attribute instead of the databases attribute.
#416 - Very similar to #397.
#431 - Adds django_db_testcase fixture which allows the user to completely customize the test case class, including setting databases. Rejected for being too flexible, I'd prefer direct support for multi-db.
#896 - Adds a global per-database setting for whether to add to the databases value or not. Rejected because I think it should be possible to customize per-test.
Proposed solution
IMO we want something like #397/#416, but modernized to use databases instead of multi_db. The fixture part would be a bit problematic because it's no longer just a boolean (fixture enabled/not enabled), but a list of database aliases. So some solution would be needed for that, or maybe only the mark would be supported.
I'll try to work on it myself, but if for some reason I don't, PRs are definitely welcome!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the existing django_db mark, django_db fixture, and dynamically generated TestCase/TransactionTestCase path, then compare them with Django's TransactionTestCase.databases behavior. Done means a per-test solution can select database aliases for independent, read-only, or mirrored databases without relying on the older multi_db attribute.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, python
- Domain
- backend, databases, testing
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100