summaryrefslogtreecommitdiff
path: root/tests/model_forms
diff options
context:
space:
mode:
authordjango-bot <ops@djangoproject.com>2025-07-22 20:41:41 -0700
committernessita <124304+nessita@users.noreply.github.com>2025-07-23 20:17:55 -0300
commit69a93a88edb56ba47f624dac7a21aacc47ea474f (patch)
treef57507a4435d032493cae40e06ecb254790b67b2 /tests/model_forms
parent55b0cc21310b76ce4018dd793ba50556eaf0af06 (diff)
Refs #36500 -- Rewrapped long docstrings and block comments via a script.
Rewrapped long docstrings and block comments to 79 characters + newline using script from https://github.com/medmunds/autofix-w505.
Diffstat (limited to 'tests/model_forms')
-rw-r--r--tests/model_forms/test_modelchoicefield.py6
-rw-r--r--tests/model_forms/tests.py121
2 files changed, 68 insertions, 59 deletions
diff --git a/tests/model_forms/test_modelchoicefield.py b/tests/model_forms/test_modelchoicefield.py
index 83d801768a..8765f1a6d0 100644
--- a/tests/model_forms/test_modelchoicefield.py
+++ b/tests/model_forms/test_modelchoicefield.py
@@ -52,9 +52,9 @@ class ModelChoiceFieldTests(TestCase):
c4 = Category.objects.create(name="Fourth", url="4th")
self.assertEqual(f.clean(c4.id).name, "Fourth")
- # Delete a Category object *after* the ModelChoiceField has already been
- # instantiated. This proves clean() checks the database during clean()
- # rather than caching it at instantiation time.
+ # Delete a Category object *after* the ModelChoiceField has already
+ # been instantiated. This proves clean() checks the database during
+ # clean() rather than caching it at instantiation time.
Category.objects.get(url="4th").delete()
msg = (
"['Select a valid choice. That choice is not one of the available "
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
index e7bdd1ac89..f0334e1e86 100644
--- a/tests/model_forms/tests.py
+++ b/tests/model_forms/tests.py
@@ -236,7 +236,8 @@ class ModelFormBaseTest(TestCase):
def test_empty_fields_to_fields_for_model(self):
"""
- An argument of fields=() to fields_for_model should return an empty dictionary
+ An argument of fields=() to fields_for_model should return an empty
+ dictionary
"""
field_dict = fields_for_model(Person, fields=())
self.assertEqual(len(field_dict), 0)
@@ -395,7 +396,8 @@ class ModelFormBaseTest(TestCase):
self.assertEqual(form.instance.slug, empty_value)
self.assertEqual(form.instance.url, empty_value)
- # Save a second form to verify there isn't a unique constraint violation.
+ # Save a second form to verify there isn't a unique constraint
+ # violation.
form = form_class(data=data)
self.assertTrue(form.is_valid())
form.save()
@@ -553,9 +555,9 @@ class ModelFormBaseTest(TestCase):
exclude = "url" # note the missing comma
def test_exclude_and_validation(self):
- # This Price instance generated by this form is not valid because the quantity
- # field is required, but the form is valid because the field is excluded from
- # the form. This is for backwards compatibility.
+ # This Price instance generated by this form is not valid because the
+ # quantity field is required, but the form is valid because the field
+ # is excluded from the form. This is for backwards compatibility.
class PriceFormWithoutQuantity(forms.ModelForm):
class Meta:
model = Price
@@ -568,8 +570,8 @@ class ModelFormBaseTest(TestCase):
with self.assertRaisesMessage(ValidationError, msg):
price.full_clean()
- # The form should not validate fields that it doesn't contain even if they are
- # specified using 'fields', not 'exclude'.
+ # The form should not validate fields that it doesn't contain even if
+ # they are specified using 'fields', not 'exclude'.
class PriceFormWithoutQuantity(forms.ModelForm):
class Meta:
model = Price
@@ -578,8 +580,8 @@ class ModelFormBaseTest(TestCase):
form = PriceFormWithoutQuantity({"price": "6.00"})
self.assertTrue(form.is_valid())
- # The form should still have an instance of a model that is not complete and
- # not saved into a DB yet.
+ # The form should still have an instance of a model that is not
+ # complete and not saved into a DB yet.
self.assertEqual(form.instance.price, Decimal("6.00"))
self.assertIsNone(form.instance.quantity)
self.assertIsNone(form.instance.pk)
@@ -610,8 +612,8 @@ class ModelFormBaseTest(TestCase):
model = Article
fields = "__all__"
- # MixModelForm is now an Article-related thing, because MixModelForm.Meta
- # overrides BaseCategoryForm.Meta.
+ # MixModelForm is now an Article-related thing, because
+ # MixModelForm.Meta overrides BaseCategoryForm.Meta.
self.assertEqual(
list(MixModelForm.base_fields),
@@ -1236,7 +1238,9 @@ class UniqueTest(TestCase):
self.assertFalse(form.is_valid())
def test_explicitpk_unique(self):
- """Ensure keys and blank character strings are tested for uniqueness."""
+ """
+ Ensure keys and blank character strings are tested for uniqueness.
+ """
form = ExplicitPKForm({"key": "key1", "desc": ""})
self.assertTrue(form.is_valid())
form.save()
@@ -1547,8 +1551,8 @@ class ModelFormBasicTests(TestCase):
% (self.w_woodward.pk, self.w_royko.pk, self.c1.pk, self.c2.pk, self.c3.pk),
)
- # When the ModelForm is passed an instance, that instance's current values are
- # inserted as 'initial' data in each Field.
+ # When the ModelForm is passed an instance, that instance's current
+ # values are inserted as 'initial' data in each Field.
f = RoykoForm(auto_id=False, instance=self.w_royko)
self.assertHTMLEqual(
str(f),
@@ -1631,7 +1635,8 @@ class ModelFormBasicTests(TestCase):
kwargs["initial"] = lambda: Category.objects.order_by("name")[:2]
return db_field.formfield(**kwargs)
- # Create a ModelForm, instantiate it, and check that the output is as expected
+ # Create a ModelForm, instantiate it, and check that the output is as
+ # expected
ModelForm = modelform_factory(
Article,
fields=["headline", "categories"],
@@ -1673,9 +1678,9 @@ class ModelFormBasicTests(TestCase):
self.assertEqual(c1.name, "Entertainment")
def test_save_commit_false(self):
- # If you call save() with commit=False, then it will return an object that
- # hasn't yet been saved to the database. In this case, it's up to you to call
- # save() on the resulting model instance.
+ # If you call save() with commit=False, then it will return an object
+ # that hasn't yet been saved to the database. In this case, it's up to
+ # you to call save() on the resulting model instance.
f = BaseCategoryForm(
{"name": "Third test", "slug": "third-test", "url": "third"}
)
@@ -1708,8 +1713,9 @@ class ModelFormBasicTests(TestCase):
def test_multi_fields(self):
self.create_basic_data()
self.maxDiff = None
- # ManyToManyFields are represented by a MultipleChoiceField, ForeignKeys and any
- # fields with the 'choices' attribute are represented by a ChoiceField.
+ # ManyToManyFields are represented by a MultipleChoiceField,
+ # ForeignKeys and any fields with the 'choices' attribute are
+ # represented by a ChoiceField.
f = ArticleForm(auto_id=False)
self.assertHTMLEqual(
str(f),
@@ -1800,9 +1806,9 @@ class ModelFormBasicTests(TestCase):
# You can restrict a form to a subset of the complete list of fields
# by providing a 'fields' argument. If you try to save a
# model created with such a form, you need to ensure that the fields
- # that are _not_ on the form have default values, or are allowed to have
- # a value of None. If a field isn't specified on a form, the object created
- # from the form can't provide a value for that field!
+ # that are _not_ on the form have default values, or are allowed to
+ # have a value of None. If a field isn't specified on a form, the
+ # object created from the form can't provide a value for that field!
class PartialArticleForm(forms.ModelForm):
class Meta:
model = Article
@@ -1894,8 +1900,9 @@ class ModelFormBasicTests(TestCase):
new_art = Article.objects.get(id=art_id_2)
self.assertSequenceEqual(new_art.categories.all(), [])
- # Create a new article, with categories, via the form, but use commit=False.
- # The m2m data won't be saved until save_m2m() is invoked on the form.
+ # Create a new article, with categories, via the form, but use
+ # commit=False. The m2m data won't be saved until save_m2m() is invoked
+ # on the form.
form_data["categories"] = [str(self.c1.id), str(self.c2.id)]
f = ArticleForm(form_data)
new_art = f.save(commit=False)
@@ -1937,9 +1944,10 @@ class ModelFormBasicTests(TestCase):
def test_runtime_choicefield_populated(self):
self.maxDiff = None
- # Here, we demonstrate that choices for a ForeignKey ChoiceField are determined
- # at runtime, based on the data in the database when the form is displayed, not
- # the data in the database when the form is instantiated.
+ # Here, we demonstrate that choices for a ForeignKey ChoiceField are
+ # determined at runtime, based on the data in the database when the
+ # form is displayed, not the data in the database when the form is
+ # instantiated.
self.create_basic_data()
f = ArticleForm(auto_id=False)
self.assertHTMLEqual(
@@ -2162,18 +2170,18 @@ class ModelMultipleChoiceFieldTests(TestCase):
with self.assertRaises(ValidationError):
f.clean([{"foo": "bar"}])
- # Add a Category object *after* the ModelMultipleChoiceField has already been
- # instantiated. This proves clean() checks the database during clean() rather
- # than caching it at time of instantiation.
- # Note, we are using an id of 1006 here since tests that run before
- # this may create categories with primary keys up to 6. Use
- # a number that will not conflict.
+ # Add a Category object *after* the ModelMultipleChoiceField has
+ # already been instantiated. This proves clean() checks the database
+ # during clean() rather than caching it at time of instantiation. Note,
+ # we are using an id of 1006 here since tests that run before this may
+ # create categories with primary keys up to 6. Use a number that will
+ # not conflict.
c6 = Category.objects.create(id=1006, name="Sixth", url="6th")
self.assertCountEqual(f.clean([c6.id]), [c6])
- # Delete a Category object *after* the ModelMultipleChoiceField has already been
- # instantiated. This proves clean() checks the database during clean() rather
- # than caching it at time of instantiation.
+ # Delete a Category object *after* the ModelMultipleChoiceField has
+ # already been instantiated. This proves clean() checks the database
+ # during clean() rather than caching it at time of instantiation.
Category.objects.get(url="6th").delete()
with self.assertRaises(ValidationError):
f.clean([c6.id])
@@ -2456,8 +2464,8 @@ class ModelOneToOneFieldTests(TestCase):
self.assertTrue(form.is_valid())
self.assertIsNone(form.cleaned_data["publication"])
author = form.save()
- # author object returned from form still retains original publication object
- # that's why we need to retrieve it from database again
+ # author object returned from form still retains original publication
+ # object that's why we need to retrieve it from database again
new_author = Author.objects.get(pk=author.pk)
self.assertIsNone(new_author.publication)
@@ -2607,8 +2615,8 @@ class FileAndImageFieldTests(TestCase):
)
self.assertFalse(f.is_valid())
- # Edit an instance that already has the file defined in the model. This will not
- # save the file again, but leave it exactly as it is.
+ # Edit an instance that already has the file defined in the model. This
+ # will not save the file again, but leave it exactly as it is.
f = TextFileForm({"description": "Assistance"}, instance=instance)
self.assertTrue(f.is_valid())
self.assertEqual(f.cleaned_data["file"].name, "tests/test1.txt")
@@ -2672,8 +2680,9 @@ class FileAndImageFieldTests(TestCase):
model = CustomFF
fields = "__all__"
- # It's enough that the form saves without error -- the custom save routine will
- # generate an AssertionError if it is called more than once during save.
+ # It's enough that the form saves without error -- the custom save
+ # routine will generate an AssertionError if it is called more than
+ # once during save.
form = CFFForm(data={"f": None})
form.save()
@@ -2723,9 +2732,9 @@ class FileAndImageFieldTests(TestCase):
@skipUnless(test_images, "Pillow not installed")
def test_image_field(self):
- # ImageField and FileField are nearly identical, but they differ slightly when
- # it comes to validation. This specifically tests that #6302 is fixed for
- # both file fields and image fields.
+ # ImageField and FileField are nearly identical, but they differ
+ # slightly when it comes to validation. This specifically tests that
+ # #6302 is fixed for both file fields and image fields.
with open(os.path.join(os.path.dirname(__file__), "test.png"), "rb") as fp:
image_data = fp.read()
@@ -2743,8 +2752,8 @@ class FileAndImageFieldTests(TestCase):
self.assertEqual(instance.width, 16)
self.assertEqual(instance.height, 16)
- # Delete the current file since this is not done by Django, but don't save
- # because the dimension fields are not null=True.
+ # Delete the current file since this is not done by Django, but don't
+ # save because the dimension fields are not null=True.
instance.image.delete(save=False)
f = ImageFileForm(
data={"description": "An image"},
@@ -2769,8 +2778,8 @@ class FileAndImageFieldTests(TestCase):
self.assertEqual(instance.height, 16)
self.assertEqual(instance.width, 16)
- # Delete the current file since this is not done by Django, but don't save
- # because the dimension fields are not null=True.
+ # Delete the current file since this is not done by Django, but don't
+ # save because the dimension fields are not null=True.
instance.image.delete(save=False)
# Override the file by uploading a new one.
@@ -2785,8 +2794,8 @@ class FileAndImageFieldTests(TestCase):
self.assertEqual(instance.height, 32)
self.assertEqual(instance.width, 48)
- # Delete the current file since this is not done by Django, but don't save
- # because the dimension fields are not null=True.
+ # Delete the current file since this is not done by Django, but don't
+ # save because the dimension fields are not null=True.
instance.image.delete(save=False)
instance.delete()
@@ -2800,8 +2809,8 @@ class FileAndImageFieldTests(TestCase):
self.assertEqual(instance.height, 32)
self.assertEqual(instance.width, 48)
- # Delete the current file since this is not done by Django, but don't save
- # because the dimension fields are not null=True.
+ # Delete the current file since this is not done by Django, but don't
+ # save because the dimension fields are not null=True.
instance.image.delete(save=False)
instance.delete()
@@ -2975,8 +2984,8 @@ class ModelOtherFieldTests(SimpleTestCase):
class OtherModelFormTests(TestCase):
def test_media_on_modelform(self):
- # Similar to a regular Form class you can define custom media to be used on
- # the ModelForm.
+ # Similar to a regular Form class you can define custom media to be
+ # used on the ModelForm.
f = ModelFormWithMedia()
self.assertHTMLEqual(
str(f.media),