Ticket #33335: 33335-test.diff

File 33335-test.diff, 2.1 KB (added by Claude Paroz, 3 years ago)

Test code

  • tests/constraints/models.py

    diff --git a/tests/constraints/models.py b/tests/constraints/models.py
    index f8a4ebe675..faef2163bf 100644
    a b  
    11from django.db import models
     2from django.db.models.functions import Lower
    23
    34
    45class Product(models.Model):
    class UniqueConstraintInclude(models.Model):  
    107108        ]
    108109
    109110
     111class UniqueConstraintExpression(models.Model):
     112    name = models.CharField(max_length=15)
     113
     114    class Meta:
     115        required_db_features = {
     116            'supports_table_check_constraints',
     117            'supports_covering_indexes',
     118        }
     119        constraints = [
     120            models.UniqueConstraint(Lower('name'), name='name_lower_unique'),
     121        ]
     122
     123
    110124class AbstractModel(models.Model):
    111125    age = models.IntegerField()
    112126
  • tests/constraints/tests.py

    diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py
    index 3d59d07b24..7e52cb3c24 100644
    a b from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature  
    1010
    1111from .models import (
    1212    ChildModel, Product, UniqueConstraintConditionProduct,
    13     UniqueConstraintDeferrable, UniqueConstraintInclude,
    14     UniqueConstraintProduct,
     13    UniqueConstraintDeferrable, UniqueConstraintExpression,
     14    UniqueConstraintInclude, UniqueConstraintProduct,
    1515)
    1616
    1717
    class UniqueConstraintTests(TestCase):  
    561561                opclasses=['jsonb_path_ops'],
    562562            )
    563563
     564    @skipUnlessDBFeature(
     565        'supports_table_check_constraints',
     566        'supports_covering_indexes',
     567    )
     568    def test_expressions_validate_unique(self):
     569        UniqueConstraintExpression.objects.create(name='Blah')
     570        msg = 'Unique constraint expression with this ? already exists.'
     571        instance = UniqueConstraintExpression(name='BlAh')
     572        with self.assertRaisesMessage(ValidationError, msg):
     573            instance.validate_unique()
     574
    564575    def test_requires_name(self):
    565576        msg = 'A unique constraint must be named.'
    566577        with self.assertRaisesMessage(ValueError, msg):
Back to Top