Beispiel #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
Beispiel #2
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
Beispiel #3
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
Beispiel #4
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()
Beispiel #5
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"
Beispiel #6
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
Beispiel #7
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
Beispiel #8
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"]
Beispiel #9
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
Beispiel #10
0
 def test__check_fct_not_good_too():
     with pytest.raises(ActionException):
         StatefullAction._check_fct(lambda state, something_else: 10)
Beispiel #11
0
 def test__check_kwargs_for_action_superfluous():
     action = StatefullAction(["input"])
     with pytest.raises(ActionException):
         action._check_kwargs_for_action({"input": 10, "superfluous": 20})
Beispiel #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)
Beispiel #13
0
 def test_action(monkeypatch):
     monkeypatch.setattr(StatefullAction, "_check_fct", MagicMock())
     action = StatefullAction()
     assert action.action(lambda state: 10)
Beispiel #14
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)
Beispiel #15
0
 def test__check_fct():
     StatefullAction._check_fct(lambda state: 10)
Beispiel #16
0
 def test_call_with_state():
     action = StatefullAction(action_fct=lambda state: 10)
     action._state = {}
     with pytest.raises(ActionException):
         action({})
Beispiel #17
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
Beispiel #18
0
 def test_call():
     action = StatefullAction(action_fct=lambda state: 10)
     assert action({}) == 10
Beispiel #19
0
 def test_call_no_fct():
     with pytest.raises(ActionException):
         action = StatefullAction()
         action({})
Beispiel #20
0
 def test__check_fct_not_callable():
     with pytest.raises(ActionException):
         StatefullAction._check_fct(10)
Beispiel #21
0
 def test_notify(monkeypatch):
     notify_mock = MagicMock()
     monkeypatch.setattr(Action, "notify", notify_mock)
     StatefullAction().notify("action", "begin")
     assert notify_mock.called
Beispiel #22
0
 def test_state_write():
     with pytest.raises(ActionException):
         action = StatefullAction()
         action.state = {}
Beispiel #23
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)
Beispiel #24
0
 def test__check_kwargs_for_action():
     action = StatefullAction(["input"])
     action._check_kwargs_for_action({"input": 10})
Beispiel #25
0
 def test_get_prepared_action_no_action():
     action = StatefullAction()
     with pytest.raises(ActionException):
         action.get_prepared_action()
Beispiel #26
0
 def test__check_kwargs_for_action_missing():
     action = StatefullAction(["input"])
     with pytest.raises(ActionException):
         action._check_kwargs_for_action({})
Beispiel #27
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
Beispiel #28
0
 def test__check_fct_not_good():
     with pytest.raises(ActionException):
         StatefullAction._check_fct(lambda: 10)
Beispiel #29
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"
Beispiel #30
0
 def test_state_fail():
     with pytest.raises(ActionException):
         StatefullAction().state