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