diff options
| author | Simon Charette <charette.s@gmail.com> | 2018-08-05 22:30:44 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-11-13 17:57:27 -0500 |
| commit | db13bca60a6758d5fe63eeb01c00c3f54f650715 (patch) | |
| tree | e1c3620032623488f2dac8320e3e646ed18cef1a /tests | |
| parent | 8eae094638acf802c8047b341d126d94bc9b45a3 (diff) | |
Fixed #29641 -- Added support for unique constraints in Meta.constraints.
This constraint is similar to Meta.unique_together but also allows
specifying a name.
Co-authored-by: Ian Foote <python@ian.feete.org>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/constraints/models.py | 5 | ||||
| -rw-r--r-- | tests/constraints/tests.py | 40 |
2 files changed, 43 insertions, 2 deletions
diff --git a/tests/constraints/models.py b/tests/constraints/models.py index 08fbe9e1df..b1645ecedb 100644 --- a/tests/constraints/models.py +++ b/tests/constraints/models.py @@ -3,8 +3,8 @@ from django.db import models class Product(models.Model): name = models.CharField(max_length=255) - price = models.IntegerField() - discounted_price = models.IntegerField() + price = models.IntegerField(null=True) + discounted_price = models.IntegerField(null=True) class Meta: constraints = [ @@ -12,4 +12,5 @@ class Product(models.Model): check=models.Q(price__gt=models.F('discounted_price')), name='price_gt_discounted_price', ), + models.UniqueConstraint(fields=['name'], name='unique_name'), ] diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py index b144c24a28..ddcaf7cfe0 100644 --- a/tests/constraints/tests.py +++ b/tests/constraints/tests.py @@ -1,3 +1,4 @@ +from django.core.exceptions import ValidationError from django.db import IntegrityError, connection, models from django.db.models.constraints import BaseConstraint from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature @@ -50,3 +51,42 @@ class CheckConstraintTests(TestCase): if connection.features.uppercases_column_names: expected_name = expected_name.upper() self.assertIn(expected_name, constraints) + + +class UniqueConstraintTests(TestCase): + @classmethod + def setUpTestData(cls): + cls.p1 = Product.objects.create(name='p1') + + def test_repr(self): + fields = ['foo', 'bar'] + name = 'unique_fields' + constraint = models.UniqueConstraint(fields=fields, name=name) + self.assertEqual( + repr(constraint), + "<UniqueConstraint: fields=('foo', 'bar') name='unique_fields'>", + ) + + def test_deconstruction(self): + fields = ['foo', 'bar'] + name = 'unique_fields' + check = models.UniqueConstraint(fields=fields, name=name) + path, args, kwargs = check.deconstruct() + self.assertEqual(path, 'django.db.models.UniqueConstraint') + self.assertEqual(args, ()) + self.assertEqual(kwargs, {'fields': tuple(fields), 'name': name}) + + def test_database_constraint(self): + with self.assertRaises(IntegrityError): + Product.objects.create(name=self.p1.name) + + def test_model_validation(self): + with self.assertRaisesMessage(ValidationError, 'Product with this Name already exists.'): + Product(name=self.p1.name).validate_unique() + + def test_name(self): + constraints = get_constraints(Product._meta.db_table) + expected_name = 'unique_name' + if connection.features.uppercases_column_names: + expected_name = expected_name.upper() + self.assertIn(expected_name, constraints) |
