summaryrefslogtreecommitdiff
path: root/tests/constraints
diff options
context:
space:
mode:
authorIan Foote <python@ian.feete.org>2016-11-05 13:12:12 +0000
committerTim Graham <timograham@gmail.com>2018-07-10 15:32:33 -0400
commit952f05a6db2665d83c04075119285f2164b03432 (patch)
tree616ddfb21cd44c19292c025c494d1995afca13b8 /tests/constraints
parent6fbfb5cb9602574adc867d34241172226291d367 (diff)
downloaddjango-952f05a6db2665d83c04075119285f2164b03432.tar.gz
Fixed #11964 -- Added support for database check constraints.
Diffstat (limited to 'tests/constraints')
-rw-r--r--tests/constraints/__init__.py0
-rw-r--r--tests/constraints/models.py15
-rw-r--r--tests/constraints/tests.py30
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)