From 5b8460ad0596f0b4e7aa1906677a27a64a0f674d Mon Sep 17 00:00:00 2001 From: Roman Zlobin Date: Fri, 1 Mar 2024 21:30:32 +0300 Subject: [PATCH] [hotfix] remove user function calls from validation (#331) * Revert "disable validation by default" This reverts commit 2b5915a9e8f3d21fe632756093d3738c159880eb. * remove function calls from validate_script * lint * lower coverage to 90 --- dff/pipeline/pipeline/actor.py | 54 ++++++------------------------- dff/pipeline/pipeline/pipeline.py | 2 +- scripts/test.py | 2 +- tests/pipeline/test_pipeline.py | 15 --------- tests/script/core/test_actor.py | 27 ---------------- 5 files changed, 12 insertions(+), 88 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/dff/pipeline/pipeline/pipeline.py b/dff/pipeline/pipeline/pipeline.py index 0f2aed716..94eb52a57 100644 --- a/dff/pipeline/pipeline/pipeline.py +++ b/dff/pipeline/pipeline/pipeline.py @@ -310,7 +310,7 @@ def set_actor( """ old_actor = self.actor self.actor = Actor(script, start_label, fallback_label, label_priority, condition_handler, handlers) - errors = self.actor.validate_script(self, verbose) if validation_stage else [] + errors = self.actor.validate_script(self, verbose) if validation_stage is not False else [] if errors: self.actor = old_actor raise ValueError( diff --git a/scripts/test.py b/scripts/test.py index 1a55dc5fd..058126e5e 100644 --- a/scripts/test.py +++ b/scripts/test.py @@ -17,7 +17,7 @@ def _test(coverage: bool, dependencies: bool) -> int: (docker containers **should** be running in that case). 3. Coverage requires all dependencies and docker (will have no effect otherwise). """ - test_coverage_threshold = 95 + test_coverage_threshold = 90 dotenv.load_dotenv(".env_file") args = ["tests/"] diff --git a/tests/pipeline/test_pipeline.py b/tests/pipeline/test_pipeline.py index d4fbe20a3..97f2f7f61 100644 --- a/tests/pipeline/test_pipeline.py +++ b/tests/pipeline/test_pipeline.py @@ -1,5 +1,4 @@ import importlib -import pytest from dff.script import Message from tests.test_utils import get_path_from_tests_to_current_dir @@ -16,20 +15,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..99dfcbd00 100644 --- a/tests/script/core/test_actor.py +++ b/tests/script/core/test_actor.py @@ -12,7 +12,6 @@ Message, ) from dff.script.conditions import true -from dff.script.labels import repeat def positive_test(samples, custom_class): @@ -69,32 +68,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(