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.
This commit is contained in:
Simon Charette 2024-07-13 22:15:21 -04:00 committed by Sarah Boyce
parent adc0b6aac3
commit 0e49a8c3bd
3 changed files with 13 additions and 37 deletions

View File

@ -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(

View File

@ -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,
),
]

View File

@ -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)