How to use the sciunit.validators.ObservationValidator function in sciunit

To help you get started, we’ve selected a few sciunit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scidash / sciunit / sciunit / tests.py View on Github external
raise ObservationError("Observation is missing.")
        if not isinstance(observation, dict):
            raise ObservationError("Observation is not a dictionary.")
        if "mean" in observation and observation["mean"] is None:
            raise ObservationError("Observation mean cannot be 'None'.")
        if self.observation_schema:
            if isinstance(self.observation_schema, list):
                schemas = [x[1] if isinstance(x, tuple) else x
                           for x in self.observation_schema]
                schema = {'oneof_schema': schemas,
                          'type': 'dict'}
            else:
                schema = {'schema': self.observation_schema,
                          'type': 'dict'}
            schema = {'observation': schema}
            v = ObservationValidator(schema, test=self)
            if not v.validate({'observation': observation}):
                raise ObservationError(v.errors)
        return observation
github scidash / sciunit / sciunit / unit_test / validator_tests.py View on Github external
import random

        long_test_list = [None] * 100
        for index in range(100):
            long_test_list[index] = random.randint(1, 1000)

        q = pq.Quantity([1, 2, 3], 'ft')
        units = q.simplified.units
        units.name = "UnitName"
        testObj = Test(long_test_list)
        testObj.units = units
        obsVal = ObservationValidator(test=testObj)

        # test constructor
        self.assertRaises(BaseException, ObservationValidator)
        self.assertIsInstance(obsVal, ObservationValidator)

        # test _validate_iterable
        obsVal._validate_iterable(True, "key", long_test_list)
        self.assertRaises(
            BaseException, obsVal._validate_iterable, is_iterable=True, key="Test key", value=0)

        # test _validate_units
        self.assertRaises(
            BaseException, obsVal._validate_units, has_units=True, key="Test Key", value="I am not units")
        
        # units in test object is q.simplified.units
        obsVal._validate_units(has_units=True, key="TestKey", value=q)

        # units in test object is a dict
        testObj.units = {'TestKey': units}
        obsVal._validate_units(has_units=True, key="TestKey", value=q)
github scidash / sciunit / sciunit / unit_test / validator_tests.py View on Github external
def test_ObservationValidator(self):
        from sciunit.validators import ObservationValidator
        from sciunit import Test
        import random

        long_test_list = [None] * 100
        for index in range(100):
            long_test_list[index] = random.randint(1, 1000)

        q = pq.Quantity([1, 2, 3], 'ft')
        units = q.simplified.units
        units.name = "UnitName"
        testObj = Test(long_test_list)
        testObj.units = units
        obsVal = ObservationValidator(test=testObj)

        # test constructor
        self.assertRaises(BaseException, ObservationValidator)
        self.assertIsInstance(obsVal, ObservationValidator)

        # test _validate_iterable
        obsVal._validate_iterable(True, "key", long_test_list)
        self.assertRaises(
            BaseException, obsVal._validate_iterable, is_iterable=True, key="Test key", value=0)

        # test _validate_units
        self.assertRaises(
            BaseException, obsVal._validate_units, has_units=True, key="Test Key", value="I am not units")
        
        # units in test object is q.simplified.units
        obsVal._validate_units(has_units=True, key="TestKey", value=q)
github scidash / sciunit / sciunit / validators.py View on Github external
def __init__(self, *args, **kwargs):
        """Must pass `test` as a keyword argument.

        Cannot be a positional argument without modifications to cerberus
        """
        try:
            self.test = kwargs['test']
        except AttributeError:
            raise Exception(("Observation validator constructor must have "
                             "a `test` keyword argument"))
        super(ObservationValidator, self).__init__(*args, **kwargs)
        register_type(pq.quantity.Quantity, 'quantity')