Fixed #30484 -- Added conditional expressions support to CheckConstraint.

This commit is contained in:
Simon Charette 2019-11-15 00:08:36 -05:00 committed by Mariusz Felisiak
parent 37e6c5b79b
commit e9a0e1d4f6
7 changed files with 84 additions and 19 deletions

View file

@ -61,6 +61,13 @@ class CheckConstraintTests(TestCase):
"<CheckConstraint: check='{}' name='{}'>".format(check, name),
)
def test_invalid_check_types(self):
msg = (
'CheckConstraint.check must be a Q instance or boolean expression.'
)
with self.assertRaisesMessage(TypeError, msg):
models.CheckConstraint(check=models.F('discounted_price'), name='check')
def test_deconstruction(self):
check = models.Q(price__gt=models.F('discounted_price'))
name = 'price_gt_discounted_price'
@ -76,11 +83,25 @@ class CheckConstraintTests(TestCase):
with self.assertRaises(IntegrityError):
Product.objects.create(price=10, discounted_price=20)
@skipUnlessDBFeature('supports_table_check_constraints')
def test_database_constraint_expression(self):
Product.objects.create(price=999, discounted_price=5)
with self.assertRaises(IntegrityError):
Product.objects.create(price=1000, discounted_price=5)
@skipUnlessDBFeature('supports_table_check_constraints')
def test_database_constraint_expressionwrapper(self):
Product.objects.create(price=499, discounted_price=5)
with self.assertRaises(IntegrityError):
Product.objects.create(price=500, discounted_price=5)
@skipUnlessDBFeature('supports_table_check_constraints', 'can_introspect_check_constraints')
def test_name(self):
constraints = get_constraints(Product._meta.db_table)
for expected_name in (
'price_gt_discounted_price',
'constraints_price_lt_1000_raw',
'constraints_price_neq_500_wrap',
'constraints_product_price_gt_0',
):
with self.subTest(expected_name):