From 56f8c3bef2bbec9e9fb29c6e7303b5ca82dabf88 Mon Sep 17 00:00:00 2001 From: Sidney Richards Date: Wed, 27 Nov 2024 16:54:05 +0100 Subject: [PATCH] Ignore the enabled setting when executing a step during testing --- README.rst | 3 +++ django_setup_configuration/runner.py | 6 +++--- django_setup_configuration/test_utils.py | 3 ++- tests/conftest.py | 3 ++- tests/test_runner.py | 27 +++++++++++++++++++++--- tests/test_test_utils.py | 22 +++++++++++++++++++ 6 files changed, 56 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index 881b9ca..5b86b6c 100644 --- a/README.rst +++ b/README.rst @@ -257,6 +257,9 @@ Using Test Helpers # Add assertions +Note that when using ``execute_single_step``, the enabled flag in your setting source +will be ignored and the step will be executed regardless of its presence or value. + Best Practices -------------- diff --git a/django_setup_configuration/runner.py b/django_setup_configuration/runner.py index ebe0f64..9f6e7e1 100644 --- a/django_setup_configuration/runner.py +++ b/django_setup_configuration/runner.py @@ -127,8 +127,7 @@ def _validate_requirements_for_step(self, step: BaseConfigurationStep): return getattr(model_settings_instance, step.namespace) def _execute_step( - self, - step: BaseConfigurationStep, + self, step: BaseConfigurationStep, *, ignore_enabled: bool = False ): if step not in self.configured_steps: raise ConfigurationRunFailed( @@ -140,8 +139,9 @@ def _execute_step( step=step, config_model=None, is_enabled=False, + has_run=False, ) - if not (is_enabled := step in self.enabled_steps): + if not (is_enabled := step in self.enabled_steps) and not ignore_enabled: return result_factory() result_factory = partial(result_factory, is_enabled=True) diff --git a/django_setup_configuration/test_utils.py b/django_setup_configuration/test_utils.py index 688f804..761a6c5 100644 --- a/django_setup_configuration/test_utils.py +++ b/django_setup_configuration/test_utils.py @@ -36,6 +36,7 @@ def build_step_config_from_sources( def execute_single_step( step: type[BaseConfigurationStep] | str, + *, yaml_source: str | None = None, object_source: dict | None = None, ) -> StepExecutionResult: @@ -59,7 +60,7 @@ def execute_single_step( yaml_source=yaml_source, object_source=object_source, ) - result = runner._execute_step(runner.configured_steps[0]) + result = runner._execute_step(runner.configured_steps[0], ignore_enabled=True) if result.run_exception: raise result.run_exception diff --git a/tests/conftest.py b/tests/conftest.py index 68efd55..892c2af 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -93,10 +93,11 @@ def test_step_bad_yaml_path(yaml_file_factory): @pytest.fixture() -def test_step_disabled_yaml_path(yaml_file_factory): +def test_step_disabled_yaml_path(yaml_file_factory, test_step_valid_config): return yaml_file_factory( { "test_step_is_enabled": False, + "test_step": test_step_valid_config["test_step"], } ) diff --git a/tests/test_runner.py b/tests/test_runner.py index 2fe498f..40eb93c 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -121,7 +121,6 @@ def test_exception_during_execute_all_is_included_in_result( runner, step_execute_mock, expected_step_config, - runner_step, ): step_execute_mock.side_effect = Exception() results = runner.execute_all() @@ -146,7 +145,7 @@ def test_disabled_steps_are_not_run( (step,) = runner_with_step_disabled_yaml.configured_steps result = runner_with_step_disabled_yaml._execute_step(step) - assert not result.is_enabled + assert result.is_enabled is False assert result == StepExecutionResult( step=result.step, is_enabled=False, @@ -158,7 +157,29 @@ def test_disabled_steps_are_not_run( step_execute_mock.assert_not_called() -def test_settings_can_be_overriden(test_step_yaml_path, test_step_valid_config): +def test_disabled_steps_are_run_if_override_flag_is_provided( + runner_with_step_disabled_yaml, + step_execute_mock, + expected_step_config, +): + (step,) = runner_with_step_disabled_yaml.configured_steps + result = runner_with_step_disabled_yaml._execute_step(step, ignore_enabled=True) + + assert result.is_enabled is False + assert result == StepExecutionResult( + step=result.step, + is_enabled=False, + has_run=True, + run_exception=None, + config_model=expected_step_config, + ) + assert type(result.step) is TestStep + step_execute_mock.assert_called_once_with(expected_step_config) + + +def test_settings_can_be_overriden_with_object( + test_step_yaml_path, test_step_valid_config +): runner = SetupConfigurationRunner( steps=[TestStep], yaml_source=test_step_yaml_path, diff --git a/tests/test_test_utils.py b/tests/test_test_utils.py index 6674403..8efb192 100644 --- a/tests/test_test_utils.py +++ b/tests/test_test_utils.py @@ -36,3 +36,25 @@ def test_execute_single_step_returns_result_if_no_exceptions( assert isinstance(result.step, TestStep) step_execute_mock.assert_called_once_with(expected_step_config) + + +def test_execute_single_step_ignores_enabled_setting( + step_execute_mock, + test_step_yaml_path, + expected_step_config, +): + result = execute_single_step( + TestStep, + yaml_source=test_step_yaml_path, + object_source={"test_step_is_enabled": False}, + ) + + assert result == StepExecutionResult( + step=result.step, + is_enabled=False, + has_run=True, + run_exception=None, + config_model=expected_step_config, + ) + assert isinstance(result.step, TestStep) + step_execute_mock.assert_called_once_with(expected_step_config)