예제 #1
0
    def evaluate(cls, sequence, spec_vars, is_a_test_case=False):
        """Receives a sequence of characters and evaluates any python code
        present on it

        Args:
            sequence[string]: sequence of characters to be evaluated
            spec_vars[dict]: dictionary containing the SpecEvaluator variables
            is_a_test_case[bool]: indicator for checking if the given evalution
            is a test case

        Returns:
            tuple: a tuple containing:
                -  [Boolean]: True if python statement is valid
                -  [string]: None if valid evalution, tested code otherwise

        Raises:
            InvalidPythonCodeError: If receives invalid python statements
            (eg. 1/0)

        """
        match = cls.python_code_pattern.search(str(sequence))

        if not match:
            return sequence

        code = match.group("python_code")
        response = spec_vars.get("response")

        try:
            if is_a_test_case:
                return cls._assert_code(code, response)

            return cls._evaluate_sequence(sequence, match, code, response)
        except Exception as e:
            raise InvalidPythonCodeError(str(e), code)
예제 #2
0
    def evaluate(cls, sequence, vars):
        # To avoid circular imports
        from scanapi.evaluators.string_evaluator import StringEvaluator

        # Available imports to be used dinamically in the API spec
        import datetime
        import math
        import random
        import time
        import uuid

        match = cls.python_code_pattern.search(sequence)

        if not match:
            return sequence

        code = match.group("python_code")

        try:
            response = vars.get("response")
            python_code_value = str(eval(code))
            return StringEvaluator.replace_var_with_value(
                sequence, match.group(), python_code_value
            )
        except Exception as e:
            raise InvalidPythonCodeError(str(e))
예제 #3
0
def evaluate_python_code(sequence):
    match = python_code_pattern.search(sequence)

    if not match:
        return sequence

    code = match.group(2)

    try:
        return str(eval(code))
    except Exception as e:
        raise InvalidPythonCodeError(str(e))
예제 #4
0
    def evaluate(cls, sequence, vars, is_a_test_case=False):
        match = cls.python_code_pattern.search(sequence)

        if not match:
            return sequence

        code = match.group("python_code")
        response = vars.get("response")

        try:
            if is_a_test_case:
                return cls._assert_code(code, response)

            return cls._evaluate_sequence(sequence, match, code, response)
        except Exception as e:
            raise InvalidPythonCodeError(str(e), code)
예제 #5
0
    def evaluate(self, sequence):
        # Available imports to be used dinamically in the api spec
        import datetime
        import math
        import random
        import time
        import uuid

        match = python_code_pattern.search(sequence)

        if not match:
            return sequence

        code = match.group("python_code")
        responses = SimpleNamespace(**self.api_tree.responses)

        try:
            python_code_value = str(eval(code))
            return self.string_evaluator.replace_var_with_value(
                sequence, match.group(), python_code_value)
        except Exception as e:
            raise InvalidPythonCodeError(str(e))