Skip to content

Commit

Permalink
Merge pull request #23 from maykinmedia/ignore-enabled-flag-in-test-h…
Browse files Browse the repository at this point in the history
…elper

Ignore the enabled setting when executing a step during testing
  • Loading branch information
swrichards authored Nov 28, 2024
2 parents 6976add + 56f8c3b commit c624f45
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,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
==============

Expand Down
6 changes: 3 additions & 3 deletions django_setup_configuration/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion django_setup_configuration/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
}
)

Expand Down
27 changes: 24 additions & 3 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand 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,
Expand All @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions tests/test_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit c624f45

Please sign in to comment.