diff options
| author | Nick Pope <nick@nickpope.me.uk> | 2023-08-31 02:57:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-30 22:57:40 -0300 |
| commit | 500e01073adda32d5149624ee9a5cb7aa3d3583f (patch) | |
| tree | f9416872a811aa39646deaf002414e0a7841b6d1 /tests/forms_tests/widget_tests/test_select.py | |
| parent | 68a8996bdfce2d191decd7b1c1a2b9fdea8e4b2f (diff) | |
Fixed #31262 -- Added support for mappings on model fields and ChoiceField's choices.
Diffstat (limited to 'tests/forms_tests/widget_tests/test_select.py')
| -rw-r--r-- | tests/forms_tests/widget_tests/test_select.py | 142 |
1 files changed, 69 insertions, 73 deletions
diff --git a/tests/forms_tests/widget_tests/test_select.py b/tests/forms_tests/widget_tests/test_select.py index 60a0b72880..6164d0b6b3 100644 --- a/tests/forms_tests/widget_tests/test_select.py +++ b/tests/forms_tests/widget_tests/test_select.py @@ -11,19 +11,17 @@ class SelectTest(ChoiceWidgetTest): widget = Select def test_render(self): - self.check_html( - self.widget(choices=self.beatles), - "beatle", - "J", - html=( - """<select name="beatle"> - <option value="J" selected>John</option> - <option value="P">Paul</option> - <option value="G">George</option> - <option value="R">Ringo</option> - </select>""" - ), - ) + html = """ + <select name="beatle"> + <option value="J" selected>John</option> + <option value="P">Paul</option> + <option value="G">George</option> + <option value="R">Ringo</option> + </select> + """ + for choices in (self.beatles, dict(self.beatles)): + with self.subTest(choices): + self.check_html(self.widget(choices=choices), "beatle", "J", html=html) def test_render_none(self): """ @@ -237,52 +235,46 @@ class SelectTest(ChoiceWidgetTest): """ Choices can be nested one level in order to create HTML optgroups. """ - self.check_html( - self.nested_widget, - "nestchoice", - None, - html=( - """<select name="nestchoice"> - <option value="outer1">Outer 1</option> - <optgroup label="Group "1""> - <option value="inner1">Inner 1</option> - <option value="inner2">Inner 2</option> - </optgroup> - </select>""" - ), - ) + html = """ + <select name="nestchoice"> + <option value="outer1">Outer 1</option> + <optgroup label="Group "1""> + <option value="inner1">Inner 1</option> + <option value="inner2">Inner 2</option> + </optgroup> + </select> + """ + for widget in self.nested_widgets: + with self.subTest(widget): + self.check_html(widget, "nestchoice", None, html=html) def test_choices_select_outer(self): - self.check_html( - self.nested_widget, - "nestchoice", - "outer1", - html=( - """<select name="nestchoice"> - <option value="outer1" selected>Outer 1</option> - <optgroup label="Group "1""> - <option value="inner1">Inner 1</option> - <option value="inner2">Inner 2</option> - </optgroup> - </select>""" - ), - ) + html = """ + <select name="nestchoice"> + <option value="outer1" selected>Outer 1</option> + <optgroup label="Group "1""> + <option value="inner1">Inner 1</option> + <option value="inner2">Inner 2</option> + </optgroup> + </select> + """ + for widget in self.nested_widgets: + with self.subTest(widget): + self.check_html(widget, "nestchoice", "outer1", html=html) def test_choices_select_inner(self): - self.check_html( - self.nested_widget, - "nestchoice", - "inner1", - html=( - """<select name="nestchoice"> - <option value="outer1">Outer 1</option> - <optgroup label="Group "1""> - <option value="inner1" selected>Inner 1</option> - <option value="inner2">Inner 2</option> - </optgroup> - </select>""" - ), - ) + html = """ + <select name="nestchoice"> + <option value="outer1">Outer 1</option> + <optgroup label="Group "1""> + <option value="inner1" selected>Inner 1</option> + <option value="inner2">Inner 2</option> + </optgroup> + </select> + """ + for widget in self.nested_widgets: + with self.subTest(widget): + self.check_html(widget, "nestchoice", "inner1", html=html) @override_settings(USE_THOUSAND_SEPARATOR=True) def test_doesnt_localize_option_value(self): @@ -312,24 +304,7 @@ class SelectTest(ChoiceWidgetTest): """ self.check_html(self.widget(choices=choices), "time", None, html=html) - def test_optgroups(self): - choices = [ - ( - "Audio", - [ - ("vinyl", "Vinyl"), - ("cd", "CD"), - ], - ), - ( - "Video", - [ - ("vhs", "VHS Tape"), - ("dvd", "DVD"), - ], - ), - ("unknown", "Unknown"), - ] + def _test_optgroups(self, choices): groups = list( self.widget(choices=choices).optgroups( "name", @@ -418,6 +393,27 @@ class SelectTest(ChoiceWidgetTest): ) self.assertEqual(index, 2) + def test_optgroups(self): + choices_dict = { + "Audio": [ + ("vinyl", "Vinyl"), + ("cd", "CD"), + ], + "Video": [ + ("vhs", "VHS Tape"), + ("dvd", "DVD"), + ], + "unknown": "Unknown", + } + choices_list = list(choices_dict.items()) + choices_nested_dict = { + k: dict(v) if isinstance(v, list) else v for k, v in choices_dict.items() + } + + for choices in (choices_dict, choices_list, choices_nested_dict): + with self.subTest(choices): + self._test_optgroups(choices) + def test_doesnt_render_required_when_impossible_to_select_empty_field(self): widget = self.widget(choices=[("J", "John"), ("P", "Paul")]) self.assertIs(widget.use_required_attribute(initial=None), False) |
