diff options
Diffstat (limited to 'tests/constraints')
-rw-r--r-- | tests/constraints/__init__.py | 0 | ||||
-rw-r--r-- | tests/constraints/models.py | 15 | ||||
-rw-r--r-- | tests/constraints/tests.py | 30 |
3 files changed, 45 insertions, 0 deletions
diff --git a/tests/constraints/__init__.py b/tests/constraints/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/constraints/__init__.py diff --git a/tests/constraints/models.py b/tests/constraints/models.py new file mode 100644 index 0000000000..de49fa2765 --- /dev/null +++ b/tests/constraints/models.py @@ -0,0 +1,15 @@ +from django.db import models + + +class Product(models.Model): + name = models.CharField(max_length=255) + price = models.IntegerField() + discounted_price = models.IntegerField() + + class Meta: + constraints = [ + models.CheckConstraint( + models.Q(price__gt=models.F('discounted_price')), + 'price_gt_discounted_price' + ) + ] diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py new file mode 100644 index 0000000000..19573dffa1 --- /dev/null +++ b/tests/constraints/tests.py @@ -0,0 +1,30 @@ +from django.db import IntegrityError, models +from django.test import TestCase, skipUnlessDBFeature + +from .models import Product + + +class CheckConstraintTests(TestCase): + def test_repr(self): + constraint = models.Q(price__gt=models.F('discounted_price')) + name = 'price_gt_discounted_price' + check = models.CheckConstraint(constraint, name) + self.assertEqual( + repr(check), + "<CheckConstraint: constraint='{}' name='{}'>".format(constraint, name), + ) + + def test_deconstruction(self): + constraint = models.Q(price__gt=models.F('discounted_price')) + name = 'price_gt_discounted_price' + check = models.CheckConstraint(constraint, name) + path, args, kwargs = check.deconstruct() + self.assertEqual(path, 'django.db.models.CheckConstraint') + self.assertEqual(args, ()) + self.assertEqual(kwargs, {'constraint': constraint, 'name': name}) + + @skipUnlessDBFeature('supports_table_check_constraints') + def test_model_constraint(self): + Product.objects.create(name='Valid', price=10, discounted_price=5) + with self.assertRaises(IntegrityError): + Product.objects.create(name='Invalid', price=10, discounted_price=20) |