class ContactSchema(Schema): """ serialization-deserialization-validation class for Contacts. """ id = fields.UUID(dump_only=True) first_name = fields.String(50, required=True) surname = fields.String(50, required=True) username = fields.String(required=True, validator=validate_username, error_messages={'validator_failed': {'message': 'Username must be 6-52 characters length and' ' can only contain: a-zA-Z0-9.'}}) inserted = fields.DateTime(dump_only=True) emails = fields.Nested(EmailSchema, exlude=('contact_id',), many=True) @post_load def _post_load(self, data): if self.partial: return data return Contact(**data)
class ContactSchema(Schema): """ serialization-deserialization-validation class for Contacts. """ id = fields.UUID(dump_only=True) first_name = fields.String(50, required=True) surname = fields.String(50, required=True) username = fields.String( required=True, validator=validate_username, error_messages={ 'validator_failed': { 'message': 'Username must be 6-52 characters length and' ' can only contain: a-zA-Z0-9.' } }) email = fields.Email(required=True, validate=validate.Length(5, 128)) @post_load def _post_load(self, data): if self.partial: return data return Contact(**data)
def test_as_text(self): field = fields.UUID(as_text=True) self.assertEqual( 'cdc9e548-d56f-4054-bf6e-650772901f35', field.deserialize('cdc9e548-d56f-4054-bf6e-650772901f35'))
def test_valid_uuid(self): field = fields.UUID() self.assertEqual( UUID('cdc9e548-d56f-4054-bf6e-650772901f35'), field.deserialize('cdc9e548-d56f-4054-bf6e-650772901f35'))
def test_invalid_uuid(self): field = fields.UUID() self.assertRaises(ValidationError, field.deserialize, '12345')