Example #1
0
    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)
Example #2
0
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")
Example #3
0
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')
Example #4
0
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')")
Example #5
0
class PandaSerializer(Serializer):
    name = CharField()
    email = EmailField()
    created_at = DateTimeField()
Example #6
0
def test_emailsfield_valid(required, nullable, value):
    field = EmailField(required, nullable)
    assert not field.validate(value)
Example #7
0
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
Example #8
0
class CommentSerializer(Serializer):
    email = EmailField()
    content = CharField()
    created_at = DateTimeField()