Enforce custom field validation on the model

This commit is contained in:
Jeremy Stretch
2020-11-12 14:23:08 -05:00
parent 4a8a1ce45c
commit f3f3993963
3 changed files with 83 additions and 71 deletions

View File

@@ -550,6 +550,10 @@ class CustomFieldModelTest(TestCase):
cf2.content_types.set([ContentType.objects.get_for_model(Rack)])
def test_cf_data(self):
"""
Check that custom field data is present on the instance immediately after being set and after being fetched
from the database.
"""
site = Site(name='Test Site', slug='test-site')
# Check custom field data on new instance
@@ -570,9 +574,26 @@ class CustomFieldModelTest(TestCase):
# Set custom field data
site.cf['foo'] = 'abc'
site.cf['bar'] = 'def'
with self.assertRaises(ValidationError):
site.clean()
del(site.cf['bar'])
site.clean()
def test_missing_required_field(self):
"""
Check that a ValidationError is raised if any required custom fields are not present.
"""
cf3 = CustomField(type=CustomFieldTypeChoices.TYPE_TEXT, name='baz', required=True)
cf3.save()
cf3.content_types.set([ContentType.objects.get_for_model(Site)])
site = Site(name='Test Site', slug='test-site')
# Set custom field data with a required field omitted
site.cf['foo'] = 'abc'
with self.assertRaises(ValidationError):
site.clean()
site.cf['baz'] = 'def'
site.clean()