diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-02-08 16:38:55 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-02-08 16:39:53 +0100 |
| commit | 836ae73a8962876a3beb98bc711fa397d700c82f (patch) | |
| tree | 43b2832783a9fb1b03bc2cd120a1c6fb1b123c28 /tests | |
| parent | 1f193f7f56d0f83c188dca8b03266fbaa339d0f5 (diff) | |
[4.2.x] Fixed #34319 -- Fixed Model.validate_constraints() crash on ValidationError with no code.
Thanks Mateusz Kurowski for the report.
Regression in 667105877e6723c6985399803a364848891513cc.
Backport of 2fd755b361d3da2cd0440fc9839feb2bb69b027b from main
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/constraints/tests.py | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py index 223b4b3cd5..e52d15233c 100644 --- a/tests/constraints/tests.py +++ b/tests/constraints/tests.py @@ -3,7 +3,7 @@ from unittest import mock from django.core.exceptions import ValidationError from django.db import IntegrityError, connection, models from django.db.models import F -from django.db.models.constraints import BaseConstraint +from django.db.models.constraints import BaseConstraint, UniqueConstraint from django.db.models.functions import Lower from django.db.transaction import atomic from django.test import SimpleTestCase, TestCase, skipIfDBFeature, skipUnlessDBFeature @@ -589,6 +589,26 @@ class UniqueConstraintTests(TestCase): with self.assertRaisesMessage(ValidationError, msg): UniqueConstraintConditionProduct(name=obj2.name).validate_constraints() + def test_model_validation_constraint_no_code_error(self): + class ValidateNoCodeErrorConstraint(UniqueConstraint): + def validate(self, model, instance, **kwargs): + raise ValidationError({"name": ValidationError("Already exists.")}) + + class NoCodeErrorConstraintModel(models.Model): + name = models.CharField(max_length=255) + + class Meta: + constraints = [ + ValidateNoCodeErrorConstraint( + Lower("name"), + name="custom_validate_no_code_error", + ) + ] + + msg = "{'name': ['Already exists.']}" + with self.assertRaisesMessage(ValidationError, msg): + NoCodeErrorConstraintModel(name="test").validate_constraints() + def test_validate(self): constraint = UniqueConstraintProduct._meta.constraints[0] msg = "Unique constraint product with this Name and Color already exists." |
