Refs #28009 -- Added empty_value tests for CharField subclasses.

This commit is contained in:
David Smith 2020-09-25 12:10:45 +02:00 committed by Mariusz Felisiak
parent 997f87c5b0
commit b8239cae19
6 changed files with 36 additions and 4 deletions

View File

@ -52,6 +52,7 @@ class EmailFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
def test_emailfield_strip_on_none_value(self): def test_emailfield_strip_on_none_value(self):
f = EmailField(required=False, empty_value=None) f = EmailField(required=False, empty_value=None)
self.assertIsNone(f.clean(''))
self.assertIsNone(f.clean(None)) self.assertIsNone(f.clean(None))
def test_emailfield_unable_to_set_strip_kwarg(self): def test_emailfield_unable_to_set_strip_kwarg(self):

View File

@ -75,3 +75,11 @@ class RegexFieldTest(SimpleTestCase):
f = RegexField('^[a-z]+$', strip=True) f = RegexField('^[a-z]+$', strip=True)
self.assertEqual(f.clean(' a'), 'a') self.assertEqual(f.clean(' a'), 'a')
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))

View File

@ -17,3 +17,11 @@ class SlugFieldTest(SimpleTestCase):
self.assertEqual(f.clean(' 你-好 '), '你-好') self.assertEqual(f.clean(' 你-好 '), '你-好')
self.assertEqual(f.clean('ıçğüş'), 'ıçğüş') self.assertEqual(f.clean('ıçğüş'), 'ıçğüş')
self.assertEqual(f.clean('foo-ıç-bar'), 'foo-ıç-bar') 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))

View File

@ -154,6 +154,7 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
def test_urlfield_strip_on_none_value(self): def test_urlfield_strip_on_none_value(self):
f = URLField(required=False, empty_value=None) f = URLField(required=False, empty_value=None)
self.assertIsNone(f.clean(''))
self.assertIsNone(f.clean(None)) self.assertIsNone(f.clean(None))
def test_urlfield_unable_to_set_strip_kwarg(self): def test_urlfield_unable_to_set_strip_kwarg(self):

View File

@ -472,3 +472,6 @@ class Award(models.Model):
class NullableUniqueCharFieldModel(models.Model): class NullableUniqueCharFieldModel(models.Model):
codename = models.CharField(max_length=50, blank=True, null=True, unique=True) 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)

View File

@ -301,19 +301,30 @@ class ModelFormBaseTest(TestCase):
self.assertEqual(obj.name, '') self.assertEqual(obj.name, '')
def test_save_blank_null_unique_charfield_saves_null(self): 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 empty_value = '' if connection.features.interprets_empty_strings_as_nulls else None
data = {
form = form_class(data={'codename': ''}) 'codename': '',
'email': '',
'slug': '',
'url': '',
}
form = form_class(data=data)
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
form.save() form.save()
self.assertEqual(form.instance.codename, empty_value) 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. # 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()) self.assertTrue(form.is_valid())
form.save() form.save()
self.assertEqual(form.instance.codename, empty_value) 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): def test_missing_fields_attribute(self):
message = ( message = (