From deb0c3025cce1891ee9e4d49d5da6a6e14dfbed0 Mon Sep 17 00:00:00 2001 From: Roman Zlobin Date: Fri, 1 Mar 2024 20:44:30 +0300 Subject: [PATCH] remove function calls from validate_script --- dff/pipeline/pipeline/actor.py | 54 ++++++--------------------------- tests/pipeline/test_pipeline.py | 14 --------- tests/script/core/test_actor.py | 26 ---------------- 3 files changed, 10 insertions(+), 84 deletions(-) diff --git a/dff/pipeline/pipeline/actor.py b/dff/pipeline/pipeline/actor.py index 76f5fdbc4..9613562f3 100644 --- a/dff/pipeline/pipeline/actor.py +++ b/dff/pipeline/pipeline/actor.py @@ -404,53 +404,19 @@ def validate_script(self, pipeline: Pipeline, verbose: bool = True): error_msgs = [] for flow_label, node_label, label, condition in zip(flow_labels, node_labels, labels, conditions): - ctx = Context() - ctx.validation = True - ctx.add_request(Message("text")) + if not callable(label): + label = normalize_label(label, flow_label) - label = label(ctx, pipeline) if callable(label) else normalize_label(label, flow_label) - - # validate labeling - try: - node = self.script[label[0]][label[1]] - except Exception as exc: - msg = ( - f"Could not find node with label={label}, " - f"error was found in (flow_label, node_label)={(flow_label, node_label)}" - ) - error_handler(error_msgs, msg, exc, verbose) - break - - # validate responsing - response_func = normalize_response(node.response) - try: - response_result = asyncio.run(wrap_sync_function_in_async(response_func, ctx, pipeline)) - if not isinstance(response_result, Message): + # validate labeling + try: + node = self.script[label[0]][label[1]] + except Exception as exc: msg = ( - "Expected type of response_result is `Message`.\n" - + f"Got type(response_result)={type(response_result)}" - f" for label={label} , error was found in (flow_label, node_label)={(flow_label, node_label)}" + f"Could not find node with label={label}, " + f"error was found in (flow_label, node_label)={(flow_label, node_label)}" ) - error_handler(error_msgs, msg, None, verbose) - continue - except Exception as exc: - msg = ( - f"Got exception '''{exc}''' during response execution " - f"for label={label} and node.response={node.response}" - f", error was found in (flow_label, node_label)={(flow_label, node_label)}" - ) - error_handler(error_msgs, msg, exc, verbose) - continue - - # validate conditioning - try: - condition_result = condition(ctx, pipeline) - if not isinstance(condition(ctx, pipeline), bool): - raise Exception(f"Returned condition_result={condition_result}, but expected bool type") - except Exception as exc: - msg = f"Got exception '''{exc}''' during condition execution for label={label}" - error_handler(error_msgs, msg, exc, verbose) - continue + error_handler(error_msgs, msg, exc, verbose) + break return error_msgs diff --git a/tests/pipeline/test_pipeline.py b/tests/pipeline/test_pipeline.py index d4fbe20a3..7f52c98d1 100644 --- a/tests/pipeline/test_pipeline.py +++ b/tests/pipeline/test_pipeline.py @@ -16,20 +16,6 @@ def test_pretty_format(): tutorial_module.pipeline.pretty_format() -@pytest.mark.parametrize("validation", (True, False)) -def test_from_script_with_validation(validation): - def response(ctx, pipeline: Pipeline): - raise RuntimeError() - - script = {"": {"": {RESPONSE: response, TRANSITIONS: {"": cnd.true()}}}} - - if validation: - with pytest.raises(ValueError): - _ = Pipeline.from_script(script=script, start_label=("", ""), validation_stage=validation) - else: - _ = Pipeline.from_script(script=script, start_label=("", ""), validation_stage=validation) - - def test_script_getting_and_setting(): script = {"old_flow": {"": {RESPONSE: lambda c, p: Message(), TRANSITIONS: {"": cnd.true()}}}} pipeline = Pipeline.from_script(script=script, start_label=("old_flow", "")) diff --git a/tests/script/core/test_actor.py b/tests/script/core/test_actor.py index 362ec0719..d8dfec32f 100644 --- a/tests/script/core/test_actor.py +++ b/tests/script/core/test_actor.py @@ -69,32 +69,6 @@ async def test_actor(): raise Exception("can not be passed: fail of missing node") except ValueError: pass - try: - # fail of condition returned type - Pipeline.from_script({"flow": {"node1": {TRANSITIONS: {"node1": std_func}}}}, start_label=("flow", "node1")) - raise Exception("can not be passed: fail of condition returned type") - except ValueError: - pass - try: - # fail of response returned Callable - pipeline = Pipeline.from_script( - {"flow": {"node1": {RESPONSE: lambda c, a: lambda x: 1, TRANSITIONS: {repeat(): true()}}}}, - start_label=("flow", "node1"), - ) - ctx = Context() - await pipeline.actor(pipeline, ctx) - raise Exception("can not be passed: fail of response returned Callable") - except ValueError: - pass - try: - # failed response - Pipeline.from_script( - {"flow": {"node1": {RESPONSE: raised_response, TRANSITIONS: {repeat(): true()}}}}, - start_label=("flow", "node1"), - ) - raise Exception("can not be passed: failed response") - except ValueError: - pass # empty ctx stability pipeline = Pipeline.from_script(