def test_maybe(self): with(self.assertRaises(exceptions.PyCombValidationError)): c.String(None) self.assertIsNone(c.maybe(c.String)(None)) my_maybe = c.maybe(c.String) self.assertEqual('hello', my_maybe('hello')) with self.assertRaises(exceptions.PyCombValidationError) as e: my_maybe(1) self.assertEqual( 'Error on Maybe (String): expected None or String but was int', e.exception.args[0])
def test_named_maybe(self): my_maybe = c.maybe(c.String, name='MyMaybe') self.assertIsNone(my_maybe(None)) self.assertEqual('hello', my_maybe('hello')) with self.assertRaises(exceptions.PyCombValidationError) as e: my_maybe(1) self.assertEqual( 'Error on MyMaybe: expected None or String but was int', e.exception.args[0])
def test_maybe_production(self): my_maybe = c.maybe(c.String) self.assertEqual(1, my_maybe(1, ctx=context.create(production_mode=True)))
def test_struct_maybe(self): r = c.maybe(c.struct({'name': c.String}))({'name': 'John'}) self.assertEqual(StructType, type(r))
def test_struct_maybe_field(self): User = c.struct({'name': c.String, 'age': c.Int, 'city': c.maybe(c.String)}) User({'name': 'John Burns', 'age': 30})
return wrapper return decorator UUIDValidator = validators.subtype(validators.String, is_uuid) SplitProtocolValidator = validators.subtype(validators.String, lambda x: x in ['fxc1']) SplitSessionValidator = validators.struct({ "secret": validators.struct({ "value": validators.String, "protocol": validators.maybe(SplitProtocolValidator), }) }) SplitSessionCreateValidator = validators.struct( { "client_alias": validators.String, "session_alias": validators.String, "session_policies": validators.struct({ "shares": validators.Int, "quorum": validators.Int }) },