Exemplo n.º 1
0
 def test_notify_no_rollback(monkeypatch):
     notify_mock = MagicMock()
     monkeypatch.setattr(Action, "notify", notify_mock)
     act = StatefullAction([], lambda state: 10)
     prep = act.get_prepared_action()
     prep.notify("rollback", "begin")
     assert not notify_mock.called
Exemplo n.º 2
0
 def test__checks_state_items_for_rollback_failure_missing():
     action = StatefullAction()
     action.rollback_action(["input"], lambda state: 10)
     action._state = {}
     with pytest.raises(ActionException):
         with action._checks_state_items_for_rollback():
             pass
Exemplo n.º 3
0
 def test_simulate(monkeypatch):
     monkeypatch.setattr(InfoStreamer, "send_info", MagicMock())
     action = StatefullAction()
     action._state = ReferencesDict()
     state = {"a": 10}
     action.simulate("action", state)
     assert dict(action.state) == state
Exemplo n.º 4
0
 def test_get_prepared_action(monkeypatch):
     monkeypatch.setattr(InfoStreamer, "send_info", MagicMock())
     monkeypatch.setattr(StatefullAction, "_check_kwargs_for_action",
                         MagicMock())
     monkeypatch.setattr(StatefullAction, "add_context_manager",
                         MagicMock())
     action = StatefullAction(["input"], lambda state: state["input"])
     prepared = action.get_prepared_action(input=10)
     assert prepared.do() == 10
Exemplo n.º 5
0
 def test__checks_state_items_for_rollback_failure_exception():
     action = StatefullAction()
     action.rollback_action(["input"], lambda state: 10)
     action._state = {}
     try:
         with action._checks_state_items_for_rollback():
             raise Exception()
     except Exception:
         pass
     assert action._state["action_failed"]
Exemplo n.º 6
0
 def test_get_prepared_action_already_prepared(monkeypatch):
     monkeypatch.setattr(InfoStreamer, "send_info", MagicMock())
     monkeypatch.setattr(StatefullAction, "_check_kwargs_for_action",
                         MagicMock())
     monkeypatch.setattr(StatefullAction, "add_context_manager",
                         MagicMock())
     action = StatefullAction(["input"], lambda state: state["input"])
     prepared = action.get_prepared_action(input=10)
     with pytest.raises(ActionException):
         prepared.get_prepared_action()
Exemplo n.º 7
0
 def test_get_prepared_action_change_name(monkeypatch):
     monkeypatch.setattr(InfoStreamer, "send_info", MagicMock())
     monkeypatch.setattr(StatefullAction, "_check_kwargs_for_action",
                         MagicMock())
     monkeypatch.setattr(StatefullAction, "add_context_manager",
                         MagicMock())
     action = StatefullAction(["input"], lambda state: 10, name="base_name")
     prepared = action.get_prepared_action(input=20)
     prepared.name = "prepared_action_name"
     assert action.name == "base_name"
     assert prepared.name == "prepared_action_name"
Exemplo n.º 8
0
 def test_get_prepared_action_add_context_manager(monkeypatch):
     monkeypatch.setattr(InfoStreamer, "send_info", MagicMock())
     monkeypatch.setattr(StatefullAction, "_check_kwargs_for_action",
                         MagicMock())
     action = StatefullAction(["input"], lambda state: 10)
     prepared = action.get_prepared_action(input=20)
     context_manager_mock = MagicMock()
     prepared.add_context_manager("action", context_manager_mock)
     prepared2 = action.get_prepared_action(input=20)
     context_manager_mock2 = MagicMock()
     prepared2.add_context_manager("action", context_manager_mock2)
     prepared.do()
     assert context_manager_mock.called and not context_manager_mock2.called
     prepared2.do()
     assert context_manager_mock.call_count == 1 and context_manager_mock2.called
Exemplo n.º 9
0
 def test_get_prepared_action_no_action():
     action = StatefullAction()
     with pytest.raises(ActionException):
         action.get_prepared_action()
Exemplo n.º 10
0
 def test_action(monkeypatch):
     monkeypatch.setattr(StatefullAction, "_check_fct", MagicMock())
     action = StatefullAction()
     assert action.action(lambda state: 10)
Exemplo n.º 11
0
 def test_state_write():
     with pytest.raises(ActionException):
         action = StatefullAction()
         action.state = {}
Exemplo n.º 12
0
 def test_action_not_good(monkeypatch):
     monkeypatch.setattr(StatefullAction, "_check_fct",
                         MagicMock(side_effect=ActionException))
     action = StatefullAction()
     with pytest.raises(ActionException):
         action.action(lambda state: 10)
Exemplo n.º 13
0
 def test_notify(monkeypatch):
     notify_mock = MagicMock()
     monkeypatch.setattr(Action, "notify", notify_mock)
     StatefullAction().notify("action", "begin")
     assert notify_mock.called
Exemplo n.º 14
0
 def test_call_with_state():
     action = StatefullAction(action_fct=lambda state: 10)
     action._state = {}
     with pytest.raises(ActionException):
         action({})
Exemplo n.º 15
0
 def test_action_twice(monkeypatch):
     monkeypatch.setattr(StatefullAction, "_check_fct", MagicMock())
     action = StatefullAction()
     action.action(lambda state: 10)
     with pytest.raises(ActionException):
         action.action(lambda state: 10)
Exemplo n.º 16
0
 def test__check_kwargs_for_action_missing():
     action = StatefullAction(["input"])
     with pytest.raises(ActionException):
         action._check_kwargs_for_action({})
Exemplo n.º 17
0
 def test__check_kwargs_for_action_superfluous():
     action = StatefullAction(["input"])
     with pytest.raises(ActionException):
         action._check_kwargs_for_action({"input": 10, "superfluous": 20})
Exemplo n.º 18
0
 def test_call_no_fct():
     with pytest.raises(ActionException):
         action = StatefullAction()
         action({})
Exemplo n.º 19
0
 def test_init_params():
     assert StatefullAction(["test_item"],
                            lambda state: 10,
                            name="better_name")
Exemplo n.º 20
0
 def test_rollback_action_with_fct(monkeypatch):
     monkeypatch.setattr(StatefullAction, "_check_fct", MagicMock())
     action = StatefullAction()
     action.rollback_action(fct=lambda state: 10)
     assert action._rollback_fct({}) == 10
Exemplo n.º 21
0
 def test_rollback_action_twice(monkeypatch):
     monkeypatch.setattr(StatefullAction, "_check_fct", MagicMock())
     decorator = StatefullAction().rollback_action()
     decorator(lambda state: 10)
     with pytest.raises(ActionException):
         decorator(lambda state: 10)
Exemplo n.º 22
0
 def test_rollback_action(monkeypatch):
     monkeypatch.setattr(StatefullAction, "_check_fct", MagicMock())
     action = StatefullAction()
     decorator = action.rollback_action()
     decorator(lambda state: 10)
     assert action._rollback_fct({}) == 10
Exemplo n.º 23
0
 def test_state_fail():
     with pytest.raises(ActionException):
         StatefullAction().state
Exemplo n.º 24
0
 def test__checks_state_items_for_rollback():
     action = StatefullAction()
     action.rollback_action(["input"], lambda state: 10)
     action._state = {}
     with action._checks_state_items_for_rollback():
         action._state["input"] = 10
Exemplo n.º 25
0
 def test_rollback_action_with_name(monkeypatch):
     monkeypatch.setattr(StatefullAction, "_check_fct", MagicMock())
     action = StatefullAction(name="better_name")
     decorator = action.rollback_action(name="better_rollback_name")
     decorator(lambda state: 10)
     assert action.rollback_name == "better_rollback_name"
Exemplo n.º 26
0
 def test_call():
     action = StatefullAction(action_fct=lambda state: 10)
     assert action({}) == 10
Exemplo n.º 27
0
 def test__check_kwargs_for_action():
     action = StatefullAction(["input"])
     action._check_kwargs_for_action({"input": 10})
Exemplo n.º 28
0
 def test_init():
     assert StatefullAction()