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