def test_executes_no_task(): law = Law(name="Empty", description="empty law", rules=()) law.commit = Mock() law.enforce() assert len(law.rules) == 0 assert law.commit.called is True
def test_executes_task(): rule = get_passing_rule() rule.execute = Mock() law = Law(name="Passing", description="passing law", rules=(rule,)) law.commit = Mock() law.enforce() rule.execute.assert_called_once_with() law.commit.assert_called_once_with()
def test_executes_task_failed_preconditions(): rule = get_passing_rule() rule.execute = Mock() law = Law( name="Passing", description="passing law", rules=(rule,), preconditions=[ExamplePrecondition(param=False)], ) law.commit = Mock() law.enforce() assert rule.execute.called is False assert law.commit.called is False
def test_executes_task_preconditions(): rule = get_passing_rule() rule.execute = Mock() law = Law( name="Passing", description="passing law", rules=(rule,), preconditions=[ExamplePrecondition(param=True)], ) law.commit = Mock() law.enforce() rule.execute.assert_called_once_with() law.commit.assert_called_once_with()
def test_rule_execution_failed_no_abort(mocked_config, mocked_logging): mocked_config.settings.rule_can_abort = False mocked_logging.error = Mock() expected_exception = "failed" rule = get_failing_rule() rule.made_changes = False rule.param = expected_exception rule.get_rule_chain = Mock(return_value=[get_passing_rule()]) law = Law(name="Failing", description="failing law", rules=(rule,)) law.commit = Mock() law.enforce() assert mocked_logging.error.called rule.get_rule_chain.assert_called_once_with(rule) assert law.commit.called is True
def test_rule_execution_aborted(mocked_logging, law_config, base_rule_config): law_config.settings.rule_can_abort = True base_rule_config.settings.dry_run = False mocked_logging.error = Mock() expected_exception = "failed" rule = get_failing_rule() rule.param = expected_exception rule.get_rule_chain = Mock(return_value=[get_passing_rule()]) law = Law(name="Passing", description="passing law", rules=(rule,)) law.commit = Mock() with pytest.raises(Exception) as exc: law.enforce() assert mocked_logging.error.called assert str(exc.value) == expected_exception rule.get_rule_chain.assert_called_once_with(rule) assert law.commit.called is False
def test_rule_execution_failed_precondition_no_abort(mocked_config, mocked_logging): mocked_config.settings.rule_can_abort = False mocked_logging.warning = Mock() expected_exception = "failed" rule = get_passing_rule() rule.made_changes = False rule.preconditions = [ExamplePrecondition(param=False)] rule.param = expected_exception rule.get_rule_chain = Mock(return_value=[get_passing_rule()]) law = Law(name="Failing", description="failing law", rules=(rule,)) law.commit = Mock() law.enforce() assert mocked_logging.warning.called assert law.commit.called is True