Ejemplo n.º 1
0
    def assert_that_pv_is_not_number(self,
                                     pv,
                                     restricted,
                                     tolerance=0,
                                     timeout=None):
        """
        Assert that the pv is at least tolerance from the restricted value within the timeout

        Args:
             pv: pv name
             restricted: the value we don't want the PV to have
             tolerance: the minimal deviation from the expected value
             timeout: if it hasn't changed within this time raise assertion error
        Raises:
             AssertionError: if value does not enter the desired range
             UnableToConnectToPVException: if pv does not exist within timeout
        """
        message = "Expected PV value to be not equal to {} (tolerance: {})"\
            .format(format_value(restricted), format_value(tolerance))

        return self.assert_that_pv_value_causes_func_to_return_true(
            pv,
            lambda val: not self._within_tolerance_condition(
                val, restricted, tolerance),
            timeout,
            message=message)
Ejemplo n.º 2
0
    def assert_that_pv_is_number(self,
                                 pv,
                                 expected,
                                 tolerance=0.0,
                                 timeout=None,
                                 pv_value_source=None):
        """
        Assert that the pv has the expected value or that it becomes the expected value within the timeout
        
        Args:
            pv: pv name
            expected: expected value
            tolerance: the allowable deviation from the expected value
            timeout: if it hasn't changed within this time raise assertion error
            pv_value_source: where to get the value from, None for caget from pv
        Raises:
            AssertionError: if value does not become requested value
            UnableToConnectToPVException: if pv does not exist within timeout
        """
        message = "Expected PV '{}' value to be equal to {} (tolerance: {})"\
            .format(self.create_pv_with_prefix(pv), format_value(expected), format_value(tolerance))

        return self.assert_that_pv_value_causes_func_to_return_true(
            pv,
            lambda val: self._within_tolerance_condition(
                val, expected, tolerance),
            timeout,
            message=message,
            pv_value_source=pv_value_source)
 def wrapper(msg):
     value = self.backdoor_get_from_device(emulator_property)
     try:
         return_value = func(value)
     except Exception as e:
         return "Exception was thrown while evaluating function '{}' on emulator property {}. " \
                "Exception was: {} {}".format(func.__name__,
                                              format_value(value), e.__class__.__name__, e.message)
     if return_value:
         return None
     else:
         return "{}{}{}".format(
             msg, os.linesep,
             "Final emulator property value was {}".format(
                 format_value(value)))
    def assert_that_emulator_value_is(self,
                                      emulator_property,
                                      expected_value,
                                      timeout=None,
                                      message=None,
                                      cast=lambda val: val):
        """
        Assert that the emulator property has the expected value or that it becomes the expected value within the
        timeout.

        Args:
            emulator_property (string): emulator property to check
            expected_value: expected value. Emulator backdoor always returns a string, so the value should be a string.
            timeout (float): if it hasn't changed within this time raise assertion error
            message (string): Extra message to print
            cast (callable): function which casts the returned value to an appropriate type before
                checking equality. E.g. to cast to float pass the float class as this argument.
        Raises:
            AssertionError: if emulator property is not the expected value
            UnableToConnectToPVException: if emulator property does not exist within timeout
        """

        if message is None:
            message = "Expected PV to have value {}.".format(
                format_value(expected_value))

        return self.assert_that_emulator_value_causes_func_to_return_true(
            emulator_property,
            lambda val: cast(val) == expected_value,
            timeout=timeout,
            msg=message)
Ejemplo n.º 5
0
    def assert_that_pv_is_path(self,
                               pv,
                               expected_path,
                               timeout=None,
                               msg=None,
                               pv_value_source=None):
        """
        Assert that a pv is a path that when normalised matches the expected path.

        Args:
            pv: pv name
            expected_path: expected path
            timeout: if it hasn't changed within this time raise assertion error
            msg: Extra message to print
            pv_value_source: place to get pv value from on get; None pv is read using caget;
              otherwise attribute value will be used
        Raises:
            AssertionError: if value does not become requested value
            UnableToConnectToPVException: if pv does not exist within timeout
        """
        normalised_expected_path = self._normalise_path(expected_path)
        if msg is None:
            msg = "Expected PV, '{}' to have path {}.".format(
                self.create_pv_with_prefix(pv),
                format_value(normalised_expected_path))

        return self.assert_that_pv_value_causes_func_to_return_true(
            pv,
            lambda val: self._normalise_path(val) == normalised_expected_path,
            timeout=timeout,
            message=msg,
            pv_value_source=pv_value_source)
Ejemplo n.º 6
0
    def assert_that_pv_is(self,
                          pv,
                          expected_value,
                          timeout=None,
                          msg=None,
                          pv_value_source=None):
        """
        Assert that the pv has the expected value or that it becomes the expected value within the timeout.

        Args:
            pv: pv name
            expected_value: expected value
            timeout: if it hasn't changed within this time raise assertion error
            msg: Extra message to print
            pv_value_source: place to get pv value from on get; None pv is read using caget; otherwise attribute value will be used
        Raises:
            AssertionError: if value does not become requested value
            UnableToConnectToPVException: if pv does not exist within timeout
        """

        if msg is None:
            msg = "Expected PV, '{}' to have value {}.".format(
                self.create_pv_with_prefix(pv), format_value(expected_value))

        return self.assert_that_pv_value_causes_func_to_return_true(
            pv,
            lambda val: val == expected_value,
            timeout=timeout,
            message=msg,
            pv_value_source=pv_value_source)
Ejemplo n.º 7
0
 def _wrapper(message):
     if pv_value_source is None:
         value = self.get_pv_value(pv)
     else:
         value = pv_value_source.value
     try:
         return_value = func(value)
     except Exception as e:
         return "Exception was thrown while evaluating function '{}' on pv value {}. Exception was: {} {}"\
             .format(func.__name__, format_value(value), e.__class__.__name__, e.message)
     if return_value:
         return None
     else:
         return "{}{}{}".format(
             message, os.linesep,
             "Final PV value was {}".format(format_value(value)))
Ejemplo n.º 8
0
    def assert_that_pv_value_over_time_satisfies_comparator(
            self, pv, wait, comparator):
        """
        Check that a PV satisfies a given function over time. The initial value is compared to the final value after
        a given time using the comparator.

        Args:
             pv: the PV to check
             wait: the number of seconds to wait
             comparator: a function taking two arguments; the initial and final values, which should return a boolean
        Raises:
             AssertionError: if the value of the pv did not satisfy the comparator
        """
        initial_value = self.get_pv_value(pv)
        time.sleep(wait)

        message = "Expected value trend to satisfy comparator '{}'. Initial value was {}."\
            .format(comparator.__name__, format_value(initial_value))

        def _condition(val):
            return comparator(val, initial_value)

        return self.assert_that_pv_value_causes_func_to_return_true(
            pv, _condition, message=message)
Ejemplo n.º 9
0
    def assert_that_pv_is_not(self,
                              pv,
                              restricted_value,
                              timeout=None,
                              msg=None):
        """
        Assert that the pv does not have a particular value and optionally it does not become that value within the
        timeout.

        Args:
            pv: pv name
            restricted_value: value the PV shouldn't become
            timeout: if it becomes the value within this time, raise an assertion error
            msg: Extra message to print
        Raises:
            AssertionError: if value has the restricted value
            UnableToConnectToPVException: if pv does not exist within timeout
        """
        if msg is None:
            msg = "Expected PV to not have value {}.".format(
                format_value(restricted_value))

        return self.assert_that_pv_value_causes_func_to_return_true(
            pv, lambda val: val != restricted_value, timeout, message=msg)