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_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 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_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"), ]