1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00
django/tests/validation/test_constraints.py
Gagaro 667105877e Fixed #30581 -- Added support for Meta.constraints validation.
Thanks Simon Charette, Keryn Knight, and Mariusz Felisiak for reviews.
2022-05-10 11:22:23 +02:00

96 lines
3.7 KiB
Python

from django.core.exceptions import ValidationError
from django.test import TestCase, skipUnlessDBFeature
from .models import (
ChildProduct,
ChildUniqueConstraintProduct,
Product,
UniqueConstraintConditionProduct,
UniqueConstraintProduct,
)
class PerformConstraintChecksTest(TestCase):
@skipUnlessDBFeature("supports_table_check_constraints")
def test_full_clean_with_check_constraints(self):
product = Product(price=10, discounted_price=15)
with self.assertRaises(ValidationError) as cm:
product.full_clean()
self.assertEqual(
cm.exception.message_dict,
{
"__all__": [
"Constraint “price_gt_discounted_price_validation” is violated."
]
},
)
@skipUnlessDBFeature("supports_table_check_constraints")
def test_full_clean_with_check_constraints_on_child_model(self):
product = ChildProduct(price=10, discounted_price=15)
with self.assertRaises(ValidationError) as cm:
product.full_clean()
self.assertEqual(
cm.exception.message_dict,
{
"__all__": [
"Constraint “price_gt_discounted_price_validation” is violated."
]
},
)
@skipUnlessDBFeature("supports_table_check_constraints")
def test_full_clean_with_check_constraints_disabled(self):
product = Product(price=10, discounted_price=15)
product.full_clean(validate_constraints=False)
def test_full_clean_with_unique_constraints(self):
UniqueConstraintProduct.objects.create(name="product", color="yellow", rank=1)
tests = [
UniqueConstraintProduct(name="product", color="yellow", rank=1),
# Child model.
ChildUniqueConstraintProduct(name="product", color="yellow", rank=1),
]
for product in tests:
with self.subTest(model=product.__class__.__name__):
with self.assertRaises(ValidationError) as cm:
product.full_clean()
self.assertEqual(
cm.exception.message_dict,
{
"__all__": [
"Unique constraint product with this Name and Color "
"already exists."
],
"rank": [
"Unique constraint product with this Rank already exists."
],
},
)
def test_full_clean_with_unique_constraints_disabled(self):
UniqueConstraintProduct.objects.create(name="product", color="yellow", rank=1)
product = UniqueConstraintProduct(name="product", color="yellow", rank=1)
product.full_clean(validate_constraints=False)
@skipUnlessDBFeature("supports_partial_indexes")
def test_full_clean_with_partial_unique_constraints(self):
UniqueConstraintConditionProduct.objects.create(name="product")
product = UniqueConstraintConditionProduct(name="product")
with self.assertRaises(ValidationError) as cm:
product.full_clean()
self.assertEqual(
cm.exception.message_dict,
{
"__all__": [
"Constraint “name_without_color_uniq_validation” is violated."
]
},
)
@skipUnlessDBFeature("supports_partial_indexes")
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)