|
2 | 2 | from datetime import date
|
3 | 3 |
|
4 | 4 | from django import forms
|
5 |
| -from django.forms.models import modelform_factory, ModelChoiceField |
| 5 | +from django.forms.models import modelform_factory, ModelChoiceField, fields_for_model, construct_instance |
6 | 6 | from django.test import TestCase
|
7 | 7 | from django.core.exceptions import FieldError, ValidationError
|
8 | 8 | from django.core.files.uploadedfile import SimpleUploadedFile
|
@@ -417,3 +417,28 @@ def test_unique_together_error_message(self):
|
417 | 417 | self.assertEquals(form.errors, {'__all__': [u'Edition with this Author and Publication already exists.']})
|
418 | 418 | form = EditionForm(data={'author': self.author2.pk, 'publication': self.pub1.pk, 'edition': 1, 'isbn': '9783161487777'})
|
419 | 419 | self.assertEquals(form.errors, {'__all__': [u'Edition with this Publication and Edition already exists.']})
|
| 420 | + |
| 421 | + |
| 422 | +class EmptyFieldsTestCase(TestCase): |
| 423 | + "Tests for fields=() cases as reported in #14119" |
| 424 | + class EmptyPersonForm(forms.ModelForm): |
| 425 | + class Meta: |
| 426 | + model = Person |
| 427 | + fields = () |
| 428 | + |
| 429 | + def test_empty_fields_to_fields_for_model(self): |
| 430 | + "An argument of fields=() to fields_for_model should return an empty dictionary" |
| 431 | + field_dict = fields_for_model(Person, fields=()) |
| 432 | + self.assertEqual(len(field_dict), 0) |
| 433 | + |
| 434 | + def test_empty_fields_on_modelform(self): |
| 435 | + "No fields on a ModelForm should actually result in no fields" |
| 436 | + form = self.EmptyPersonForm() |
| 437 | + self.assertEqual(len(form.fields), 0) |
| 438 | + |
| 439 | + def test_empty_fields_to_construct_instance(self): |
| 440 | + "No fields should be set on a model instance if construct_instance receives fields=()" |
| 441 | + form = modelform_factory(Person)({'name': 'John Doe'}) |
| 442 | + self.assertTrue(form.is_valid()) |
| 443 | + instance = construct_instance(form, Person(), fields=()) |
| 444 | + self.assertEqual(instance.name, '') |
0 commit comments