From f9c5958b8fe452983122b6a13c8f806e4e4e1eef Mon Sep 17 00:00:00 2001 From: Jakub Bagiński <88288777+Jacob1507@users.noreply.github.com> Date: Fri, 28 Jul 2023 14:18:07 +0200 Subject: Fixed #34655 -- Increased radioselect's test coverage. --- tests/forms_tests/widget_tests/test_radioselect.py | 433 +++++++++++++++++---- 1 file changed, 358 insertions(+), 75 deletions(-) (limited to 'tests/forms_tests/widget_tests/test_radioselect.py') diff --git a/tests/forms_tests/widget_tests/test_radioselect.py b/tests/forms_tests/widget_tests/test_radioselect.py index dc3f3d9bad..a861ccf0f3 100644 --- a/tests/forms_tests/widget_tests/test_radioselect.py +++ b/tests/forms_tests/widget_tests/test_radioselect.py @@ -1,32 +1,39 @@ import datetime -from django.forms import ChoiceField, Form, MultiWidget, RadioSelect +from django.forms import ChoiceField, Form, MultiWidget, RadioSelect, TextInput from django.test import override_settings +from django.utils.safestring import mark_safe -from .base import WidgetTest +from .test_choicewidget import ChoiceWidgetTest +BLANK_CHOICE_DASH = (("", "------"),) -class RadioSelectTest(WidgetTest): + +class RadioSelectTest(ChoiceWidgetTest): widget = RadioSelect def test_render(self): - choices = (("", "------"),) + self.beatles - self.check_html( - self.widget(choices=choices), - "beatle", - "J", - html=""" -
-
-
-
-
-
-
- """, - ) + choices = BLANK_CHOICE_DASH + self.beatles + html = """ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ """ + self.check_html(self.widget(choices=choices), "beatle", "J", html=html) def test_nested_choices(self): nested_choices = ( @@ -70,27 +77,286 @@ class RadioSelectTest(WidgetTest): html=html, ) + def test_render_none(self): + """ + If value is None, none of the options are selected. + """ + choices = BLANK_CHOICE_DASH + self.beatles + html = """ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ """ + self.check_html(self.widget(choices=choices), "beatle", None, html=html) + + def test_render_label_value(self): + """ + If the value corresponds to a label (but not to an option value), none + of the options are selected. + """ + html = """ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ """ + self.check_html(self.widget(choices=self.beatles), "beatle", "Ringo", html=html) + + def test_render_selected(self): + """ + Only one option can be selected. + """ + choices = [("0", "0"), ("1", "1"), ("2", "2"), ("3", "3"), ("0", "extra")] + html = """ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ """ + self.check_html(self.widget(choices=choices), "choices", "0", html=html) + def test_constructor_attrs(self): """ Attributes provided at instantiation are passed to the constituent inputs. """ - widget = RadioSelect(attrs={"id": "foo"}, choices=self.beatles) + widget = self.widget(attrs={"id": "foo"}, choices=self.beatles) html = """
+
+ +
+
+
+
+
+
+
+
+ """ + self.check_html(widget, "beatle", "J", html=html) + + def test_compare_to_str(self): + """ + The value is compared to its str() + """ + html = """
- +
+ +
+
+ +
+
+ +
-
-
-
+ """ + self.check_html( + self.widget(choices=[("1", "1"), ("2", "2"), ("3", "3")]), + "num", + 3, + html=html, + ) + self.check_html( + self.widget(choices=[(1, 1), (2, 2), (3, 3)]), "num", "3", html=html + ) + self.check_html( + self.widget(choices=[(1, 1), (2, 2), (3, 3)]), "num", 3, html=html + ) + + def test_choices_constructor(self): + widget = self.widget(choices=[(1, 1), (2, 2), (3, 3)]) + html = """ +
+
+ +
+
+ +
+
+ +
""" - self.check_html(widget, "beatle", "J", html=html) + self.check_html(widget, "num", 3, html=html) + + def test_choices_constructor_generator(self): + """ + If choices is passed to the constructor and is a generator, it can be + iterated over multiple times without getting consumed. + """ + + def get_choices(): + for i in range(4): + yield (i, i) + + html = """ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ """ + widget = self.widget(choices=get_choices()) + self.check_html(widget, "num", 3, html=html) + + def test_choices_escaping(self): + choices = (("bad", "you & me"), ("good", mark_safe("you > me"))) + html = """ +
+
+ +
+
+ +
+
+ """ + self.check_html(self.widget(choices=choices), "escape", None, html=html) + + def test_choices_unicode(self): + html = """ +
+
+ +
+
+ +
+
+ """ + self.check_html( + self.widget(choices=[("ŠĐĆŽćžšđ", "ŠĐabcĆŽćžšđ"), ("ćžšđ", "abcćžšđ")]), + "email", + "ŠĐĆŽćžšđ", + html=html, + ) + + def test_choices_optgroup(self): + """ + Choices can be nested one level in order to create HTML optgroups + """ + html = """ +
+
+ +
+
+ +
+ +
+
+ +
+
+
+ """ + self.check_html(self.nested_widget, "nestchoice", None, html=html) + + def test_choices_select_outer(self): + html = """ +
+
+ +
+
+ +
+ +
+
+ +
+
+
+ """ + self.check_html(self.nested_widget, "nestchoice", "outer1", html=html) + + def test_choices_select_inner(self): + html = """ +
+
+ +
+
+ +
+ +
+
+ +
+
+
+ """ + self.check_html(self.nested_widget, "nestchoice", "inner2", html=html) def test_render_attrs(self): """ @@ -99,16 +365,19 @@ class RadioSelectTest(WidgetTest): """ html = """
-
- -
-
-
-
+
+ +
+
+
+
+
+
+
""" self.check_html( @@ -126,15 +395,18 @@ class RadioSelectTest(WidgetTest): """ html = """
-
-
-
-
-
+
+
+
+
+
+
+
+
""" self.check_html( @@ -154,11 +426,13 @@ class RadioSelectTest(WidgetTest): ] html = """
-
-
-
+
+
+ +
+
+ +
""" self.check_html(self.widget(choices=choices), "number", None, html=html) @@ -169,35 +443,44 @@ class RadioSelectTest(WidgetTest): ] html = """
-
-
+
+ +
+
+ +
""" self.check_html(self.widget(choices=choices), "time", None, html=html) def test_render_as_subwidget(self): """A RadioSelect as a subwidget of MultiWidget.""" - choices = (("", "------"),) + self.beatles + choices = BLANK_CHOICE_DASH + self.beatles + html = """ +
+
+
+
+
+
+
+
+
+
+
+
+ + """ self.check_html( - MultiWidget([self.widget(choices=choices)]), + MultiWidget([self.widget(choices=choices), TextInput()]), "beatle", - ["J"], - html=""" -
-
-
-
-
-
-
- """, + ["J", "Some text"], + html=html, ) def test_fieldset(self): -- cgit v1.3