Tests updated after previous refactoring

This commit is contained in:
Emmanuel BENOîT 2022-11-06 11:19:39 +01:00
parent bebf799dff
commit 84cc28b60f
No known key found for this signature in database
GPG key ID: 2356DC6956CF54EF

View file

@ -12,7 +12,7 @@ class _Instruction(reconstructed.RcInstruction):
def parse_action(self, record): def parse_action(self, record):
pass pass
def execute_action(self, host_name, variables): def execute_action(self, host_name, context):
pass pass
@ -37,6 +37,14 @@ def variables():
return mock.MagicMock() return mock.MagicMock()
@pytest.fixture
def context(variables):
"""Create a mock execution context."""
c = mock.MagicMock()
c.variables = variables
return c
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def mock_isidentifier(): def mock_isidentifier():
reconstructed.isidentifier = mock.MagicMock() reconstructed.isidentifier = mock.MagicMock()
@ -450,23 +458,23 @@ class TestRunFor:
instr.evaluate_loop = mock.MagicMock() instr.evaluate_loop = mock.MagicMock()
return instr return instr
def test_run_no_loop(self, instr, variables): def test_run_no_loop(self, instr, context):
"""Running with no loop set causes ``run_iteration()`` to be called.""" """Running with no loop set causes ``run_iteration()`` to be called."""
hn = object() hn = object()
save = object() save = object()
instr._save = save instr._save = save
instr._executed_once = None instr._executed_once = None
# #
rv = instr.run_for(hn, variables) rv = instr.run_for(hn, context)
# #
assert instr._executed_once is None assert instr._executed_once is None
variables._script_stack_push.assert_called_once_with(save) context.variables._script_stack_push.assert_called_once_with(save)
variables._script_stack_pop.assert_called_once_with() context.variables._script_stack_pop.assert_called_once_with()
instr.run_iteration.assert_called_once_with(hn, variables) instr.run_iteration.assert_called_once_with(hn, context)
instr.evaluate_loop.assert_not_called() instr.evaluate_loop.assert_not_called()
assert rv is instr.run_iteration.return_value assert rv is instr.run_iteration.return_value
def test_crash_no_loop(self, instr, variables): def test_crash_no_loop(self, instr, context):
"""If ``run_iteration()`` crashes when there is no loop, the stack \ """If ``run_iteration()`` crashes when there is no loop, the stack \
is popped and the exception is propagated.""" is popped and the exception is propagated."""
hn = object() hn = object()
@ -476,14 +484,14 @@ class TestRunFor:
instr.run_iteration.side_effect = RuntimeError instr.run_iteration.side_effect = RuntimeError
# #
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
instr.run_for(hn, variables) instr.run_for(hn, context)
# #
assert instr._executed_once is None assert instr._executed_once is None
variables._script_stack_push.assert_called_once_with(save) context.variables._script_stack_push.assert_called_once_with(save)
variables._script_stack_pop.assert_called_once_with() context.variables._script_stack_pop.assert_called_once_with()
instr.run_iteration.assert_called_once_with(hn, variables) instr.run_iteration.assert_called_once_with(hn, context)
def test_run_once_first_time(self, instr, variables): def test_run_once_first_time(self, instr, context):
"""The method updates the execution flag and executes the iteration \ """The method updates the execution flag and executes the iteration \
if it is set to run once but hasn't been called yet.""" if it is set to run once but hasn't been called yet."""
hn = object() hn = object()
@ -491,16 +499,16 @@ class TestRunFor:
instr._save = save instr._save = save
instr._executed_once = False instr._executed_once = False
# #
rv = instr.run_for(hn, variables) rv = instr.run_for(hn, context)
# #
assert instr._executed_once is True assert instr._executed_once is True
variables._script_stack_push.assert_called_once_with(save) context.variables._script_stack_push.assert_called_once_with(save)
variables._script_stack_pop.assert_called_once_with() context.variables._script_stack_pop.assert_called_once_with()
instr.run_iteration.assert_called_once_with(hn, variables) instr.run_iteration.assert_called_once_with(hn, context)
instr.evaluate_loop.assert_not_called() instr.evaluate_loop.assert_not_called()
assert rv is instr.run_iteration.return_value assert rv is instr.run_iteration.return_value
def test_run_once_already_called(self, instr, variables): def test_run_once_already_called(self, instr, context):
"""The method returns ``True`` but does nothing if it has already been \ """The method returns ``True`` but does nothing if it has already been \
called.""" called."""
hn = object() hn = object()
@ -508,16 +516,16 @@ class TestRunFor:
instr._save = save instr._save = save
instr._executed_once = True instr._executed_once = True
# #
rv = instr.run_for(hn, variables) rv = instr.run_for(hn, context)
# #
assert instr._executed_once is True assert instr._executed_once is True
variables._script_stack_push.assert_not_called() context.variables._script_stack_push.assert_not_called()
variables._script_stack_pop.assert_not_called() context.variables._script_stack_pop.assert_not_called()
instr.run_iteration.assert_not_called() instr.run_iteration.assert_not_called()
instr.evaluate_loop.assert_not_called() instr.evaluate_loop.assert_not_called()
assert rv is True assert rv is True
def test_run_loop(self, instr, variables): def test_run_loop(self, instr, context):
"""Running with a loop set causes ``evaluate_loop()`` to be called, \ """Running with a loop set causes ``evaluate_loop()`` to be called, \
followed by a call to ``run_iteration()`` for each value it \ followed by a call to ``run_iteration()`` for each value it \
returned.""" returned."""
@ -530,25 +538,25 @@ class TestRunFor:
instr._loop_var = lv instr._loop_var = lv
instr.evaluate_loop.return_value = (1, 2, 3) instr.evaluate_loop.return_value = (1, 2, 3)
# #
rv = instr.run_for(hn, variables) rv = instr.run_for(hn, context)
# #
assert instr._executed_once is None assert instr._executed_once is None
variables._script_stack_push.assert_called_once_with(save) context.variables._script_stack_push.assert_called_once_with(save)
variables._script_stack_pop.assert_called_once_with() context.variables._script_stack_pop.assert_called_once_with()
instr.evaluate_loop.assert_called_once_with(hn, variables) instr.evaluate_loop.assert_called_once_with(hn, context.variables)
assert variables.__setitem__.call_args_list == [ assert context.variables.__setitem__.call_args_list == [
mock.call(lv, 1), mock.call(lv, 1),
mock.call(lv, 2), mock.call(lv, 2),
mock.call(lv, 3), mock.call(lv, 3),
] ]
assert instr.run_iteration.call_args_list == [ assert instr.run_iteration.call_args_list == [
mock.call(hn, variables), mock.call(hn, context),
mock.call(hn, variables), mock.call(hn, context),
mock.call(hn, variables), mock.call(hn, context),
] ]
assert rv is True assert rv is True
def test_run_loop_exit(self, instr, variables): def test_run_loop_exit(self, instr, context):
"""If ``run_iteration()`` returns a falsy value, the loop is interrupted.""" """If ``run_iteration()`` returns a falsy value, the loop is interrupted."""
hn = object() hn = object()
save = object() save = object()
@ -560,17 +568,17 @@ class TestRunFor:
instr.evaluate_loop.return_value = (1, 2, 3) instr.evaluate_loop.return_value = (1, 2, 3)
instr.run_iteration.return_value = False instr.run_iteration.return_value = False
# #
rv = instr.run_for(hn, variables) rv = instr.run_for(hn, context)
# #
assert instr._executed_once is None assert instr._executed_once is None
variables._script_stack_push.assert_called_once_with(save) context.variables._script_stack_push.assert_called_once_with(save)
variables._script_stack_pop.assert_called_once_with() context.variables._script_stack_pop.assert_called_once_with()
instr.evaluate_loop.assert_called_once_with(hn, variables) instr.evaluate_loop.assert_called_once_with(hn, context.variables)
assert variables.__setitem__.call_args_list == [mock.call(lv, 1)] assert context.variables.__setitem__.call_args_list == [mock.call(lv, 1)]
assert instr.run_iteration.call_args_list == [mock.call(hn, variables)] assert instr.run_iteration.call_args_list == [mock.call(hn, context)]
assert rv is False assert rv is False
def test_crash_loop(self, instr, variables): def test_crash_loop(self, instr, context):
"""If ``run_iteration()`` crashes when there is a loop, the stack \ """If ``run_iteration()`` crashes when there is a loop, the stack \
is popped and the exception is propagated.""" is popped and the exception is propagated."""
hn = object() hn = object()
@ -584,13 +592,13 @@ class TestRunFor:
instr.run_iteration.side_effect = RuntimeError instr.run_iteration.side_effect = RuntimeError
# #
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
instr.run_for(hn, variables) instr.run_for(hn, context)
# #
assert instr._executed_once is None assert instr._executed_once is None
variables._script_stack_push.assert_called_once_with(save) context.variables._script_stack_push.assert_called_once_with(save)
variables._script_stack_pop.assert_called_once_with() context.variables._script_stack_pop.assert_called_once_with()
assert variables.__setitem__.call_args_list == [mock.call(lv, 1)] assert context.variables.__setitem__.call_args_list == [mock.call(lv, 1)]
assert instr.run_iteration.call_args_list == [mock.call(hn, variables)] assert instr.run_iteration.call_args_list == [mock.call(hn, context)]
class TestRunIteration: class TestRunIteration:
@ -607,46 +615,46 @@ class TestRunIteration:
instr.execute_action = mock.MagicMock() instr.execute_action = mock.MagicMock()
return instr return instr
def test_run_cond_false(self, instr, variables): def test_run_cond_false(self, instr, context):
"""If the condition is not satisfied, ``True`` is returned but the \ """If the condition is not satisfied, ``True`` is returned but the \
action is not executed.""" action is not executed."""
hn = object() hn = object()
instr.evaluate_condition.return_value = False instr.evaluate_condition.return_value = False
# #
rv = instr.run_iteration(hn, variables) rv = instr.run_iteration(hn, context)
# #
instr.compute_locals.assert_called_once_with(variables) instr.compute_locals.assert_called_once_with(context.variables)
instr.evaluate_condition.assert_called_once_with(hn, variables) instr.evaluate_condition.assert_called_once_with(hn, context.variables)
instr.execute_action.assert_not_called() instr.execute_action.assert_not_called()
instr._display.vvvvv.assert_not_called() instr._display.vvvvv.assert_not_called()
assert rv is True assert rv is True
def test_run_cond_true(self, instr, variables): def test_run_cond_true(self, instr, context):
"""If the condition is satisfied, the action is executed and its \ """If the condition is satisfied, the action is executed and its \
return value is returned.""" return value is returned."""
hn = object() hn = object()
instr.evaluate_condition.return_value = True instr.evaluate_condition.return_value = True
# #
rv = instr.run_iteration(hn, variables) rv = instr.run_iteration(hn, context)
# #
instr.compute_locals.assert_called_once_with(variables) instr.compute_locals.assert_called_once_with(context.variables)
instr.evaluate_condition.assert_called_once_with(hn, variables) instr.evaluate_condition.assert_called_once_with(hn, context.variables)
instr.execute_action.assert_called_once_with(hn, variables) instr.execute_action.assert_called_once_with(hn, context)
instr._display.vvvvv.assert_not_called() instr._display.vvvvv.assert_not_called()
assert rv is instr.execute_action.return_value assert rv is instr.execute_action.return_value
def test_run_interrupt(self, instr, variables): def test_run_interrupt(self, instr, context):
"""If the condition is satisfied and the action returns ``False``, a \ """If the condition is satisfied and the action returns ``False``, a \
debug message is displayed.""" debug message is displayed."""
hn = object() hn = object()
instr.evaluate_condition.return_value = True instr.evaluate_condition.return_value = True
instr.execute_action.return_value = False instr.execute_action.return_value = False
# #
rv = instr.run_iteration(hn, variables) rv = instr.run_iteration(hn, context)
# #
instr.compute_locals.assert_called_once_with(variables) instr.compute_locals.assert_called_once_with(context.variables)
instr.evaluate_condition.assert_called_once_with(hn, variables) instr.evaluate_condition.assert_called_once_with(hn, context.variables)
instr.execute_action.assert_called_once_with(hn, variables) instr.execute_action.assert_called_once_with(hn, context)
instr._display.vvvvv.assert_called_once() instr._display.vvvvv.assert_called_once()
assert rv is False assert rv is False