Beispiel #1
0
    def _evaluate_sequence(cls, sequence, match, code, response):
        # To avoid circular imports
        from scanapi.evaluators.string_evaluator import StringEvaluator

        return StringEvaluator.replace_var_with_value(
            sequence, match.group(), str(eval(code))
        )
Beispiel #2
0
class SpecEvaluator:
    def __init__(self, api_tree):
        self.api_tree = api_tree
        self.string_evaluator = StringEvaluator(self)

    def evaluate(self, element):
        if isinstance(element, dict):
            return self.evaluate_dict(element)

        if isinstance(element, list):
            return self.evaluate_list(element)

        if not isinstance(element, str):
            return element

        return self.string_evaluator.evaluate(element)

    def evaluate_dict(self, element):
        evaluated_dict = {}
        for key, value in element.items():
            evaluated_dict[key] = self.evaluate(value)

        return evaluated_dict

    def evaluate_list(self, elements):
        evaluated_list = []
        for item in elements:
            evaluated_list.append(self.evaluate(item))

        return evaluated_list
Beispiel #3
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))
Beispiel #4
0
def _evaluate_str(element, spec_vars, is_a_test_case=False):
    return StringEvaluator.evaluate(element, spec_vars, is_a_test_case)
 def string_evaluator(self, spec_evaluator):
     return StringEvaluator(spec_evaluator)
Beispiel #6
0
 def __init__(self, api_tree):
     self.api_tree = api_tree
     self.string_evaluator = StringEvaluator(self)
Beispiel #7
0
def _evaluate_str(element, vars):
    return StringEvaluator.evaluate(element, vars)