def test_email_field(self): # data type f = EmailField() self.assertRaises(ValidationError, f.validate, 123) # max length f = EmailField(max_length=15) self.assertRaises(ValidationError, f.validate, "*****@*****.**") self.assertEqual(f.validate("*****@*****.**"), "*****@*****.**") # email format f = EmailField() for item in ["123", "123@", "@qq.com", "@qq", "123@", "123@qq", "123#qq.com"]: self.assertRaises(ValidationError, f.validate, item) for item in ["*****@*****.**", "*****@*****.**"]: self.assertEqual(f.validate(item), item)
class OnlineScoreRequest(ApiRequest): first_name = CharField(required=False, nullable=True) last_name = CharField(required=False, nullable=True) email = EmailField(required=False, nullable=True) phone = PhoneField(required=False, nullable=True) birthday = BirthDayField(required=False, nullable=True) gender = GenderField(required=False, nullable=True) def validate(self, kwargs): super().validate(kwargs) validate_pairs = any([ self.phone and self.email, self.first_name and self.last_name, self.gender is not None and self.birthday ]) if not validate_pairs: logging.error( 'At least one pair expected from the (first_name - last_name, email - phone, birthday - gender)' ) raise ValidationError("Fields validate error")
class OnlineScoreRequest(Request): first_name = CharField(required=False, nullable=True) last_name = CharField(required=False, nullable=True) email = EmailField(required=False, nullable=True) phone = PhoneField(required=False, nullable=True) birthday = BirthDayField(required=False, nullable=True) gender = GenderField(required=False, nullable=True) def __init__(self, body): super(OnlineScoreRequest, self).__init__(body) valid = any( ((self.first_name is not None and self.last_name is not None), (self.email is not None and self.phone is not None), (self.birthday is not None and self.gender is not None))) if not valid: self._errors.append('invalid arguments set')
class OnlineScoreRequest(ApiRequest): first_name = CharField(required=False, nullable=True) last_name = CharField(required=False, nullable=True) email = EmailField(required=False, nullable=True) phone = PhoneField(required=False, nullable=True) birthday = BirthDayField(required=False, nullable=True) gender = GenderField(required=False, nullable=True) def validate(self): validating_pairs = [ ('first_name', 'last_name'), ('email', 'phone'), ('birthday', 'gender'), ] if ('first_name' in self.has and 'last_name' in self.has) or \ ('email' in self.has and 'phone' in self.has) or \ ('birthday' in self.has and 'gender' in self.has): return True else: raise AttributeError( "Required at least one of this fields pars: ('first_name', 'last_name'), " "('email', 'phone'), ('birthday', 'gender')")
class PandaSerializer(Serializer): name = CharField() email = EmailField() created_at = DateTimeField()
def test_emailsfield_valid(required, nullable, value): field = EmailField(required, nullable) assert not field.validate(value)
def test_emailfield_not_valid(required, nullable, value, ex_mes): field = EmailField(required, nullable) with pytest.raises(ValidationError) as e: field.validate(value) assert str(e.value) == ex_mes
class CommentSerializer(Serializer): email = EmailField() content = CharField() created_at = DateTimeField()