From 0e49a8c3bd9119795525d9f076f73740741479b7 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sat, 13 Jul 2024 22:15:21 -0400 Subject: [PATCH] Refs #34701 -- Moved UniqueConstraint(nulls_distinct) validation tests. The original tests required the creation of a model that is no longer necessary and were exercising Model.full_clean(validate_constraints) which has nothing to do with the nulls_distinct feature. --- tests/constraints/tests.py | 13 +++++++++++++ tests/validation/models.py | 14 -------------- tests/validation/test_constraints.py | 23 ----------------------- 3 files changed, 13 insertions(+), 37 deletions(-) diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py index fd68c5156d..31c5d64652 100644 --- a/tests/constraints/tests.py +++ b/tests/constraints/tests.py @@ -1070,6 +1070,19 @@ class UniqueConstraintTests(TestCase): is_not_null_constraint.validate(Product, Product(price=4, discounted_price=3)) is_not_null_constraint.validate(Product, Product(price=2, discounted_price=1)) + def test_validate_nulls_distinct_fields(self): + Product.objects.create(price=42) + constraint = models.UniqueConstraint( + fields=["price"], + nulls_distinct=False, + name="uniq_prices_nulls_distinct", + ) + constraint.validate(Product, Product(price=None)) + Product.objects.create(price=None) + msg = "Product with this Price already exists." + with self.assertRaisesMessage(ValidationError, msg): + constraint.validate(Product, Product(price=None)) + def test_validate_nulls_distinct_expressions(self): Product.objects.create(price=42) constraint = models.UniqueConstraint( diff --git a/tests/validation/models.py b/tests/validation/models.py index f6b1e0cd62..653be4a239 100644 --- a/tests/validation/models.py +++ b/tests/validation/models.py @@ -217,17 +217,3 @@ class UniqueConstraintConditionProduct(models.Model): condition=models.Q(color__isnull=True), ), ] - - -class UniqueConstraintNullsDistinctProduct(models.Model): - name = models.CharField(max_length=255, blank=True, null=True) - - class Meta: - required_db_features = {"supports_nulls_distinct_unique_constraints"} - constraints = [ - models.UniqueConstraint( - fields=["name"], - name="name_nulls_not_distinct_uniq", - nulls_distinct=False, - ), - ] diff --git a/tests/validation/test_constraints.py b/tests/validation/test_constraints.py index eea2d0c533..0b1ee6518e 100644 --- a/tests/validation/test_constraints.py +++ b/tests/validation/test_constraints.py @@ -6,7 +6,6 @@ from .models import ( ChildUniqueConstraintProduct, Product, UniqueConstraintConditionProduct, - UniqueConstraintNullsDistinctProduct, UniqueConstraintProduct, ) @@ -94,25 +93,3 @@ class PerformConstraintChecksTest(TestCase): UniqueConstraintConditionProduct.objects.create(name="product") product = UniqueConstraintConditionProduct(name="product") product.full_clean(validate_constraints=False) - - @skipUnlessDBFeature("supports_nulls_distinct_unique_constraints") - def test_full_clean_with_nulls_distinct_unique_constraints(self): - UniqueConstraintNullsDistinctProduct.objects.create(name=None) - product = UniqueConstraintNullsDistinctProduct(name=None) - with self.assertRaises(ValidationError) as cm: - product.full_clean() - self.assertEqual( - cm.exception.message_dict, - { - "name": [ - "Unique constraint nulls distinct product with this Name " - "already exists." - ] - }, - ) - - @skipUnlessDBFeature("supports_nulls_distinct_unique_constraints") - def test_full_clean_with_nulls_distinct_unique_constraints_disabled(self): - UniqueConstraintNullsDistinctProduct.objects.create(name=None) - product = UniqueConstraintNullsDistinctProduct(name=None) - product.full_clean(validate_constraints=False)