| 1 |
from django.core.exceptions import ValidationError |
| 2 |
from django.test import TestCase, skipUnlessDBFeature |
| 3 |
|
| 4 |
from .models import ( |
| 5 |
ChildProduct, |
| 6 |
ChildUniqueConstraintProduct, |
| 7 |
Product, |
| 8 |
UniqueConstraintConditionProduct, |
| 9 |
UniqueConstraintProduct, |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
class PerformConstraintChecksTest(TestCase): |
| 14 |
@skipUnlessDBFeature("supports_table_check_constraints") |
| 15 |
def test_full_clean_with_check_constraints(self): |
| 16 |
product = Product(price=10, discounted_price=15) |
| 17 |
with self.assertRaises(ValidationError) as cm: |
| 18 |
product.full_clean() |
| 19 |
self.assertEqual( |
| 20 |
cm.exception.message_dict, |
| 21 |
{ |
| 22 |
"__all__": [ |
| 23 |
"Constraint “price_gt_discounted_price_validation” is violated." |
| 24 |
] |
| 25 |
}, |
| 26 |
) |
| 27 |
|
| 28 |
@skipUnlessDBFeature("supports_table_check_constraints") |
| 29 |
def test_full_clean_with_check_constraints_on_child_model(self): |
| 30 |
product = ChildProduct(price=10, discounted_price=15) |
| 31 |
with self.assertRaises(ValidationError) as cm: |
| 32 |
product.full_clean() |
| 33 |
self.assertEqual( |
| 34 |
cm.exception.message_dict, |
| 35 |
{ |
| 36 |
"__all__": [ |
| 37 |
"Constraint “price_gt_discounted_price_validation” is violated." |
| 38 |
] |
| 39 |
}, |
| 40 |
) |
| 41 |
|
| 42 |
@skipUnlessDBFeature("supports_table_check_constraints") |
| 43 |
def test_full_clean_with_check_constraints_disabled(self): |
| 44 |
product = Product(price=10, discounted_price=15) |
| 45 |
product.full_clean(validate_constraints=False) |
| 46 |
|
| 47 |
def test_full_clean_with_unique_constraints(self): |
| 48 |
UniqueConstraintProduct.objects.create(name="product", color="yellow", rank=1) |
| 49 |
tests = [ |
| 50 |
UniqueConstraintProduct(name="product", color="yellow", rank=1), |
| 51 |
# Child model. |
| 52 |
ChildUniqueConstraintProduct(name="product", color="yellow", rank=1), |
| 53 |
] |
| 54 |
for product in tests: |
| 55 |
with self.subTest(model=product.__class__.__name__): |
| 56 |
with self.assertRaises(ValidationError) as cm: |
| 57 |
product.full_clean() |
| 58 |
self.assertEqual( |
| 59 |
cm.exception.message_dict, |
| 60 |
{ |
| 61 |
"__all__": [ |
| 62 |
"Unique constraint product with this Name and Color " |
| 63 |
"already exists." |
| 64 |
], |
| 65 |
"rank": [ |
| 66 |
"Unique constraint product with this Rank already exists." |
| 67 |
], |
| 68 |
}, |
| 69 |
) |
| 70 |
|
| 71 |
def test_full_clean_with_unique_constraints_disabled(self): |
| 72 |
UniqueConstraintProduct.objects.create(name="product", color="yellow", rank=1) |
| 73 |
product = UniqueConstraintProduct(name="product", color="yellow", rank=1) |
| 74 |
product.full_clean(validate_constraints=False) |
| 75 |
|
| 76 |
@skipUnlessDBFeature("supports_partial_indexes") |
| 77 |
def test_full_clean_with_partial_unique_constraints(self): |
| 78 |
UniqueConstraintConditionProduct.objects.create(name="product") |
| 79 |
product = UniqueConstraintConditionProduct(name="product") |
| 80 |
with self.assertRaises(ValidationError) as cm: |
| 81 |
product.full_clean() |
| 82 |
self.assertEqual( |
| 83 |
cm.exception.message_dict, |
| 84 |
{ |
| 85 |
"__all__": [ |
| 86 |
"Constraint “name_without_color_uniq_validation” is violated." |
| 87 |
] |
| 88 |
}, |
| 89 |
) |
| 90 |
|
| 91 |
@skipUnlessDBFeature("supports_partial_indexes") |
| 92 |
def test_full_clean_with_partial_unique_constraints_disabled(self): |
| 93 |
UniqueConstraintConditionProduct.objects.create(name="product") |
| 94 |
product = UniqueConstraintConditionProduct(name="product") |
| 95 |
product.full_clean(validate_constraints=False) |