From 3278c31fa59b41d03aea167f4cf85f4ddf7f848d Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 8 Feb 2022 12:27:04 +0100 Subject: [4.0.x] Refs #33476 -- Refactored code to strictly match 88 characters line length. Backport of 7119f40c9881666b6f9b5cf7df09ee1d21cc8344 from main. --- tests/forms_tests/field_tests/test_booleanfield.py | 3 +- tests/forms_tests/field_tests/test_decimalfield.py | 3 +- tests/forms_tests/field_tests/test_emailfield.py | 3 +- tests/forms_tests/field_tests/test_floatfield.py | 6 +- .../field_tests/test_multivaluefield.py | 9 +- .../field_tests/test_nullbooleanfield.py | 9 +- tests/forms_tests/field_tests/test_urlfield.py | 3 +- tests/forms_tests/models.py | 7 +- tests/forms_tests/tests/test_forms.py | 919 +++++++++++++-------- tests/forms_tests/tests/test_formsets.py | 72 +- tests/forms_tests/tests/test_i18n.py | 37 +- tests/forms_tests/tests/test_input_formats.py | 50 +- tests/forms_tests/tests/test_media.py | 244 +++--- tests/forms_tests/tests/test_utils.py | 6 +- tests/forms_tests/tests/tests.py | 234 ++++-- .../forms_tests/widget_tests/test_checkboxinput.py | 3 +- .../widget_tests/test_checkboxselectmultiple.py | 100 ++- .../widget_tests/test_clearablefileinput.py | 14 +- .../forms_tests/widget_tests/test_datetimeinput.py | 5 +- .../widget_tests/test_multiplehiddeninput.py | 23 +- tests/forms_tests/widget_tests/test_multiwidget.py | 6 +- tests/forms_tests/widget_tests/test_numberinput.py | 3 +- tests/forms_tests/widget_tests/test_radioselect.py | 91 +- tests/forms_tests/widget_tests/test_select.py | 12 +- .../widget_tests/test_selectdatewidget.py | 3 +- tests/forms_tests/widget_tests/test_textarea.py | 6 +- tests/forms_tests/widget_tests/test_textinput.py | 13 +- 27 files changed, 1191 insertions(+), 693 deletions(-) (limited to 'tests/forms_tests') diff --git a/tests/forms_tests/field_tests/test_booleanfield.py b/tests/forms_tests/field_tests/test_booleanfield.py index 560a0f473b..d6b5121b10 100644 --- a/tests/forms_tests/field_tests/test_booleanfield.py +++ b/tests/forms_tests/field_tests/test_booleanfield.py @@ -52,7 +52,8 @@ class BooleanFieldTest(SimpleTestCase): self.assertTrue(f.has_changed(True, "")) # Initial value may have mutated to a string due to show_hidden_initial (#19537) self.assertTrue(f.has_changed("False", "on")) - # HiddenInput widget sends string values for boolean but doesn't clean them in value_from_datadict + # HiddenInput widget sends string values for boolean but doesn't clean + # them in value_from_datadict. self.assertFalse(f.has_changed(False, "False")) self.assertFalse(f.has_changed(True, "True")) self.assertTrue(f.has_changed(False, "True")) diff --git a/tests/forms_tests/field_tests/test_decimalfield.py b/tests/forms_tests/field_tests/test_decimalfield.py index 6e49a41341..119a74292c 100644 --- a/tests/forms_tests/field_tests/test_decimalfield.py +++ b/tests/forms_tests/field_tests/test_decimalfield.py @@ -105,7 +105,8 @@ class DecimalFieldTest(FormFieldAssertionsMixin, SimpleTestCase): ) self.assertWidgetRendersTo( f, - '', + '", ) with self.assertRaisesMessage( ValidationError, "'Ensure this value is less than or equal to 1.5.'" diff --git a/tests/forms_tests/field_tests/test_emailfield.py b/tests/forms_tests/field_tests/test_emailfield.py index d1d52dd608..869a1aacc5 100644 --- a/tests/forms_tests/field_tests/test_emailfield.py +++ b/tests/forms_tests/field_tests/test_emailfield.py @@ -49,7 +49,8 @@ class EmailFieldTest(FormFieldAssertionsMixin, SimpleTestCase): f = EmailField(min_length=10, max_length=15) self.assertWidgetRendersTo( f, - '', + '", ) with self.assertRaisesMessage( ValidationError, diff --git a/tests/forms_tests/field_tests/test_floatfield.py b/tests/forms_tests/field_tests/test_floatfield.py index 194cce4c10..3ee8644534 100644 --- a/tests/forms_tests/field_tests/test_floatfield.py +++ b/tests/forms_tests/field_tests/test_floatfield.py @@ -52,7 +52,8 @@ class FloatFieldTest(FormFieldAssertionsMixin, SimpleTestCase): f = FloatField(max_value=1.5, min_value=0.5) self.assertWidgetRendersTo( f, - '', + '", ) with self.assertRaisesMessage( ValidationError, "'Ensure this value is less than or equal to 1.5.'" @@ -71,7 +72,8 @@ class FloatFieldTest(FormFieldAssertionsMixin, SimpleTestCase): f = FloatField(widget=NumberInput(attrs={"step": 0.01, "max": 1.0, "min": 0.0})) self.assertWidgetRendersTo( f, - '', + '", ) def test_floatfield_localized(self): diff --git a/tests/forms_tests/field_tests/test_multivaluefield.py b/tests/forms_tests/field_tests/test_multivaluefield.py index da94206f4d..74e126deb2 100644 --- a/tests/forms_tests/field_tests/test_multivaluefield.py +++ b/tests/forms_tests/field_tests/test_multivaluefield.py @@ -183,15 +183,18 @@ class MultiValueFieldTest(SimpleTestCase): form.as_table(), """ - + - - + + """, ) diff --git a/tests/forms_tests/field_tests/test_nullbooleanfield.py b/tests/forms_tests/field_tests/test_nullbooleanfield.py index 5b632c8de3..c1e120c0bb 100644 --- a/tests/forms_tests/field_tests/test_nullbooleanfield.py +++ b/tests/forms_tests/field_tests/test_nullbooleanfield.py @@ -27,9 +27,11 @@ class NullBooleanFieldTest(FormFieldAssertionsMixin, SimpleTestCase): f = HiddenNullBooleanForm() self.assertHTMLEqual( - '' - '', str(f), + '' + '', ) def test_nullbooleanfield_3(self): @@ -69,7 +71,8 @@ class NullBooleanFieldTest(FormFieldAssertionsMixin, SimpleTestCase): self.assertTrue(f.has_changed(True, False)) self.assertTrue(f.has_changed(True, None)) self.assertTrue(f.has_changed(True, False)) - # HiddenInput widget sends string values for boolean but doesn't clean them in value_from_datadict + # HiddenInput widget sends string values for boolean but doesn't clean + # them in value_from_datadict. self.assertFalse(f.has_changed(False, "False")) self.assertFalse(f.has_changed(True, "True")) self.assertFalse(f.has_changed(None, "")) diff --git a/tests/forms_tests/field_tests/test_urlfield.py b/tests/forms_tests/field_tests/test_urlfield.py index f74c6567c1..e0afd4a7e7 100644 --- a/tests/forms_tests/field_tests/test_urlfield.py +++ b/tests/forms_tests/field_tests/test_urlfield.py @@ -99,7 +99,8 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase): f = URLField(min_length=15, max_length=20) self.assertWidgetRendersTo( f, - '', + '", ) with self.assertRaisesMessage( ValidationError, diff --git a/tests/forms_tests/models.py b/tests/forms_tests/models.py index ae212f332d..d6d0725b32 100644 --- a/tests/forms_tests/models.py +++ b/tests/forms_tests/models.py @@ -56,8 +56,11 @@ class ChoiceModel(models.Model): class ChoiceOptionModel(models.Model): - """Destination for ChoiceFieldModel's ForeignKey. - Can't reuse ChoiceModel because error_message tests require that it have no instances.""" + """ + Destination for ChoiceFieldModel's ForeignKey. + Can't reuse ChoiceModel because error_message tests require that it have no + instances. + """ name = models.CharField(max_length=10) diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index d5833b972e..233a3a622d 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -100,18 +100,24 @@ class FormsTestCase(SimpleTestCase): self.assertEqual(p.cleaned_data["birthday"], datetime.date(1940, 10, 9)) self.assertHTMLEqual( str(p["first_name"]), - '', + '", ) self.assertHTMLEqual( str(p["last_name"]), - '', + '", ) self.assertHTMLEqual( str(p["birthday"]), - '', + '", ) - msg = "Key 'nonexistentfield' not found in 'Person'. Choices are: birthday, first_name, last_name." + msg = ( + "Key 'nonexistentfield' not found in 'Person'. Choices are: birthday, " + "first_name, last_name." + ) with self.assertRaisesMessage(KeyError, msg): p["nonexistentfield"] @@ -142,12 +148,17 @@ class FormsTestCase(SimpleTestCase): ) self.assertHTMLEqual( str(p), - """ - - - - -""", + """ + + + + + + + """, ) def test_empty_dict(self): @@ -275,33 +286,41 @@ class FormsTestCase(SimpleTestCase): self.assertHTMLEqual( p.as_table(), '' - '\n' + '\n" '' '\n' '' - '', + '", ) self.assertHTMLEqual( p.as_ul(), '
  • ' - '
  • \n' + '\n" '
  • ' '
  • \n' + 'value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" ' + 'id="id_last_name" required>\n' '
  • ' - '
  • ', + '", ) self.assertHTMLEqual( p.as_p(), '

    ' - '

    \n' + '

    \n" '

    ' '

    \n' + 'value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" ' + 'id="id_last_name" required>

    \n' '

    ' - '

    ', + '

    ", ) p = Person({"last_name": "Lennon"}) @@ -356,11 +375,11 @@ class FormsTestCase(SimpleTestCase): self.assertEqual(p.cleaned_data["birthday"], datetime.date(1940, 10, 9)) def test_optional_data(self): - # cleaned_data will include a key and value for *all* fields defined in the Form, - # even if the Form's data didn't include a value for fields that are not - # required. In this example, the data dictionary doesn't include a value for the - # "nick_name" field, but cleaned_data includes it. For CharFields, it's set to the - # empty string. + # cleaned_data will include a key and value for *all* fields defined in + # the Form, even if the Form's data didn't include a value for fields + # that are not required. In this example, the data dictionary doesn't + # include a value for the "nick_name" field, but cleaned_data includes + # it. For CharFields, it's set to the empty string. class OptionalPersonForm(Form): first_name = CharField() last_name = CharField() @@ -387,10 +406,10 @@ class FormsTestCase(SimpleTestCase): self.assertEqual(f.cleaned_data["last_name"], "Lennon") def test_auto_id(self): - # "auto_id" tells the Form to add an "id" attribute to each form element. - # If it's a string that contains '%s', Django will use that as a format string - # into which the field's name will be inserted. It will also put a self.assertTrue(f.is_valid()) self.assertHTMLEqual( str(f["when"]), - '' + '" '', ) self.assertHTMLEqual( @@ -1033,26 +1077,38 @@ Java f = SongForm(auto_id=False) self.assertHTMLEqual( str(f["composers"]), - """
    -
    -
    -
    """, + """ +
    +
    +
    +
    + """, ) f = SongForm({"composers": ["J"]}, auto_id=False) self.assertHTMLEqual( str(f["composers"]), - """
    -
    -
    -
    """, + """ +
    +
    +
    +
    + """, ) f = SongForm({"composers": ["J", "P"]}, auto_id=False) self.assertHTMLEqual( str(f["composers"]), - """
    -
    -
    -
    """, + """ +
    +
    +
    +
    + """, ) def test_checkbox_auto_id(self): @@ -1069,12 +1125,16 @@ Java f = SongForm(auto_id="%s_id") self.assertHTMLEqual( str(f["composers"]), - """
    -
    -
    -
    """, + """ +
    +
    +
    +
    + """, ) def test_multiple_choice_list_data(self): @@ -1179,12 +1239,18 @@ Java ) self.assertHTMLEqual( f.as_table(), - """<em>Special</em> Field: - - -Special Field: - -""", + """ + <em>Special</em> Field: + + + + Special Field: + + + """, ) f = EscapingForm( { @@ -1195,24 +1261,29 @@ Java ) self.assertHTMLEqual( f.as_table(), - """<em>Special</em> Field: - - -Special Field: - -""", + "<em>Special</em> Field:" + '" + '' + "" + "Special Field:" + '" + '', ) def test_validating_multiple_fields(self): - # There are a couple of ways to do multiple-field validation. If you want the - # validation message to be associated with a particular field, implement the - # clean_XXX() method on the Form, where XXX is the field name. As in - # Field.clean(), the clean_XXX() method should return the cleaned value. In the - # clean_XXX() method, you have access to self.cleaned_data, which is a dictionary - # of all the data that has been cleaned *so far*, in order by the fields, - # including the current field (e.g., the field XXX if you're in clean_XXX()). + # There are a couple of ways to do multiple-field validation. If you + # want the validation message to be associated with a particular field, + # implement the clean_XXX() method on the Form, where XXX is the field + # name. As in Field.clean(), the clean_XXX() method should return the + # cleaned value. In the clean_XXX() method, you have access to + # self.cleaned_data, which is a dictionary of all the data that has + # been cleaned *so far*, in order by the fields, including the current + # field (e.g., the field XXX if you're in clean_XXX()). class UserRegistration(Form): username = CharField(max_length=10) password1 = CharField(widget=PasswordInput) @@ -1322,19 +1393,30 @@ value="Should escape < & > and <script>alert('xss')< ) self.assertHTMLEqual( f.as_table(), - """ - -Username: -Password1: -Password2:""", + """ + + + Username: + + + Password1: + + Password2: + + """, ) self.assertHTMLEqual( f.as_ul(), - """
  • -
  • Username:
  • -
  • Password1:
  • -
  • Password2:
  • """, + """ +
  • +
  • Username: + +
  • +
  • Password1:
  • +
  • Password2:
  • + """, ) f = UserRegistration( @@ -1509,9 +1591,14 @@ value="Should escape < & > and <script>alert('xss')< p = Person(auto_id=False) self.assertHTMLEqual( p.as_table(), - """First name: -Last name: -Birthday:""", + """ + First name: + + Last name: + + Birthday: + + """, ) # Instances of a dynamic Form do not persist fields from one Form instance to @@ -1527,15 +1614,19 @@ value="Should escape < & > and <script>alert('xss')< my_form = MyForm(field_list=field_list) self.assertHTMLEqual( my_form.as_table(), - """Field1: -Field2:""", + """ + Field1: + Field2: + """, ) field_list = [("field3", CharField()), ("field4", CharField())] my_form = MyForm(field_list=field_list) self.assertHTMLEqual( my_form.as_table(), - """Field3: -Field4:""", + """ + Field3: + Field4: + """, ) class MyForm(Form): @@ -1552,19 +1643,27 @@ value="Should escape < & > and <script>alert('xss')< my_form = MyForm(field_list=field_list) self.assertHTMLEqual( my_form.as_table(), - """Default field 1: -Default field 2: -Field1: -Field2:""", + """ + Default field 1: + + Default field 2: + + Field1: + Field2: + """, ) field_list = [("field3", CharField()), ("field4", CharField())] my_form = MyForm(field_list=field_list) self.assertHTMLEqual( my_form.as_table(), - """Default field 1: -Default field 2: -Field3: -Field4:""", + """ + Default field 1: + + Default field 2: + + Field3: + Field4: + """, ) # Similarly, changes to field attributes do not persist from one Form instance @@ -1692,22 +1791,33 @@ value="Should escape < & > and <script>alert('xss')< p = Person(auto_id=False) self.assertHTMLEqual( p.as_table(), - """First name: -Last name: -Birthday: -""", + """ + First name: + + Last name: + + Birthday: + + + """, ) self.assertHTMLEqual( p.as_ul(), - """
  • First name:
  • -
  • Last name:
  • -
  • Birthday:
  • """, + """ +
  • First name:
  • +
  • Last name:
  • +
  • Birthday: +
  • + """, ) self.assertHTMLEqual( p.as_p(), - """

    First name:

    -

    Last name:

    -

    Birthday:

    """, + """ +

    First name:

    +

    Last name:

    +

    Birthday: +

    + """, ) # With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label. @@ -1753,28 +1863,44 @@ value="Should escape < & > and <script>alert('xss')< ) self.assertHTMLEqual( p.as_table(), - """ - -First name: -Last name: -Birthday: -""", + """ + + + First name: + + Last name: + + Birthday: + + + """, ) self.assertHTMLEqual( p.as_ul(), - """
  • -
  • First name:
  • -
  • Last name:
  • -
  • Birthday: -
  • """, + """ +
  • +
  • First name: +
  • +
  • Last name: +
  • +
  • Birthday: +
  • + """, ) self.assertHTMLEqual( p.as_p(), - """ -

    First name:

    -

    Last name:

    -

    Birthday: -

    """, + """ + +

    First name: +

    +

    Last name: +

    +

    Birthday: +

    + """, ) # A corner case: It's possible for a form to have only HiddenInputs. @@ -1816,20 +1942,11 @@ value="Should escape < & > and <script>alert('xss')< p = TestForm(auto_id=False) self.assertHTMLEqual( p.as_table(), - """Field1: -Field2: -Field3: -Field4: -Field5: -Field6: -Field7: -Field8: -Field9: -Field10: -Field11: -Field12: -Field13: -Field14:""", + "".join( + f"Field{i}:" + f'' + for i in range(1, 15) + ), ) def test_explicit_field_order(self): @@ -1893,15 +2010,20 @@ value="Should escape < & > and <script>alert('xss')< p = UserRegistration(auto_id=False) self.assertHTMLEqual( p.as_ul(), - """
  • Username:
  • -
  • Password:
  • -
  • Realname:
  • -
  • Address:
  • """, + """ +
  • Username: +
  • +
  • Password:
  • +
  • Realname: +
  • +
  • Address:
  • + """, ) - # If you specify a custom "attrs" that includes the "maxlength" attribute, - # the Field's max_length attribute will override whatever "maxlength" you specify - # in "attrs". + # If you specify a custom "attrs" that includes the "maxlength" + # attribute, the Field's max_length attribute will override whatever + # "maxlength" you specify in "attrs". class UserRegistration(Form): username = CharField( max_length=10, widget=TextInput(attrs={"maxlength": 20}) @@ -1927,9 +2049,13 @@ value="Should escape < & > and <script>alert('xss')< p = UserRegistration(auto_id=False) self.assertHTMLEqual( p.as_ul(), - """
  • Your username:
  • -
  • Password1:
  • -
  • Contraseña (de nuevo):
  • """, + """ +
  • Your username: +
  • +
  • Password1:
  • +
  • Contraseña (de nuevo): +
  • + """, ) # Labels for as_* methods will only end in a colon if they don't end in other @@ -1951,14 +2077,22 @@ value="Should escape < & > and <script>alert('xss')< ) self.assertHTMLEqual( Questions().as_p(), - """

    -

    -

    -

    -

    """, + """ +

    +

    +

    +

    +

    +

    +

    +

    +

    +

    + """, ) - # If a label is set to the empty string for a field, that field won't get a label. + # If a label is set to the empty string for a field, that field won't + # get a label. class UserRegistration(Form): username = CharField(max_length=10, label="") password = CharField(widget=PasswordInput) @@ -1972,9 +2106,13 @@ value="Should escape < & > and <script>alert('xss')< p = UserRegistration(auto_id="id_%s") self.assertHTMLEqual( p.as_ul(), - """
  • -
  • -
  • """, + """ +
  • + +
  • +
  • +
  • + """, ) # If label is None, Django will auto-create the label from the field name. This @@ -1999,11 +2137,12 @@ value="Should escape < & > and <script>alert('xss')< ) def test_label_suffix(self): - # You can specify the 'label_suffix' argument to a Form class to modify the - # punctuation symbol used at the end of a label. By default, the colon (:) is - # used, and is only appended to the label if the label doesn't already end with a - # punctuation symbol: ., !, ? or :. If you specify a different suffix, it will - # be appended regardless of the last character of the label. + # You can specify the 'label_suffix' argument to a Form class to modify + # the punctuation symbol used at the end of a label. By default, the + # colon (:) is used, and is only appended to the label if the label + # doesn't already end with a punctuation symbol: ., !, ? or :. If you + # specify a different suffix, it will be appended regardless of the + # last character of the label. class FavoriteForm(Form): color = CharField(label="Favorite color?") animal = CharField(label="Favorite animal") @@ -2037,7 +2176,8 @@ value="Should escape < & > and <script>alert('xss')< self.assertHTMLEqual( f.as_ul(), '
  • Favorite color?
  • \n' - '
  • Favorite animal\u2192
  • \n' + "
  • Favorite animal\u2192 " + '
  • \n' '
  • Secret answer =
  • ', ) @@ -2055,8 +2195,11 @@ value="Should escape < & > and <script>alert('xss')< p = UserRegistration(auto_id=False) self.assertHTMLEqual( p.as_ul(), - """
  • Username:
  • -
  • Password:
  • """, + """ +
  • Username:
  • +
  • Password:
  • + """, ) # Here, we're submitting data, so the initial value will *not* be displayed. @@ -2079,14 +2222,18 @@ Password: """, p = UserRegistration({"username": "foo"}, auto_id=False) self.assertHTMLEqual( p.as_ul(), - """
  • Username:
  • -
  • -Password:
  • """, + """ +
  • Username:
  • +
  • + Password:
  • + """, ) - # An 'initial' value is *not* used as a fallback if data is not provided. In this - # example, we don't provide a value for 'username', and the form raises a - # validation error rather than using the initial value for 'username'. + # An 'initial' value is *not* used as a fallback if data is not + # provided. In this example, we don't provide a value for 'username', + # and the form raises a validation error rather than using the initial + # value for 'username'. p = UserRegistration({"password": "secret"}) self.assertEqual(p.errors["username"], ["This field is required."]) self.assertFalse(p.is_valid()) @@ -2105,14 +2252,20 @@ Password: """, p = UserRegistration(initial={"username": "django"}, auto_id=False) self.assertHTMLEqual( p.as_ul(), - """
  • Username:
  • -
  • Password:
  • """, + """ +
  • Username:
  • +
  • Password:
  • + """, ) p = UserRegistration(initial={"username": "stephane"}, auto_id=False) self.assertHTMLEqual( p.as_ul(), - """
  • Username:
  • -
  • Password:
  • """, + """ +
  • Username:
  • +
  • Password:
  • + """, ) # The 'initial' parameter is meaningless if you pass data. @@ -2139,20 +2292,24 @@ Password: """, ) self.assertHTMLEqual( p.as_ul(), - """
  • Username:
  • -
  • -Password:
  • """, + """ +
  • Username:
  • +
  • + Password:
  • + """, ) # A dynamic 'initial' value is *not* used as a fallback if data is not provided. - # In this example, we don't provide a value for 'username', and the form raises a - # validation error rather than using the initial value for 'username'. + # In this example, we don't provide a value for 'username', and the + # form raises a validation error rather than using the initial value + # for 'username'. p = UserRegistration({"password": "secret"}, initial={"username": "django"}) self.assertEqual(p.errors["username"], ["This field is required."]) self.assertFalse(p.is_valid()) - # If a Form defines 'initial' *and* 'initial' is passed as a parameter to Form(), - # then the latter will get precedence. + # If a Form defines 'initial' *and* 'initial' is passed as a parameter + # to Form(), then the latter will get precedence. class UserRegistration(Form): username = CharField(max_length=10, initial="django") password = CharField(widget=PasswordInput) @@ -2160,8 +2317,11 @@ Password: """, p = UserRegistration(initial={"username": "babik"}, auto_id=False) self.assertHTMLEqual( p.as_ul(), - """
  • Username:
  • -
  • Password:
  • """, + """ +
  • Username:
  • +
  • Password:
  • + """, ) def test_callable_initial_data(self): @@ -2194,13 +2354,16 @@ Password: """, ) self.assertHTMLEqual( p.as_ul(), - """
  • Username:
  • -
  • Password:
  • -
  • Options:
  • """, + """ +
  • Username:
  • +
  • Password:
  • +
  • Options:
  • + """, ) # The 'initial' parameter is meaningless if you pass data. @@ -2245,19 +2408,23 @@ Options: -
  • -Password:
  • -
  • Options:
  • """, + """ +
  • Username:
  • +
  • + Password:
  • +
  • Options:
  • + """, ) - # A callable 'initial' value is *not* used as a fallback if data is not provided. - # In this example, we don't provide a value for 'username', and the form raises a - # validation error rather than using the initial value for 'username'. + # A callable 'initial' value is *not* used as a fallback if data is not + # provided. In this example, we don't provide a value for 'username', + # and the form raises a validation error rather than using the initial + # value for 'username'. p = UserRegistration( {"password": "secret"}, initial={"username": initial_django, "options": initial_options}, @@ -2265,8 +2432,8 @@ Password: self.assertEqual(p.errors["username"], ["This field is required."]) self.assertFalse(p.is_valid()) - # If a Form defines 'initial' *and* 'initial' is passed as a parameter to Form(), - # then the latter will get precedence. + # If a Form defines 'initial' *and* 'initial' is passed as a parameter + # to Form(), then the latter will get precedence. class UserRegistration(Form): username = CharField(max_length=10, initial=initial_django) password = CharField(widget=PasswordInput) @@ -2278,13 +2445,16 @@ Password: p = UserRegistration(auto_id=False) self.assertHTMLEqual( p.as_ul(), - """
  • Username:
  • -
  • Password:
  • -
  • Options:
  • """, + """ +
  • Username:
  • +
  • Password:
  • +
  • Options:
  • + """, ) p = UserRegistration( initial={"username": initial_stephane, "options": initial_options}, @@ -2292,13 +2462,16 @@ Password: ) self.assertHTMLEqual( p.as_ul(), - """
  • Username:
  • -
  • Password:
  • -
  • Options:
  • """, + """ +
  • Username:
  • +
  • Password:
  • +
  • Options:
  • + """, ) def test_get_initial_for_field(self): @@ -2531,7 +2704,8 @@ Password: self.assertEqual(form.changed_data, []) def test_help_text(self): - # You can specify descriptive text for a field by using the 'help_text' argument) + # You can specify descriptive text for a field by using the 'help_text' + # argument. class UserRegistration(Form): username = CharField(max_length=10, help_text="e.g., user@example.com") password = CharField( @@ -2555,10 +2729,13 @@ Password: ) self.assertHTMLEqual( p.as_table(), - """Username:
    -e.g., user@example.com -Password:
    -Wählen Sie mit Bedacht.""", + """ + Username: +
    + e.g., user@example.com + Password: +
    + Wählen Sie mit Bedacht.""", ) # The help text is displayed whether or not data is provided for the form. @@ -2678,13 +2855,14 @@ Password: ) def test_forms_with_prefixes(self): - # Sometimes it's necessary to have multiple forms display on the same HTML page, - # or multiple copies of the same form. We can accomplish this with form prefixes. - # Pass the keyword argument 'prefix' to the Form constructor to use this feature. - # This value will be prepended to each HTML form field name. One way to think - # about this is "namespaces for HTML forms". Notice that in the data argument, - # each field's key has the prefix, in this case 'person1', prepended to the - # actual field name. + # Sometimes it's necessary to have multiple forms display on the same + # HTML page, or multiple copies of the same form. We can accomplish + # this with form prefixes. Pass the keyword argument 'prefix' to the + # Form constructor to use this feature. This value will be prepended to + # each HTML form field name. One way to think about this is "namespaces + # for HTML forms". Notice that in the data argument, each field's key + # has the prefix, in this case 'person1', prepended to the actual field + # name. class Person(Form): first_name = CharField() last_name = CharField() @@ -2698,24 +2876,32 @@ Password: p = Person(data, prefix="person1") self.assertHTMLEqual( p.as_ul(), - """
  • -
  • -
  • -
  • -
  • -
  • """, + """ +
  • +
  • +
  • +
  • +
  • +
  • + """, ) self.assertHTMLEqual( str(p["first_name"]), - '', + '', ) self.assertHTMLEqual( str(p["last_name"]), - '', + '', ) self.assertHTMLEqual( str(p["birthday"]), - '', + '', ) self.assertEqual(p.errors, {}) self.assertTrue(p.is_valid()) @@ -2787,12 +2973,17 @@ Password: p = Person(prefix="foo") self.assertHTMLEqual( p.as_ul(), - """
  • -
  • -
  • -
  • -
  • -
  • """, + """ +
  • +
  • +
  • +
  • +
  • +
  • + """, ) data = { "foo-prefix-first_name": "John", @@ -2907,15 +3098,16 @@ Password: ) def test_forms_with_file_fields(self): - # FileFields are a special case because they take their data from the request.FILES, - # not request.POST. + # FileFields are a special case because they take their data from the + # request.FILES, not request.POST. class FileForm(Form): file1 = FileField() f = FileForm(auto_id=False) self.assertHTMLEqual( f.as_table(), - 'File1:', + "File1:" + '', ) f = FileForm(data={}, files={}, auto_id=False) @@ -2954,7 +3146,8 @@ Password: ) self.assertHTMLEqual( f.as_table(), - 'File1:', + "File1:" + '', ) self.assertTrue(f.is_valid()) @@ -2964,7 +3157,8 @@ Password: f = FileForm(data={}, files={"file1": file1}, auto_id=False) self.assertHTMLEqual( f.as_table(), - 'File1:', + "File1:" + '', ) # A required file field with initial data should not contain the @@ -3084,9 +3278,10 @@ Password: self.assertHTMLEqual( MyForm().as_table(), - '' - '' - '', + '' + '' + '' + "", ) def test_error_html_required_html_classes(self): @@ -3102,34 +3297,46 @@ Password: self.assertHTMLEqual( p.as_ul(), - """
  • -
  • -
  • -
  • -
  • -
  • -
  • """, + """ +
  • + +
  • +
  • + +
  • +
  • +
  • +
  • + +
  • """, ) self.assertHTMLEqual( p.as_p(), - """ -

    -

    -

    -

    -

    - -

    -

    """, + """ + +

    + +

    +

    + +

    +

    +

    + +

    +

    + """, ) self.assertHTMLEqual( @@ -3394,10 +3601,14 @@ Password: self.assertHTMLEqual( form.as_p(), """ -

    Phone1:

    -

    Phone2:

    -

    Phone3:

    -

    Phone4:

    +

    Phone1: +

    +

    Phone2: +

    +

    Phone3: +

    +

    Phone4: +

    """, ) @@ -3585,7 +3796,8 @@ Password: control = [ '
  • foo
  • ', '
  • bar
  • ', - '
  • __all__
  • ', + '
  • __all__' + "
  • ", ] for error in control: self.assertInHTML(error, errors) @@ -3689,10 +3901,14 @@ Password: ) self.assertHTMLEqual( p.as_p(), - """ -

    - -

    """, + """ + +

    + +

    + """, ) self.assertHTMLEqual( p.as_table(), @@ -3738,11 +3954,18 @@ Password: ) self.assertHTMLEqual( p.as_table(), - """ - - - -""", + """ + + + + + + + + """, ) def test_error_escaping(self): @@ -3759,7 +3982,8 @@ Password: form.is_valid() self.assertHTMLEqual( form.as_ul(), - '
  • ' + '
  • " '
  • ' ' ' '' @@ -3774,14 +3998,16 @@ Password: p = Person() self.assertEqual( repr(p), - "", + "", ) p = Person( {"first_name": "John", "last_name": "Lennon", "birthday": "1940-10-9"} ) self.assertEqual( repr(p), - "", + "", ) p.is_valid() self.assertEqual( @@ -3894,9 +4120,12 @@ Password: form = MyForm() self.assertHTMLEqual( form.as_p(), - '

    ' - '

    ' - '

    " '

    form.as_ul(), '

  • ' '
  • ' - '
  • ' - '
  • " '
  • self.assertHTMLEqual( form.as_table(), '' - '' + '' + "" '' '' '' '" - '' '' '' "", @@ -3941,10 +4174,12 @@ Password: form = MyForm() self.assertHTMLEqual( form.as_p(), - '

    ' - '

    ' - '

    " + '

    ' + '

    ' + '

    ' + '

    ' + '

    ' + '

    ' '

    ) self.assertHTMLEqual( form.as_ul(), - '

  • ' - '
  • ' - '
  • " + '
  • ' + '
  • ' + '
  • ' + '
  • ' + '
  • ' + '
  • ' '
  • '' '' '' - '" - '' '' '' "", diff --git a/tests/forms_tests/tests/test_formsets.py b/tests/forms_tests/tests/test_formsets.py index c37e95a071..8634b3df29 100644 --- a/tests/forms_tests/tests/test_formsets.py +++ b/tests/forms_tests/tests/test_formsets.py @@ -641,7 +641,8 @@ class FormsFormsetTestCase(SimpleTestCase): def test_formsets_with_ordering(self): """ formset_factory's can_order argument adds an integer field to each - form. When form validation succeeds, [form.cleaned_data for form in formset.forms] + form. When form validation succeeds, + [form.cleaned_data for form in formset.forms] will have the data in the correct order specified by the ordering fields. If a number is duplicated in the set of ordering fields, for instance form 0 and form 3 are both marked as 1, then the form index @@ -943,10 +944,13 @@ class FormsFormsetTestCase(SimpleTestCase): formset = LimitedFavoriteDrinkFormSet(initial=[{"name": "Fernet and Coke"}]) self.assertHTMLEqual( "\n".join(str(form) for form in formset.forms), - """ - - -""", + """ + + + + + """, ) def test_max_num_zero(self): @@ -972,10 +976,14 @@ class FormsFormsetTestCase(SimpleTestCase): formset = LimitedFavoriteDrinkFormSet(initial=initial) self.assertHTMLEqual( "\n".join(str(form) for form in formset.forms), - """ - - -""", + """ + + + + + """, ) def test_more_initial_than_max_num(self): @@ -994,12 +1002,20 @@ class FormsFormsetTestCase(SimpleTestCase): formset = LimitedFavoriteDrinkFormSet(initial=initial) self.assertHTMLEqual( "\n".join(str(form) for form in formset.forms), - """ - - - - -""", + """ + + + + + + + + + + + """, ) def test_default_absolute_max(self): @@ -1077,10 +1093,13 @@ class FormsFormsetTestCase(SimpleTestCase): formset = LimitedFavoriteDrinkFormSet(initial=[{"name": "Gin Tonic"}]) self.assertHTMLEqual( "\n".join(str(form) for form in formset.forms), - """ - - -""", + """ + + + + + + """, ) def test_management_form_prefix(self): @@ -1454,7 +1473,8 @@ class FormsetAsTagTests(SimpleTestCase): self.formset.as_p(), self.management_form_html + ( - '

    Choice:

    ' + "

    Choice: " + '

    ' '

    Votes:

    ' ), ) @@ -1464,8 +1484,10 @@ class FormsetAsTagTests(SimpleTestCase): self.formset.as_ul(), self.management_form_html + ( - '
  • Choice:
  • ' - '
  • Votes:
  • ' + "
  • Choice: " + '
  • ' + "
  • Votes: " + '
  • ' ), ) @@ -1535,8 +1557,10 @@ class TestIsBoundBehavior(SimpleTestCase): "
  • (Hidden field TOTAL_FORMS) Enter a whole number.
  • " "
  • (Hidden field INITIAL_FORMS) Enter a whole number.
  • " "" - '' - '' + '' + '' '' '' "\n", diff --git a/tests/forms_tests/tests/test_i18n.py b/tests/forms_tests/tests/test_i18n.py index f7a4aabbf8..ea6a8e104b 100644 --- a/tests/forms_tests/tests/test_i18n.py +++ b/tests/forms_tests/tests/test_i18n.py @@ -22,21 +22,25 @@ class FormsI18nTests(SimpleTestCase): self.assertHTMLEqual( f.as_p(), '

    ' - '

    ', + '

    ", ) - # Translations are done at rendering time, so multi-lingual apps can define forms) + # Translations are done at rendering time, so multi-lingual apps can + # define forms. with translation.override("de"): self.assertHTMLEqual( f.as_p(), '

    ' - '

    ', + '

    ", ) with translation.override("pl"): self.assertHTMLEqual( f.as_p(), '

    ' - '

    ', + '

    ", ) def test_non_ascii_label(self): @@ -70,13 +74,13 @@ class FormsI18nTests(SimpleTestCase): "

    " '

    \n' '
    \n" + ' En tied\xe4
    \n" '
    \n
    \n

    ", + ' Mies\n
    \n

    ", ) # Translated error messages @@ -89,13 +93,14 @@ class FormsI18nTests(SimpleTestCase): "\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.\n" "

    " '

    \n
    \n" + ' En tied\xe4
    \n' '
    \n
    \n

    ", + ' Mies\n
    ' + '
    \n

    ', ) def test_select_translated_text(self): diff --git a/tests/forms_tests/tests/test_input_formats.py b/tests/forms_tests/tests/test_input_formats.py index 3ac28b1b30..cd3817d421 100644 --- a/tests/forms_tests/tests/test_input_formats.py +++ b/tests/forms_tests/tests/test_input_formats.py @@ -92,7 +92,10 @@ class LocalizedTimeTests(SimpleTestCase): self.assertEqual(text, "13:30:00") def test_localized_timeField_with_inputformat(self): - "Localized TimeFields with manually specified input formats can accept those formats" + """ + Localized TimeFields with manually specified input formats can accept + those formats. + """ f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"], localize=True) # Parse a time in an unaccepted format; get an error with self.assertRaises(ValidationError): @@ -192,7 +195,10 @@ class CustomTimeInputFormatsTests(SimpleTestCase): self.assertEqual(text, "01:30:00 PM") def test_localized_timeField_with_inputformat(self): - "Localized TimeFields with manually specified input formats can accept those formats" + """ + Localized TimeFields with manually specified input formats can accept + those formats. + """ f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"], localize=True) # Parse a time in an unaccepted format; get an error with self.assertRaises(ValidationError): @@ -288,7 +294,10 @@ class SimpleTimeFormatTests(SimpleTestCase): self.assertEqual(text, "13:30:00") def test_localized_timeField_with_inputformat(self): - "Localized TimeFields with manually specified input formats can accept those formats" + """ + Localized TimeFields with manually specified input formats can accept + those formats. + """ f = forms.TimeField(input_formats=["%I:%M:%S %p", "%I:%M %p"], localize=True) # Parse a time in an unaccepted format; get an error with self.assertRaises(ValidationError): @@ -395,7 +404,10 @@ class LocalizedDateTests(SimpleTestCase): self.assertEqual(text, "21.12.2010") def test_localized_dateField_with_inputformat(self): - "Localized DateFields with manually specified input formats can accept those formats" + """ + Localized DateFields with manually specified input formats can accept + those formats. + """ f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"], localize=True) # Parse a date in an unaccepted format; get an error with self.assertRaises(ValidationError): @@ -497,7 +509,10 @@ class CustomDateInputFormatsTests(SimpleTestCase): self.assertEqual(text, "21.12.2010") def test_localized_dateField_with_inputformat(self): - "Localized DateFields with manually specified input formats can accept those formats" + """ + Localized DateFields with manually specified input formats can accept + those formats. + """ f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"], localize=True) # Parse a date in an unaccepted format; get an error with self.assertRaises(ValidationError): @@ -593,7 +608,10 @@ class SimpleDateFormatTests(SimpleTestCase): self.assertEqual(text, "2010-12-21") def test_localized_dateField_with_inputformat(self): - "Localized DateFields with manually specified input formats can accept those formats" + """ + Localized DateFields with manually specified input formats can accept + those formats. + """ f = forms.DateField(input_formats=["%d.%m.%Y", "%d-%m-%Y"], localize=True) # Parse a date in an unaccepted format; get an error with self.assertRaises(ValidationError): @@ -702,7 +720,10 @@ class LocalizedDateTimeTests(SimpleTestCase): self.assertEqual(text, "21.12.2010 13:30:00") def test_localized_dateTimeField_with_inputformat(self): - "Localized DateTimeFields with manually specified input formats can accept those formats" + """ + Localized DateTimeFields with manually specified input formats can + accept those formats. + """ f = forms.DateTimeField( input_formats=["%H.%M.%S %m.%d.%Y", "%H.%M %m-%d-%Y"], localize=True ) @@ -810,7 +831,10 @@ class CustomDateTimeInputFormatsTests(SimpleTestCase): self.assertEqual(text, "01:30:00 PM 21/12/2010") def test_localized_dateTimeField_with_inputformat(self): - "Localized DateTimeFields with manually specified input formats can accept those formats" + """ + Localized DateTimeFields with manually specified input formats can + accept those formats. + """ f = forms.DateTimeField( input_formats=["%m.%d.%Y %H:%M:%S", "%m-%d-%Y %H:%M"], localize=True ) @@ -862,7 +886,10 @@ class SimpleDateTimeFormatTests(SimpleTestCase): self.assertEqual(text, "2010-12-21 13:30:05") def test_localized_dateTimeField(self): - "Localized DateTimeFields in a non-localized environment act as unlocalized widgets" + """ + Localized DateTimeFields in a non-localized environment act as + unlocalized widgets. + """ f = forms.DateTimeField() # Parse a date in an unaccepted format; get an error with self.assertRaises(ValidationError): @@ -910,7 +937,10 @@ class SimpleDateTimeFormatTests(SimpleTestCase): self.assertEqual(text, "2010-12-21 13:30:00") def test_localized_dateTimeField_with_inputformat(self): - "Localized DateTimeFields with manually specified input formats can accept those formats" + """ + Localized DateTimeFields with manually specified input formats can + accept those formats. + """ f = forms.DateTimeField( input_formats=["%I:%M:%S %p %d.%m.%Y", "%I:%M %p %d-%m-%Y"], localize=True ) diff --git a/tests/forms_tests/tests/test_media.py b/tests/forms_tests/tests/test_media.py index fe229599f4..5439e161c1 100644 --- a/tests/forms_tests/tests/test_media.py +++ b/tests/forms_tests/tests/test_media.py @@ -21,16 +21,18 @@ class FormsMediaTestCase(SimpleTestCase): ) self.assertEqual( str(m), - """ - - - -""", + '\n' + '\n' + '\n' + '\n' + '', ) self.assertEqual( repr(m), "Media(css={'all': ['path/to/css1', '/path/to/css2']}, " - "js=['/path/to/js1', 'http://media.other.com/path/to/js2', 'https://secure.other.com/path/to/js3'])", + "js=['/path/to/js1', 'http://media.other.com/path/to/js2', " + "'https://secure.other.com/path/to/js3'])", ) class Foo: @@ -44,11 +46,12 @@ class FormsMediaTestCase(SimpleTestCase): m3 = Media(Foo) self.assertEqual( str(m3), - """ - - - -""", + '\n' + '\n' + '\n' + '\n' + '', ) # A widget can exist without a media definition @@ -78,18 +81,20 @@ class FormsMediaTestCase(SimpleTestCase): w1 = MyWidget1() self.assertEqual( str(w1.media), - """ - - - -""", + '\n' + '\n' + '\n' + '\n' + '', ) # Media objects can be interrogated by media type self.assertEqual( str(w1.media["css"]), - """ -""", + '\n' + '', ) self.assertEqual( @@ -126,23 +131,25 @@ class FormsMediaTestCase(SimpleTestCase): w3 = MyWidget3() self.assertEqual( str(w1.media + w2.media + w3.media), - """ - - - - - -""", + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '', ) # media addition hasn't affected the original objects self.assertEqual( str(w1.media), - """ - - - -""", + '\n' + '\n' + '\n' + '\n' + '', ) # Regression check for #12879: specifying the same CSS or JS file @@ -234,13 +241,14 @@ class FormsMediaTestCase(SimpleTestCase): w6 = MyWidget6() self.assertEqual( str(w6.media), - """ - - - - - -""", + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '', ) def test_media_inheritance(self): @@ -248,7 +256,8 @@ class FormsMediaTestCase(SimpleTestCase): # Inheritance of media ############################################################### - # If a widget extends another but provides no media definition, it inherits the parent widget's media + # If a widget extends another but provides no media definition, it + # inherits the parent widget's media. class MyWidget1(TextInput): class Media: css = {"all": ("path/to/css1", "/path/to/css2")} @@ -264,14 +273,16 @@ class FormsMediaTestCase(SimpleTestCase): w7 = MyWidget7() self.assertEqual( str(w7.media), - """ - - - -""", + '\n' + '\n' + '\n' + '\n' + '', ) - # If a widget extends another but defines media, it extends the parent widget's media by default + # If a widget extends another but defines media, it extends the parent + # widget's media by default. class MyWidget8(MyWidget1): class Media: css = {"all": ("/path/to/css3", "path/to/css1")} @@ -280,18 +291,19 @@ class FormsMediaTestCase(SimpleTestCase): w8 = MyWidget8() self.assertEqual( str(w8.media), - """ - - - - - -""", + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '', ) def test_media_inheritance_from_property(self): - # If a widget extends another but defines media, it extends the parents widget's media, - # even if the parent defined media using a property. + # If a widget extends another but defines media, it extends the parents + # widget's media, even if the parent defined media using a property. class MyWidget1(TextInput): class Media: css = {"all": ("path/to/css1", "/path/to/css2")} @@ -331,14 +343,16 @@ class FormsMediaTestCase(SimpleTestCase): w10 = MyWidget10() self.assertEqual( str(w10.media), - """ - - -""", + '\n' + '\n' + '\n' + '', ) def test_media_inheritance_extends(self): - # A widget can explicitly enable full media inheritance by specifying 'extend=True' + # A widget can explicitly enable full media inheritance by specifying + # 'extend=True'. class MyWidget1(TextInput): class Media: css = {"all": ("path/to/css1", "/path/to/css2")} @@ -357,17 +371,19 @@ class FormsMediaTestCase(SimpleTestCase): w11 = MyWidget11() self.assertEqual( str(w11.media), - """ - - - - - -""", + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '', ) def test_media_inheritance_single_type(self): - # A widget can enable inheritance of one media type by specifying extend as a tuple + # A widget can enable inheritance of one media type by specifying + # extend as a tuple. class MyWidget1(TextInput): class Media: css = {"all": ("path/to/css1", "/path/to/css2")} @@ -386,11 +402,12 @@ class FormsMediaTestCase(SimpleTestCase): w12 = MyWidget12() self.assertEqual( str(w12.media), - """ - - - -""", + '\n' + '\n' + '\n' + '\n' + '', ) def test_multi_media(self): @@ -453,13 +470,14 @@ class FormsMediaTestCase(SimpleTestCase): mymulti = MyMultiWidget() self.assertEqual( str(mymulti.media), - """ - - - - - -""", + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '', ) def test_form_media(self): @@ -494,13 +512,14 @@ class FormsMediaTestCase(SimpleTestCase): f1 = MyForm() self.assertEqual( str(f1.media), - """ - - - - - -""", + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '', ) # Form media can be combined to produce a single media definition. @@ -510,13 +529,14 @@ class FormsMediaTestCase(SimpleTestCase): f2 = AnotherForm() self.assertEqual( str(f1.media + f2.media), - """ - - - - - -""", + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '', ) # Forms can also define media, following the same rules as widgets. @@ -531,15 +551,17 @@ class FormsMediaTestCase(SimpleTestCase): f3 = FormWithMedia() self.assertEqual( str(f3.media), - """ - - - - - - - -""", + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '', ) # Media works in templates @@ -547,15 +569,17 @@ class FormsMediaTestCase(SimpleTestCase): Template("{{ form.media.js }}{{ form.media.css }}").render( Context({"form": f3}) ), - """ - - - -""" - """ - - -""", + '\n' + '\n' + '\n' + '\n' + '' + '\n' + '\n' + '\n' + '', ) def test_html_safe(self): diff --git a/tests/forms_tests/tests/test_utils.py b/tests/forms_tests/tests/test_utils.py index c4a2e5c651..a921a924b6 100644 --- a/tests/forms_tests/tests/test_utils.py +++ b/tests/forms_tests/tests/test_utils.py @@ -134,7 +134,8 @@ class FormsUtilsTestCase(SimpleTestCase): self.assertHTMLEqual( str(ErrorList([example])), '", + "<a href="http://www.example.com/">example</a>" + "", ) self.assertHTMLEqual( str(ErrorList([mark_safe(example)])), @@ -144,7 +145,8 @@ class FormsUtilsTestCase(SimpleTestCase): self.assertHTMLEqual( str(ErrorDict({"name": example})), '", + "<a href="http://www.example.com/">example</a>" + "", ) self.assertHTMLEqual( str(ErrorDict({"name": mark_safe(example)})), diff --git a/tests/forms_tests/tests/tests.py b/tests/forms_tests/tests/tests.py index 6a3219c60c..a6ebabcc73 100644 --- a/tests/forms_tests/tests/tests.py +++ b/tests/forms_tests/tests/tests.py @@ -66,7 +66,10 @@ class TestTicket14567(TestCase): """ def test_empty_queryset_return(self): - "If a model's ManyToManyField has blank=True and is saved with no data, a queryset is returned." + """ + If a model's ManyToManyField has blank=True and is saved with no data, + a queryset is returned. + """ option = ChoiceOptionModel.objects.create(name="default") form = OptionalMultiChoiceModelForm( {"multi_choice_optional": "", "multi_choice": [option.pk]} @@ -82,7 +85,10 @@ class TestTicket14567(TestCase): class ModelFormCallableModelDefault(TestCase): def test_no_empty_option(self): - "If a model's ForeignKey has blank=False and a default, no empty option is created (Refs #10792)." + """ + If a model's ForeignKey has blank=False and a default, no empty option + is created. + """ option = ChoiceOptionModel.objects.create(name="default") choices = list(ChoiceFieldForm().fields["choice"].choices) @@ -90,34 +96,52 @@ class ModelFormCallableModelDefault(TestCase): self.assertEqual(choices[0], (option.pk, str(option))) def test_callable_initial_value(self): - "The initial value for a callable default returning a queryset is the pk (refs #13769)" + """ + The initial value for a callable default returning a queryset is the + pk. + """ ChoiceOptionModel.objects.create(id=1, name="default") ChoiceOptionModel.objects.create(id=2, name="option 2") ChoiceOptionModel.objects.create(id=3, name="option 3") self.assertHTMLEqual( ChoiceFieldForm().as_p(), - """

    -

    -

    -

    -

    -

    """, + """ +

    + + +

    +

    + + +

    +

    + + +

    +

    + + +

    + """, ) def test_initial_instance_value(self): @@ -136,30 +160,47 @@ class ModelFormCallableModelDefault(TestCase): ), } ).as_p(), - """

    -

    -

    - -

    -

    - -

    """, + """ +

    + + +

    +

    + + +

    +

    + + + +

    +

    + + + +

    + """, ) @@ -194,9 +235,8 @@ class FormsModelTestCase(TestCase): self.assertFalse(f.is_valid()) def test_formfield_initial(self): - # Formfield initial values ######## - # If the model has default values for some fields, they are used as the formfield - # initial values. + # If the model has default values for some fields, they are used as the + # formfield initial values. class DefaultsForm(ModelForm): class Meta: model = Defaults @@ -323,25 +363,32 @@ class EmptyLabelTestCase(TestCase): f = EmptyCharLabelChoiceForm() self.assertHTMLEqual( f.as_p(), - """

    -

    """, + """ +

    +

    +

    +

    + """, ) def test_empty_field_char_none(self): f = EmptyCharLabelNoneChoiceForm() self.assertHTMLEqual( f.as_p(), - """

    -

    -

    """, + """ +

    +

    +

    +

    + """, ) def test_save_empty_label_forms(self): @@ -367,13 +414,16 @@ class EmptyLabelTestCase(TestCase): f = EmptyIntegerLabelChoiceForm() self.assertHTMLEqual( f.as_p(), - """

    -

    -

    """, + """ +

    +

    +

    +

    + """, ) def test_get_display_value_on_none(self): @@ -386,28 +436,36 @@ class EmptyLabelTestCase(TestCase): f = EmptyIntegerLabelChoiceForm(instance=none_model) self.assertHTMLEqual( f.as_p(), - """

    -

    -

    -

    """, + """ +

    + +

    +

    +

    + """, ) foo_model = ChoiceModel(name="foo-test", choice_integer=1) f = EmptyIntegerLabelChoiceForm(instance=foo_model) self.assertHTMLEqual( f.as_p(), - """

    -

    -

    -

    """, + """ +

    + +

    +

    +

    + """, ) diff --git a/tests/forms_tests/widget_tests/test_checkboxinput.py b/tests/forms_tests/widget_tests/test_checkboxinput.py index 0f65e876df..c30aadbbb1 100644 --- a/tests/forms_tests/widget_tests/test_checkboxinput.py +++ b/tests/forms_tests/widget_tests/test_checkboxinput.py @@ -86,7 +86,8 @@ class CheckboxInputTest(WidgetTest): "greeting", "hello & goodbye", html=( - '' + '' ), ) diff --git a/tests/forms_tests/widget_tests/test_checkboxselectmultiple.py b/tests/forms_tests/widget_tests/test_checkboxselectmultiple.py index 0e720022b2..3b8b9e2ff2 100644 --- a/tests/forms_tests/widget_tests/test_checkboxselectmultiple.py +++ b/tests/forms_tests/widget_tests/test_checkboxselectmultiple.py @@ -17,10 +17,14 @@ class CheckboxSelectMultipleTest(WidgetTest): ["J"], html="""
    -
    -
    -
    -
    +
    +
    +
    +
    """, ) @@ -32,10 +36,14 @@ class CheckboxSelectMultipleTest(WidgetTest): ["J", "P"], html="""
    -
    -
    -
    -
    +
    +
    +
    +
    """, ) @@ -51,11 +59,16 @@ class CheckboxSelectMultipleTest(WidgetTest): None, html="""
    -
    -
    -
    -
    -
    +
    +
    +
    +
    +
    """, ) @@ -69,19 +82,24 @@ class CheckboxSelectMultipleTest(WidgetTest): html = """
    + Unknown +
    + + Vinyl
    + CD +
    + VHS +
    + DVD + """ @@ -101,15 +119,21 @@ class CheckboxSelectMultipleTest(WidgetTest): ) html = """
    -
    +
    -
    -
    +
    +
    -
    -
    +
    +
    """ @@ -128,11 +152,14 @@ class CheckboxSelectMultipleTest(WidgetTest): html = """
    - +
    -
    +
    - +
    """ @@ -154,11 +181,14 @@ class CheckboxSelectMultipleTest(WidgetTest): html = """
    - +
    -
    +
    - +
    """ @@ -174,8 +204,10 @@ class CheckboxSelectMultipleTest(WidgetTest): html = """
    -
    -
    +
    +
    """ self.check_html(self.widget(choices=choices), "numbers", None, html=html) @@ -186,8 +218,10 @@ class CheckboxSelectMultipleTest(WidgetTest): ] html = """
    -
    -
    +
    +
    """ self.check_html(self.widget(choices=choices), "times", None, html=html) diff --git a/tests/forms_tests/widget_tests/test_clearablefileinput.py b/tests/forms_tests/widget_tests/test_clearablefileinput.py index 03fe34306b..630ff79486 100644 --- a/tests/forms_tests/widget_tests/test_clearablefileinput.py +++ b/tests/forms_tests/widget_tests/test_clearablefileinput.py @@ -56,12 +56,14 @@ class ClearableFileInputTest(WidgetTest): StrangeFieldFile(), html=( """ - Currently: - something<div onclick="alert('oops')">.jpg - -
    - Change: - """ + Currently: + + something<div onclick="alert('oops')">.jpg + +
    + Change: + """ ), ) diff --git a/tests/forms_tests/widget_tests/test_datetimeinput.py b/tests/forms_tests/widget_tests/test_datetimeinput.py index 5795660f30..e9bb77eb69 100644 --- a/tests/forms_tests/widget_tests/test_datetimeinput.py +++ b/tests/forms_tests/widget_tests/test_datetimeinput.py @@ -85,7 +85,10 @@ class DateTimeInputTest(WidgetTest): self.widget, "date", d, - html='', + html=( + '' + ), ) with translation.override("es"): self.check_html( diff --git a/tests/forms_tests/widget_tests/test_multiplehiddeninput.py b/tests/forms_tests/widget_tests/test_multiplehiddeninput.py index d0ad188929..9498b756e9 100644 --- a/tests/forms_tests/widget_tests/test_multiplehiddeninput.py +++ b/tests/forms_tests/widget_tests/test_multiplehiddeninput.py @@ -31,7 +31,10 @@ class MultipleHiddenInputTest(WidgetTest): "email", ["test@example.com"], attrs={"class": "fun"}, - html='', + html=( + '' + ), ) def test_render_attrs_multiple(self): @@ -41,7 +44,8 @@ class MultipleHiddenInputTest(WidgetTest): ["test@example.com", "foo@example.com"], attrs={"class": "fun"}, html=( - '\n' + '\n' '' ), ) @@ -53,15 +57,19 @@ class MultipleHiddenInputTest(WidgetTest): widget, "email", ["foo@example.com"], - html='', + html=( + '' + ), ) self.check_html( widget, "email", ["foo@example.com", "test@example.com"], html=( - '\n' - '' + '\n' + '' ), ) self.check_html( @@ -69,7 +77,10 @@ class MultipleHiddenInputTest(WidgetTest): "email", ["foo@example.com"], attrs={"class": "special"}, - html='', + html=( + '' + ), ) def test_render_empty(self): diff --git a/tests/forms_tests/widget_tests/test_multiwidget.py b/tests/forms_tests/widget_tests/test_multiwidget.py index c3f676e3f7..a89082c4a6 100644 --- a/tests/forms_tests/widget_tests/test_multiwidget.py +++ b/tests/forms_tests/widget_tests/test_multiwidget.py @@ -143,7 +143,8 @@ class MultiWidgetTest(WidgetTest): attrs={"id": "foo"}, html=( '' - '' + '' ), ) @@ -161,7 +162,8 @@ class MultiWidgetTest(WidgetTest): ["john", "lennon"], html=( '' - '' + '' ), ) diff --git a/tests/forms_tests/widget_tests/test_numberinput.py b/tests/forms_tests/widget_tests/test_numberinput.py index a6427e6e37..15df02db4f 100644 --- a/tests/forms_tests/widget_tests/test_numberinput.py +++ b/tests/forms_tests/widget_tests/test_numberinput.py @@ -12,5 +12,6 @@ class NumberInputTests(WidgetTest): widget, "name", "value", - '', + '', ) diff --git a/tests/forms_tests/widget_tests/test_radioselect.py b/tests/forms_tests/widget_tests/test_radioselect.py index 89e4022f1c..5aa4c194de 100644 --- a/tests/forms_tests/widget_tests/test_radioselect.py +++ b/tests/forms_tests/widget_tests/test_radioselect.py @@ -18,9 +18,11 @@ class RadioSelectTest(WidgetTest): html="""
    -
    +
    -
    +
    """, @@ -35,19 +37,28 @@ class RadioSelectTest(WidgetTest): html = """
    -
    +
    -
    -
    +
    +
    -
    +
    - -
    + """ @@ -68,11 +79,15 @@ class RadioSelectTest(WidgetTest): html = """
    - +
    -
    -
    -
    +
    +
    +
    """ self.check_html(widget, "beatle", "J", html=html) @@ -85,11 +100,15 @@ class RadioSelectTest(WidgetTest): html = """
    - +
    -
    -
    -
    +
    +
    +
    """ self.check_html( @@ -107,10 +126,15 @@ class RadioSelectTest(WidgetTest): """ html = """
    -
    -
    -
    -
    +
    +
    +
    +
    +
    """ self.check_html( @@ -131,8 +155,10 @@ class RadioSelectTest(WidgetTest): html = """
    -
    -
    +
    +
    """ self.check_html(self.widget(choices=choices), "number", None, html=html) @@ -143,8 +169,10 @@ class RadioSelectTest(WidgetTest): ] html = """
    -
    -
    +
    +
    """ self.check_html(self.widget(choices=choices), "time", None, html=html) @@ -158,11 +186,16 @@ class RadioSelectTest(WidgetTest): ["J"], html="""
    -
    -
    -
    -
    -
    +
    +
    +
    +
    +
    """, ) diff --git a/tests/forms_tests/widget_tests/test_select.py b/tests/forms_tests/widget_tests/test_select.py index e78feeb110..8a40bd8b6a 100644 --- a/tests/forms_tests/widget_tests/test_select.py +++ b/tests/forms_tests/widget_tests/test_select.py @@ -227,12 +227,16 @@ class SelectTest(WidgetTest): "email", "ŠĐĆŽćžšđ", html=( - """ + - - """ + + + """ ), ) diff --git a/tests/forms_tests/widget_tests/test_selectdatewidget.py b/tests/forms_tests/widget_tests/test_selectdatewidget.py index de0c35cd5b..9192e4c1f1 100644 --- a/tests/forms_tests/widget_tests/test_selectdatewidget.py +++ b/tests/forms_tests/widget_tests/test_selectdatewidget.py @@ -520,7 +520,8 @@ class SelectDateWidgetTest(WidgetTest): """, ) - # Even with an invalid date, the widget should reflect the entered value (#17401). + # Even with an invalid date, the widget should reflect the entered + # value. self.assertEqual(w.render("mydate", "2010-02-30").count("selected"), 3) # Years before 1900 should work. diff --git a/tests/forms_tests/widget_tests/test_textarea.py b/tests/forms_tests/widget_tests/test_textarea.py index e92dfc51f4..e7695ab018 100644 --- a/tests/forms_tests/widget_tests/test_textarea.py +++ b/tests/forms_tests/widget_tests/test_textarea.py @@ -47,7 +47,8 @@ class TextareaTest(WidgetTest): "msg", 'some "quoted" & ampersanded value', html=( - '' + '" ), ) @@ -57,6 +58,7 @@ class TextareaTest(WidgetTest): "msg", mark_safe("pre "quoted" value"), html=( - '' + '" ), ) diff --git a/tests/forms_tests/widget_tests/test_textinput.py b/tests/forms_tests/widget_tests/test_textinput.py index 52ffabec65..efd0cfed4b 100644 --- a/tests/forms_tests/widget_tests/test_textinput.py +++ b/tests/forms_tests/widget_tests/test_textinput.py @@ -48,7 +48,10 @@ class TextInputTest(WidgetTest): self.widget, "email", 'some "quoted" & ampersanded value', - html='', + html=( + '' + ), ) def test_render_custom_attrs(self): @@ -57,7 +60,9 @@ class TextInputTest(WidgetTest): "email", "test@example.com", attrs={"class": "fun"}, - html='', + html=( + '' + ), ) def test_render_unicode(self): @@ -81,7 +86,9 @@ class TextInputTest(WidgetTest): widget, "email", "foo@example.com", - html='', + html=( + '' + ), ) def test_attrs_precedence(self): -- cgit v1.3