Skip to content

Commit 91f7e29

Browse files
committed
fixtures: provide per-scope fixtures for unblocking database access
Allow the convenience of `db` fixture for all fixture scopes.
1 parent 1ffc323 commit 91f7e29

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

pytest_django/fixtures.py

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
__all__ = [
4444
"django_db_setup",
4545
"db",
46+
"db_class",
47+
"db_module",
48+
"db_package",
49+
"db_session",
4650
"transactional_db",
4751
"django_db_reset_sequences",
4852
"django_db_serialized_rollback",
@@ -159,8 +163,7 @@ def django_db_setup(
159163
)
160164

161165

162-
@pytest.fixture()
163-
def _django_db_helper(
166+
def _base_django_db_helper(
164167
request: pytest.FixtureRequest,
165168
django_db_setup: None,
166169
django_db_blocker: DjangoDbBlocker,
@@ -256,6 +259,14 @@ def tearDownClass(cls) -> None:
256259
PytestDjangoTestCase.doClassCleanups()
257260

258261

262+
# Per-scope db helpers.
263+
_django_db_helper = pytest.fixture(_base_django_db_helper, scope="function")
264+
_django_db_helper_class = pytest.fixture(_base_django_db_helper, scope="class")
265+
_django_db_helper_module = pytest.fixture(_base_django_db_helper, scope="module")
266+
_django_db_helper_package = pytest.fixture(_base_django_db_helper, scope="package")
267+
_django_db_helper_session = pytest.fixture(_base_django_db_helper, scope="session")
268+
269+
259270
def validate_django_db(marker: pytest.Mark) -> _DjangoDb:
260271
"""Validate the django_db marker.
261272
@@ -321,22 +332,53 @@ def _set_suffix_to_test_databases(suffix: str) -> None:
321332

322333
# ############### User visible fixtures ################
323334

335+
_db_fixture_help = (
336+
"""Require a django test database for use by tests and {}-scoped fixtures.
337+
338+
This database will be setup with the default fixtures and will have
339+
the transaction management disabled. At the end of the test the outer
340+
transaction that wraps the test itself will be rolled back to undo any
341+
changes to the database (in case the backend supports transactions).
342+
This is more limited than the ``transactional_db`` fixture but
343+
faster.
344+
345+
If both ``db`` and ``transactional_db`` are requested,
346+
``transactional_db`` takes precedence.
347+
""")
324348

325349
@pytest.fixture()
326350
def db(_django_db_helper: None) -> None:
327-
"""Require a django test database.
351+
# The `_django_db_helper` fixture checks if `db` is requested.
352+
pass
353+
db.__doc__ = _db_fixture_help.format('function')
328354

329-
This database will be setup with the default fixtures and will have
330-
the transaction management disabled. At the end of the test the outer
331-
transaction that wraps the test itself will be rolled back to undo any
332-
changes to the database (in case the backend supports transactions).
333-
This is more limited than the ``transactional_db`` fixture but
334-
faster.
335355

336-
If both ``db`` and ``transactional_db`` are requested,
337-
``transactional_db`` takes precedence.
338-
"""
356+
@pytest.fixture(scope="class")
357+
def db_class(_django_db_helper_class: None) -> None:
358+
# The `_django_db_helper` fixture checks if `db` is requested.
359+
pass
360+
db_class.__doc__ = _db_fixture_help.format('class')
361+
362+
363+
@pytest.fixture(scope="module")
364+
def db_module(_django_db_helper_module: None) -> None:
365+
# The `_django_db_helper` fixture checks if `db` is requested.
366+
pass
367+
db_module.__doc__ = _db_fixture_help.format('module')
368+
369+
370+
@pytest.fixture(scope="package")
371+
def db_package(_django_db_helper_package: None) -> None:
372+
# The `_django_db_helper` fixture checks if `db` is requested.
373+
pass
374+
db_package.__doc__ = _db_fixture_help.format('package')
375+
376+
377+
@pytest.fixture(scope="session")
378+
def db_session(_django_db_helper_session: None) -> None:
339379
# The `_django_db_helper` fixture checks if `db` is requested.
380+
pass
381+
db_session.__doc__ = _db_fixture_help.format('session')
340382

341383

342384
@pytest.fixture()

pytest_django/plugin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,21 @@
2020
from .django_compat import is_django_unittest
2121
from .fixtures import (
2222
_django_db_helper, # noqa: F401
23+
_django_db_helper_class, # noqa: F401
24+
_django_db_helper_module, # noqa: F401
25+
_django_db_helper_package, # noqa: F401
26+
_django_db_helper_session, # noqa: F401
2327
_live_server_helper, # noqa: F401
2428
admin_client, # noqa: F401
2529
admin_user, # noqa: F401
2630
async_client, # noqa: F401
2731
async_rf, # noqa: F401
2832
client, # noqa: F401
2933
db, # noqa: F401
34+
db_class, # noqa: F401
35+
db_module, # noqa: F401
36+
db_package, # noqa: F401
37+
db_session, # noqa: F401
3038
django_assert_max_num_queries, # noqa: F401
3139
django_assert_num_queries, # noqa: F401
3240
django_capture_on_commit_callbacks, # noqa: F401

0 commit comments

Comments
 (0)