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
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
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
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()
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"
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
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
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"]
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
def test__check_fct_not_good_too(): with pytest.raises(ActionException): StatefullAction._check_fct(lambda state, something_else: 10)
def test__check_kwargs_for_action_superfluous(): action = StatefullAction(["input"]) with pytest.raises(ActionException): action._check_kwargs_for_action({"input": 10, "superfluous": 20})
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)
def test_action(monkeypatch): monkeypatch.setattr(StatefullAction, "_check_fct", MagicMock()) action = StatefullAction() assert action.action(lambda state: 10)
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)
def test__check_fct(): StatefullAction._check_fct(lambda state: 10)
def test_call_with_state(): action = StatefullAction(action_fct=lambda state: 10) action._state = {} with pytest.raises(ActionException): action({})
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
def test_call(): action = StatefullAction(action_fct=lambda state: 10) assert action({}) == 10
def test_call_no_fct(): with pytest.raises(ActionException): action = StatefullAction() action({})
def test__check_fct_not_callable(): with pytest.raises(ActionException): StatefullAction._check_fct(10)
def test_notify(monkeypatch): notify_mock = MagicMock() monkeypatch.setattr(Action, "notify", notify_mock) StatefullAction().notify("action", "begin") assert notify_mock.called
def test_state_write(): with pytest.raises(ActionException): action = StatefullAction() action.state = {}
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)
def test__check_kwargs_for_action(): action = StatefullAction(["input"]) action._check_kwargs_for_action({"input": 10})
def test_get_prepared_action_no_action(): action = StatefullAction() with pytest.raises(ActionException): action.get_prepared_action()
def test__check_kwargs_for_action_missing(): action = StatefullAction(["input"]) with pytest.raises(ActionException): action._check_kwargs_for_action({})
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
def test__check_fct_not_good(): with pytest.raises(ActionException): StatefullAction._check_fct(lambda: 10)
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"
def test_state_fail(): with pytest.raises(ActionException): StatefullAction().state