Ejemplo n.º 1
0
    def test_valid_data(self):
        tests = (
            # (data, cleaned_data)
            (
                {
                    "name": "Иванов Иван Иванович",
                    "age": "20",
                    "phone": "+7 999 123-45-67",
                    "email": "*****@*****.**",
                    "wishes": "пожелания",
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "swimming",
                },
                {
                    "name": "Иванов Иван Иванович",
                    "age": 20,
                    "phone": "+7 999 123-45-67",
                    "email": "*****@*****.**",
                    "wishes": "пожелания",
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "swimming",
                },
            ),
            # empty wishes
            (
                {
                    "name": "Иванов Иван Иванович",
                    "age": "20",
                    "phone": "+7 999 123-45-67",
                    "email": "*****@*****.**",
                    "wishes": "",
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "swimming",
                },
                {
                    "name": "Иванов Иван Иванович",
                    "age": 20,
                    "phone": "+7 999 123-45-67",
                    "email": "*****@*****.**",
                    "wishes": "",
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "swimming",
                },
            ),
        )
        for data, cleaned_data in tests:
            form = AdultPlaceEntryForm(
                data=data, amount_choices=self.amount_choices, course_choices=self.course_choices
            )

            self.assertTrue(form.is_valid(), form.errors)
            self.assertEqual(form.cleaned_data, cleaned_data)
Ejemplo n.º 2
0
    def test_invalid_data(self):
        tests = (
            # (data, errors)
            # invalid course
            (
                {
                    "name": "Иванов Иван Иванович",
                    "age": "20",
                    "phone": "+7 999 123-45-67",
                    "email": "*****@*****.**",
                    "wishes": "пожелания",
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "unknown",
                },
                {"course": ["Выберите корректный вариант. unknown нет среди " "допустимых значений."]},
            ),
            # too long name
            (
                {
                    "name": "a" * 256,
                    "age": "20",
                    "phone": "+7 999 123-45-67",
                    "email": "*****@*****.**",
                    "wishes": "пожелания",
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "swimming",
                },
                {"name": ["Убедитесь, что это значение содержит не более 255 символов " "(сейчас 256)."]},
            ),
            # empty name
            (
                {
                    "name": "",
                    "age": "20",
                    "phone": "+7 999 123-45-67",
                    "email": "*****@*****.**",
                    "wishes": "пожелания",
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "swimming",
                },
                {"name": ["Обязательное поле."]},
            ),
            # too long wishes
            (
                {
                    "name": "Иванов Иван Иванович",
                    "age": "20",
                    "phone": "+7 999 123-45-67",
                    "email": "*****@*****.**",
                    "wishes": "a" * 256,
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "swimming",
                },
                {"wishes": ["Убедитесь, что это значение содержит не более 255 символов " "(сейчас 256)."]},
            ),
            # empty email
            (
                {
                    "name": "Иванов Иван Иванович",
                    "age": "20",
                    "phone": "+7 999 123-45-67",
                    "email": "",
                    "wishes": "пожелания",
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "swimming",
                },
                {"email": ["Обязательное поле."]},
            ),
            # invalid email
            (
                {
                    "name": "Иванов Иван Иванович",
                    "age": "20",
                    "phone": "+7 999 123-45-67",
                    "email": "invalid-email",
                    "wishes": "пожелания",
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "swimming",
                },
                {"email": ["Введите правильный адрес электронной почты."]},
            ),
            # empty phone
            (
                {
                    "name": "Иванов Иван Иванович",
                    "age": "20",
                    "phone": "",
                    "email": "*****@*****.**",
                    "wishes": "пожелания",
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "swimming",
                },
                {"phone": ["Обязательное поле."]},
            ),
            # invalid phone
            (
                {
                    "name": "Иванов Иван Иванович",
                    "age": "20",
                    "phone": "+79991234567",
                    "email": "*****@*****.**",
                    "wishes": "пожелания",
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "swimming",
                },
                {"phone": ["Ведите телефон в формате +X XXX XXX-XX-XX, например, " "+7 123 456-78-90."]},
            ),
            # too young age
            (
                {
                    "name": "Иванов Иван Иванович",
                    "age": "14",
                    "phone": "+7 999 123-45-67",
                    "email": "*****@*****.**",
                    "wishes": "пожелания",
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "swimming",
                },
                {"age": ["Убедитесь, что это значение больше либо равно 16."]},
            ),
            # too old age
            (
                {
                    "name": "Иванов Иван Иванович",
                    "age": "120",
                    "phone": "+7 999 123-45-67",
                    "email": "*****@*****.**",
                    "wishes": "пожелания",
                    "amount": Course.AMOUNTS.GROUP,
                    "course": "swimming",
                },
                {"age": ["Убедитесь, что это значение меньше либо равно 100."]},
            ),
            # invalid amount
            (
                {
                    "name": "Иванов Иван Иванович",
                    "age": "20",
                    "phone": "+7 999 123-45-67",
                    "email": "*****@*****.**",
                    "wishes": "пожелания",
                    "amount": "unknown",
                    "course": "swimming",
                },
                {"amount": ["Выберите корректный вариант. unknown нет среди " "допустимых значений."]},
            ),
        )
        for data, errors in tests:
            form = AdultPlaceEntryForm(
                data=data, amount_choices=self.amount_choices, course_choices=self.course_choices
            )

            self.assertFalse(form.is_valid(), form.cleaned_data)
            self.assertEqual(form.errors, errors)