def test_validation(self): class C: def __init__(self, x: int): self.x = x jsons.set_validator(lambda c: c.x > 5, C) jsons.load({'x': 6}, C) # Should be fine. with self.assertRaises(ValidationError): jsons.load({'x': 3}, C) def validator(c: C): assert c.x > 5 return True jsons.set_validator(validator, C) with self.assertRaises(ValidationError): jsons.load({'x': 3}, C) def validator(c: C): if c.x <= 5: raise Exception('Not good...') return True jsons.set_validator(validator, C) try: jsons.load({'x': 3}, C) except ValidationError as err: self.assertEqual('Not good...', err.message) jsons.set_validator(lambda _: True, C) jsons.set_validator(lambda x: x == 1, int) with self.assertRaises(ValidationError): jsons.load({'x': 3}, C)
def test_validation_with_assert(self): def validator(c: C): assert c.x > 5 # A validator that raises. return True jsons.set_validator(validator, C) with self.assertRaises(ValidationError): jsons.load({'x': 3}, C)
def test_validation_with_raise(self): def validator(c: C): if c.x <= 5: raise Exception('Not good...') return True jsons.set_validator(validator, C) try: jsons.load({'x': 3}, C) except ValidationError as err: self.assertEqual('Not good...', err.message)
def set_deserializers() -> None: """Set deserializers and validators for json to use to cast specific classes""" # Local import to minimize jsons usage across files from .group import ElementModP, ElementModQ, int_to_p_unchecked, int_to_q_unchecked from .proof import ProofUsage set_deserializer(lambda p_as_int, cls, **_: int_to_p_unchecked(p_as_int), ElementModP) set_validator(lambda p: p.is_in_bounds(), ElementModP) set_deserializer(lambda q_as_int, cls, **_: int_to_q_unchecked(q_as_int), ElementModQ) set_validator(lambda q: q.is_in_bounds(), ElementModQ) set_deserializer(lambda usage_string, cls, **_: ProofUsage[usage_string], ProofUsage)
def set_deserializers() -> None: """Set deserializers and validators for json to use to cast specific classes""" # Local import to minimize jsons usage across files from .group import ElementModP, ElementModQ, int_to_p_unchecked, int_to_q_unchecked set_deserializer(lambda p_as_int, cls, **_: int_to_p_unchecked(p_as_int), ElementModP) set_validator(lambda p: p.is_in_bounds(), ElementModP) set_deserializer(lambda q_as_int, cls, **_: int_to_q_unchecked(q_as_int), ElementModQ) set_validator(lambda q: q.is_in_bounds(), ElementModQ) set_deserializer( lambda none, cls, **_: None if none == "None" else default_nonetype_deserializer(none), NoneType, ) set_deserializer(lambda dt, cls, **_: _deserialize_datetime(dt), datetime)
def tearDown(self) -> None: jsons.set_validator(lambda _: True, C) jsons.set_validator(lambda _: True, int) jsons.set_validator(lambda _: True, str)
def test_validate_sequence(self): jsons.set_validator(lambda _: False, [int, str]) with self.assertRaises(ValidationError): jsons.load({'x': 1}, C)
def test_validate_primitive_attribute_valid(self): jsons.set_validator(lambda x: x == 1, int) jsons.load({'x': 1}, C)
def test_validate_primitive_attribute_invalid(self): jsons.set_validator(lambda x: x == 1, int) with self.assertRaises(ValidationError): jsons.load({'x': 3}, C)
def test_validation_invalid(self): jsons.set_validator(lambda c: c.x > 5, C) with self.assertRaises(ValidationError): jsons.load({'x': 3}, C)
def test_validation_valid(self): jsons.set_validator(lambda c: c.x > 5, C) jsons.load({'x': 6}, C) # Should be fine.