From df910086a22a0008b928646afa973488f1147bfc Mon Sep 17 00:00:00 2001 From: Haakon Karstensen Date: Tue, 5 Nov 2024 20:49:43 +0100 Subject: [PATCH] Delete node_checker.py and implement logic directly in views.py --- .../server_comm/test_runner/node_checker.py | 22 ------------------- server/server_comm/test_runner/views.py | 20 +++++++++++------ 2 files changed, 13 insertions(+), 29 deletions(-) delete mode 100644 server/server_comm/test_runner/node_checker.py diff --git a/server/server_comm/test_runner/node_checker.py b/server/server_comm/test_runner/node_checker.py deleted file mode 100644 index 137740f..0000000 --- a/server/server_comm/test_runner/node_checker.py +++ /dev/null @@ -1,22 +0,0 @@ -import ast - - -class node_checker: - - @staticmethod - def check_valid_input(function: str) -> bool: - try: - tree = ast.parse(function) - except SyntaxError: - raise ValueError("Input is not correct syntax for Python.") - - for node in ast.walk(tree): - if isinstance(node, ast.Return): - if isinstance(node.value, ast.Constant) and isinstance( - node.value.value, bool - ): - return True - - raise ValueError( - "Input does not contain a return statement with a boolean value." - ) diff --git a/server/server_comm/test_runner/views.py b/server/server_comm/test_runner/views.py index 7062ca4..8db4226 100644 --- a/server/server_comm/test_runner/views.py +++ b/server/server_comm/test_runner/views.py @@ -1,3 +1,4 @@ +import ast from urllib.parse import urlencode import requests @@ -9,8 +10,6 @@ from rest_framework.response import Response from rest_framework.views import APIView -from . import node_checker - class RunTestFlow(APIView): """ @@ -111,13 +110,20 @@ def run_node(self, node): try: result = None if node.node_type == Node.ASSERT: - node_checker.check_valid_input(node.function) + try: + tree = ast.parse(node.function) + except SyntaxError: + raise ValueError("Input is not correct syntax for Python.") + + for node in ast.walk(tree): + if isinstance(node, ast.Return): + if isinstance(node.value, ast.Constant) and isinstance( + node.value.value, bool + ): + return True + ValueError("Input does not return type of boolean.") try: result = exec(node.function) - if result: - result = "Assert OK" - else: - result = "Assert failed" except Exception as e: return f"Exection failed: {str(e)}"