|
43 | 43 | __all__ = [ |
44 | 44 | "django_db_setup", |
45 | 45 | "db", |
| 46 | + "db_class", |
| 47 | + "db_module", |
| 48 | + "db_package", |
| 49 | + "db_session", |
46 | 50 | "transactional_db", |
47 | 51 | "django_db_reset_sequences", |
48 | 52 | "django_db_serialized_rollback", |
@@ -159,8 +163,7 @@ def django_db_setup( |
159 | 163 | ) |
160 | 164 |
|
161 | 165 |
|
162 | | -@pytest.fixture() |
163 | | -def _django_db_helper( |
| 166 | +def _base_django_db_helper( |
164 | 167 | request: pytest.FixtureRequest, |
165 | 168 | django_db_setup: None, |
166 | 169 | django_db_blocker: DjangoDbBlocker, |
@@ -256,6 +259,14 @@ def tearDownClass(cls) -> None: |
256 | 259 | PytestDjangoTestCase.doClassCleanups() |
257 | 260 |
|
258 | 261 |
|
| 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 | + |
259 | 270 | def validate_django_db(marker: pytest.Mark) -> _DjangoDb: |
260 | 271 | """Validate the django_db marker. |
261 | 272 |
|
@@ -321,22 +332,53 @@ def _set_suffix_to_test_databases(suffix: str) -> None: |
321 | 332 |
|
322 | 333 | # ############### User visible fixtures ################ |
323 | 334 |
|
| 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 | +""") |
324 | 348 |
|
325 | 349 | @pytest.fixture() |
326 | 350 | 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') |
328 | 354 |
|
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. |
335 | 355 |
|
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: |
339 | 379 | # The `_django_db_helper` fixture checks if `db` is requested. |
| 380 | + pass |
| 381 | +db_session.__doc__ = _db_fixture_help.format('session') |
340 | 382 |
|
341 | 383 |
|
342 | 384 | @pytest.fixture() |
|
0 commit comments