Skip to content

Commit

Permalink
Refs #34701 -- Moved UniqueConstraint(nulls_distinct) validation tests.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
charettes authored and sarahboyce committed Jul 17, 2024
1 parent adc0b6a commit 0e49a8c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 37 deletions.
13 changes: 13 additions & 0 deletions tests/constraints/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,19 @@ def test_validate_nullable_textfield_with_isnull_true(self):
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(
Expand Down
14 changes: 0 additions & 14 deletions tests/validation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,3 @@ class Meta:
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,
),
]
23 changes: 0 additions & 23 deletions tests/validation/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
ChildUniqueConstraintProduct,
Product,
UniqueConstraintConditionProduct,
UniqueConstraintNullsDistinctProduct,
UniqueConstraintProduct,
)

Expand Down Expand Up @@ -94,25 +93,3 @@ def test_full_clean_with_partial_unique_constraints_disabled(self):
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)

0 comments on commit 0e49a8c

Please sign in to comment.