From febe136d4c3310ec8901abecca3ea5ba2be3952c Mon Sep 17 00:00:00 2001 From: can Date: Fri, 5 Jul 2019 15:15:41 +0300 Subject: Fixed #30397 -- Added app_label/class interpolation for names of indexes and constraints. --- tests/constraints/models.py | 21 +++++++++++++++++++++ tests/constraints/tests.py | 15 ++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) (limited to 'tests/constraints') diff --git a/tests/constraints/models.py b/tests/constraints/models.py index f316b95161..fb6ef620f0 100644 --- a/tests/constraints/models.py +++ b/tests/constraints/models.py @@ -13,6 +13,10 @@ class Product(models.Model): check=models.Q(price__gt=models.F('discounted_price')), name='price_gt_discounted_price', ), + models.CheckConstraint( + check=models.Q(price__gt=0), + name='%(app_label)s_%(class)s_price_gt_0', + ), models.UniqueConstraint(fields=['name', 'color'], name='name_color_uniq'), models.UniqueConstraint( fields=['name'], @@ -20,3 +24,20 @@ class Product(models.Model): condition=models.Q(color__isnull=True), ), ] + + +class AbstractModel(models.Model): + age = models.IntegerField() + + class Meta: + abstract = True + constraints = [ + models.CheckConstraint( + check=models.Q(age__gte=18), + name='%(app_label)s_%(class)s_adult', + ), + ] + + +class ChildModel(AbstractModel): + pass diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py index b7aeb1e7f0..c2f99c565d 100644 --- a/tests/constraints/tests.py +++ b/tests/constraints/tests.py @@ -3,7 +3,7 @@ from django.db import IntegrityError, connection, models from django.db.models.constraints import BaseConstraint from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature -from .models import Product +from .models import ChildModel, Product def get_constraints(table): @@ -76,8 +76,17 @@ class CheckConstraintTests(TestCase): @skipUnlessDBFeature('supports_table_check_constraints') def test_name(self): constraints = get_constraints(Product._meta.db_table) - expected_name = 'price_gt_discounted_price' - self.assertIn(expected_name, constraints) + for expected_name in ( + 'price_gt_discounted_price', + 'constraints_product_price_gt_0', + ): + with self.subTest(expected_name): + self.assertIn(expected_name, constraints) + + @skipUnlessDBFeature('supports_table_check_constraints') + def test_abstract_name(self): + constraints = get_constraints(ChildModel._meta.db_table) + self.assertIn('constraints_childmodel_adult', constraints) class UniqueConstraintTests(TestCase): -- cgit v1.3