summaryrefslogtreecommitdiff
path: root/tests/forms_tests
diff options
context:
space:
mode:
authorAlex Couper <alex.couper@glassesdirect.com>2013-07-20 21:49:33 +0000
committerTim Graham <timograham@gmail.com>2013-07-31 14:12:03 -0400
commit1123f4551158b7fc65d3bd88c375a4517dcd0720 (patch)
treecfded236931c3360205bf35351d81c75a78ef743 /tests/forms_tests
parenta1889397a9f0e6a35189de455098b4c70923e561 (diff)
Fixed #20649 -- Allowed blank field display to be defined in the initial list of choices.
Diffstat (limited to 'tests/forms_tests')
-rw-r--r--tests/forms_tests/models.py23
-rw-r--r--tests/forms_tests/tests/tests.py97
2 files changed, 118 insertions, 2 deletions
diff --git a/tests/forms_tests/models.py b/tests/forms_tests/models.py
index bec31d12d7..33898ffbb8 100644
--- a/tests/forms_tests/models.py
+++ b/tests/forms_tests/models.py
@@ -34,7 +34,30 @@ class Defaults(models.Model):
class ChoiceModel(models.Model):
"""For ModelChoiceField and ModelMultipleChoiceField tests."""
+ CHOICES = [
+ ('', 'No Preference'),
+ ('f', 'Foo'),
+ ('b', 'Bar'),
+ ]
+
+ INTEGER_CHOICES = [
+ (None, 'No Preference'),
+ (1, 'Foo'),
+ (2, 'Bar'),
+ ]
+
+ STRING_CHOICES_WITH_NONE = [
+ (None, 'No Preference'),
+ ('f', 'Foo'),
+ ('b', 'Bar'),
+ ]
+
name = models.CharField(max_length=10)
+ choice = models.CharField(max_length=2, blank=True, choices=CHOICES)
+ choice_string_w_none = models.CharField(
+ max_length=2, blank=True, null=True, choices=STRING_CHOICES_WITH_NONE)
+ choice_integer = models.IntegerField(choices=INTEGER_CHOICES, blank=True,
+ null=True)
@python_2_unicode_compatible
diff --git a/tests/forms_tests/tests/tests.py b/tests/forms_tests/tests/tests.py
index 4c391646e7..618ab8e07c 100644
--- a/tests/forms_tests/tests/tests.py
+++ b/tests/forms_tests/tests/tests.py
@@ -10,8 +10,8 @@ from django.forms.models import ModelFormMetaclass
from django.test import TestCase
from django.utils import six
-from ..models import (ChoiceOptionModel, ChoiceFieldModel, FileModel, Group,
- BoundaryModel, Defaults, OptionalMultiChoiceModel)
+from ..models import (ChoiceModel, ChoiceOptionModel, ChoiceFieldModel,
+ FileModel, Group, BoundaryModel, Defaults, OptionalMultiChoiceModel)
class ChoiceFieldForm(ModelForm):
@@ -34,6 +34,24 @@ class ChoiceFieldExclusionForm(ModelForm):
model = ChoiceFieldModel
+class EmptyCharLabelChoiceForm(ModelForm):
+ class Meta:
+ model = ChoiceModel
+ fields = ['name', 'choice']
+
+
+class EmptyIntegerLabelChoiceForm(ModelForm):
+ class Meta:
+ model = ChoiceModel
+ fields = ['name', 'choice_integer']
+
+
+class EmptyCharLabelNoneChoiceForm(ModelForm):
+ class Meta:
+ model = ChoiceModel
+ fields = ['name', 'choice_string_w_none']
+
+
class FileForm(Form):
file1 = FileField()
@@ -259,3 +277,78 @@ class ManyToManyExclusionTestCase(TestCase):
self.assertEqual(form.instance.choice_int.pk, data['choice_int'])
self.assertEqual(list(form.instance.multi_choice.all()), [opt2, opt3])
self.assertEqual([obj.pk for obj in form.instance.multi_choice_int.all()], data['multi_choice_int'])
+
+
+class EmptyLabelTestCase(TestCase):
+ def test_empty_field_char(self):
+ f = EmptyCharLabelChoiceForm()
+ self.assertHTMLEqual(f.as_p(),
+ """<p><label for="id_name">Name:</label> <input id="id_name" maxlength="10" name="name" type="text" /></p>
+<p><label for="id_choice">Choice:</label> <select id="id_choice" name="choice">
+<option value="" selected="selected">No Preference</option>
+<option value="f">Foo</option>
+<option value="b">Bar</option>
+</select></p>""")
+
+ def test_empty_field_char_none(self):
+ f = EmptyCharLabelNoneChoiceForm()
+ self.assertHTMLEqual(f.as_p(),
+ """<p><label for="id_name">Name:</label> <input id="id_name" maxlength="10" name="name" type="text" /></p>
+<p><label for="id_choice_string_w_none">Choice string w none:</label> <select id="id_choice_string_w_none" name="choice_string_w_none">
+<option value="" selected="selected">No Preference</option>
+<option value="f">Foo</option>
+<option value="b">Bar</option>
+</select></p>""")
+
+ def test_save_empty_label_forms(self):
+ # Test that saving a form with a blank choice results in the expected
+ # value being stored in the database.
+ tests = [
+ (EmptyCharLabelNoneChoiceForm, 'choice_string_w_none', None),
+ (EmptyIntegerLabelChoiceForm, 'choice_integer', None),
+ (EmptyCharLabelChoiceForm, 'choice', ''),
+ ]
+
+ for form, key, expected in tests:
+ f = form({'name': 'some-key', key: ''})
+ self.assertTrue(f.is_valid())
+ m = f.save()
+ self.assertEqual(expected, getattr(m, key))
+ self.assertEqual('No Preference',
+ getattr(m, 'get_{0}_display'.format(key))())
+
+ def test_empty_field_integer(self):
+ f = EmptyIntegerLabelChoiceForm()
+ self.assertHTMLEqual(f.as_p(),
+ """<p><label for="id_name">Name:</label> <input id="id_name" maxlength="10" name="name" type="text" /></p>
+<p><label for="id_choice_integer">Choice integer:</label> <select id="id_choice_integer" name="choice_integer">
+<option value="" selected="selected">No Preference</option>
+<option value="1">Foo</option>
+<option value="2">Bar</option>
+</select></p>""")
+
+ def test_get_display_value_on_none(self):
+ m = ChoiceModel.objects.create(name='test', choice='', choice_integer=None)
+ self.assertEqual(None, m.choice_integer)
+ self.assertEqual('No Preference', m.get_choice_integer_display())
+
+ def test_html_rendering_of_prepopulated_models(self):
+ none_model = ChoiceModel(name='none-test', choice_integer=None)
+ f = EmptyIntegerLabelChoiceForm(instance=none_model)
+ self.assertHTMLEqual(f.as_p(),
+ """<p><label for="id_name">Name:</label> <input id="id_name" maxlength="10" name="name" type="text" value="none-test"/></p>
+<p><label for="id_choice_integer">Choice integer:</label> <select id="id_choice_integer" name="choice_integer">
+<option value="" selected="selected">No Preference</option>
+<option value="1">Foo</option>
+<option value="2">Bar</option>
+</select></p>""")
+
+ foo_model = ChoiceModel(name='foo-test', choice_integer=1)
+ f = EmptyIntegerLabelChoiceForm(instance=foo_model)
+ self.assertHTMLEqual(f.as_p(),
+ """<p><label for="id_name">Name:</label> <input id="id_name" maxlength="10" name="name" type="text" value="foo-test"/></p>
+<p><label for="id_choice_integer">Choice integer:</label> <select id="id_choice_integer" name="choice_integer">
+<option value="">No Preference</option>
+<option value="1" selected="selected">Foo</option>
+<option value="2">Bar</option>
+</select></p>""")