Esempio 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
Esempio n. 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
Esempio n. 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
Esempio n. 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()
Esempio n. 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"
Esempio n. 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
Esempio n. 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
Esempio n. 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"]
Esempio n. 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
Esempio n. 10
0
 def test__check_fct_not_good_too():
     with pytest.raises(ActionException):
         StatefullAction._check_fct(lambda state, something_else: 10)
Esempio n. 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})
Esempio 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)
Esempio n. 13
0
 def test_action(monkeypatch):
     monkeypatch.setattr(StatefullAction, "_check_fct", MagicMock())
     action = StatefullAction()
     assert action.action(lambda state: 10)
Esempio n. 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)
Esempio n. 15
0
 def test__check_fct():
     StatefullAction._check_fct(lambda state: 10)
Esempio n. 16
0
 def test_call_with_state():
     action = StatefullAction(action_fct=lambda state: 10)
     action._state = {}
     with pytest.raises(ActionException):
         action({})
Esempio n. 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
Esempio n. 18
0
 def test_call():
     action = StatefullAction(action_fct=lambda state: 10)
     assert action({}) == 10
Esempio n. 19
0
 def test_call_no_fct():
     with pytest.raises(ActionException):
         action = StatefullAction()
         action({})
Esempio n. 20
0
 def test__check_fct_not_callable():
     with pytest.raises(ActionException):
         StatefullAction._check_fct(10)
Esempio n. 21
0
 def test_notify(monkeypatch):
     notify_mock = MagicMock()
     monkeypatch.setattr(Action, "notify", notify_mock)
     StatefullAction().notify("action", "begin")
     assert notify_mock.called
Esempio n. 22
0
 def test_state_write():
     with pytest.raises(ActionException):
         action = StatefullAction()
         action.state = {}
Esempio n. 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)
Esempio n. 24
0
 def test__check_kwargs_for_action():
     action = StatefullAction(["input"])
     action._check_kwargs_for_action({"input": 10})
Esempio n. 25
0
 def test_get_prepared_action_no_action():
     action = StatefullAction()
     with pytest.raises(ActionException):
         action.get_prepared_action()
Esempio n. 26
0
 def test__check_kwargs_for_action_missing():
     action = StatefullAction(["input"])
     with pytest.raises(ActionException):
         action._check_kwargs_for_action({})
Esempio n. 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
Esempio n. 28
0
 def test__check_fct_not_good():
     with pytest.raises(ActionException):
         StatefullAction._check_fct(lambda: 10)
Esempio n. 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"
Esempio n. 30
0
 def test_state_fail():
     with pytest.raises(ActionException):
         StatefullAction().state