From 4c30fa905d9d47b3a2ee82095b1fe56cc2ec2ab5 Mon Sep 17 00:00:00 2001 From: Preston Timmons Date: Sun, 30 Aug 2015 21:13:42 -0500 Subject: Rewrote form widget tests as proper unittests. This is preparation for landing the template-based widget rendering patch and goes a long way to making these tests more useful for future development. The old doctest heritage is strong here. --- .../widget_tests/test_selectmultiple.py | 125 +++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 tests/forms_tests/widget_tests/test_selectmultiple.py (limited to 'tests/forms_tests/widget_tests/test_selectmultiple.py') diff --git a/tests/forms_tests/widget_tests/test_selectmultiple.py b/tests/forms_tests/widget_tests/test_selectmultiple.py new file mode 100644 index 0000000000..502aba3bf4 --- /dev/null +++ b/tests/forms_tests/widget_tests/test_selectmultiple.py @@ -0,0 +1,125 @@ +from django.forms import SelectMultiple + +from .base import WidgetTest + + +class SelectMultipleTest(WidgetTest): + widget = SelectMultiple() + numeric_choices = (('0', '0'), ('1', '1'), ('2', '2'), ('3', '3'), ('0', 'extra')) + + def test_render_selected(self): + self.check_html(self.widget, 'beatles', ['J'], choices=self.beatles, html=( + """""" + )) + + def test_render_multiple_selected(self): + self.check_html(self.widget, 'beatles', ['J', 'P'], choices=self.beatles, html=( + """""" + )) + + def test_render_none(self): + """ + If the value is None, none of the options are selected. + """ + self.check_html(self.widget, 'beatles', None, choices=self.beatles, html=( + """""" + )) + + def test_render_value_label(self): + """ + If the value corresponds to a label (but not to an option value), none + of the options are selected. + """ + self.check_html(self.widget, 'beatles', ['John'], choices=self.beatles, html=( + """""" + )) + + def test_multiple_options_same_value(self): + """ + Multiple options with the same value can be selected (#8103). + """ + self.check_html(self.widget, 'choices', ['0'], choices=self.numeric_choices, html=( + """""" + )) + + def test_multiple_values_invalid(self): + """ + If multiple values are given, but some of them are not valid, the valid + ones are selected. + """ + self.check_html(self.widget, 'beatles', ['J', 'G', 'foo'], choices=self.beatles, html=( + """""" + )) + + def test_compare_string(self): + choices = [('1', '1'), ('2', '2'), ('3', '3')] + + self.check_html(self.widget, 'nums', [2], choices=choices, html=( + """""" + )) + + self.check_html(self.widget, 'nums', ['2'], choices=choices, html=( + """""" + )) + + self.check_html(self.widget, 'nums', [2], choices=choices, html=( + """""" + )) + + def test_optgroup_select_multiple(self): + widget = SelectMultiple(choices=( + ('outer1', 'Outer 1'), + ('Group "1"', (('inner1', 'Inner 1'), ('inner2', 'Inner 2'))), + )) + self.check_html(widget, 'nestchoice', ['outer1', 'inner2'], html=( + """""" + )) -- cgit v1.3