Ejemplo n.º 1
0
    def test_fieldlist_required_false(self):
        field_list = FieldList(field=fields.IntegerField(), required=False)

        # TEST: list of values matching the FieldList's field
        test_val = [1, 2, 3, 4]
        self.assertEqual(test_val, field_list.clean(test_val))

        # TEST: required=False - empty values outside of a list return []
        for empty_val in EMPTY_VALUES:
            self.assertEqual([], field_list.clean(empty_val))
Ejemplo n.º 2
0
    def test_fieldlist_required(self):
        field_list = FieldList(field=fields.IntegerField())

        # TEST: valid input
        valid_val = [1, 2, 3]
        self.assertEqual(valid_val, field_list.clean(valid_val))

        # TEST: invalid input (individual non-list truthy values)
        invalid_vals = [True, 1, datetime.datetime.now(), 'blah', {'blah'}]
        expected_error = FieldList.default_error_messages['not_list']
        for invalid_val in invalid_vals:
            with self.assertRaisesMessage(ValidationError,
                                          str(expected_error)):
                field_list.clean(invalid_val)

        # TEST: invalid input (list of values the FieldList's IntegerField considers invalid)
        invalid_vals = [False, datetime.datetime.now(), 'blah', {'blah'}]
        expected_error = str(['Enter a whole number.'] * len(invalid_vals))
        with self.assertRaisesMessage(ValidationError, expected_error):
            field_list.clean(invalid_vals)

        # TEST: invalid input (list of values that the FieldList's IntegerField considers empty)
        invalid_vals = list(EMPTY_VALUES)
        expected_error = str(['This field is required.'] * len(invalid_vals))
        with self.assertRaisesMessage(ValidationError, expected_error):
            field_list.clean(invalid_vals)

        # TEST: required=True - empty values outside of a list throw error
        for empty_value in EMPTY_VALUES:
            with self.assertRaisesMessage(ValidationError,
                                          "['This field is required.']"):
                field_list.clean(empty_value)