|
48 | 48 |
|
49 | 49 | import django |
50 | 50 |
|
| 51 | + from .fixtures import _DjangoDb, _DjangoDbDatabases |
| 52 | + |
51 | 53 |
|
52 | 54 | SETTINGS_MODULE_ENV = "DJANGO_SETTINGS_MODULE" |
53 | 55 | CONFIGURATION_ENV = "DJANGO_CONFIGURATION" |
@@ -262,12 +264,14 @@ def pytest_load_initial_conftests( |
262 | 264 | # Register the marks |
263 | 265 | early_config.addinivalue_line( |
264 | 266 | "markers", |
265 | | - "django_db(transaction=False, reset_sequences=False): " |
| 267 | + "django_db(transaction=False, reset_sequences=False, databases=None): " |
266 | 268 | "Mark the test as using the Django test database. " |
267 | 269 | "The *transaction* argument allows you to use real transactions " |
268 | 270 | "in the test like Django's TransactionTestCase. " |
269 | 271 | "The *reset_sequences* argument resets database sequences before " |
270 | | - "the test.", |
| 272 | + "the test. " |
| 273 | + "The *databases* argument sets which database aliases the test " |
| 274 | + "uses (by default, only 'default'). Use '__all__' for all databases.", |
271 | 275 | ) |
272 | 276 | early_config.addinivalue_line( |
273 | 277 | "markers", |
@@ -452,7 +456,11 @@ def _django_db_marker(request) -> None: |
452 | 456 | """ |
453 | 457 | marker = request.node.get_closest_marker("django_db") |
454 | 458 | if marker: |
455 | | - transaction, reset_sequences = validate_django_db(marker) |
| 459 | + transaction, reset_sequences, databases = validate_django_db(marker) |
| 460 | + |
| 461 | + # TODO: Use pytest Store (item.store) once that's stable. |
| 462 | + request.node._pytest_django_databases = databases |
| 463 | + |
456 | 464 | if reset_sequences: |
457 | 465 | request.getfixturevalue("django_db_reset_sequences") |
458 | 466 | elif transaction: |
@@ -727,21 +735,22 @@ def restore(self) -> None: |
727 | 735 | _blocking_manager = _DatabaseBlocker() |
728 | 736 |
|
729 | 737 |
|
730 | | -def validate_django_db(marker) -> Tuple[bool, bool]: |
| 738 | +def validate_django_db(marker) -> "_DjangoDb": |
731 | 739 | """Validate the django_db marker. |
732 | 740 |
|
733 | | - It checks the signature and creates the ``transaction`` and |
734 | | - ``reset_sequences`` attributes on the marker which will have the |
735 | | - correct values. |
| 741 | + It checks the signature and creates the ``transaction``, |
| 742 | + ``reset_sequences`` and ``databases`` attributes on the marker |
| 743 | + which will have the correct values. |
736 | 744 |
|
737 | 745 | A sequence reset is only allowed when combined with a transaction. |
738 | 746 | """ |
739 | 747 |
|
740 | 748 | def apifun( |
741 | 749 | transaction: bool = False, |
742 | 750 | reset_sequences: bool = False, |
743 | | - ) -> Tuple[bool, bool]: |
744 | | - return transaction, reset_sequences |
| 751 | + databases: "_DjangoDbDatabases" = None, |
| 752 | + ) -> "_DjangoDb": |
| 753 | + return transaction, reset_sequences, databases |
745 | 754 |
|
746 | 755 | return apifun(*marker.args, **marker.kwargs) |
747 | 756 |
|
|
0 commit comments