diff --git a/tests/forms_tests/field_tests/test_emailfield.py b/tests/forms_tests/field_tests/test_emailfield.py index 7bea420a54..8b85e4dcc1 100644 --- a/tests/forms_tests/field_tests/test_emailfield.py +++ b/tests/forms_tests/field_tests/test_emailfield.py @@ -52,6 +52,7 @@ class EmailFieldTest(FormFieldAssertionsMixin, SimpleTestCase): def test_emailfield_strip_on_none_value(self): f = EmailField(required=False, empty_value=None) + self.assertIsNone(f.clean('')) self.assertIsNone(f.clean(None)) def test_emailfield_unable_to_set_strip_kwarg(self): diff --git a/tests/forms_tests/field_tests/test_regexfield.py b/tests/forms_tests/field_tests/test_regexfield.py index 51b6fe6fd5..80d0f1c7cd 100644 --- a/tests/forms_tests/field_tests/test_regexfield.py +++ b/tests/forms_tests/field_tests/test_regexfield.py @@ -75,3 +75,11 @@ class RegexFieldTest(SimpleTestCase): f = RegexField('^[a-z]+$', strip=True) self.assertEqual(f.clean(' a'), 'a') self.assertEqual(f.clean('a '), 'a') + + def test_empty_value(self): + f = RegexField('', required=False) + self.assertEqual(f.clean(''), '') + self.assertEqual(f.clean(None), '') + f = RegexField('', empty_value=None, required=False) + self.assertIsNone(f.clean('')) + self.assertIsNone(f.clean(None)) diff --git a/tests/forms_tests/field_tests/test_slugfield.py b/tests/forms_tests/field_tests/test_slugfield.py index d883f7f5c9..6d766e8af5 100644 --- a/tests/forms_tests/field_tests/test_slugfield.py +++ b/tests/forms_tests/field_tests/test_slugfield.py @@ -17,3 +17,11 @@ class SlugFieldTest(SimpleTestCase): self.assertEqual(f.clean(' 你-好 '), '你-好') self.assertEqual(f.clean('ıçğüş'), 'ıçğüş') self.assertEqual(f.clean('foo-ıç-bar'), 'foo-ıç-bar') + + def test_empty_value(self): + f = SlugField(required=False) + self.assertEqual(f.clean(''), '') + self.assertEqual(f.clean(None), '') + f = SlugField(required=False, empty_value=None) + self.assertIsNone(f.clean('')) + self.assertIsNone(f.clean(None)) diff --git a/tests/forms_tests/field_tests/test_urlfield.py b/tests/forms_tests/field_tests/test_urlfield.py index 46cc38acb8..19e4351c6a 100644 --- a/tests/forms_tests/field_tests/test_urlfield.py +++ b/tests/forms_tests/field_tests/test_urlfield.py @@ -154,6 +154,7 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase): def test_urlfield_strip_on_none_value(self): f = URLField(required=False, empty_value=None) + self.assertIsNone(f.clean('')) self.assertIsNone(f.clean(None)) def test_urlfield_unable_to_set_strip_kwarg(self): diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py index 103ed71b44..1a2102f898 100644 --- a/tests/model_forms/models.py +++ b/tests/model_forms/models.py @@ -472,3 +472,6 @@ class Award(models.Model): class NullableUniqueCharFieldModel(models.Model): codename = models.CharField(max_length=50, blank=True, null=True, unique=True) + email = models.EmailField(blank=True, null=True) + slug = models.SlugField(blank=True, null=True) + url = models.URLField(blank=True, null=True) diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index b7885c231e..9e900e35f4 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -301,19 +301,30 @@ class ModelFormBaseTest(TestCase): self.assertEqual(obj.name, '') def test_save_blank_null_unique_charfield_saves_null(self): - form_class = modelform_factory(model=NullableUniqueCharFieldModel, fields=['codename']) + form_class = modelform_factory(model=NullableUniqueCharFieldModel, fields='__all__') empty_value = '' if connection.features.interprets_empty_strings_as_nulls else None - - form = form_class(data={'codename': ''}) + data = { + 'codename': '', + 'email': '', + 'slug': '', + 'url': '', + } + form = form_class(data=data) self.assertTrue(form.is_valid()) form.save() self.assertEqual(form.instance.codename, empty_value) + self.assertEqual(form.instance.email, empty_value) + self.assertEqual(form.instance.slug, empty_value) + self.assertEqual(form.instance.url, empty_value) # Save a second form to verify there isn't a unique constraint violation. - form = form_class(data={'codename': ''}) + form = form_class(data=data) self.assertTrue(form.is_valid()) form.save() self.assertEqual(form.instance.codename, empty_value) + self.assertEqual(form.instance.email, empty_value) + self.assertEqual(form.instance.slug, empty_value) + self.assertEqual(form.instance.url, empty_value) def test_missing_fields_attribute(self): message = (