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/model_forms/tests.py | 384 ++++++++++++++++++++++++++++----------------- 1 file changed, 240 insertions(+), 144 deletions(-) (limited to 'tests/model_forms/tests.py') diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 096c3aae63..a615d4516d 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -251,7 +251,8 @@ class ModelFormBaseTest(TestCase): def test_empty_fields_to_construct_instance(self): """ - No fields should be set on a model instance if construct_instance receives fields=(). + No fields should be set on a model instance if construct_instance + receives fields=(). """ form = modelform_factory(Person, fields="__all__")({"name": "John Doe"}) self.assertTrue(form.is_valid()) @@ -498,8 +499,11 @@ class ModelFormBaseTest(TestCase): fields = ["nonexistent"] def test_limit_fields_with_string(self): - expected_msg = "CategoryForm.Meta.fields cannot be a string. Did you mean to type: ('url',)?" - with self.assertRaisesMessage(TypeError, expected_msg): + msg = ( + "CategoryForm.Meta.fields cannot be a string. Did you mean to type: " + "('url',)?" + ) + with self.assertRaisesMessage(TypeError, msg): class CategoryForm(forms.ModelForm): class Meta: @@ -523,8 +527,11 @@ class ModelFormBaseTest(TestCase): self.assertEqual(list(ExcludeFields.base_fields), ["name", "slug", "url"]) def test_exclude_fields_with_string(self): - expected_msg = "CategoryForm.Meta.exclude cannot be a string. Did you mean to type: ('url',)?" - with self.assertRaisesMessage(TypeError, expected_msg): + msg = ( + "CategoryForm.Meta.exclude cannot be a string. Did you mean to type: " + "('url',)?" + ) + with self.assertRaisesMessage(TypeError, msg): class CategoryForm(forms.ModelForm): class Meta: @@ -923,11 +930,13 @@ class TestFieldOverridesByFormMeta(SimpleTestCase): form = FieldOverridesByFormMetaForm() self.assertHTMLEqual( str(form["name"]), - '', + '", ) self.assertHTMLEqual( str(form["url"]), - '', + '", ) self.assertHTMLEqual( str(form["slug"]), @@ -1380,7 +1389,9 @@ class UniqueTest(TestCase): class Meta(PriceForm.Meta): error_messages = { NON_FIELD_ERRORS: { - "unique_together": "%(model_name)s's %(field_labels)s not unique.", + "unique_together": ( + "%(model_name)s's %(field_labels)s not unique." + ), } } @@ -1446,9 +1457,14 @@ class ModelFormBasicTests(TestCase): ) self.assertHTMLEqual( str(f.as_ul()), - """
  • -
  • -
  • """, + """ +
  • +
  • +
  • +
  • +
  • +
  • + """, ) self.assertHTMLEqual( str(f["name"]), @@ -1476,26 +1492,32 @@ class ModelFormBasicTests(TestCase): ) self.assertHTMLEqual( f.as_ul(), - """
  • Headline:
  • -
  • Slug:
  • -
  • Pub date:
  • -
  • Writer:
  • -
  • Article:
  • -
  • Categories:
  • -
  • Status:
  • """ + """ +
  • Headline: + +
  • +
  • Slug:
  • +
  • Pub date:
  • +
  • Writer:
  • +
  • Article: +
  • +
  • Categories:
  • +
  • Status:
  • + """ % (self.w_woodward.pk, self.w_royko.pk, self.c1.pk, self.c2.pk, self.c3.pk), ) @@ -1504,8 +1526,12 @@ class ModelFormBasicTests(TestCase): f = RoykoForm(auto_id=False, instance=self.w_royko) self.assertHTMLEqual( str(f), - """Name:
    - Use both first and last names.""", + """ + Name: + +
    + Use both first and last names. + """, ) art = Article.objects.create( @@ -1520,26 +1546,35 @@ class ModelFormBasicTests(TestCase): f = ArticleForm(auto_id=False, instance=art) self.assertHTMLEqual( f.as_ul(), - """
  • Headline:
  • -
  • Slug:
  • -
  • Pub date:
  • -
  • Writer:
  • -
  • Article:
  • -
  • Categories:
  • -
  • Status:
  • """ + """ +
  • Headline: + +
  • +
  • Slug: + +
  • +
  • Pub date: +
  • +
  • Writer:
  • +
  • Article: +
  • +
  • Categories:
  • +
  • Status:
  • + """ % (self.w_woodward.pk, self.w_royko.pk, self.c1.pk, self.c2.pk, self.c3.pk), ) @@ -1562,7 +1597,7 @@ class ModelFormBasicTests(TestCase): def test_m2m_initial_callable(self): """ - Regression for #10349: A callable can be provided as the initial value for an m2m field + A callable can be provided as the initial value for an m2m field. """ self.maxDiff = 1200 self.create_basic_data() @@ -1635,7 +1670,8 @@ class ModelFormBasicTests(TestCase): self.assertEqual( f.errors["slug"], [ - "Enter a valid “slug” consisting of letters, numbers, underscores or hyphens." + "Enter a valid “slug” consisting of letters, numbers, underscores or " + "hyphens." ], ) self.assertEqual(f.cleaned_data, {"url": "foo"}) @@ -1654,26 +1690,32 @@ class ModelFormBasicTests(TestCase): f = ArticleForm(auto_id=False) self.assertHTMLEqual( str(f), - """Headline: -Slug: -Pub date: -Writer: -Article: -Categories: -Status:""" + """ + Headline: + + Slug: + + Pub date: + + Writer: + Article: + + Categories: + Status: + """ % (self.w_woodward.pk, self.w_royko.pk, self.c1.pk, self.c2.pk, self.c3.pk), ) @@ -1690,26 +1732,35 @@ class ModelFormBasicTests(TestCase): f = ArticleForm(auto_id=False, instance=new_art) self.assertHTMLEqual( f.as_ul(), - """
  • Headline:
  • -
  • Slug:
  • -
  • Pub date:
  • -
  • Writer:
  • -
  • Article:
  • -
  • Categories:
  • -
  • Status:
  • """ + """ +
  • Headline: + +
  • +
  • Slug: + +
  • +
  • Pub date: +
  • +
  • Writer:
  • +
  • Article: +
  • +
  • Categories:
  • +
  • Status:
  • + """ % (self.w_woodward.pk, self.w_royko.pk, self.c1.pk, self.c2.pk, self.c3.pk), ) @@ -1728,8 +1779,12 @@ class ModelFormBasicTests(TestCase): f = PartialArticleForm(auto_id=False) self.assertHTMLEqual( str(f), - """Headline: -Pub date:""", + """ + Headline: + + Pub date: + + """, ) class PartialArticleFormWithSlug(forms.ModelForm): @@ -1756,9 +1811,18 @@ class ModelFormBasicTests(TestCase): ) self.assertHTMLEqual( f.as_ul(), - """
  • Headline:
  • -
  • Slug:
  • -
  • Pub date:
  • """, + """ +
  • Headline: + +
  • +
  • Slug: + +
  • +
  • Pub date: +
  • + """, ) self.assertTrue(f.is_valid()) new_art = f.save() @@ -1785,7 +1849,8 @@ class ModelFormBasicTests(TestCase): new_art.categories.order_by("name"), [self.c1, self.c2] ) - # Now, submit form data with no categories. This deletes the existing categories. + # Now, submit form data with no categories. This deletes the existing + # categories. form_data["categories"] = [] f = ArticleForm(form_data, instance=new_art) new_art = f.save() @@ -1823,9 +1888,9 @@ class ModelFormBasicTests(TestCase): ) def test_custom_form_fields(self): - # Here, we define a custom ModelForm. Because it happens to have the same fields as - # the Category model, we can just call the form's save() to apply its changes to an - # existing Category instance. + # Here, we define a custom ModelForm. Because it happens to have the + # same fields as the Category model, we can just call the form's save() + # to apply its changes to an existing Category instance. class ShortCategory(forms.ModelForm): name = forms.CharField(max_length=5) slug = forms.CharField(max_length=5) @@ -1949,7 +2014,8 @@ class ModelFormBasicTests(TestCase): form.errors, { "writer": [ - "Select a valid choice. That choice is not one of the available choices." + "Select a valid choice. That choice is not one of the available " + "choices." ] }, ) @@ -2222,7 +2288,8 @@ class ModelOneToOneFieldTests(TestCase): def test_modelform_subclassed_model(self): class BetterWriterForm(forms.ModelForm): class Meta: - # BetterWriter model is a subclass of Writer with an additional `score` field + # BetterWriter model is a subclass of Writer with an additional + # `score` field. model = BetterWriter fields = "__all__" @@ -2259,12 +2326,16 @@ class ModelOneToOneFieldTests(TestCase): form = WriterProfileForm() self.assertHTMLEqual( form.as_p(), - """

    -

    """ + """ +

    +

    +

    +

    + """ % ( self.w_woodward.pk, self.w_royko.pk, @@ -2282,13 +2353,16 @@ class ModelOneToOneFieldTests(TestCase): form = WriterProfileForm(instance=instance) self.assertHTMLEqual( form.as_p(), - """

    -

    -

    """ + """ +

    +

    +

    + +

    """ % ( self.w_woodward.pk, self.w_royko.pk, @@ -2496,7 +2570,8 @@ class FileAndImageFieldTests(TestCase): instance = f.save() self.assertEqual(instance.file.name, "tests/test3.txt") - # Instance can be edited w/out re-uploading the file and existing file should be preserved. + # Instance can be edited w/out re-uploading the file and existing file + # should be preserved. f = TextFileForm({"description": "New Description"}, instance=instance) f.fields["file"].required = False self.assertTrue(f.is_valid()) @@ -2603,8 +2678,9 @@ class FileAndImageFieldTests(TestCase): self.assertEqual(instance.width, 16) self.assertEqual(instance.height, 16) - # Edit an instance that already has the (required) image defined in the model. This will not - # save the image again, but leave it exactly as it is. + # Edit an instance that already has the (required) image defined in the + # model. This will not save the image again, but leave it exactly as it + # is. f = ImageFileForm(data={"description": "Look, it changed"}, instance=instance) self.assertTrue(f.is_valid()) @@ -2700,7 +2776,8 @@ class FileAndImageFieldTests(TestCase): self.assertEqual(instance.width, 48) self.assertEqual(instance.height, 32) instance.delete() - # Test callable upload_to behavior that's dependent on the value of another field in the model + # Callable upload_to behavior that's dependent on the value of another + # field in the model. f = ImageFileForm( data={"description": "And a final one", "path": "foo"}, files={"image": SimpleUploadedFile("test4.png", image_data)}, @@ -2736,7 +2813,8 @@ class ModelOtherFieldTests(SimpleTestCase): bif.errors, { "biggie": [ - "Ensure this value is greater than or equal to -9223372036854775808." + "Ensure this value is greater than or equal to " + "-9223372036854775808." ] }, ) @@ -2785,7 +2863,10 @@ class ModelOtherFieldTests(SimpleTestCase): # 'created', non-editable, is excluded by default self.assertNotIn("created", ArticleForm().fields) - msg = "'created' cannot be specified for Article model form as it is a non-editable field" + msg = ( + "'created' cannot be specified for Article model form as it is a " + "non-editable field" + ) with self.assertRaisesMessage(FieldError, msg): class InvalidArticleForm(forms.ModelForm): @@ -2795,7 +2876,7 @@ class ModelOtherFieldTests(SimpleTestCase): def test_http_prefixing(self): """ - If the http:// prefix is omitted on form input, the field adds it again. (Refs #13613) + If the http:// prefix is omitted on form input, the field adds it again. """ class HomepageForm(forms.ModelForm): @@ -2936,11 +3017,15 @@ class OtherModelFormTests(TestCase): self.maxDiff = 1024 self.assertHTMLEqual( form.as_p(), - """

    -

    -

    """ + """ +

    + +

    +

    +

    + """ % {"blue_pk": colour.pk}, ) @@ -2957,19 +3042,27 @@ class OtherModelFormTests(TestCase): form.as_p(), """

    -

    + +

    - -

    + + +

    -

    -

    - + + + +

    +

    + + + """.format( today_str ), @@ -3013,7 +3106,8 @@ class ModelFormCustomErrorTests(SimpleTestCase): self.assertFalse(form.is_valid()) self.assertHTMLEqual( str(form.errors["name1"]), - '

    ', + '", ) data = {"name1": "GLOBAL_ERROR", "name2": "ABC"} form = CustomErrorMessageForm(data) @@ -3289,7 +3383,9 @@ class LimitChoicesToTests(TestCase): class FormFieldCallbackTests(SimpleTestCase): def test_baseform_with_widgets_in_meta(self): - """Regression for #13095: Using base forms with widgets defined in Meta should not raise errors.""" + """ + Using base forms with widgets defined in Meta should not raise errors. + """ widget = forms.Textarea() class BaseForm(forms.ModelForm): -- cgit v1.3