Example #1
0
    def test_enumfield_required_false(self):
        enum_field = EnumField(enum=self.Color, required=False)

        # TEST: valid enum value
        valid_val = self.Color.BLUE
        self.assertEqual(valid_val, enum_field.clean(valid_val))

        # TEST: required=False - non-None empty values throw an error
        for empty_val in ['', [], (), {}]:
            expected_error = EnumField.default_error_messages['invalid']
            expected_error = str(
                [expected_error.format(empty_val, self.Color)])
            with self.assertRaisesMessage(ValidationError, expected_error):
                log_input(empty_val)
                enum_field.clean(empty_val)

        # TEST: required=False - None allowed
        self.assertIsNone(enum_field.clean(None))
Example #2
0
    def test_enumfield_required(self):
        enum_field = EnumField(enum=self.Color)

        # TEST: valid enum value
        valid_val = self.Color.GREEN
        self.assertEqual(valid_val, enum_field.clean(valid_val))

        # TEST: invalid enum value
        invalid_val = 4
        expected_error = EnumField.default_error_messages['invalid']
        expected_error = str([expected_error.format(invalid_val, self.Color)])
        with self.assertRaisesMessage(ValidationError, expected_error):
            enum_field.clean(invalid_val)

        # TEST: required=True - non-None empty values throw an error
        for empty_val in ['', [], (), {}]:
            expected_error = EnumField.default_error_messages['invalid']
            expected_error = str(
                [expected_error.format(empty_val, self.Color)])
            with self.assertRaisesMessage(ValidationError, expected_error):
                log_input(empty_val)
                enum_field.clean(empty_val)

        # TEST: required=True - None throws error
        with self.assertRaisesMessage(ValidationError,
                                      "['This field is required.']"):
            self.assertEqual(valid_val, enum_field.clean(None))