1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
from django.apps.registry import apps
from django.conf import settings
from django.contrib.contenttypes import management as contenttypes_management
from django.contrib.contenttypes.models import ContentType
from django.core.management import call_command
from django.db import migrations, models
from django.test import TransactionTestCase, override_settings
@override_settings(
MIGRATION_MODULES=dict(
settings.MIGRATION_MODULES,
contenttypes_tests="contenttypes_tests.operations_migrations",
),
)
class ContentTypeOperationsTests(TransactionTestCase):
databases = {"default", "other"}
available_apps = [
"contenttypes_tests",
"django.contrib.contenttypes",
]
class TestRouter:
def db_for_write(self, model, **hints):
return "default"
def setUp(self):
app_config = apps.get_app_config("contenttypes_tests")
models.signals.post_migrate.connect(
self.assertOperationsInjected, sender=app_config
)
self.addCleanup(
models.signals.post_migrate.disconnect,
self.assertOperationsInjected,
sender=app_config,
)
def assertOperationsInjected(self, plan, **kwargs):
for migration, _backward in plan:
operations = iter(migration.operations)
for operation in operations:
if isinstance(operation, migrations.RenameModel):
next_operation = next(operations)
self.assertIsInstance(
next_operation, contenttypes_management.RenameContentType
)
self.assertEqual(next_operation.app_label, migration.app_label)
self.assertEqual(next_operation.old_model, operation.old_name_lower)
self.assertEqual(next_operation.new_model, operation.new_name_lower)
def test_existing_content_type_rename(self):
ContentType.objects.create(app_label="contenttypes_tests", model="foo")
call_command(
"migrate",
"contenttypes_tests",
database="default",
interactive=False,
verbosity=0,
)
self.assertFalse(
ContentType.objects.filter(
app_label="contenttypes_tests", model="foo"
).exists()
)
self.assertTrue(
ContentType.objects.filter(
app_label="contenttypes_tests", model="renamedfoo"
).exists()
)
call_command(
"migrate",
"contenttypes_tests",
"zero",
database="default",
interactive=False,
verbosity=0,
)
self.assertTrue(
ContentType.objects.filter(
app_label="contenttypes_tests", model="foo"
).exists()
)
self.assertFalse(
ContentType.objects.filter(
app_label="contenttypes_tests", model="renamedfoo"
).exists()
)
@override_settings(DATABASE_ROUTERS=[TestRouter()])
def test_existing_content_type_rename_other_database(self):
ContentType.objects.using("other").create(
app_label="contenttypes_tests", model="foo"
)
other_content_types = ContentType.objects.using("other").filter(
app_label="contenttypes_tests"
)
call_command(
"migrate",
"contenttypes_tests",
database="other",
interactive=False,
verbosity=0,
)
self.assertFalse(other_content_types.filter(model="foo").exists())
self.assertTrue(other_content_types.filter(model="renamedfoo").exists())
call_command(
"migrate",
"contenttypes_tests",
"zero",
database="other",
interactive=False,
verbosity=0,
)
self.assertTrue(other_content_types.filter(model="foo").exists())
self.assertFalse(other_content_types.filter(model="renamedfoo").exists())
def test_missing_content_type_rename_ignore(self):
call_command(
"migrate",
"contenttypes_tests",
database="default",
interactive=False,
verbosity=0,
)
self.assertFalse(
ContentType.objects.filter(
app_label="contenttypes_tests", model="foo"
).exists()
)
self.assertTrue(
ContentType.objects.filter(
app_label="contenttypes_tests", model="renamedfoo"
).exists()
)
call_command(
"migrate",
"contenttypes_tests",
"zero",
database="default",
interactive=False,
verbosity=0,
)
self.assertTrue(
ContentType.objects.filter(
app_label="contenttypes_tests", model="foo"
).exists()
)
self.assertFalse(
ContentType.objects.filter(
app_label="contenttypes_tests", model="renamedfoo"
).exists()
)
def test_content_type_rename_conflict(self):
ContentType.objects.create(app_label="contenttypes_tests", model="foo")
ContentType.objects.create(app_label="contenttypes_tests", model="renamedfoo")
call_command(
"migrate",
"contenttypes_tests",
database="default",
interactive=False,
verbosity=0,
)
self.assertTrue(
ContentType.objects.filter(
app_label="contenttypes_tests", model="foo"
).exists()
)
self.assertTrue(
ContentType.objects.filter(
app_label="contenttypes_tests", model="renamedfoo"
).exists()
)
call_command(
"migrate",
"contenttypes_tests",
"zero",
database="default",
interactive=False,
verbosity=0,
)
self.assertTrue(
ContentType.objects.filter(
app_label="contenttypes_tests", model="foo"
).exists()
)
self.assertTrue(
ContentType.objects.filter(
app_label="contenttypes_tests", model="renamedfoo"
).exists()
)
|