Ticket #30408: 30408.diff

File 30408.diff, 1.5 KB (added by Mariusz Felisiak, 6 years ago)
  • tests/postgres_tests/test_constraints.py

    diff --git a/tests/postgres_tests/test_constraints.py b/tests/postgres_tests/test_constraints.py
    index 0e09a1c546..24557939ee 100644
    a b from django.db.models.constraints import CheckConstraint  
    44from django.db.utils import IntegrityError
    55
    66from . import PostgreSQLTestCase
    7 from .models import RangesModel
     7from .models import CharFieldModel, RangesModel
    88
    99try:
    1010    from psycopg2.extras import NumericRange
    class SchemaTests(PostgreSQLTestCase):  
    3333        with self.assertRaises(IntegrityError), transaction.atomic():
    3434            RangesModel.objects.create(ints=(20, 50))
    3535        RangesModel.objects.create(ints=(10, 30))
     36
     37    def test_check_constraint_startswith(self):
     38        constraint_name = 'char_startswith_bar'
     39        self.assertNotIn(constraint_name, self.get_constraints(CharFieldModel._meta.db_table))
     40        constraint = CheckConstraint(
     41            check=Q(field__startswith='BAR'),
     42            name=constraint_name,
     43        )
     44        with connection.schema_editor() as editor:
     45            editor.add_constraint(CharFieldModel, constraint)
     46        with connection.cursor() as cursor:
     47            constraints = connection.introspection.get_constraints(cursor, CharFieldModel._meta.db_table)
     48        self.assertIn(constraint_name, constraints)
     49        with self.assertRaises(IntegrityError), transaction.atomic():
     50            CharFieldModel.objects.create(field='FOO')
     51        CharFieldModel.objects.create(field='BAR1')
Back to Top