def test_simulate_until_error(): ap = ActionsPipeline() shared_result = SharedResultAction() action1 = Action(shared_result.gen_action("a"), shared_result.gen_action("c")) ap.append(action1) with pytest.raises(ActionException): ap.simulate_until([("garbage", {})])
def test_append_in_order(monkeypatch): monkeypatch.setattr(Action, "set_info_streamer", MagicMock()) ap = ActionsPipeline() action1 = Action() action2 = Action() ap.append(action1) ap.append(action2) assert ap.actions == [action1, action2]
def test__action(): ap = ActionsPipeline() shared_result = SharedResultAction() action1 = Action(shared_result.gen_action("a")) action2 = Action(shared_result.gen_action("b")) ap.append(action1) ap.append(action2) ap.do() assert shared_result.result == "ab"
def test_copy(): ap = ActionsPipeline() shared_result = SharedResultAction() action1 = Action(shared_result.gen_action("a"), shared_result.gen_action("c")) ap.append(action1) ap2 = copy(ap) ap.do() ap2.do() assert shared_result.result == "aa"
def test_actions_are_accessible(): ap = ActionsPipeline() shared_result = SharedResultAction() action1 = Action(shared_result.gen_action("a")) action2 = Action(shared_result.gen_action("b")) ap.append(action1) ap.append(action2) assert ap.actions[1] # This should have no impact. ap.do() assert shared_result.result == "ab"
def test_set_info_streamer(monkeypatch): ap = ActionsPipeline() action1 = Action() action2 = Action() ap.append(action1) ap.append(action2) set_mock_1 = MagicMock() set_mock_2 = MagicMock() monkeypatch.setattr(action1, "set_info_streamer", set_mock_1) monkeypatch.setattr(action2, "set_info_streamer", set_mock_2) ap.set_info_streamer(InfoStreamer()) assert set_mock_1.called and set_mock_2.called
def test__rollback_action(): ap = ActionsPipeline() shared_result = SharedResultAction() action1 = Action(shared_result.gen_action("a"), shared_result.gen_action("c")) action2 = Action(shared_result.gen_action("b"), shared_result.gen_action("d")) ap.append(action1) ap.append(action2) ap.do() ap.undo() assert shared_result.result == "abdc"
def test_simulate_until(): ap = ActionsPipeline() shared_result = SharedResultAction() action1 = Action(shared_result.gen_action("a"), shared_result.gen_action("c")) action2 = Action(shared_result.gen_action("b"), shared_result.gen_action("d")) ap.append(action1) ap.append(action2) action1_name = action1.get_name("action") ap.simulate_until([(action1_name, {})]) ap.do() ap.undo() assert shared_result.result == "bdc"
def test__rollback_action_partial(): ap = ActionsPipeline() shared_result = SharedResultAction() action1 = Action(shared_result.gen_action("a"), shared_result.gen_action("c")) action2 = Action(MagicMock(side_effect=Exception()), shared_result.gen_action("d")) ap.append(action1) ap.append(action2) try: ap.do() except Exception: pass ap.undo() assert shared_result.result == "adc"
def test_simulate_until_partial(): ap = ActionsPipeline() shared_result = SharedResultAction() action1 = Action(shared_result.gen_action("a"), shared_result.gen_action("c")) action2 = Action(shared_result.gen_action("b"), shared_result.gen_action("d")) ap.append(action1) ap.append(action2) # Mandatory to redefine names unless they all have the same name and simulate can't work fine. action1.set_name("action", "action1") action1.set_name("rollback", "rollback1") ap.simulate_until([("action1", {}), ("rollback1", {})]) ap.undo() assert shared_result.result == ""
def complex_pipeline(complex_actions): my_action, my_action_2, results = complex_actions prep_action = my_action.get_prepared_action(text="WOLOLOO") prep_action_2 = my_action_2.get_prepared_action( id=prep_action.state.ref_to("id2")) ap = ActionsPipeline() ap.append(prep_action) ap.append(prep_action_2) if sys.version_info < (3, 0): actions_names = [ "tests.test_actions.my_action", "tests.test_actions.my_action_2", "tests.test_actions.my_action_2_rollback", "tests.test_actions.my_action_rollback", ] else: actions_names = [ "tests.test_actions.complex_actions.my_action", "tests.test_actions.complex_actions.my_action_2", "tests.test_actions.complex_actions.my_action_2_rollback", "tests.test_actions.complex_actions.my_action_rollback", ] return ap, results, actions_names
def pipeline_gen(): @statefull_action([], name="action1") def action1(state): pass @action1.rollback_action([], name="rollback1") def rollback1(state): pass @statefull_action([], name="action2") def action2(state): pass @action2.rollback_action([], name="rollback2") def rollback2(state): pass ap = ActionsPipeline(name="pipeline") ap.rollback_name = "pipeline_rollback" ap.append(action1.get_prepared_action()) ap.append(action2.get_prepared_action()) ap.set_info_streamer(info_streamer) return ap
def test_init(): assert ActionsPipeline(name="pipeline_name")
def test_append_bad(): with pytest.raises(ActionException): ActionsPipeline().append(object())
def test_init_with_list(): action = Action() action2 = Action() ap = ActionsPipeline([action, action2]) assert ap.actions[0] == action and ap.actions[1] == action2
def test_actions(): assert not ActionsPipeline().actions
def test_notify(monkeypatch): notify_mock = MagicMock() monkeypatch.setattr(Action, "notify", notify_mock) ActionsPipeline(name="pipeline").notify() assert notify_mock.called
def test_actions_read_only(): with pytest.raises(ActionException): ActionsPipeline().actions = []
def test_append(monkeypatch): monkeypatch.setattr(Action, "set_info_streamer", MagicMock()) ap = ActionsPipeline() action = Action() ap.append(action) assert ap.actions[0] == action
def test_info_streaming(): @statefull_action(["item"]) def fake_action(state): state["other"] = state["item"] + 5 pass @fake_action.rollback_action(["other"]) def fake_rollback(state): state["last"] = state["other"] + 5 pass prep1 = fake_action.get_prepared_action(item=10) prep1.name = "action1" prep1.rollback_name = "rollback1" prep2 = fake_action.get_prepared_action(item=100) prep2.name = "action2" prep2.rollback_name = "rollback2" ap = ActionsPipeline() ap.append(prep1) ap.append(prep2) ap.name = "pipeline" ap.rollback_name = "pipeline_rollback" streamed_info = [] class FakeInfoStreamer(InfoStreamer): def send_info(self, **kwargs): if "begin" in kwargs: step_type = "begin" if "end" in kwargs: step_type = "end" if "state" in kwargs: info = (kwargs["action_name"], step_type, dict(kwargs["state"])) else: info = (kwargs["action_name"], step_type) streamed_info.append(info) ap.set_info_streamer(FakeInfoStreamer()) ap.do() ap.undo() assert streamed_info == [ ("pipeline", "begin"), ("action1", "begin", { "item": 10 }), ("action1", "end", { "item": 10, "other": 15 }), ("action2", "begin", { "item": 100 }), ("action2", "end", { "item": 100, "other": 105 }), ("pipeline", "end"), ("pipeline_rollback", "begin"), ("rollback2", "begin", { "item": 100, "other": 105 }), ("rollback2", "end", { "item": 100, "other": 105, "last": 110 }), ("rollback1", "begin", { "item": 10, "other": 15 }), ("rollback1", "end", { "item": 10, "other": 15, "last": 20 }), ("pipeline_rollback", "end"), ]