def test_dynamic_form(self):
        for field_count in range(2, 7):

            @given(from_form(DynamicForm, form_kwargs={"field_count": field_count}))
            def _test(dynamic_form):
                self.assertTrue(dynamic_form.is_valid())

            _test()
Ejemplo n.º 2
0
def test_field_types(
    patch_field_strategies,
    duration_strategy: st.SearchStrategy[timedelta],
    rollback,
    field_type: FieldType,
    data: st.DataObject,
) -> None:
    """Ensure that each field type behaves appropriately."""
    with rollback():
        quiz = QuizFactory()
        question = QuizQuestionFactory(
            quiz=quiz,
            name=f"{field_type}_question".lower(),
            field_type=field_type,
            required=True,
        )

        with patch_field_strategies({forms.DurationField: duration_strategy}):
            django_form = cast(
                forms.ModelForm,
                data.draw(from_form(type(quiz.as_django_form()))))

        django_form.files = {
            k: v
            for k, v in django_form.data.items() if isinstance(v, File)
        }

        # The form should be valid.
        assert django_form.is_valid(
        ), f"The form was not valid: {django_form.errors}"

        # Saving the form should result in a QuizSubmission instance.
        quiz_submission = django_form.save()
        assert isinstance(quiz_submission, QuizSubmission)
        assert (str(quiz_submission) ==
                f"{quiz_submission.quiz.label} {quiz_submission.pk}")
        assert (str(type(quiz_submission)(
            form=quiz)) == f"New {quiz_submission.quiz.label}")
        assert str(type(quiz_submission)()) == f"New Quiz Submission"

        # The QuizSubmission should have one answer.
        assert quiz_submission.answers.count() == 1
        answer = quiz_submission.answers.first()
        assert str(answer) == f"Quiz Answer {answer.pk}"

        # The field value on the model should be identical to the one in the form's cleaned_data dict.
        record_value = getattr(quiz_submission, question.name)
        form_value = django_form.cleaned_data[question.name]

        # Files can't be reliably compared directly, so we compare their SHA1
        # digests instead of their direct values.
        if isinstance(record_value, File):
            record_value = hashlib.sha1(record_value.read()).hexdigest()
            form_value = hashlib.sha1(form_value.read()).hexdigest()

        assert (
            record_value == form_value
        ), f"Expected the record {question.name} ({field_type}) to have value {repr(form_value)} but got {repr(record_value)}"
Ejemplo n.º 3
0
    def test_dynamic_form(self):
        for field_count in range(2, 7):

            @given(
                from_form(DynamicForm,
                          form_kwargs={"field_count": field_count}))
            def _test(dynamic_form):
                self.assertTrue(dynamic_form.is_valid())

            _test()
Ejemplo n.º 4
0
class TestGetsBasicForms(TestCase):
    @given(from_form(CustomerForm))
    def test_valid_customer(self, customer_form):
        self.assertTrue(customer_form.is_valid())

    @given(from_form(ManyNumericsForm))
    def test_valid_numerics(self, numerics_form):
        self.assertTrue(numerics_form.is_valid())

    @given(from_form(ManyTimesForm))
    def test_valid_times(self, times_form):
        self.assertTrue(times_form.is_valid())

    @given(from_form(OddFieldsForm))
    def test_valid_odd_fields(self, odd_form):
        self.assertTrue(odd_form.is_valid())

    def test_dynamic_form(self):
        for field_count in range(2, 7):

            @given(
                from_form(DynamicForm,
                          form_kwargs={"field_count": field_count}))
            def _test(dynamic_form):
                self.assertTrue(dynamic_form.is_valid())

            _test()

    @given(from_form(BasicFieldForm))
    def test_basic_fields_form(self, basic_field_form):
        self.assertTrue(basic_field_form.is_valid())

    @given(from_form(TemporalFieldForm))
    def test_temporal_fields_form(self, time_field_form):
        self.assertTrue(time_field_form.is_valid())

    @given(from_form(EmailFieldForm))
    def test_email_field_form(self, email_field_form):
        self.assertTrue(email_field_form.is_valid())

    @given(from_form(SlugFieldForm))
    def test_slug_field_form(self, slug_field_form):
        self.assertTrue(slug_field_form.is_valid())

    @given(from_form(URLFieldForm))
    def test_url_field_form(self, url_field_form):
        self.assertTrue(url_field_form.is_valid())

    @given(from_form(RegexFieldForm))
    def test_regex_field_form(self, regex_field_form):
        self.assertTrue(regex_field_form.is_valid())

    @given(from_form(UUIDFieldForm))
    def test_uuid_field_form(self, uuid_field_form):
        self.assertTrue(uuid_field_form.is_valid())

    @given(from_form(ChoiceFieldForm))
    def test_choice_fields_form(self, choice_field_form):
        self.assertTrue(choice_field_form.is_valid())

    @given(from_form(InternetProtocolForm))
    def test_ip_fields_form(self, ip_field_form):
        self.assertTrue(ip_field_form.is_valid())

    @given(from_form(ManyMultiValueForm, form_kwargs={"subfield_count": 2}))
    def test_many_values_in_multi_value_field(self, many_multi_value_form):
        self.assertTrue(many_multi_value_form.is_valid())

    @given(from_form(ManyMultiValueForm, form_kwargs={"subfield_count": 105}))
    def test_excessive_values_in_multi_value_field(self, excessive_form):
        self.assertTrue(excessive_form.is_valid())

    @given(from_form(ShortStringForm))
    def test_short_string_form(self, short_string_form):
        self.assertTrue(short_string_form.is_valid())