Ticket #28861: 28861.diff

File 28861.diff, 3.4 KB (added by Tim Graham, 6 years ago)
  • tests/schema/models.py

    diff --git a/tests/schema/models.py b/tests/schema/models.py
    index 4512d8b..8490c80 100644
    a b  
    11from django.apps.registry import Apps
    2 from django.db import models
     2from django.db import connection, models
    33
    44# Because we want to test creation and deletion of these as separate things,
    55# these models are all inserted into a separate Apps so the main test
    class BookForeignObj(models.Model):  
    121121        apps = new_apps
    122122
    123123
     124if connection.vendor == 'postgresql':
     125    from django.contrib.postgres.fields import CICharField
     126
     127    class CIBookModel(models.Model):
     128        title = CICharField(max_length=100, db_index=True)
     129
     130        class Meta:
     131            apps = new_apps
     132
     133
    124134class IntegerPK(models.Model):
    125135    i = models.IntegerField(primary_key=True)
    126136    j = models.IntegerField(unique=True)
  • tests/schema/tests.py

    diff --git a/tests/schema/tests.py b/tests/schema/tests.py
    index 66a54b1..7c1d010 100644
    a b import datetime  
    22import itertools
    33import unittest
    44from copy import copy
    5 from unittest import mock
     5from unittest import mock, skipUnless
    66
    77from django.db import (
    88    DatabaseError, IntegrityError, OperationalError, connection,
    99)
     10from django.db.migrations import Migration
     11from django.db.migrations.state import ProjectState
    1012from django.db.models import Model
    1113from django.db.models.deletion import CASCADE, PROTECT
    1214from django.db.models.fields import (
    from django.db.transaction import TransactionManagementError, atomic  
    2224from django.test import (
    2325    TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature,
    2426)
    25 from django.test.utils import CaptureQueriesContext, isolate_apps
     27from django.test.utils import (
     28    CaptureQueriesContext, isolate_apps, modify_settings,
     29)
    2630from django.utils import timezone
    2731
    2832from .fields import (
    class SchemaTests(TransactionTestCase):  
    248252            editor.alter_field(AuthorCharFieldWithIndex, old_field, new_field, strict=True)
    249253        self.assertForeignKeyExists(AuthorCharFieldWithIndex, 'char_field_id', 'schema_author')
    250254
     255    @skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific tests for citext')
     256    @modify_settings(INSTALLED_APPS={'append': 'django.contrib.postgres'})
     257    def test_cichar_field_with_db_index_to_fk(self):
     258        from django.contrib.postgres.operations import CITextExtension
     259        from .models import CIBookModel
     260        self.models.append(CIBookModel)
     261        # Create the table and the necessary citext extension
     262        migration = Migration('name', app_label='apps')
     263        migration.operations = [CITextExtension()]
     264        with connection.schema_editor() as editor:
     265            editor.create_model(Book)
     266            editor.create_model(Author)
     267            migration.apply(ProjectState(), editor)
     268            editor.create_model(CIBookModel)
     269        # Change CICharField to FK
     270        old_field = CIBookModel._meta.get_field('title')
     271        new_field = ForeignKey(Book, CASCADE, blank=True)
     272        new_field.set_attributes_from_name('title')
     273        with connection.schema_editor() as editor:
     274            editor.alter_field(CIBookModel, old_field, new_field, strict=True)
     275        self.assertForeignKeyExists(CIBookModel, 'title_id', 'schema_book')
     276
    251277    @skipUnlessDBFeature('supports_foreign_keys')
    252278    @skipUnlessDBFeature('supports_index_on_text_field')
    253279    def test_text_field_with_db_index_to_fk(self):
Back to Top