Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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)
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)
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')