Example #1
0
    def do_function(self, line):
        """Send a function test after validating the function test (as JSON)."""
        point_defs = PointDefinitions(point_definitions_path=POINT_DEFINITIONS_PATH)
        ftest = FunctionTest(FUNCTION_DEFINITIONS_PATH, CURVE_JSON)
        ftest.is_valid()
        for func_step_def in ftest.get_function_def().steps:
            try:
                point_value = ftest.points[func_step_def.name]
            except KeyError:
                continue
            pdef = point_defs.point_named(func_step_def.name)
            if not pdef:
                raise ValueError("Point definition not found: {}".format(pdef.name))

            if type(point_value) == list:
                self.application.send_array(point_value, pdef)
            else:
                try:
                    send_func = self.application.SEND_FUNCTIONS[func_step_def.fcodes[0]
                    if func_step_def.fcodes
                    else DIRECT_OPERATE]
                except (KeyError, IndexError):
                    raise ValueError("Unrecognized sent command function")

                self.application.send_command(send_func, pdef, point_value)
Example #2
0
    def send_function_test(self, point_def_path='', func_def_path='', func_test_path='', func_test_json=None):
        """
            Send a function test after validating the function test (as JSON).

        :param point_def_path: path to point definition config
        :param func_def_path: path to function definition config
        :param func_test_path: path to function test json
        :param func_test_json: function test json
        """
        ftest = FunctionTest(func_test_path, func_test_json, point_def_path=point_def_path, func_def_path=func_def_path)

        ftest.is_valid()

        pdefs = PointDefinitions(point_definitions_path=point_def_path or POINT_DEF_PATH)

        func_def = ftest.get_function_def()
        for func_step_def in func_def.steps:
            try:
                point_value = ftest.points[func_step_def.name]
            except KeyError:
                continue

            pdef = pdefs.point_named(func_step_def.name)  # No need to test for valid point name, as that was done above
            if not pdef:
                raise MesaMasterException("Point definition not found: {}".format(func_step_def.name))

            if type(point_value) == list:
                self.send_array(point_value, pdef)
            else:
                try:
                    send_func = self.SEND_FUNCTIONS[func_step_def.fcodes[0] if func_step_def.fcodes else DIRECT_OPERATE]
                except (KeyError, IndexError):
                    raise MesaMasterException("Unrecognized sent command function")

                self.send_command(send_func, pdef, point_value)
Example #3
0
    def do_function(self, line):
        """Send a function test after validating the function test (as JSON)."""
        point_defs = PointDefinitions(
            point_definitions_path=POINT_DEFINITIONS_PATH)
        ftest = FunctionTest(FUNCTION_DEFINITIONS_PATH, CURVE_JSON)
        ftest.is_valid()
        for func_step_def in ftest.get_function_def().steps:
            try:
                point_value = ftest.points[func_step_def.name]
            except KeyError:
                continue
            pdef = point_defs.point_named(func_step_def.name)
            if not pdef:
                raise ValueError("Point definition not found: {}".format(
                    pdef.name))

            if type(point_value) == list:
                self.application.send_array(point_value, pdef)
            else:
                try:
                    send_func = self.application.SEND_FUNCTIONS[
                        func_step_def.fcodes[0] if func_step_def.
                        fcodes else DIRECT_OPERATE]
                except (KeyError, IndexError):
                    raise ValueError("Unrecognized sent command function")

                self.application.send_command(send_func, pdef, point_value)
Example #4
0
class FunctionTest(object):

    def __init__(self, func_test_path='', func_test_json=None, func_def_path='', point_def_path=''):
        self.func_def_path = func_def_path or FUNCTION_DEF_PATH
        self.point_definitions = PointDefinitions(point_definitions_path=point_def_path or POINT_DEF_PATH)
        self.ftest = func_test_json or json.load(open(func_test_path))
        self.function_id = self.ftest.get('function_id', self.ftest.get('id', None))
        self.function_name = self.ftest.get('function_name', self.ftest.get('name', None))
        self.name = self.ftest.get('name', None)
        self.points = {k: v for k, v in self.ftest.items() if k not in ["name", "function_id", "function_name", "id"]}

    def get_function_def(self):
        """
            Gets the function definition for the function test. Returns None if no definition is found.
        """
        fdefs = FunctionDefinitions(point_definitions=self.point_definitions,
                                    function_definitions_path=self.func_def_path)
        return fdefs.function_for_id(self.function_id)

    @staticmethod
    def get_mandatory_steps(func_def):
        """
            Returns list of mandatory steps for the given function definition.

        :param func_def: function definition
        """
        return [step.name for step in func_def.steps if step.optional == 'M']

    def has_mandatory_steps(self, fdef=None):
        """
            Returns True if the instance has all required steps, and raises an exception if not.

        :param fdef: function definition
        """
        fdef = fdef or self.get_function_def()
        if not fdef:
            raise FunctionTestException("Function definition not found: {}".format(self.function_id))

        if not all(step in self.ftest.keys() for step in self.get_mandatory_steps(fdef)):
            raise FunctionTestException("Function Test missing mandatory steps")

        return True

    def points_resolve(self, func_def):
        """
            Returns true if all the points in the instance resolve to point names in the function definition,
            and raises an exception if not.

        :param func_def: function definition of the given instance
        """
        # It would have been more informative to identify the mismatched step/point name,
        # but that would break a pytest assertion that matches on this specific exception description.
        if not all(step_name in [step.point_def.name for step in func_def.steps] for step_name in self.points.keys()):
            raise FunctionTestException("Not all points resolve")
        return True

    def correct_point_types(self):
        """
            Check valid point value.
        """
        for point_name, point_value in self.points.items():
            point_def = self.point_definitions.point_named(point_name)
            point_values = sum([list(v.values()) for v in point_value], []) if point_def.is_array else [point_value]
            for value in point_values:
                if type(value) not in POINT_TYPE_TO_PYTHON_TYPE[POINT_TYPES_BY_GROUP[point_def.group]]:
                    # It would have been more informative to display the value and/or type in the error message,
                    # but that would break a pytest assertion that matches on this specific exception description.
                    raise FunctionTestException("Invalid point value: {}".format(point_name))
        return True

    def is_valid(self):
        """
            Returns True if the function test passes two validation steps:
                1. it has all the mandatory steps
                2. its point names resolve to point names in the function definition
                3. its point value is valid
            If the function test is invalid, an exception is raised.
        """
        f_def = self.get_function_def()

        try:
            self.has_mandatory_steps(f_def)
            self.points_resolve(f_def)
            self.correct_point_types()
            return True
        except Exception as err:
            raise FunctionTestException("Validation Error: {}".format(str(err)))
Example #5
0
class FunctionTest(object):
    def __init__(self,
                 func_test_path='',
                 func_test_json=None,
                 func_def_path='',
                 point_def_path=''):
        self.func_def_path = func_def_path or FUNCTION_DEF_PATH
        self.point_definitions = PointDefinitions(
            point_definitions_path=point_def_path or POINT_DEF_PATH)
        self.ftest = func_test_json or json.load(open(func_test_path))
        self.function_id = self.ftest.get('function_id',
                                          self.ftest.get('id', None))
        self.function_name = self.ftest.get('function_name',
                                            self.ftest.get('name', None))
        self.name = self.ftest.get('name', None)
        self.points = {
            k: v
            for k, v in self.ftest.items()
            if k not in ["name", "function_id", "function_name", "id"]
        }

    def get_function_def(self):
        """
            Gets the function definition for the function test. Returns None if no definition is found.
        """
        fdefs = FunctionDefinitions(
            point_definitions=self.point_definitions,
            function_definitions_path=self.func_def_path)
        return fdefs.function_for_id(self.function_id)

    @staticmethod
    def get_mandatory_steps(func_def):
        """
            Returns list of mandatory steps for the given function definition.

        :param func_def: function definition
        """
        return [step.name for step in func_def.steps if step.optional == 'M']

    def has_mandatory_steps(self, fdef=None):
        """
            Returns True if the instance has all required steps, and raises an exception if not.

        :param fdef: function definition
        """
        fdef = fdef or self.get_function_def()
        if not fdef:
            raise FunctionTestException(
                "Function definition not found: {}".format(self.function_id))

        if not all(step in self.ftest.keys()
                   for step in self.get_mandatory_steps(fdef)):
            raise FunctionTestException(
                "Function Test missing mandatory steps")

        return True

    def points_resolve(self, func_def):
        """
            Returns true if all the points in the instance resolve to point names in the function definition,
            and raises an exception if not.

        :param func_def: function definition of the given instance
        """
        # It would have been more informative to identify the mismatched step/point name,
        # but that would break a pytest assertion that matches on this specific exception description.
        if not all(
                step_name in [step.point_def.name for step in func_def.steps]
                for step_name in self.points.keys()):
            raise FunctionTestException("Not all points resolve")
        return True

    def correct_point_types(self):
        """
            Check valid point value.
        """
        for point_name, point_value in self.points.items():
            point_def = self.point_definitions.point_named(point_name)
            point_values = sum([list(v.values()) for v in point_value],
                               []) if point_def.is_array else [point_value]
            for value in point_values:
                if type(value) not in POINT_TYPE_TO_PYTHON_TYPE[
                        POINT_TYPES_BY_GROUP[point_def.group]]:
                    # It would have been more informative to display the value and/or type in the error message,
                    # but that would break a pytest assertion that matches on this specific exception description.
                    raise FunctionTestException(
                        "Invalid point value: {}".format(point_name))
        return True

    def is_valid(self):
        """
            Returns True if the function test passes two validation steps:
                1. it has all the mandatory steps
                2. its point names resolve to point names in the function definition
                3. its point value is valid
            If the function test is invalid, an exception is raised.
        """
        f_def = self.get_function_def()

        try:
            self.has_mandatory_steps(f_def)
            self.points_resolve(f_def)
            self.correct_point_types()
            return True
        except Exception as err:
            raise FunctionTestException("Validation Error: {}".format(
                str(err)))