|
1 | 1 | from unittest.mock import MagicMock |
2 | 2 |
|
3 | 3 | import pytest |
4 | | -from django.core.management import call_command |
| 4 | +from django.core.management import CommandError, call_command |
| 5 | +from pytest_django.fixtures import SettingsWrapper |
5 | 6 | from pytest_mock import MockerFixture |
6 | 7 |
|
7 | 8 | from environments.identities.models import Identity |
|
21 | 22 | pytestmark = pytest.mark.django_db |
22 | 23 |
|
23 | 24 |
|
| 25 | +@pytest.fixture(autouse=True) |
| 26 | +def enable_local_database_reset(settings: SettingsWrapper) -> None: |
| 27 | + """Enable the reset_local_database command for tests.""" |
| 28 | + settings.ENABLE_LOCAL_DATABASE_RESET = True |
| 29 | + |
| 30 | + |
24 | 31 | @pytest.fixture(autouse=True) |
25 | 32 | def mock_reset_commands(mocker: MockerFixture) -> MagicMock: |
26 | 33 | """Mock flush/migrate/createcachetable to avoid resetting the test database.""" |
@@ -99,3 +106,16 @@ def test_reset_local_database__creates_expected_data() -> None: |
99 | 106 | identifiers = list(Identity.objects.values_list("identifier", flat=True)) |
100 | 107 | assert "alice@example.com" in identifiers |
101 | 108 | assert "bob@example.com" in identifiers |
| 109 | + |
| 110 | + |
| 111 | +def test_reset_local_database__raises_error_when_disabled( |
| 112 | + settings: SettingsWrapper, |
| 113 | +) -> None: |
| 114 | + # Given |
| 115 | + settings.ENABLE_LOCAL_DATABASE_RESET = False |
| 116 | + |
| 117 | + # When / Then |
| 118 | + with pytest.raises(CommandError) as exc_info: |
| 119 | + call_command("reset_local_database") |
| 120 | + |
| 121 | + assert "ENABLE_LOCAL_DATABASE_RESET" in str(exc_info.value) |
0 commit comments