| 1 |
import copy |
| 2 |
import functools |
| 3 |
import re |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
from django.apps import apps |
| 7 |
from django.conf import settings |
| 8 |
from django.contrib.auth.models import AbstractBaseUser |
| 9 |
from django.core.validators import RegexValidator, validate_slug |
| 10 |
from django.db import connection, migrations, models |
| 11 |
from django.db.migrations.autodetector import MigrationAutodetector |
| 12 |
from django.db.migrations.graph import MigrationGraph |
| 13 |
from django.db.migrations.loader import MigrationLoader |
| 14 |
from django.db.migrations.questioner import MigrationQuestioner |
| 15 |
from django.db.migrations.state import ModelState, ProjectState |
| 16 |
from django.db.models.functions import Concat, Lower |
| 17 |
from django.test import SimpleTestCase, TestCase, override_settings |
| 18 |
from django.test.utils import isolate_lru_cache |
| 19 |
|
| 20 |
from .models import FoodManager, FoodQuerySet |
| 21 |
|
| 22 |
|
| 23 |
class DeconstructibleObject: |
| 24 |
""" |
| 25 |
A custom deconstructible object. |
| 26 |
""" |
| 27 |
|
| 28 |
def __init__(self, *args, **kwargs): |
| 29 |
self.args = args |
| 30 |
self.kwargs = kwargs |
| 31 |
|
| 32 |
def deconstruct(self): |
| 33 |
return (self.__module__ + "." + self.__class__.__name__, self.args, self.kwargs) |
| 34 |
|
| 35 |
|
| 36 |
class BaseAutodetectorTests(TestCase): |
| 37 |
def repr_changes(self, changes, include_dependencies=False): |
| 38 |
output = "" |
| 39 |
for app_label, migrations_ in sorted(changes.items()): |
| 40 |
output += " %s:\n" % app_label |
| 41 |
for migration in migrations_: |
| 42 |
output += " %s\n" % migration.name |
| 43 |
for operation in migration.operations: |
| 44 |
output += " %s\n" % operation |
| 45 |
if include_dependencies: |
| 46 |
output += " Dependencies:\n" |
| 47 |
if migration.dependencies: |
| 48 |
for dep in migration.dependencies: |
| 49 |
output += " %s\n" % (dep,) |
| 50 |
else: |
| 51 |
output += " None\n" |
| 52 |
return output |
| 53 |
|
| 54 |
def assertNumberMigrations(self, changes, app_label, number): |
| 55 |
if len(changes.get(app_label, [])) != number: |
| 56 |
self.fail( |
| 57 |
"Incorrect number of migrations (%s) for %s (expected %s)\n%s" |
| 58 |
% ( |
| 59 |
len(changes.get(app_label, [])), |
| 60 |
app_label, |
| 61 |
number, |
| 62 |
self.repr_changes(changes), |
| 63 |
) |
| 64 |
) |
| 65 |
|
| 66 |
def assertMigrationDependencies(self, changes, app_label, position, dependencies): |
| 67 |
if not changes.get(app_label): |
| 68 |
self.fail( |
| 69 |
"No migrations found for %s\n%s" |
| 70 |
% (app_label, self.repr_changes(changes)) |
| 71 |
) |
| 72 |
if len(changes[app_label]) < position + 1: |
| 73 |
self.fail( |
| 74 |
"No migration at index %s for %s\n%s" |
| 75 |
% (position, app_label, self.repr_changes(changes)) |
| 76 |
) |
| 77 |
migration = changes[app_label][position] |
| 78 |
if set(migration.dependencies) != set(dependencies): |
| 79 |
self.fail( |
| 80 |
"Migration dependencies mismatch for %s.%s (expected %s):\n%s" |
| 81 |
% ( |
| 82 |
app_label, |
| 83 |
migration.name, |
| 84 |
dependencies, |
| 85 |
self.repr_changes(changes, include_dependencies=True), |
| 86 |
) |
| 87 |
) |
| 88 |
|
| 89 |
def assertOperationTypes(self, changes, app_label, position, types): |
| 90 |
if not changes.get(app_label): |
| 91 |
self.fail( |
| 92 |
"No migrations found for %s\n%s" |
| 93 |
% (app_label, self.repr_changes(changes)) |
| 94 |
) |
| 95 |
if len(changes[app_label]) < position + 1: |
| 96 |
self.fail( |
| 97 |
"No migration at index %s for %s\n%s" |
| 98 |
% (position, app_label, self.repr_changes(changes)) |
| 99 |
) |
| 100 |
migration = changes[app_label][position] |
| 101 |
real_types = [ |
| 102 |
operation.__class__.__name__ for operation in migration.operations |
| 103 |
] |
| 104 |
if types != real_types: |
| 105 |
self.fail( |
| 106 |
"Operation type mismatch for %s.%s (expected %s):\n%s" |
| 107 |
% ( |
| 108 |
app_label, |
| 109 |
migration.name, |
| 110 |
types, |
| 111 |
self.repr_changes(changes), |
| 112 |
) |
| 113 |
) |
| 114 |
|
| 115 |
def assertOperationAttributes( |
| 116 |
self, changes, app_label, position, operation_position, **attrs |
| 117 |
): |
| 118 |
if not changes.get(app_label): |
| 119 |
self.fail( |
| 120 |
"No migrations found for %s\n%s" |
| 121 |
% (app_label, self.repr_changes(changes)) |
| 122 |
) |
| 123 |
if len(changes[app_label]) < position + 1: |
| 124 |
self.fail( |
| 125 |
"No migration at index %s for %s\n%s" |
| 126 |
% (position, app_label, self.repr_changes(changes)) |
| 127 |
) |
| 128 |
migration = changes[app_label][position] |
| 129 |
if len(changes[app_label]) < position + 1: |
| 130 |
self.fail( |
| 131 |
"No operation at index %s for %s.%s\n%s" |
| 132 |
% ( |
| 133 |
operation_position, |
| 134 |
app_label, |
| 135 |
migration.name, |
| 136 |
self.repr_changes(changes), |
| 137 |
) |
| 138 |
) |
| 139 |
operation = migration.operations[operation_position] |
| 140 |
for attr, value in attrs.items(): |
| 141 |
if getattr(operation, attr, None) != value: |
| 142 |
self.fail( |
| 143 |
"Attribute mismatch for %s.%s op #%s, %s (expected %r, got %r):\n%s" |
| 144 |
% ( |
| 145 |
app_label, |
| 146 |
migration.name, |
| 147 |
operation_position, |
| 148 |
attr, |
| 149 |
value, |
| 150 |
getattr(operation, attr, None), |
| 151 |
self.repr_changes(changes), |
| 152 |
) |
| 153 |
) |
| 154 |
|
| 155 |
def assertOperationFieldAttributes( |
| 156 |
self, changes, app_label, position, operation_position, **attrs |
| 157 |
): |
| 158 |
if not changes.get(app_label): |
| 159 |
self.fail( |
| 160 |
"No migrations found for %s\n%s" |
| 161 |
% (app_label, self.repr_changes(changes)) |
| 162 |
) |
| 163 |
if len(changes[app_label]) < position + 1: |
| 164 |
self.fail( |
| 165 |
"No migration at index %s for %s\n%s" |
| 166 |
% (position, app_label, self.repr_changes(changes)) |
| 167 |
) |
| 168 |
migration = changes[app_label][position] |
| 169 |
if len(changes[app_label]) < position + 1: |
| 170 |
self.fail( |
| 171 |
"No operation at index %s for %s.%s\n%s" |
| 172 |
% ( |
| 173 |
operation_position, |
| 174 |
app_label, |
| 175 |
migration.name, |
| 176 |
self.repr_changes(changes), |
| 177 |
) |
| 178 |
) |
| 179 |
operation = migration.operations[operation_position] |
| 180 |
if not hasattr(operation, "field"): |
| 181 |
self.fail( |
| 182 |
"No field attribute for %s.%s op #%s." |
| 183 |
% ( |
| 184 |
app_label, |
| 185 |
migration.name, |
| 186 |
operation_position, |
| 187 |
) |
| 188 |
) |
| 189 |
field = operation.field |
| 190 |
for attr, value in attrs.items(): |
| 191 |
if getattr(field, attr, None) != value: |
| 192 |
self.fail( |
| 193 |
"Field attribute mismatch for %s.%s op #%s, field.%s (expected %r, " |
| 194 |
"got %r):\n%s" |
| 195 |
% ( |
| 196 |
app_label, |
| 197 |
migration.name, |
| 198 |
operation_position, |
| 199 |
attr, |
| 200 |
value, |
| 201 |
getattr(field, attr, None), |
| 202 |
self.repr_changes(changes), |
| 203 |
) |
| 204 |
) |
| 205 |
|
| 206 |
def make_project_state(self, model_states): |
| 207 |
"Shortcut to make ProjectStates from lists of predefined models" |
| 208 |
project_state = ProjectState() |
| 209 |
for model_state in model_states: |
| 210 |
project_state.add_model(model_state.clone()) |
| 211 |
return project_state |
| 212 |
|
| 213 |
def get_changes(self, before_states, after_states, questioner=None): |
| 214 |
if not isinstance(before_states, ProjectState): |
| 215 |
before_states = self.make_project_state(before_states) |
| 216 |
if not isinstance(after_states, ProjectState): |
| 217 |
after_states = self.make_project_state(after_states) |
| 218 |
return MigrationAutodetector( |
| 219 |
before_states, |
| 220 |
after_states, |
| 221 |
questioner, |
| 222 |
)._detect_changes() |
| 223 |
|
| 224 |
|
| 225 |
class AutodetectorTests(BaseAutodetectorTests): |
| 226 |
""" |
| 227 |
Tests the migration autodetector. |
| 228 |
""" |
| 229 |
|
| 230 |
author_empty = ModelState( |
| 231 |
"testapp", "Author", [("id", models.AutoField(primary_key=True))] |
| 232 |
) |
| 233 |
author_name = ModelState( |
| 234 |
"testapp", |
| 235 |
"Author", |
| 236 |
[ |
| 237 |
("id", models.AutoField(primary_key=True)), |
| 238 |
("name", models.CharField(max_length=200)), |
| 239 |
], |
| 240 |
) |
| 241 |
author_name_null = ModelState( |
| 242 |
"testapp", |
| 243 |
"Author", |
| 244 |
[ |
| 245 |
("id", models.AutoField(primary_key=True)), |
| 246 |
("name", models.CharField(max_length=200, null=True)), |
| 247 |
], |
| 248 |
) |
| 249 |
author_name_longer = ModelState( |
| 250 |
"testapp", |
| 251 |
"Author", |
| 252 |
[ |
| 253 |
("id", models.AutoField(primary_key=True)), |
| 254 |
("name", models.CharField(max_length=400)), |
| 255 |
], |
| 256 |
) |
| 257 |
author_name_renamed = ModelState( |
| 258 |
"testapp", |
| 259 |
"Author", |
| 260 |
[ |
| 261 |
("id", models.AutoField(primary_key=True)), |
| 262 |
("names", models.CharField(max_length=200)), |
| 263 |
], |
| 264 |
) |
| 265 |
author_name_default = ModelState( |
| 266 |
"testapp", |
| 267 |
"Author", |
| 268 |
[ |
| 269 |
("id", models.AutoField(primary_key=True)), |
| 270 |
("name", models.CharField(max_length=200, default="Ada Lovelace")), |
| 271 |
], |
| 272 |
) |
| 273 |
author_name_db_default = ModelState( |
| 274 |
"testapp", |
| 275 |
"Author", |
| 276 |
[ |
| 277 |
("id", models.AutoField(primary_key=True)), |
| 278 |
("name", models.CharField(max_length=200, db_default="Ada Lovelace")), |
| 279 |
], |
| 280 |
) |
| 281 |
author_name_check_constraint = ModelState( |
| 282 |
"testapp", |
| 283 |
"Author", |
| 284 |
[ |
| 285 |
("id", models.AutoField(primary_key=True)), |
| 286 |
("name", models.CharField(max_length=200)), |
| 287 |
], |
| 288 |
{ |
| 289 |
"constraints": [ |
| 290 |
models.CheckConstraint( |
| 291 |
condition=models.Q(name__contains="Bob"), name="name_contains_bob" |
| 292 |
) |
| 293 |
] |
| 294 |
}, |
| 295 |
) |
| 296 |
author_dates_of_birth_auto_now = ModelState( |
| 297 |
"testapp", |
| 298 |
"Author", |
| 299 |
[ |
| 300 |
("id", models.AutoField(primary_key=True)), |
| 301 |
("date_of_birth", models.DateField(auto_now=True)), |
| 302 |
("date_time_of_birth", models.DateTimeField(auto_now=True)), |
| 303 |
("time_of_birth", models.TimeField(auto_now=True)), |
| 304 |
], |
| 305 |
) |
| 306 |
author_dates_of_birth_auto_now_add = ModelState( |
| 307 |
"testapp", |
| 308 |
"Author", |
| 309 |
[ |
| 310 |
("id", models.AutoField(primary_key=True)), |
| 311 |
("date_of_birth", models.DateField(auto_now_add=True)), |
| 312 |
("date_time_of_birth", models.DateTimeField(auto_now_add=True)), |
| 313 |
("time_of_birth", models.TimeField(auto_now_add=True)), |
| 314 |
], |
| 315 |
) |
| 316 |
author_name_deconstructible_1 = ModelState( |
| 317 |
"testapp", |
| 318 |
"Author", |
| 319 |
[ |
| 320 |
("id", models.AutoField(primary_key=True)), |
| 321 |
("name", models.CharField(max_length=200, default=DeconstructibleObject())), |
| 322 |
], |
| 323 |
) |
| 324 |
author_name_deconstructible_2 = ModelState( |
| 325 |
"testapp", |
| 326 |
"Author", |
| 327 |
[ |
| 328 |
("id", models.AutoField(primary_key=True)), |
| 329 |
("name", models.CharField(max_length=200, default=DeconstructibleObject())), |
| 330 |
], |
| 331 |
) |
| 332 |
author_name_deconstructible_3 = ModelState( |
| 333 |
"testapp", |
| 334 |
"Author", |
| 335 |
[ |
| 336 |
("id", models.AutoField(primary_key=True)), |
| 337 |
("name", models.CharField(max_length=200, default=models.IntegerField())), |
| 338 |
], |
| 339 |
) |
| 340 |
author_name_deconstructible_4 = ModelState( |
| 341 |
"testapp", |
| 342 |
"Author", |
| 343 |
[ |
| 344 |
("id", models.AutoField(primary_key=True)), |
| 345 |
("name", models.CharField(max_length=200, default=models.IntegerField())), |
| 346 |
], |
| 347 |
) |
| 348 |
author_name_deconstructible_list_1 = ModelState( |
| 349 |
"testapp", |
| 350 |
"Author", |
| 351 |
[ |
| 352 |
("id", models.AutoField(primary_key=True)), |
| 353 |
( |
| 354 |
"name", |
| 355 |
models.CharField( |
| 356 |
max_length=200, default=[DeconstructibleObject(), 123] |
| 357 |
), |
| 358 |
), |
| 359 |
], |
| 360 |
) |
| 361 |
author_name_deconstructible_list_2 = ModelState( |
| 362 |
"testapp", |
| 363 |
"Author", |
| 364 |
[ |
| 365 |
("id", models.AutoField(primary_key=True)), |
| 366 |
( |
| 367 |
"name", |
| 368 |
models.CharField( |
| 369 |
max_length=200, default=[DeconstructibleObject(), 123] |
| 370 |
), |
| 371 |
), |
| 372 |
], |
| 373 |
) |
| 374 |
author_name_deconstructible_list_3 = ModelState( |
| 375 |
"testapp", |
| 376 |
"Author", |
| 377 |
[ |
| 378 |
("id", models.AutoField(primary_key=True)), |
| 379 |
( |
| 380 |
"name", |
| 381 |
models.CharField( |
| 382 |
max_length=200, default=[DeconstructibleObject(), 999] |
| 383 |
), |
| 384 |
), |
| 385 |
], |
| 386 |
) |
| 387 |
author_name_deconstructible_tuple_1 = ModelState( |
| 388 |
"testapp", |
| 389 |
"Author", |
| 390 |
[ |
| 391 |
("id", models.AutoField(primary_key=True)), |
| 392 |
( |
| 393 |
"name", |
| 394 |
models.CharField( |
| 395 |
max_length=200, default=(DeconstructibleObject(), 123) |
| 396 |
), |
| 397 |
), |
| 398 |
], |
| 399 |
) |
| 400 |
author_name_deconstructible_tuple_2 = ModelState( |
| 401 |
"testapp", |
| 402 |
"Author", |
| 403 |
[ |
| 404 |
("id", models.AutoField(primary_key=True)), |
| 405 |
( |
| 406 |
"name", |
| 407 |
models.CharField( |
| 408 |
max_length=200, default=(DeconstructibleObject(), 123) |
| 409 |
), |
| 410 |
), |
| 411 |
], |
| 412 |
) |
| 413 |
author_name_deconstructible_tuple_3 = ModelState( |
| 414 |
"testapp", |
| 415 |
"Author", |
| 416 |
[ |
| 417 |
("id", models.AutoField(primary_key=True)), |
| 418 |
( |
| 419 |
"name", |
| 420 |
models.CharField( |
| 421 |
max_length=200, default=(DeconstructibleObject(), 999) |
| 422 |
), |
| 423 |
), |
| 424 |
], |
| 425 |
) |
| 426 |
author_name_deconstructible_dict_1 = ModelState( |
| 427 |
"testapp", |
| 428 |
"Author", |
| 429 |
[ |
| 430 |
("id", models.AutoField(primary_key=True)), |
| 431 |
( |
| 432 |
"name", |
| 433 |
models.CharField( |
| 434 |
max_length=200, |
| 435 |
default={"item": DeconstructibleObject(), "otheritem": 123}, |
| 436 |
), |
| 437 |
), |
| 438 |
], |
| 439 |
) |
| 440 |
author_name_deconstructible_dict_2 = ModelState( |
| 441 |
"testapp", |
| 442 |
"Author", |
| 443 |
[ |
| 444 |
("id", models.AutoField(primary_key=True)), |
| 445 |
( |
| 446 |
"name", |
| 447 |
models.CharField( |
| 448 |
max_length=200, |
| 449 |
default={"item": DeconstructibleObject(), "otheritem": 123}, |
| 450 |
), |
| 451 |
), |
| 452 |
], |
| 453 |
) |
| 454 |
author_name_deconstructible_dict_3 = ModelState( |
| 455 |
"testapp", |
| 456 |
"Author", |
| 457 |
[ |
| 458 |
("id", models.AutoField(primary_key=True)), |
| 459 |
( |
| 460 |
"name", |
| 461 |
models.CharField( |
| 462 |
max_length=200, |
| 463 |
default={"item": DeconstructibleObject(), "otheritem": 999}, |
| 464 |
), |
| 465 |
), |
| 466 |
], |
| 467 |
) |
| 468 |
author_name_nested_deconstructible_1 = ModelState( |
| 469 |
"testapp", |
| 470 |
"Author", |
| 471 |
[ |
| 472 |
("id", models.AutoField(primary_key=True)), |
| 473 |
( |
| 474 |
"name", |
| 475 |
models.CharField( |
| 476 |
max_length=200, |
| 477 |
default=DeconstructibleObject( |
| 478 |
DeconstructibleObject(1), |
| 479 |
( |
| 480 |
DeconstructibleObject("t1"), |
| 481 |
DeconstructibleObject("t2"), |
| 482 |
), |
| 483 |
a=DeconstructibleObject("A"), |
| 484 |
b=DeconstructibleObject(B=DeconstructibleObject("c")), |
| 485 |
), |
| 486 |
), |
| 487 |
), |
| 488 |
], |
| 489 |
) |
| 490 |
author_name_nested_deconstructible_2 = ModelState( |
| 491 |
"testapp", |
| 492 |
"Author", |
| 493 |
[ |
| 494 |
("id", models.AutoField(primary_key=True)), |
| 495 |
( |
| 496 |
"name", |
| 497 |
models.CharField( |
| 498 |
max_length=200, |
| 499 |
default=DeconstructibleObject( |
| 500 |
DeconstructibleObject(1), |
| 501 |
( |
| 502 |
DeconstructibleObject("t1"), |
| 503 |
DeconstructibleObject("t2"), |
| 504 |
), |
| 505 |
a=DeconstructibleObject("A"), |
| 506 |
b=DeconstructibleObject(B=DeconstructibleObject("c")), |
| 507 |
), |
| 508 |
), |
| 509 |
), |
| 510 |
], |
| 511 |
) |
| 512 |
author_name_nested_deconstructible_changed_arg = ModelState( |
| 513 |
"testapp", |
| 514 |
"Author", |
| 515 |
[ |
| 516 |
("id", models.AutoField(primary_key=True)), |
| 517 |
( |
| 518 |
"name", |
| 519 |
models.CharField( |
| 520 |
max_length=200, |
| 521 |
default=DeconstructibleObject( |
| 522 |
DeconstructibleObject(1), |
| 523 |
( |
| 524 |
DeconstructibleObject("t1"), |
| 525 |
DeconstructibleObject("t2-changed"), |
| 526 |
), |
| 527 |
a=DeconstructibleObject("A"), |
| 528 |
b=DeconstructibleObject(B=DeconstructibleObject("c")), |
| 529 |
), |
| 530 |
), |
| 531 |
), |
| 532 |
], |
| 533 |
) |
| 534 |
author_name_nested_deconstructible_extra_arg = ModelState( |
| 535 |
"testapp", |
| 536 |
"Author", |
| 537 |
[ |
| 538 |
("id", models.AutoField(primary_key=True)), |
| 539 |
( |
| 540 |
"name", |
| 541 |
models.CharField( |
| 542 |
max_length=200, |
| 543 |
default=DeconstructibleObject( |
| 544 |
DeconstructibleObject(1), |
| 545 |
( |
| 546 |
DeconstructibleObject("t1"), |
| 547 |
DeconstructibleObject("t2"), |
| 548 |
), |
| 549 |
None, |
| 550 |
a=DeconstructibleObject("A"), |
| 551 |
b=DeconstructibleObject(B=DeconstructibleObject("c")), |
| 552 |
), |
| 553 |
), |
| 554 |
), |
| 555 |
], |
| 556 |
) |
| 557 |
author_name_nested_deconstructible_changed_kwarg = ModelState( |
| 558 |
"testapp", |
| 559 |
"Author", |
| 560 |
[ |
| 561 |
("id", models.AutoField(primary_key=True)), |
| 562 |
( |
| 563 |
"name", |
| 564 |
models.CharField( |
| 565 |
max_length=200, |
| 566 |
default=DeconstructibleObject( |
| 567 |
DeconstructibleObject(1), |
| 568 |
( |
| 569 |
DeconstructibleObject("t1"), |
| 570 |
DeconstructibleObject("t2"), |
| 571 |
), |
| 572 |
a=DeconstructibleObject("A"), |
| 573 |
b=DeconstructibleObject(B=DeconstructibleObject("c-changed")), |
| 574 |
), |
| 575 |
), |
| 576 |
), |
| 577 |
], |
| 578 |
) |
| 579 |
author_name_nested_deconstructible_extra_kwarg = ModelState( |
| 580 |
"testapp", |
| 581 |
"Author", |
| 582 |
[ |
| 583 |
("id", models.AutoField(primary_key=True)), |
| 584 |
( |
| 585 |
"name", |
| 586 |
models.CharField( |
| 587 |
max_length=200, |
| 588 |
default=DeconstructibleObject( |
| 589 |
DeconstructibleObject(1), |
| 590 |
( |
| 591 |
DeconstructibleObject("t1"), |
| 592 |
DeconstructibleObject("t2"), |
| 593 |
), |
| 594 |
a=DeconstructibleObject("A"), |
| 595 |
b=DeconstructibleObject(B=DeconstructibleObject("c")), |
| 596 |
c=None, |
| 597 |
), |
| 598 |
), |
| 599 |
), |
| 600 |
], |
| 601 |
) |
| 602 |
author_custom_pk = ModelState( |
| 603 |
"testapp", "Author", [("pk_field", models.IntegerField(primary_key=True))] |
| 604 |
) |
| 605 |
author_with_biography_non_blank = ModelState( |
| 606 |
"testapp", |
| 607 |
"Author", |
| 608 |
[ |
| 609 |
("id", models.AutoField(primary_key=True)), |
| 610 |
("name", models.CharField()), |
| 611 |
("biography", models.TextField()), |
| 612 |
], |
| 613 |
) |
| 614 |
author_with_biography_blank = ModelState( |
| 615 |
"testapp", |
| 616 |
"Author", |
| 617 |
[ |
| 618 |
("id", models.AutoField(primary_key=True)), |
| 619 |
("name", models.CharField(blank=True)), |
| 620 |
("biography", models.TextField(blank=True)), |
| 621 |
], |
| 622 |
) |
| 623 |
author_with_book = ModelState( |
| 624 |
"testapp", |
| 625 |
"Author", |
| 626 |
[ |
| 627 |
("id", models.AutoField(primary_key=True)), |
| 628 |
("name", models.CharField(max_length=200)), |
| 629 |
("book", models.ForeignKey("otherapp.Book", models.CASCADE)), |
| 630 |
], |
| 631 |
) |
| 632 |
author_with_book_order_wrt = ModelState( |
| 633 |
"testapp", |
| 634 |
"Author", |
| 635 |
[ |
| 636 |
("id", models.AutoField(primary_key=True)), |
| 637 |
("name", models.CharField(max_length=200)), |
| 638 |
("book", models.ForeignKey("otherapp.Book", models.CASCADE)), |
| 639 |
], |
| 640 |
options={"order_with_respect_to": "book"}, |
| 641 |
) |
| 642 |
author_renamed_with_book = ModelState( |
| 643 |
"testapp", |
| 644 |
"Writer", |
| 645 |
[ |
| 646 |
("id", models.AutoField(primary_key=True)), |
| 647 |
("name", models.CharField(max_length=200)), |
| 648 |
("book", models.ForeignKey("otherapp.Book", models.CASCADE)), |
| 649 |
], |
| 650 |
) |
| 651 |
author_with_publisher_string = ModelState( |
| 652 |
"testapp", |
| 653 |
"Author", |
| 654 |
[ |
| 655 |
("id", models.AutoField(primary_key=True)), |
| 656 |
("name", models.CharField(max_length=200)), |
| 657 |
("publisher_name", models.CharField(max_length=200)), |
| 658 |
], |
| 659 |
) |
| 660 |
author_with_publisher = ModelState( |
| 661 |
"testapp", |
| 662 |
"Author", |
| 663 |
[ |
| 664 |
("id", models.AutoField(primary_key=True)), |
| 665 |
("name", models.CharField(max_length=200)), |
| 666 |
("publisher", models.ForeignKey("testapp.Publisher", models.CASCADE)), |
| 667 |
], |
| 668 |
) |
| 669 |
author_with_user = ModelState( |
| 670 |
"testapp", |
| 671 |
"Author", |
| 672 |
[ |
| 673 |
("id", models.AutoField(primary_key=True)), |
| 674 |
("name", models.CharField(max_length=200)), |
| 675 |
("user", models.ForeignKey("auth.User", models.CASCADE)), |
| 676 |
], |
| 677 |
) |
| 678 |
author_with_custom_user = ModelState( |
| 679 |
"testapp", |
| 680 |
"Author", |
| 681 |
[ |
| 682 |
("id", models.AutoField(primary_key=True)), |
| 683 |
("name", models.CharField(max_length=200)), |
| 684 |
("user", models.ForeignKey("thirdapp.CustomUser", models.CASCADE)), |
| 685 |
], |
| 686 |
) |
| 687 |
author_proxy = ModelState( |
| 688 |
"testapp", "AuthorProxy", [], {"proxy": True}, ("testapp.author",) |
| 689 |
) |
| 690 |
author_proxy_options = ModelState( |
| 691 |
"testapp", |
| 692 |
"AuthorProxy", |
| 693 |
[], |
| 694 |
{ |
| 695 |
"proxy": True, |
| 696 |
"verbose_name": "Super Author", |
| 697 |
}, |
| 698 |
("testapp.author",), |
| 699 |
) |
| 700 |
author_proxy_notproxy = ModelState( |
| 701 |
"testapp", "AuthorProxy", [], {}, ("testapp.author",) |
| 702 |
) |
| 703 |
author_proxy_third = ModelState( |
| 704 |
"thirdapp", "AuthorProxy", [], {"proxy": True}, ("testapp.author",) |
| 705 |
) |
| 706 |
author_proxy_third_notproxy = ModelState( |
| 707 |
"thirdapp", "AuthorProxy", [], {}, ("testapp.author",) |
| 708 |
) |
| 709 |
author_proxy_proxy = ModelState( |
| 710 |
"testapp", "AAuthorProxyProxy", [], {"proxy": True}, ("testapp.authorproxy",) |
| 711 |
) |
| 712 |
author_unmanaged = ModelState( |
| 713 |
"testapp", "AuthorUnmanaged", [], {"managed": False}, ("testapp.author",) |
| 714 |
) |
| 715 |
author_unmanaged_managed = ModelState( |
| 716 |
"testapp", "AuthorUnmanaged", [], {}, ("testapp.author",) |
| 717 |
) |
| 718 |
author_unmanaged_default_pk = ModelState( |
| 719 |
"testapp", "Author", [("id", models.AutoField(primary_key=True))] |
| 720 |
) |
| 721 |
author_unmanaged_custom_pk = ModelState( |
| 722 |
"testapp", |
| 723 |
"Author", |
| 724 |
[ |
| 725 |
("pk_field", models.IntegerField(primary_key=True)), |
| 726 |
], |
| 727 |
) |
| 728 |
author_with_m2m = ModelState( |
| 729 |
"testapp", |
| 730 |
"Author", |
| 731 |
[ |
| 732 |
("id", models.AutoField(primary_key=True)), |
| 733 |
("publishers", models.ManyToManyField("testapp.Publisher")), |
| 734 |
], |
| 735 |
) |
| 736 |
author_with_m2m_blank = ModelState( |
| 737 |
"testapp", |
| 738 |
"Author", |
| 739 |
[ |
| 740 |
("id", models.AutoField(primary_key=True)), |
| 741 |
("publishers", models.ManyToManyField("testapp.Publisher", blank=True)), |
| 742 |
], |
| 743 |
) |
| 744 |
author_with_m2m_through = ModelState( |
| 745 |
"testapp", |
| 746 |
"Author", |
| 747 |
[ |
| 748 |
("id", models.AutoField(primary_key=True)), |
| 749 |
( |
| 750 |
"publishers", |
| 751 |
models.ManyToManyField("testapp.Publisher", through="testapp.Contract"), |
| 752 |
), |
| 753 |
], |
| 754 |
) |
| 755 |
author_with_renamed_m2m_through = ModelState( |
| 756 |
"testapp", |
| 757 |
"Author", |
| 758 |
[ |
| 759 |
("id", models.AutoField(primary_key=True)), |
| 760 |
( |
| 761 |
"publishers", |
| 762 |
models.ManyToManyField("testapp.Publisher", through="testapp.Deal"), |
| 763 |
), |
| 764 |
], |
| 765 |
) |
| 766 |
author_with_former_m2m = ModelState( |
| 767 |
"testapp", |
| 768 |
"Author", |
| 769 |
[ |
| 770 |
("id", models.AutoField(primary_key=True)), |
| 771 |
("publishers", models.CharField(max_length=100)), |
| 772 |
], |
| 773 |
) |
| 774 |
author_with_options = ModelState( |
| 775 |
"testapp", |
| 776 |
"Author", |
| 777 |
[ |
| 778 |
("id", models.AutoField(primary_key=True)), |
| 779 |
], |
| 780 |
{ |
| 781 |
"permissions": [("can_hire", "Can hire")], |
| 782 |
"verbose_name": "Authi", |
| 783 |
}, |
| 784 |
) |
| 785 |
author_with_db_table_comment = ModelState( |
| 786 |
"testapp", |
| 787 |
"Author", |
| 788 |
[ |
| 789 |
("id", models.AutoField(primary_key=True)), |
| 790 |
], |
| 791 |
{"db_table_comment": "Table comment"}, |
| 792 |
) |
| 793 |
author_with_db_table_options = ModelState( |
| 794 |
"testapp", |
| 795 |
"Author", |
| 796 |
[ |
| 797 |
("id", models.AutoField(primary_key=True)), |
| 798 |
], |
| 799 |
{"db_table": "author_one"}, |
| 800 |
) |
| 801 |
author_with_new_db_table_options = ModelState( |
| 802 |
"testapp", |
| 803 |
"Author", |
| 804 |
[ |
| 805 |
("id", models.AutoField(primary_key=True)), |
| 806 |
], |
| 807 |
{"db_table": "author_two"}, |
| 808 |
) |
| 809 |
author_renamed_with_db_table_options = ModelState( |
| 810 |
"testapp", |
| 811 |
"NewAuthor", |
| 812 |
[ |
| 813 |
("id", models.AutoField(primary_key=True)), |
| 814 |
], |
| 815 |
{"db_table": "author_one"}, |
| 816 |
) |
| 817 |
author_renamed_with_new_db_table_options = ModelState( |
| 818 |
"testapp", |
| 819 |
"NewAuthor", |
| 820 |
[ |
| 821 |
("id", models.AutoField(primary_key=True)), |
| 822 |
], |
| 823 |
{"db_table": "author_three"}, |
| 824 |
) |
| 825 |
contract = ModelState( |
| 826 |
"testapp", |
| 827 |
"Contract", |
| 828 |
[ |
| 829 |
("id", models.AutoField(primary_key=True)), |
| 830 |
("author", models.ForeignKey("testapp.Author", models.CASCADE)), |
| 831 |
("publisher", models.ForeignKey("testapp.Publisher", models.CASCADE)), |
| 832 |
], |
| 833 |
) |
| 834 |
contract_renamed = ModelState( |
| 835 |
"testapp", |
| 836 |
"Deal", |
| 837 |
[ |
| 838 |
("id", models.AutoField(primary_key=True)), |
| 839 |
("author", models.ForeignKey("testapp.Author", models.CASCADE)), |
| 840 |
("publisher", models.ForeignKey("testapp.Publisher", models.CASCADE)), |
| 841 |
], |
| 842 |
) |
| 843 |
publisher = ModelState( |
| 844 |
"testapp", |
| 845 |
"Publisher", |
| 846 |
[ |
| 847 |
("id", models.AutoField(primary_key=True)), |
| 848 |
("name", models.CharField(max_length=100)), |
| 849 |
], |
| 850 |
) |
| 851 |
publisher_with_author = ModelState( |
| 852 |
"testapp", |
| 853 |
"Publisher", |
| 854 |
[ |
| 855 |
("id", models.AutoField(primary_key=True)), |
| 856 |
("author", models.ForeignKey("testapp.Author", models.CASCADE)), |
| 857 |
("name", models.CharField(max_length=100)), |
| 858 |
], |
| 859 |
) |
| 860 |
publisher_with_aardvark_author = ModelState( |
| 861 |
"testapp", |
| 862 |
"Publisher", |
| 863 |
[ |
| 864 |
("id", models.AutoField(primary_key=True)), |
| 865 |
("author", models.ForeignKey("testapp.Aardvark", models.CASCADE)), |
| 866 |
("name", models.CharField(max_length=100)), |
| 867 |
], |
| 868 |
) |
| 869 |
publisher_with_book = ModelState( |
| 870 |
"testapp", |
| 871 |
"Publisher", |
| 872 |
[ |
| 873 |
("id", models.AutoField(primary_key=True)), |
| 874 |
("author", models.ForeignKey("otherapp.Book", models.CASCADE)), |
| 875 |
("name", models.CharField(max_length=100)), |
| 876 |
], |
| 877 |
) |
| 878 |
other_pony = ModelState( |
| 879 |
"otherapp", |
| 880 |
"Pony", |
| 881 |
[ |
| 882 |
("id", models.AutoField(primary_key=True)), |
| 883 |
], |
| 884 |
) |
| 885 |
other_pony_food = ModelState( |
| 886 |
"otherapp", |
| 887 |
"Pony", |
| 888 |
[ |
| 889 |
("id", models.AutoField(primary_key=True)), |
| 890 |
], |
| 891 |
managers=[ |
| 892 |
("food_qs", FoodQuerySet.as_manager()), |
| 893 |
("food_mgr", FoodManager("a", "b")), |
| 894 |
("food_mgr_kwargs", FoodManager("x", "y", 3, 4)), |
| 895 |
], |
| 896 |
) |
| 897 |
other_stable = ModelState( |
| 898 |
"otherapp", "Stable", [("id", models.AutoField(primary_key=True))] |
| 899 |
) |
| 900 |
third_thing = ModelState( |
| 901 |
"thirdapp", "Thing", [("id", models.AutoField(primary_key=True))] |
| 902 |
) |
| 903 |
book = ModelState( |
| 904 |
"otherapp", |
| 905 |
"Book", |
| 906 |
[ |
| 907 |
("id", models.AutoField(primary_key=True)), |
| 908 |
("author", models.ForeignKey("testapp.Author", models.CASCADE)), |
| 909 |
("title", models.CharField(max_length=200)), |
| 910 |
], |
| 911 |
) |
| 912 |
book_proxy_fk = ModelState( |
| 913 |
"otherapp", |
| 914 |
"Book", |
| 915 |
[ |
| 916 |
("id", models.AutoField(primary_key=True)), |
| 917 |
("author", models.ForeignKey("thirdapp.AuthorProxy", models.CASCADE)), |
| 918 |
("title", models.CharField(max_length=200)), |
| 919 |
], |
| 920 |
) |
| 921 |
book_proxy_proxy_fk = ModelState( |
| 922 |
"otherapp", |
| 923 |
"Book", |
| 924 |
[ |
| 925 |
("id", models.AutoField(primary_key=True)), |
| 926 |
("author", models.ForeignKey("testapp.AAuthorProxyProxy", models.CASCADE)), |
| 927 |
], |
| 928 |
) |
| 929 |
book_migrations_fk = ModelState( |
| 930 |
"otherapp", |
| 931 |
"Book", |
| 932 |
[ |
| 933 |
("id", models.AutoField(primary_key=True)), |
| 934 |
("author", models.ForeignKey("migrations.UnmigratedModel", models.CASCADE)), |
| 935 |
("title", models.CharField(max_length=200)), |
| 936 |
], |
| 937 |
) |
| 938 |
book_with_no_author_fk = ModelState( |
| 939 |
"otherapp", |
| 940 |
"Book", |
| 941 |
[ |
| 942 |
("id", models.AutoField(primary_key=True)), |
| 943 |
("author", models.IntegerField()), |
| 944 |
("title", models.CharField(max_length=200)), |
| 945 |
], |
| 946 |
) |
| 947 |
book_with_no_author = ModelState( |
| 948 |
"otherapp", |
| 949 |
"Book", |
| 950 |
[ |
| 951 |
("id", models.AutoField(primary_key=True)), |
| 952 |
("title", models.CharField(max_length=200)), |
| 953 |
], |
| 954 |
) |
| 955 |
book_with_author_renamed = ModelState( |
| 956 |
"otherapp", |
| 957 |
"Book", |
| 958 |
[ |
| 959 |
("id", models.AutoField(primary_key=True)), |
| 960 |
("author", models.ForeignKey("testapp.Writer", models.CASCADE)), |
| 961 |
("title", models.CharField(max_length=200)), |
| 962 |
], |
| 963 |
) |
| 964 |
book_with_field_and_author_renamed = ModelState( |
| 965 |
"otherapp", |
| 966 |
"Book", |
| 967 |
[ |
| 968 |
("id", models.AutoField(primary_key=True)), |
| 969 |
("writer", models.ForeignKey("testapp.Writer", models.CASCADE)), |
| 970 |
("title", models.CharField(max_length=200)), |
| 971 |
], |
| 972 |
) |
| 973 |
book_with_multiple_authors = ModelState( |
| 974 |
"otherapp", |
| 975 |
"Book", |
| 976 |
[ |
| 977 |
("id", models.AutoField(primary_key=True)), |
| 978 |
("authors", models.ManyToManyField("testapp.Author")), |
| 979 |
("title", models.CharField(max_length=200)), |
| 980 |
], |
| 981 |
) |
| 982 |
book_with_multiple_authors_through_attribution = ModelState( |
| 983 |
"otherapp", |
| 984 |
"Book", |
| 985 |
[ |
| 986 |
("id", models.AutoField(primary_key=True)), |
| 987 |
( |
| 988 |
"authors", |
| 989 |
models.ManyToManyField( |
| 990 |
"testapp.Author", through="otherapp.Attribution" |
| 991 |
), |
| 992 |
), |
| 993 |
("title", models.CharField(max_length=200)), |
| 994 |
], |
| 995 |
) |
| 996 |
book_indexes = ModelState( |
| 997 |
"otherapp", |
| 998 |
"Book", |
| 999 |
[ |
| 1000 |
("id", models.AutoField(primary_key=True)), |
| 1001 |
("author", models.ForeignKey("testapp.Author", models.CASCADE)), |
| 1002 |
("title", models.CharField(max_length=200)), |
| 1003 |
], |
| 1004 |
{ |
| 1005 |
"indexes": [ |
| 1006 |
models.Index(fields=["author", "title"], name="book_title_author_idx") |
| 1007 |
], |
| 1008 |
}, |
| 1009 |
) |
| 1010 |
book_unordered_indexes = ModelState( |
| 1011 |
"otherapp", |
| 1012 |
"Book", |
| 1013 |
[ |
| 1014 |
("id", models.AutoField(primary_key=True)), |
| 1015 |
("author", models.ForeignKey("testapp.Author", models.CASCADE)), |
| 1016 |
("title", models.CharField(max_length=200)), |
| 1017 |
], |
| 1018 |
{ |
| 1019 |
"indexes": [ |
| 1020 |
models.Index(fields=["title", "author"], name="book_author_title_idx") |
| 1021 |
], |
| 1022 |
}, |
| 1023 |
) |
| 1024 |
book_unique_together = ModelState( |
| 1025 |
"otherapp", |
| 1026 |
"Book", |
| 1027 |
[ |
| 1028 |
("id", models.AutoField(primary_key=True)), |
| 1029 |
("author", models.ForeignKey("testapp.Author", models.CASCADE)), |
| 1030 |
("title", models.CharField(max_length=200)), |
| 1031 |
], |
| 1032 |
{ |
| 1033 |
"unique_together": {("author", "title")}, |
| 1034 |
}, |
| 1035 |
) |
| 1036 |
book_unique_together_2 = ModelState( |
| 1037 |
"otherapp", |
| 1038 |
"Book", |
| 1039 |
[ |
| 1040 |
("id", models.AutoField(primary_key=True)), |
| 1041 |
("author", models.ForeignKey("testapp.Author", models.CASCADE)), |
| 1042 |
("title", models.CharField(max_length=200)), |
| 1043 |
], |
| 1044 |
{ |
| 1045 |
"unique_together": {("title", "author")}, |
| 1046 |
}, |
| 1047 |
) |
| 1048 |
book_unique_together_3 = ModelState( |
| 1049 |
"otherapp", |
| 1050 |
"Book", |
| 1051 |
[ |
| 1052 |
("id", models.AutoField(primary_key=True)), |
| 1053 |
("newfield", models.IntegerField()), |
| 1054 |
("author", models.ForeignKey("testapp.Author", models.CASCADE)), |
| 1055 |
("title", models.CharField(max_length=200)), |
| 1056 |
], |
| 1057 |
{ |
| 1058 |
"unique_together": {("title", "newfield")}, |
| 1059 |
}, |
| 1060 |
) |
| 1061 |
book_unique_together_4 = ModelState( |
| 1062 |
"otherapp", |
| 1063 |
"Book", |
| 1064 |
[ |
| 1065 |
("id", models.AutoField(primary_key=True)), |
| 1066 |
("newfield2", models.IntegerField()), |
| 1067 |
("author", models.ForeignKey("testapp.Author", models.CASCADE)), |
| 1068 |
("title", models.CharField(max_length=200)), |
| 1069 |
], |
| 1070 |
{ |
| 1071 |
"unique_together": {("title", "newfield2")}, |
| 1072 |
}, |
| 1073 |
) |
| 1074 |
attribution = ModelState( |
| 1075 |
"otherapp", |
| 1076 |
"Attribution", |
| 1077 |
[ |
| 1078 |
("id", models.AutoField(primary_key=True)), |
| 1079 |
("author", models.ForeignKey("testapp.Author", models.CASCADE)), |
| 1080 |
("book", models.ForeignKey("otherapp.Book", models.CASCADE)), |
| 1081 |
], |
| 1082 |
) |
| 1083 |
edition = ModelState( |
| 1084 |
"thirdapp", |
| 1085 |
"Edition", |
| 1086 |
[ |
| 1087 |
("id", models.AutoField(primary_key=True)), |
| 1088 |
("book", models.ForeignKey("otherapp.Book", models.CASCADE)), |
| 1089 |
], |
| 1090 |
) |
| 1091 |
custom_user = ModelState( |
| 1092 |
"thirdapp", |
| 1093 |
"CustomUser", |
| 1094 |
[ |
| 1095 |
("id", models.AutoField(primary_key=True)), |
| 1096 |
("username", models.CharField(max_length=255)), |
| 1097 |
], |
| 1098 |
bases=(AbstractBaseUser,), |
| 1099 |
) |
| 1100 |
custom_user_no_inherit = ModelState( |
| 1101 |
"thirdapp", |
| 1102 |
"CustomUser", |
| 1103 |
[ |
| 1104 |
("id", models.AutoField(primary_key=True)), |
| 1105 |
("username", models.CharField(max_length=255)), |
| 1106 |
], |
| 1107 |
) |
| 1108 |
aardvark = ModelState( |
| 1109 |
"thirdapp", "Aardvark", [("id", models.AutoField(primary_key=True))] |
| 1110 |
) |
| 1111 |
aardvark_testapp = ModelState( |
| 1112 |
"testapp", "Aardvark", [("id", models.AutoField(primary_key=True))] |
| 1113 |
) |
| 1114 |
aardvark_based_on_author = ModelState( |
| 1115 |
"testapp", "Aardvark", [], bases=("testapp.Author",) |
| 1116 |
) |
| 1117 |
aardvark_pk_fk_author = ModelState( |
| 1118 |
"testapp", |
| 1119 |
"Aardvark", |
| 1120 |
[ |
| 1121 |
( |
| 1122 |
"id", |
| 1123 |
models.OneToOneField( |
| 1124 |
"testapp.Author", models.CASCADE, primary_key=True |
| 1125 |
), |
| 1126 |
), |
| 1127 |
], |
| 1128 |
) |
| 1129 |
knight = ModelState("eggs", "Knight", [("id", models.AutoField(primary_key=True))]) |
| 1130 |
rabbit = ModelState( |
| 1131 |
"eggs", |
| 1132 |
"Rabbit", |
| 1133 |
[ |
| 1134 |
("id", models.AutoField(primary_key=True)), |
| 1135 |
("knight", models.ForeignKey("eggs.Knight", models.CASCADE)), |
| 1136 |
("parent", models.ForeignKey("eggs.Rabbit", models.CASCADE)), |
| 1137 |
], |
| 1138 |
{ |
| 1139 |
"unique_together": {("parent", "knight")}, |
| 1140 |
"indexes": [ |
| 1141 |
models.Index( |
| 1142 |
fields=["parent", "knight"], name="rabbit_circular_fk_index" |
| 1143 |
) |
| 1144 |
], |
| 1145 |
}, |
| 1146 |
) |
| 1147 |
|
| 1148 |
def test_arrange_for_graph(self): |
| 1149 |
"""Tests auto-naming of migrations for graph matching.""" |
| 1150 |
# Make a fake graph |
| 1151 |
graph = MigrationGraph() |
| 1152 |
graph.add_node(("testapp", "0001_initial"), None) |
| 1153 |
graph.add_node(("testapp", "0002_foobar"), None) |
| 1154 |
graph.add_node(("otherapp", "0001_initial"), None) |
| 1155 |
graph.add_dependency( |
| 1156 |
"testapp.0002_foobar", |
| 1157 |
("testapp", "0002_foobar"), |
| 1158 |
("testapp", "0001_initial"), |
| 1159 |
) |
| 1160 |
graph.add_dependency( |
| 1161 |
"testapp.0002_foobar", |
| 1162 |
("testapp", "0002_foobar"), |
| 1163 |
("otherapp", "0001_initial"), |
| 1164 |
) |
| 1165 |
# Use project state to make a new migration change set |
| 1166 |
before = self.make_project_state([self.publisher, self.other_pony]) |
| 1167 |
after = self.make_project_state( |
| 1168 |
[ |
| 1169 |
self.author_empty, |
| 1170 |
self.publisher, |
| 1171 |
self.other_pony, |
| 1172 |
self.other_stable, |
| 1173 |
] |
| 1174 |
) |
| 1175 |
autodetector = MigrationAutodetector(before, after) |
| 1176 |
changes = autodetector._detect_changes() |
| 1177 |
# Run through arrange_for_graph |
| 1178 |
changes = autodetector.arrange_for_graph(changes, graph) |
| 1179 |
# Make sure there's a new name, deps match, etc. |
| 1180 |
self.assertEqual(changes["testapp"][0].name, "0003_author") |
| 1181 |
self.assertEqual( |
| 1182 |
changes["testapp"][0].dependencies, [("testapp", "0002_foobar")] |
| 1183 |
) |
| 1184 |
self.assertEqual(changes["otherapp"][0].name, "0002_stable") |
| 1185 |
self.assertEqual( |
| 1186 |
changes["otherapp"][0].dependencies, [("otherapp", "0001_initial")] |
| 1187 |
) |
| 1188 |
|
| 1189 |
def test_arrange_for_graph_with_multiple_initial(self): |
| 1190 |
# Make a fake graph. |
| 1191 |
graph = MigrationGraph() |
| 1192 |
# Use project state to make a new migration change set. |
| 1193 |
before = self.make_project_state([]) |
| 1194 |
after = self.make_project_state( |
| 1195 |
[self.author_with_book, self.book, self.attribution] |
| 1196 |
) |
| 1197 |
autodetector = MigrationAutodetector( |
| 1198 |
before, after, MigrationQuestioner({"ask_initial": True}) |
| 1199 |
) |
| 1200 |
changes = autodetector._detect_changes() |
| 1201 |
changes = autodetector.arrange_for_graph(changes, graph) |
| 1202 |
|
| 1203 |
self.assertEqual(changes["otherapp"][0].name, "0001_initial") |
| 1204 |
self.assertEqual(changes["otherapp"][0].dependencies, []) |
| 1205 |
self.assertEqual(changes["otherapp"][1].name, "0002_initial") |
| 1206 |
self.assertCountEqual( |
| 1207 |
changes["otherapp"][1].dependencies, |
| 1208 |
[("testapp", "0001_initial"), ("otherapp", "0001_initial")], |
| 1209 |
) |
| 1210 |
self.assertEqual(changes["testapp"][0].name, "0001_initial") |
| 1211 |
self.assertEqual( |
| 1212 |
changes["testapp"][0].dependencies, [("otherapp", "0001_initial")] |
| 1213 |
) |
| 1214 |
|
| 1215 |
def test_trim_apps(self): |
| 1216 |
""" |
| 1217 |
Trim does not remove dependencies but does remove unwanted apps. |
| 1218 |
""" |
| 1219 |
# Use project state to make a new migration change set |
| 1220 |
before = self.make_project_state([]) |
| 1221 |
after = self.make_project_state( |
| 1222 |
[self.author_empty, self.other_pony, self.other_stable, self.third_thing] |
| 1223 |
) |
| 1224 |
autodetector = MigrationAutodetector( |
| 1225 |
before, after, MigrationQuestioner({"ask_initial": True}) |
| 1226 |
) |
| 1227 |
changes = autodetector._detect_changes() |
| 1228 |
# Run through arrange_for_graph |
| 1229 |
graph = MigrationGraph() |
| 1230 |
changes = autodetector.arrange_for_graph(changes, graph) |
| 1231 |
changes["testapp"][0].dependencies.append(("otherapp", "0001_initial")) |
| 1232 |
changes = autodetector._trim_to_apps(changes, {"testapp"}) |
| 1233 |
# Make sure there's the right set of migrations |
| 1234 |
self.assertEqual(changes["testapp"][0].name, "0001_initial") |
| 1235 |
self.assertEqual(changes["otherapp"][0].name, "0001_initial") |
| 1236 |
self.assertNotIn("thirdapp", changes) |
| 1237 |
|
| 1238 |
def test_custom_migration_name(self): |
| 1239 |
"""Tests custom naming of migrations for graph matching.""" |
| 1240 |
# Make a fake graph |
| 1241 |
graph = MigrationGraph() |
| 1242 |
graph.add_node(("testapp", "0001_initial"), None) |
| 1243 |
graph.add_node(("testapp", "0002_foobar"), None) |
| 1244 |
graph.add_node(("otherapp", "0001_initial"), None) |
| 1245 |
graph.add_dependency( |
| 1246 |
"testapp.0002_foobar", |
| 1247 |
("testapp", "0002_foobar"), |
| 1248 |
("testapp", "0001_initial"), |
| 1249 |
) |
| 1250 |
|
| 1251 |
# Use project state to make a new migration change set |
| 1252 |
before = self.make_project_state([]) |
| 1253 |
after = self.make_project_state( |
| 1254 |
[self.author_empty, self.other_pony, self.other_stable] |
| 1255 |
) |
| 1256 |
autodetector = MigrationAutodetector(before, after) |
| 1257 |
changes = autodetector._detect_changes() |
| 1258 |
|
| 1259 |
# Run through arrange_for_graph |
| 1260 |
migration_name = "custom_name" |
| 1261 |
changes = autodetector.arrange_for_graph(changes, graph, migration_name) |
| 1262 |
|
| 1263 |
# Make sure there's a new name, deps match, etc. |
| 1264 |
self.assertEqual(changes["testapp"][0].name, "0003_%s" % migration_name) |
| 1265 |
self.assertEqual( |
| 1266 |
changes["testapp"][0].dependencies, [("testapp", "0002_foobar")] |
| 1267 |
) |
| 1268 |
self.assertEqual(changes["otherapp"][0].name, "0002_%s" % migration_name) |
| 1269 |
self.assertEqual( |
| 1270 |
changes["otherapp"][0].dependencies, [("otherapp", "0001_initial")] |
| 1271 |
) |
| 1272 |
|
| 1273 |
def test_new_model(self): |
| 1274 |
"""Tests autodetection of new models.""" |
| 1275 |
changes = self.get_changes([], [self.other_pony_food]) |
| 1276 |
# Right number/type of migrations? |
| 1277 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 1278 |
self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"]) |
| 1279 |
self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Pony") |
| 1280 |
self.assertEqual( |
| 1281 |
[name for name, mgr in changes["otherapp"][0].operations[0].managers], |
| 1282 |
["food_qs", "food_mgr", "food_mgr_kwargs"], |
| 1283 |
) |
| 1284 |
|
| 1285 |
def test_old_model(self): |
| 1286 |
"""Tests deletion of old models.""" |
| 1287 |
changes = self.get_changes([self.author_empty], []) |
| 1288 |
# Right number/type of migrations? |
| 1289 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1290 |
self.assertOperationTypes(changes, "testapp", 0, ["DeleteModel"]) |
| 1291 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author") |
| 1292 |
|
| 1293 |
def test_add_field(self): |
| 1294 |
"""Tests autodetection of new fields.""" |
| 1295 |
changes = self.get_changes([self.author_empty], [self.author_name]) |
| 1296 |
# Right number/type of migrations? |
| 1297 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1298 |
self.assertOperationTypes(changes, "testapp", 0, ["AddField"]) |
| 1299 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="name") |
| 1300 |
|
| 1301 |
@mock.patch( |
| 1302 |
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition", |
| 1303 |
side_effect=AssertionError("Should not have prompted for not null addition"), |
| 1304 |
) |
| 1305 |
def test_add_not_null_field_with_db_default(self, mocked_ask_method): |
| 1306 |
changes = self.get_changes([self.author_empty], [self.author_name_db_default]) |
| 1307 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1308 |
self.assertOperationTypes(changes, "testapp", 0, ["AddField"]) |
| 1309 |
self.assertOperationAttributes( |
| 1310 |
changes, "testapp", 0, 0, name="name", preserve_default=True |
| 1311 |
) |
| 1312 |
self.assertOperationFieldAttributes( |
| 1313 |
changes, "testapp", 0, 0, db_default="Ada Lovelace" |
| 1314 |
) |
| 1315 |
|
| 1316 |
@mock.patch( |
| 1317 |
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition", |
| 1318 |
side_effect=AssertionError("Should not have prompted for not null addition"), |
| 1319 |
) |
| 1320 |
def test_add_date_fields_with_auto_now_not_asking_for_default( |
| 1321 |
self, mocked_ask_method |
| 1322 |
): |
| 1323 |
changes = self.get_changes( |
| 1324 |
[self.author_empty], [self.author_dates_of_birth_auto_now] |
| 1325 |
) |
| 1326 |
# Right number/type of migrations? |
| 1327 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1328 |
self.assertOperationTypes( |
| 1329 |
changes, "testapp", 0, ["AddField", "AddField", "AddField"] |
| 1330 |
) |
| 1331 |
self.assertOperationFieldAttributes(changes, "testapp", 0, 0, auto_now=True) |
| 1332 |
self.assertOperationFieldAttributes(changes, "testapp", 0, 1, auto_now=True) |
| 1333 |
self.assertOperationFieldAttributes(changes, "testapp", 0, 2, auto_now=True) |
| 1334 |
|
| 1335 |
@mock.patch( |
| 1336 |
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition", |
| 1337 |
side_effect=AssertionError("Should not have prompted for not null addition"), |
| 1338 |
) |
| 1339 |
def test_add_date_fields_with_auto_now_add_not_asking_for_null_addition( |
| 1340 |
self, mocked_ask_method |
| 1341 |
): |
| 1342 |
changes = self.get_changes( |
| 1343 |
[self.author_empty], [self.author_dates_of_birth_auto_now_add] |
| 1344 |
) |
| 1345 |
# Right number/type of migrations? |
| 1346 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1347 |
self.assertOperationTypes( |
| 1348 |
changes, "testapp", 0, ["AddField", "AddField", "AddField"] |
| 1349 |
) |
| 1350 |
self.assertOperationFieldAttributes(changes, "testapp", 0, 0, auto_now_add=True) |
| 1351 |
self.assertOperationFieldAttributes(changes, "testapp", 0, 1, auto_now_add=True) |
| 1352 |
self.assertOperationFieldAttributes(changes, "testapp", 0, 2, auto_now_add=True) |
| 1353 |
|
| 1354 |
@mock.patch( |
| 1355 |
"django.db.migrations.questioner.MigrationQuestioner.ask_auto_now_add_addition" |
| 1356 |
) |
| 1357 |
def test_add_date_fields_with_auto_now_add_asking_for_default( |
| 1358 |
self, mocked_ask_method |
| 1359 |
): |
| 1360 |
changes = self.get_changes( |
| 1361 |
[self.author_empty], [self.author_dates_of_birth_auto_now_add] |
| 1362 |
) |
| 1363 |
# Right number/type of migrations? |
| 1364 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1365 |
self.assertOperationTypes( |
| 1366 |
changes, "testapp", 0, ["AddField", "AddField", "AddField"] |
| 1367 |
) |
| 1368 |
self.assertOperationFieldAttributes(changes, "testapp", 0, 0, auto_now_add=True) |
| 1369 |
self.assertOperationFieldAttributes(changes, "testapp", 0, 1, auto_now_add=True) |
| 1370 |
self.assertOperationFieldAttributes(changes, "testapp", 0, 2, auto_now_add=True) |
| 1371 |
self.assertEqual(mocked_ask_method.call_count, 3) |
| 1372 |
|
| 1373 |
def test_add_field_before_generated_field(self): |
| 1374 |
initial_state = ModelState( |
| 1375 |
"testapp", |
| 1376 |
"Author", |
| 1377 |
[ |
| 1378 |
("name", models.CharField(max_length=20)), |
| 1379 |
], |
| 1380 |
) |
| 1381 |
updated_state = ModelState( |
| 1382 |
"testapp", |
| 1383 |
"Author", |
| 1384 |
[ |
| 1385 |
("name", models.CharField(max_length=20)), |
| 1386 |
("surname", models.CharField(max_length=20)), |
| 1387 |
( |
| 1388 |
"lower_full_name", |
| 1389 |
models.GeneratedField( |
| 1390 |
expression=Concat(Lower("name"), Lower("surname")), |
| 1391 |
output_field=models.CharField(max_length=30), |
| 1392 |
db_persist=True, |
| 1393 |
), |
| 1394 |
), |
| 1395 |
], |
| 1396 |
) |
| 1397 |
changes = self.get_changes([initial_state], [updated_state]) |
| 1398 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1399 |
self.assertOperationTypes(changes, "testapp", 0, ["AddField", "AddField"]) |
| 1400 |
self.assertOperationFieldAttributes( |
| 1401 |
changes, "testapp", 0, 1, expression=Concat(Lower("name"), Lower("surname")) |
| 1402 |
) |
| 1403 |
|
| 1404 |
def test_add_fk_before_generated_field(self): |
| 1405 |
initial_state = ModelState( |
| 1406 |
"testapp", |
| 1407 |
"Author", |
| 1408 |
[ |
| 1409 |
("name", models.CharField(max_length=20)), |
| 1410 |
], |
| 1411 |
) |
| 1412 |
updated_state = [ |
| 1413 |
ModelState( |
| 1414 |
"testapp", |
| 1415 |
"Publisher", |
| 1416 |
[ |
| 1417 |
("name", models.CharField(max_length=20)), |
| 1418 |
], |
| 1419 |
), |
| 1420 |
ModelState( |
| 1421 |
"testapp", |
| 1422 |
"Author", |
| 1423 |
[ |
| 1424 |
("name", models.CharField(max_length=20)), |
| 1425 |
( |
| 1426 |
"publisher", |
| 1427 |
models.ForeignKey("testapp.Publisher", models.CASCADE), |
| 1428 |
), |
| 1429 |
( |
| 1430 |
"lower_full_name", |
| 1431 |
models.GeneratedField( |
| 1432 |
expression=Concat("name", "publisher_id"), |
| 1433 |
output_field=models.CharField(max_length=20), |
| 1434 |
db_persist=True, |
| 1435 |
), |
| 1436 |
), |
| 1437 |
], |
| 1438 |
), |
| 1439 |
] |
| 1440 |
changes = self.get_changes([initial_state], updated_state) |
| 1441 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1442 |
self.assertOperationTypes( |
| 1443 |
changes, "testapp", 0, ["CreateModel", "AddField", "AddField"] |
| 1444 |
) |
| 1445 |
self.assertOperationFieldAttributes( |
| 1446 |
changes, "testapp", 0, 2, expression=Concat("name", "publisher_id") |
| 1447 |
) |
| 1448 |
|
| 1449 |
def test_remove_field(self): |
| 1450 |
"""Tests autodetection of removed fields.""" |
| 1451 |
changes = self.get_changes([self.author_name], [self.author_empty]) |
| 1452 |
# Right number/type of migrations? |
| 1453 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1454 |
self.assertOperationTypes(changes, "testapp", 0, ["RemoveField"]) |
| 1455 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="name") |
| 1456 |
|
| 1457 |
def test_alter_field(self): |
| 1458 |
"""Tests autodetection of new fields.""" |
| 1459 |
changes = self.get_changes([self.author_name], [self.author_name_longer]) |
| 1460 |
# Right number/type of migrations? |
| 1461 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1462 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterField"]) |
| 1463 |
self.assertOperationAttributes( |
| 1464 |
changes, "testapp", 0, 0, name="name", preserve_default=True |
| 1465 |
) |
| 1466 |
|
| 1467 |
def test_supports_functools_partial(self): |
| 1468 |
def _content_file_name(instance, filename, key, **kwargs): |
| 1469 |
return "{}/{}".format(instance, filename) |
| 1470 |
|
| 1471 |
def content_file_name(key, **kwargs): |
| 1472 |
return functools.partial(_content_file_name, key, **kwargs) |
| 1473 |
|
| 1474 |
# An unchanged partial reference. |
| 1475 |
before = [ |
| 1476 |
ModelState( |
| 1477 |
"testapp", |
| 1478 |
"Author", |
| 1479 |
[ |
| 1480 |
("id", models.AutoField(primary_key=True)), |
| 1481 |
( |
| 1482 |
"file", |
| 1483 |
models.FileField( |
| 1484 |
max_length=200, upload_to=content_file_name("file") |
| 1485 |
), |
| 1486 |
), |
| 1487 |
], |
| 1488 |
) |
| 1489 |
] |
| 1490 |
after = [ |
| 1491 |
ModelState( |
| 1492 |
"testapp", |
| 1493 |
"Author", |
| 1494 |
[ |
| 1495 |
("id", models.AutoField(primary_key=True)), |
| 1496 |
( |
| 1497 |
"file", |
| 1498 |
models.FileField( |
| 1499 |
max_length=200, upload_to=content_file_name("file") |
| 1500 |
), |
| 1501 |
), |
| 1502 |
], |
| 1503 |
) |
| 1504 |
] |
| 1505 |
changes = self.get_changes(before, after) |
| 1506 |
self.assertNumberMigrations(changes, "testapp", 0) |
| 1507 |
|
| 1508 |
# A changed partial reference. |
| 1509 |
args_changed = [ |
| 1510 |
ModelState( |
| 1511 |
"testapp", |
| 1512 |
"Author", |
| 1513 |
[ |
| 1514 |
("id", models.AutoField(primary_key=True)), |
| 1515 |
( |
| 1516 |
"file", |
| 1517 |
models.FileField( |
| 1518 |
max_length=200, upload_to=content_file_name("other-file") |
| 1519 |
), |
| 1520 |
), |
| 1521 |
], |
| 1522 |
) |
| 1523 |
] |
| 1524 |
changes = self.get_changes(before, args_changed) |
| 1525 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1526 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterField"]) |
| 1527 |
# Can't use assertOperationFieldAttributes because we need the |
| 1528 |
# deconstructed version, i.e., the exploded func/args/keywords rather |
| 1529 |
# than the partial: we don't care if it's not the same instance of the |
| 1530 |
# partial, only if it's the same source function, args, and keywords. |
| 1531 |
value = changes["testapp"][0].operations[0].field.upload_to |
| 1532 |
self.assertEqual( |
| 1533 |
(_content_file_name, ("other-file",), {}), |
| 1534 |
(value.func, value.args, value.keywords), |
| 1535 |
) |
| 1536 |
|
| 1537 |
kwargs_changed = [ |
| 1538 |
ModelState( |
| 1539 |
"testapp", |
| 1540 |
"Author", |
| 1541 |
[ |
| 1542 |
("id", models.AutoField(primary_key=True)), |
| 1543 |
( |
| 1544 |
"file", |
| 1545 |
models.FileField( |
| 1546 |
max_length=200, |
| 1547 |
upload_to=content_file_name("file", spam="eggs"), |
| 1548 |
), |
| 1549 |
), |
| 1550 |
], |
| 1551 |
) |
| 1552 |
] |
| 1553 |
changes = self.get_changes(before, kwargs_changed) |
| 1554 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1555 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterField"]) |
| 1556 |
value = changes["testapp"][0].operations[0].field.upload_to |
| 1557 |
self.assertEqual( |
| 1558 |
(_content_file_name, ("file",), {"spam": "eggs"}), |
| 1559 |
(value.func, value.args, value.keywords), |
| 1560 |
) |
| 1561 |
|
| 1562 |
@mock.patch( |
| 1563 |
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration", |
| 1564 |
side_effect=AssertionError("Should not have prompted for not null addition"), |
| 1565 |
) |
| 1566 |
def test_alter_field_to_not_null_with_default(self, mocked_ask_method): |
| 1567 |
""" |
| 1568 |
#23609 - Tests autodetection of nullable to non-nullable alterations. |
| 1569 |
""" |
| 1570 |
changes = self.get_changes([self.author_name_null], [self.author_name_default]) |
| 1571 |
# Right number/type of migrations? |
| 1572 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1573 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterField"]) |
| 1574 |
self.assertOperationAttributes( |
| 1575 |
changes, "testapp", 0, 0, name="name", preserve_default=True |
| 1576 |
) |
| 1577 |
self.assertOperationFieldAttributes( |
| 1578 |
changes, "testapp", 0, 0, default="Ada Lovelace" |
| 1579 |
) |
| 1580 |
|
| 1581 |
@mock.patch( |
| 1582 |
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration", |
| 1583 |
side_effect=AssertionError("Should not have prompted for not null alteration"), |
| 1584 |
) |
| 1585 |
def test_alter_field_to_not_null_with_db_default(self, mocked_ask_method): |
| 1586 |
changes = self.get_changes( |
| 1587 |
[self.author_name_null], [self.author_name_db_default] |
| 1588 |
) |
| 1589 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1590 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterField"]) |
| 1591 |
self.assertOperationAttributes( |
| 1592 |
changes, "testapp", 0, 0, name="name", preserve_default=True |
| 1593 |
) |
| 1594 |
self.assertOperationFieldAttributes( |
| 1595 |
changes, "testapp", 0, 0, db_default="Ada Lovelace" |
| 1596 |
) |
| 1597 |
|
| 1598 |
@mock.patch( |
| 1599 |
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition" |
| 1600 |
) |
| 1601 |
def test_add_auto_field_does_not_request_default(self, mocked_ask_method): |
| 1602 |
initial_state = ModelState( |
| 1603 |
"testapp", |
| 1604 |
"Author", |
| 1605 |
[ |
| 1606 |
("pkfield", models.IntegerField(primary_key=True)), |
| 1607 |
], |
| 1608 |
) |
| 1609 |
for auto_field in [ |
| 1610 |
models.AutoField, |
| 1611 |
models.BigAutoField, |
| 1612 |
models.SmallAutoField, |
| 1613 |
]: |
| 1614 |
with self.subTest(auto_field=auto_field): |
| 1615 |
updated_state = ModelState( |
| 1616 |
"testapp", |
| 1617 |
"Author", |
| 1618 |
[ |
| 1619 |
("id", auto_field(primary_key=True)), |
| 1620 |
("pkfield", models.IntegerField(primary_key=False)), |
| 1621 |
], |
| 1622 |
) |
| 1623 |
self.get_changes([initial_state], [updated_state]) |
| 1624 |
mocked_ask_method.assert_not_called() |
| 1625 |
|
| 1626 |
@mock.patch( |
| 1627 |
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration", |
| 1628 |
return_value=models.NOT_PROVIDED, |
| 1629 |
) |
| 1630 |
def test_alter_field_to_not_null_without_default(self, mocked_ask_method): |
| 1631 |
""" |
| 1632 |
#23609 - Tests autodetection of nullable to non-nullable alterations. |
| 1633 |
""" |
| 1634 |
changes = self.get_changes([self.author_name_null], [self.author_name]) |
| 1635 |
self.assertEqual(mocked_ask_method.call_count, 1) |
| 1636 |
# Right number/type of migrations? |
| 1637 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1638 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterField"]) |
| 1639 |
self.assertOperationAttributes( |
| 1640 |
changes, "testapp", 0, 0, name="name", preserve_default=True |
| 1641 |
) |
| 1642 |
self.assertOperationFieldAttributes( |
| 1643 |
changes, "testapp", 0, 0, default=models.NOT_PROVIDED |
| 1644 |
) |
| 1645 |
|
| 1646 |
@mock.patch( |
| 1647 |
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration", |
| 1648 |
return_value="Some Name", |
| 1649 |
) |
| 1650 |
def test_alter_field_to_not_null_oneoff_default(self, mocked_ask_method): |
| 1651 |
""" |
| 1652 |
#23609 - Tests autodetection of nullable to non-nullable alterations. |
| 1653 |
""" |
| 1654 |
changes = self.get_changes([self.author_name_null], [self.author_name]) |
| 1655 |
self.assertEqual(mocked_ask_method.call_count, 1) |
| 1656 |
# Right number/type of migrations? |
| 1657 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1658 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterField"]) |
| 1659 |
self.assertOperationAttributes( |
| 1660 |
changes, "testapp", 0, 0, name="name", preserve_default=False |
| 1661 |
) |
| 1662 |
self.assertOperationFieldAttributes( |
| 1663 |
changes, "testapp", 0, 0, default="Some Name" |
| 1664 |
) |
| 1665 |
|
| 1666 |
def test_rename_field(self): |
| 1667 |
"""Tests autodetection of renamed fields.""" |
| 1668 |
changes = self.get_changes( |
| 1669 |
[self.author_name], |
| 1670 |
[self.author_name_renamed], |
| 1671 |
MigrationQuestioner({"ask_rename": True}), |
| 1672 |
) |
| 1673 |
# Right number/type of migrations? |
| 1674 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 1675 |
self.assertOperationTypes(changes, "testapp", 0, ["RenameField"]) |
| 1676 |
self.assertOperationAttributes( |
| 1677 |
changes, "testapp", 0, 0, old_name="name", new_name="names" |
| 1678 |
) |
| 1679 |
|
| 1680 |
def test_rename_field_foreign_key_to_field(self): |
| 1681 |
before = [ |
| 1682 |
ModelState( |
| 1683 |
"app", |
| 1684 |
"Foo", |
| 1685 |
[ |
| 1686 |
("id", models.AutoField(primary_key=True)), |
| 1687 |
("field", models.IntegerField(unique=True)), |
| 1688 |
], |
| 1689 |
), |
| 1690 |
ModelState( |
| 1691 |
"app", |
| 1692 |
"Bar", |
| 1693 |
[ |
| 1694 |
("id", models.AutoField(primary_key=True)), |
| 1695 |
( |
| 1696 |
"foo", |
| 1697 |
models.ForeignKey("app.Foo", models.CASCADE, to_field="field"), |
| 1698 |
), |
| 1699 |
], |
| 1700 |
), |
| 1701 |
] |
| 1702 |
after = [ |
| 1703 |
ModelState( |
| 1704 |
"app", |
| 1705 |
"Foo", |
| 1706 |
[ |
| 1707 |
("id", models.AutoField(primary_key=True)), |
| 1708 |
("renamed_field", models.IntegerField(unique=True)), |
| 1709 |
], |
| 1710 |
), |
| 1711 |
ModelState( |
| 1712 |
"app", |
| 1713 |
"Bar", |
| 1714 |
[ |
| 1715 |
("id", models.AutoField(primary_key=True)), |
| 1716 |
( |
| 1717 |
"foo", |
| 1718 |
models.ForeignKey( |
| 1719 |
"app.Foo", models.CASCADE, to_field="renamed_field" |
| 1720 |
), |
| 1721 |
), |
| 1722 |
], |
| 1723 |
), |
| 1724 |
] |
| 1725 |
changes = self.get_changes( |
| 1726 |
before, after, MigrationQuestioner({"ask_rename": True}) |
| 1727 |
) |
| 1728 |
# Right number/type of migrations? |
| 1729 |
self.assertNumberMigrations(changes, "app", 1) |
| 1730 |
self.assertOperationTypes(changes, "app", 0, ["RenameField"]) |
| 1731 |
self.assertOperationAttributes( |
| 1732 |
changes, "app", 0, 0, old_name="field", new_name="renamed_field" |
| 1733 |
) |
| 1734 |
|
| 1735 |
def test_foreign_object_from_to_fields_list(self): |
| 1736 |
author_state = ModelState( |
| 1737 |
"app", |
| 1738 |
"Author", |
| 1739 |
[("id", models.AutoField(primary_key=True))], |
| 1740 |
) |
| 1741 |
book_state = ModelState( |
| 1742 |
"app", |
| 1743 |
"Book", |
| 1744 |
[ |
| 1745 |
("id", models.AutoField(primary_key=True)), |
| 1746 |
("name", models.CharField()), |
| 1747 |
("author_id", models.IntegerField()), |
| 1748 |
( |
| 1749 |
"author", |
| 1750 |
models.ForeignObject( |
| 1751 |
"app.Author", |
| 1752 |
models.CASCADE, |
| 1753 |
from_fields=["author_id"], |
| 1754 |
to_fields=["id"], |
| 1755 |
), |
| 1756 |
), |
| 1757 |
], |
| 1758 |
) |
| 1759 |
book_state_copy = copy.deepcopy(book_state) |
| 1760 |
changes = self.get_changes( |
| 1761 |
[author_state, book_state], |
| 1762 |
[author_state, book_state_copy], |
| 1763 |
) |
| 1764 |
self.assertEqual(changes, {}) |
| 1765 |
|
| 1766 |
def test_rename_foreign_object_fields(self): |
| 1767 |
fields = ("first", "second") |
| 1768 |
renamed_fields = ("first_renamed", "second_renamed") |
| 1769 |
before = [ |
| 1770 |
ModelState( |
| 1771 |
"app", |
| 1772 |
"Foo", |
| 1773 |
[ |
| 1774 |
("id", models.AutoField(primary_key=True)), |
| 1775 |
("first", models.IntegerField()), |
| 1776 |
("second", models.IntegerField()), |
| 1777 |
], |
| 1778 |
options={"unique_together": {fields}}, |
| 1779 |
), |
| 1780 |
ModelState( |
| 1781 |
"app", |
| 1782 |
"Bar", |
| 1783 |
[ |
| 1784 |
("id", models.AutoField(primary_key=True)), |
| 1785 |
("first", models.IntegerField()), |
| 1786 |
("second", models.IntegerField()), |
| 1787 |
( |
| 1788 |
"foo", |
| 1789 |
models.ForeignObject( |
| 1790 |
"app.Foo", |
| 1791 |
models.CASCADE, |
| 1792 |
from_fields=fields, |
| 1793 |
to_fields=fields, |
| 1794 |
), |
| 1795 |
), |
| 1796 |
], |
| 1797 |
), |
| 1798 |
] |
| 1799 |
# Case 1: to_fields renames. |
| 1800 |
after = [ |
| 1801 |
ModelState( |
| 1802 |
"app", |
| 1803 |
"Foo", |
| 1804 |
[ |
| 1805 |
("id", models.AutoField(primary_key=True)), |
| 1806 |
("first_renamed", models.IntegerField()), |
| 1807 |
("second_renamed", models.IntegerField()), |
| 1808 |
], |
| 1809 |
options={"unique_together": {renamed_fields}}, |
| 1810 |
), |
| 1811 |
ModelState( |
| 1812 |
"app", |
| 1813 |
"Bar", |
| 1814 |
[ |
| 1815 |
("id", models.AutoField(primary_key=True)), |
| 1816 |
("first", models.IntegerField()), |
| 1817 |
("second", models.IntegerField()), |
| 1818 |
( |
| 1819 |
"foo", |
| 1820 |
models.ForeignObject( |
| 1821 |
"app.Foo", |
| 1822 |
models.CASCADE, |
| 1823 |
from_fields=fields, |
| 1824 |
to_fields=renamed_fields, |
| 1825 |
), |
| 1826 |
), |
| 1827 |
], |
| 1828 |
), |
| 1829 |
] |
| 1830 |
changes = self.get_changes( |
| 1831 |
before, after, MigrationQuestioner({"ask_rename": True}) |
| 1832 |
) |
| 1833 |
self.assertNumberMigrations(changes, "app", 1) |
| 1834 |
self.assertOperationTypes( |
| 1835 |
changes, "app", 0, ["RenameField", "RenameField", "AlterUniqueTogether"] |
| 1836 |
) |
| 1837 |
self.assertOperationAttributes( |
| 1838 |
changes, |
| 1839 |
"app", |
| 1840 |
0, |
| 1841 |
0, |
| 1842 |
model_name="foo", |
| 1843 |
old_name="first", |
| 1844 |
new_name="first_renamed", |
| 1845 |
) |
| 1846 |
self.assertOperationAttributes( |
| 1847 |
changes, |
| 1848 |
"app", |
| 1849 |
0, |
| 1850 |
1, |
| 1851 |
model_name="foo", |
| 1852 |
old_name="second", |
| 1853 |
new_name="second_renamed", |
| 1854 |
) |
| 1855 |
# Case 2: from_fields renames. |
| 1856 |
after = [ |
| 1857 |
ModelState( |
| 1858 |
"app", |
| 1859 |
"Foo", |
| 1860 |
[ |
| 1861 |
("id", models.AutoField(primary_key=True)), |
| 1862 |
("first", models.IntegerField()), |
| 1863 |
("second", models.IntegerField()), |
| 1864 |
], |
| 1865 |
options={"unique_together": {fields}}, |
| 1866 |
), |
| 1867 |
ModelState( |
| 1868 |
"app", |
| 1869 |
"Bar", |
| 1870 |
[ |
| 1871 |
("id", models.AutoField(primary_key=True)), |
| 1872 |
("first_renamed", models.IntegerField()), |
| 1873 |
("second_renamed", models.IntegerField()), |
| 1874 |
( |
| 1875 |
"foo", |
| 1876 |
models.ForeignObject( |
| 1877 |
"app.Foo", |
| 1878 |
models.CASCADE, |
| 1879 |
from_fields=renamed_fields, |
| 1880 |
to_fields=fields, |
| 1881 |
), |
| 1882 |
), |
| 1883 |
], |
| 1884 |
), |
| 1885 |
] |
| 1886 |
changes = self.get_changes( |
| 1887 |
before, after, MigrationQuestioner({"ask_rename": True}) |
| 1888 |
) |
| 1889 |
self.assertNumberMigrations(changes, "app", 1) |
| 1890 |
self.assertOperationTypes(changes, "app", 0, ["RenameField", "RenameField"]) |
| 1891 |
self.assertOperationAttributes( |
| 1892 |
changes, |
| 1893 |
"app", |
| 1894 |
0, |
| 1895 |
0, |
| 1896 |
model_name="bar", |
| 1897 |
old_name="first", |
| 1898 |
new_name="first_renamed", |
| 1899 |
) |
| 1900 |
self.assertOperationAttributes( |
| 1901 |
changes, |
| 1902 |
"app", |
| 1903 |
0, |
| 1904 |
1, |
| 1905 |
model_name="bar", |
| 1906 |
old_name="second", |
| 1907 |
new_name="second_renamed", |
| 1908 |
) |
| 1909 |
|
| 1910 |
def test_rename_referenced_primary_key(self): |
| 1911 |
before = [ |
| 1912 |
ModelState( |
| 1913 |
"app", |
| 1914 |
"Foo", |
| 1915 |
[ |
| 1916 |
("id", models.CharField(primary_key=True, serialize=False)), |
| 1917 |
], |
| 1918 |
), |
| 1919 |
ModelState( |
| 1920 |
"app", |
| 1921 |
"Bar", |
| 1922 |
[ |
| 1923 |
("id", models.AutoField(primary_key=True)), |
| 1924 |
("foo", models.ForeignKey("app.Foo", models.CASCADE)), |
| 1925 |
], |
| 1926 |
), |
| 1927 |
] |
| 1928 |
after = [ |
| 1929 |
ModelState( |
| 1930 |
"app", |
| 1931 |
"Foo", |
| 1932 |
[("renamed_id", models.CharField(primary_key=True, serialize=False))], |
| 1933 |
), |
| 1934 |
ModelState( |
| 1935 |
"app", |
| 1936 |
"Bar", |
| 1937 |
[ |
| 1938 |
("id", models.AutoField(primary_key=True)), |
| 1939 |
("foo", models.ForeignKey("app.Foo", models.CASCADE)), |
| 1940 |
], |
| 1941 |
), |
| 1942 |
] |
| 1943 |
changes = self.get_changes( |
| 1944 |
before, after, MigrationQuestioner({"ask_rename": True}) |
| 1945 |
) |
| 1946 |
self.assertNumberMigrations(changes, "app", 1) |
| 1947 |
self.assertOperationTypes(changes, "app", 0, ["RenameField"]) |
| 1948 |
self.assertOperationAttributes( |
| 1949 |
changes, "app", 0, 0, old_name="id", new_name="renamed_id" |
| 1950 |
) |
| 1951 |
|
| 1952 |
def test_rename_field_preserved_db_column(self): |
| 1953 |
""" |
| 1954 |
RenameField is used if a field is renamed and db_column equal to the |
| 1955 |
old field's column is added. |
| 1956 |
""" |
| 1957 |
before = [ |
| 1958 |
ModelState( |
| 1959 |
"app", |
| 1960 |
"Foo", |
| 1961 |
[ |
| 1962 |
("id", models.AutoField(primary_key=True)), |
| 1963 |
("field", models.IntegerField()), |
| 1964 |
], |
| 1965 |
), |
| 1966 |
] |
| 1967 |
after = [ |
| 1968 |
ModelState( |
| 1969 |
"app", |
| 1970 |
"Foo", |
| 1971 |
[ |
| 1972 |
("id", models.AutoField(primary_key=True)), |
| 1973 |
("renamed_field", models.IntegerField(db_column="field")), |
| 1974 |
], |
| 1975 |
), |
| 1976 |
] |
| 1977 |
changes = self.get_changes( |
| 1978 |
before, after, MigrationQuestioner({"ask_rename": True}) |
| 1979 |
) |
| 1980 |
self.assertNumberMigrations(changes, "app", 1) |
| 1981 |
self.assertOperationTypes(changes, "app", 0, ["AlterField", "RenameField"]) |
| 1982 |
self.assertOperationAttributes( |
| 1983 |
changes, |
| 1984 |
"app", |
| 1985 |
0, |
| 1986 |
0, |
| 1987 |
model_name="foo", |
| 1988 |
name="field", |
| 1989 |
) |
| 1990 |
self.assertEqual( |
| 1991 |
changes["app"][0].operations[0].field.deconstruct(), |
| 1992 |
( |
| 1993 |
"field", |
| 1994 |
"django.db.models.IntegerField", |
| 1995 |
[], |
| 1996 |
{"db_column": "field"}, |
| 1997 |
), |
| 1998 |
) |
| 1999 |
self.assertOperationAttributes( |
| 2000 |
changes, |
| 2001 |
"app", |
| 2002 |
0, |
| 2003 |
1, |
| 2004 |
model_name="foo", |
| 2005 |
old_name="field", |
| 2006 |
new_name="renamed_field", |
| 2007 |
) |
| 2008 |
|
| 2009 |
def test_rename_related_field_preserved_db_column(self): |
| 2010 |
before = [ |
| 2011 |
ModelState( |
| 2012 |
"app", |
| 2013 |
"Foo", |
| 2014 |
[ |
| 2015 |
("id", models.AutoField(primary_key=True)), |
| 2016 |
], |
| 2017 |
), |
| 2018 |
ModelState( |
| 2019 |
"app", |
| 2020 |
"Bar", |
| 2021 |
[ |
| 2022 |
("id", models.AutoField(primary_key=True)), |
| 2023 |
("foo", models.ForeignKey("app.Foo", models.CASCADE)), |
| 2024 |
], |
| 2025 |
), |
| 2026 |
] |
| 2027 |
after = [ |
| 2028 |
ModelState( |
| 2029 |
"app", |
| 2030 |
"Foo", |
| 2031 |
[ |
| 2032 |
("id", models.AutoField(primary_key=True)), |
| 2033 |
], |
| 2034 |
), |
| 2035 |
ModelState( |
| 2036 |
"app", |
| 2037 |
"Bar", |
| 2038 |
[ |
| 2039 |
("id", models.AutoField(primary_key=True)), |
| 2040 |
( |
| 2041 |
"renamed_foo", |
| 2042 |
models.ForeignKey( |
| 2043 |
"app.Foo", models.CASCADE, db_column="foo_id" |
| 2044 |
), |
| 2045 |
), |
| 2046 |
], |
| 2047 |
), |
| 2048 |
] |
| 2049 |
changes = self.get_changes( |
| 2050 |
before, after, MigrationQuestioner({"ask_rename": True}) |
| 2051 |
) |
| 2052 |
self.assertNumberMigrations(changes, "app", 1) |
| 2053 |
self.assertOperationTypes(changes, "app", 0, ["AlterField", "RenameField"]) |
| 2054 |
self.assertOperationAttributes( |
| 2055 |
changes, |
| 2056 |
"app", |
| 2057 |
0, |
| 2058 |
0, |
| 2059 |
model_name="bar", |
| 2060 |
name="foo", |
| 2061 |
) |
| 2062 |
self.assertEqual( |
| 2063 |
changes["app"][0].operations[0].field.deconstruct(), |
| 2064 |
( |
| 2065 |
"foo", |
| 2066 |
"django.db.models.ForeignKey", |
| 2067 |
[], |
| 2068 |
{"to": "app.foo", "on_delete": models.CASCADE, "db_column": "foo_id"}, |
| 2069 |
), |
| 2070 |
) |
| 2071 |
self.assertOperationAttributes( |
| 2072 |
changes, |
| 2073 |
"app", |
| 2074 |
0, |
| 2075 |
1, |
| 2076 |
model_name="bar", |
| 2077 |
old_name="foo", |
| 2078 |
new_name="renamed_foo", |
| 2079 |
) |
| 2080 |
|
| 2081 |
def test_rename_field_with_renamed_model(self): |
| 2082 |
changes = self.get_changes( |
| 2083 |
[self.author_name], |
| 2084 |
[ |
| 2085 |
ModelState( |
| 2086 |
"testapp", |
| 2087 |
"RenamedAuthor", |
| 2088 |
[ |
| 2089 |
("id", models.AutoField(primary_key=True)), |
| 2090 |
("renamed_name", models.CharField(max_length=200)), |
| 2091 |
], |
| 2092 |
), |
| 2093 |
], |
| 2094 |
MigrationQuestioner({"ask_rename_model": True, "ask_rename": True}), |
| 2095 |
) |
| 2096 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2097 |
self.assertOperationTypes(changes, "testapp", 0, ["RenameModel", "RenameField"]) |
| 2098 |
self.assertOperationAttributes( |
| 2099 |
changes, |
| 2100 |
"testapp", |
| 2101 |
0, |
| 2102 |
0, |
| 2103 |
old_name="Author", |
| 2104 |
new_name="RenamedAuthor", |
| 2105 |
) |
| 2106 |
self.assertOperationAttributes( |
| 2107 |
changes, |
| 2108 |
"testapp", |
| 2109 |
0, |
| 2110 |
1, |
| 2111 |
old_name="name", |
| 2112 |
new_name="renamed_name", |
| 2113 |
) |
| 2114 |
|
| 2115 |
def test_rename_model(self): |
| 2116 |
"""Tests autodetection of renamed models.""" |
| 2117 |
changes = self.get_changes( |
| 2118 |
[self.author_with_book, self.book], |
| 2119 |
[self.author_renamed_with_book, self.book_with_author_renamed], |
| 2120 |
MigrationQuestioner({"ask_rename_model": True}), |
| 2121 |
) |
| 2122 |
# Right number/type of migrations? |
| 2123 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2124 |
self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"]) |
| 2125 |
self.assertOperationAttributes( |
| 2126 |
changes, "testapp", 0, 0, old_name="Author", new_name="Writer" |
| 2127 |
) |
| 2128 |
# Now that RenameModel handles related fields too, there should be |
| 2129 |
# no AlterField for the related field. |
| 2130 |
self.assertNumberMigrations(changes, "otherapp", 0) |
| 2131 |
|
| 2132 |
def test_rename_model_case(self): |
| 2133 |
""" |
| 2134 |
Model name is case-insensitive. Changing case doesn't lead to any |
| 2135 |
autodetected operations. |
| 2136 |
""" |
| 2137 |
author_renamed = ModelState( |
| 2138 |
"testapp", |
| 2139 |
"author", |
| 2140 |
[ |
| 2141 |
("id", models.AutoField(primary_key=True)), |
| 2142 |
], |
| 2143 |
) |
| 2144 |
changes = self.get_changes( |
| 2145 |
[self.author_empty, self.book], |
| 2146 |
[author_renamed, self.book], |
| 2147 |
questioner=MigrationQuestioner({"ask_rename_model": True}), |
| 2148 |
) |
| 2149 |
self.assertNumberMigrations(changes, "testapp", 0) |
| 2150 |
self.assertNumberMigrations(changes, "otherapp", 0) |
| 2151 |
|
| 2152 |
def test_renamed_referenced_m2m_model_case(self): |
| 2153 |
publisher_renamed = ModelState( |
| 2154 |
"testapp", |
| 2155 |
"publisher", |
| 2156 |
[ |
| 2157 |
("id", models.AutoField(primary_key=True)), |
| 2158 |
("name", models.CharField(max_length=100)), |
| 2159 |
], |
| 2160 |
) |
| 2161 |
changes = self.get_changes( |
| 2162 |
[self.publisher, self.author_with_m2m], |
| 2163 |
[publisher_renamed, self.author_with_m2m], |
| 2164 |
questioner=MigrationQuestioner({"ask_rename_model": True}), |
| 2165 |
) |
| 2166 |
self.assertNumberMigrations(changes, "testapp", 0) |
| 2167 |
self.assertNumberMigrations(changes, "otherapp", 0) |
| 2168 |
|
| 2169 |
def test_rename_m2m_through_model(self): |
| 2170 |
""" |
| 2171 |
Tests autodetection of renamed models that are used in M2M relations as |
| 2172 |
through models. |
| 2173 |
""" |
| 2174 |
changes = self.get_changes( |
| 2175 |
[self.author_with_m2m_through, self.publisher, self.contract], |
| 2176 |
[ |
| 2177 |
self.author_with_renamed_m2m_through, |
| 2178 |
self.publisher, |
| 2179 |
self.contract_renamed, |
| 2180 |
], |
| 2181 |
MigrationQuestioner({"ask_rename_model": True}), |
| 2182 |
) |
| 2183 |
# Right number/type of migrations? |
| 2184 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2185 |
self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"]) |
| 2186 |
self.assertOperationAttributes( |
| 2187 |
changes, "testapp", 0, 0, old_name="Contract", new_name="Deal" |
| 2188 |
) |
| 2189 |
|
| 2190 |
def test_rename_model_with_renamed_rel_field(self): |
| 2191 |
""" |
| 2192 |
Tests autodetection of renamed models while simultaneously renaming one |
| 2193 |
of the fields that relate to the renamed model. |
| 2194 |
""" |
| 2195 |
changes = self.get_changes( |
| 2196 |
[self.author_with_book, self.book], |
| 2197 |
[self.author_renamed_with_book, self.book_with_field_and_author_renamed], |
| 2198 |
MigrationQuestioner({"ask_rename": True, "ask_rename_model": True}), |
| 2199 |
) |
| 2200 |
# Right number/type of migrations? |
| 2201 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2202 |
self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"]) |
| 2203 |
self.assertOperationAttributes( |
| 2204 |
changes, "testapp", 0, 0, old_name="Author", new_name="Writer" |
| 2205 |
) |
| 2206 |
# Right number/type of migrations for related field rename? |
| 2207 |
# Alter is already taken care of. |
| 2208 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 2209 |
self.assertOperationTypes(changes, "otherapp", 0, ["RenameField"]) |
| 2210 |
self.assertOperationAttributes( |
| 2211 |
changes, "otherapp", 0, 0, old_name="author", new_name="writer" |
| 2212 |
) |
| 2213 |
|
| 2214 |
def test_rename_model_with_fks_in_different_position(self): |
| 2215 |
""" |
| 2216 |
#24537 - The order of fields in a model does not influence |
| 2217 |
the RenameModel detection. |
| 2218 |
""" |
| 2219 |
before = [ |
| 2220 |
ModelState( |
| 2221 |
"testapp", |
| 2222 |
"EntityA", |
| 2223 |
[ |
| 2224 |
("id", models.AutoField(primary_key=True)), |
| 2225 |
], |
| 2226 |
), |
| 2227 |
ModelState( |
| 2228 |
"testapp", |
| 2229 |
"EntityB", |
| 2230 |
[ |
| 2231 |
("id", models.AutoField(primary_key=True)), |
| 2232 |
("some_label", models.CharField(max_length=255)), |
| 2233 |
("entity_a", models.ForeignKey("testapp.EntityA", models.CASCADE)), |
| 2234 |
], |
| 2235 |
), |
| 2236 |
] |
| 2237 |
after = [ |
| 2238 |
ModelState( |
| 2239 |
"testapp", |
| 2240 |
"EntityA", |
| 2241 |
[ |
| 2242 |
("id", models.AutoField(primary_key=True)), |
| 2243 |
], |
| 2244 |
), |
| 2245 |
ModelState( |
| 2246 |
"testapp", |
| 2247 |
"RenamedEntityB", |
| 2248 |
[ |
| 2249 |
("id", models.AutoField(primary_key=True)), |
| 2250 |
("entity_a", models.ForeignKey("testapp.EntityA", models.CASCADE)), |
| 2251 |
("some_label", models.CharField(max_length=255)), |
| 2252 |
], |
| 2253 |
), |
| 2254 |
] |
| 2255 |
changes = self.get_changes( |
| 2256 |
before, after, MigrationQuestioner({"ask_rename_model": True}) |
| 2257 |
) |
| 2258 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2259 |
self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"]) |
| 2260 |
self.assertOperationAttributes( |
| 2261 |
changes, "testapp", 0, 0, old_name="EntityB", new_name="RenamedEntityB" |
| 2262 |
) |
| 2263 |
|
| 2264 |
def test_rename_model_reverse_relation_dependencies(self): |
| 2265 |
""" |
| 2266 |
The migration to rename a model pointed to by a foreign key in another |
| 2267 |
app must run after the other app's migration that adds the foreign key |
| 2268 |
with model's original name. Therefore, the renaming migration has a |
| 2269 |
dependency on that other migration. |
| 2270 |
""" |
| 2271 |
before = [ |
| 2272 |
ModelState( |
| 2273 |
"testapp", |
| 2274 |
"EntityA", |
| 2275 |
[ |
| 2276 |
("id", models.AutoField(primary_key=True)), |
| 2277 |
], |
| 2278 |
), |
| 2279 |
ModelState( |
| 2280 |
"otherapp", |
| 2281 |
"EntityB", |
| 2282 |
[ |
| 2283 |
("id", models.AutoField(primary_key=True)), |
| 2284 |
("entity_a", models.ForeignKey("testapp.EntityA", models.CASCADE)), |
| 2285 |
], |
| 2286 |
), |
| 2287 |
] |
| 2288 |
after = [ |
| 2289 |
ModelState( |
| 2290 |
"testapp", |
| 2291 |
"RenamedEntityA", |
| 2292 |
[ |
| 2293 |
("id", models.AutoField(primary_key=True)), |
| 2294 |
], |
| 2295 |
), |
| 2296 |
ModelState( |
| 2297 |
"otherapp", |
| 2298 |
"EntityB", |
| 2299 |
[ |
| 2300 |
("id", models.AutoField(primary_key=True)), |
| 2301 |
( |
| 2302 |
"entity_a", |
| 2303 |
models.ForeignKey("testapp.RenamedEntityA", models.CASCADE), |
| 2304 |
), |
| 2305 |
], |
| 2306 |
), |
| 2307 |
] |
| 2308 |
changes = self.get_changes( |
| 2309 |
before, after, MigrationQuestioner({"ask_rename_model": True}) |
| 2310 |
) |
| 2311 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2312 |
self.assertMigrationDependencies( |
| 2313 |
changes, "testapp", 0, [("otherapp", "__first__")] |
| 2314 |
) |
| 2315 |
self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"]) |
| 2316 |
self.assertOperationAttributes( |
| 2317 |
changes, "testapp", 0, 0, old_name="EntityA", new_name="RenamedEntityA" |
| 2318 |
) |
| 2319 |
|
| 2320 |
def test_fk_dependency(self): |
| 2321 |
"""Having a ForeignKey automatically adds a dependency.""" |
| 2322 |
# Note that testapp (author) has no dependencies, |
| 2323 |
# otherapp (book) depends on testapp (author), |
| 2324 |
# thirdapp (edition) depends on otherapp (book) |
| 2325 |
changes = self.get_changes([], [self.author_name, self.book, self.edition]) |
| 2326 |
# Right number/type of migrations? |
| 2327 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2328 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"]) |
| 2329 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author") |
| 2330 |
self.assertMigrationDependencies(changes, "testapp", 0, []) |
| 2331 |
# Right number/type of migrations? |
| 2332 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 2333 |
self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"]) |
| 2334 |
self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Book") |
| 2335 |
self.assertMigrationDependencies( |
| 2336 |
changes, "otherapp", 0, [("testapp", "auto_1")] |
| 2337 |
) |
| 2338 |
# Right number/type of migrations? |
| 2339 |
self.assertNumberMigrations(changes, "thirdapp", 1) |
| 2340 |
self.assertOperationTypes(changes, "thirdapp", 0, ["CreateModel"]) |
| 2341 |
self.assertOperationAttributes(changes, "thirdapp", 0, 0, name="Edition") |
| 2342 |
self.assertMigrationDependencies( |
| 2343 |
changes, "thirdapp", 0, [("otherapp", "auto_1")] |
| 2344 |
) |
| 2345 |
|
| 2346 |
def test_proxy_fk_dependency(self): |
| 2347 |
"""FK dependencies still work on proxy models.""" |
| 2348 |
# Note that testapp (author) has no dependencies, |
| 2349 |
# otherapp (book) depends on testapp (authorproxy) |
| 2350 |
changes = self.get_changes( |
| 2351 |
[], [self.author_empty, self.author_proxy_third, self.book_proxy_fk] |
| 2352 |
) |
| 2353 |
# Right number/type of migrations? |
| 2354 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2355 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"]) |
| 2356 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author") |
| 2357 |
self.assertMigrationDependencies(changes, "testapp", 0, []) |
| 2358 |
# Right number/type of migrations? |
| 2359 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 2360 |
self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"]) |
| 2361 |
self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Book") |
| 2362 |
self.assertMigrationDependencies( |
| 2363 |
changes, "otherapp", 0, [("thirdapp", "auto_1")] |
| 2364 |
) |
| 2365 |
# Right number/type of migrations? |
| 2366 |
self.assertNumberMigrations(changes, "thirdapp", 1) |
| 2367 |
self.assertOperationTypes(changes, "thirdapp", 0, ["CreateModel"]) |
| 2368 |
self.assertOperationAttributes(changes, "thirdapp", 0, 0, name="AuthorProxy") |
| 2369 |
self.assertMigrationDependencies( |
| 2370 |
changes, "thirdapp", 0, [("testapp", "auto_1")] |
| 2371 |
) |
| 2372 |
|
| 2373 |
def test_same_app_no_fk_dependency(self): |
| 2374 |
""" |
| 2375 |
A migration with a FK between two models of the same app |
| 2376 |
does not have a dependency to itself. |
| 2377 |
""" |
| 2378 |
changes = self.get_changes([], [self.author_with_publisher, self.publisher]) |
| 2379 |
# Right number/type of migrations? |
| 2380 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2381 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel", "CreateModel"]) |
| 2382 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Publisher") |
| 2383 |
self.assertOperationAttributes(changes, "testapp", 0, 1, name="Author") |
| 2384 |
self.assertMigrationDependencies(changes, "testapp", 0, []) |
| 2385 |
|
| 2386 |
def test_circular_fk_dependency(self): |
| 2387 |
""" |
| 2388 |
Having a circular ForeignKey dependency automatically |
| 2389 |
resolves the situation into 2 migrations on one side and 1 on the other. |
| 2390 |
""" |
| 2391 |
changes = self.get_changes( |
| 2392 |
[], [self.author_with_book, self.book, self.publisher_with_book] |
| 2393 |
) |
| 2394 |
# Right number/type of migrations? |
| 2395 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2396 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel", "CreateModel"]) |
| 2397 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author") |
| 2398 |
self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher") |
| 2399 |
self.assertMigrationDependencies( |
| 2400 |
changes, "testapp", 0, [("otherapp", "auto_1")] |
| 2401 |
) |
| 2402 |
# Right number/type of migrations? |
| 2403 |
self.assertNumberMigrations(changes, "otherapp", 2) |
| 2404 |
self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"]) |
| 2405 |
self.assertOperationTypes(changes, "otherapp", 1, ["AddField"]) |
| 2406 |
self.assertMigrationDependencies(changes, "otherapp", 0, []) |
| 2407 |
self.assertMigrationDependencies( |
| 2408 |
changes, "otherapp", 1, [("otherapp", "auto_1"), ("testapp", "auto_1")] |
| 2409 |
) |
| 2410 |
# both split migrations should be `initial` |
| 2411 |
self.assertTrue(changes["otherapp"][0].initial) |
| 2412 |
self.assertTrue(changes["otherapp"][1].initial) |
| 2413 |
|
| 2414 |
def test_same_app_circular_fk_dependency(self): |
| 2415 |
""" |
| 2416 |
A migration with a FK between two models of the same app does |
| 2417 |
not have a dependency to itself. |
| 2418 |
""" |
| 2419 |
changes = self.get_changes( |
| 2420 |
[], [self.author_with_publisher, self.publisher_with_author] |
| 2421 |
) |
| 2422 |
# Right number/type of migrations? |
| 2423 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2424 |
self.assertOperationTypes( |
| 2425 |
changes, "testapp", 0, ["CreateModel", "CreateModel", "AddField"] |
| 2426 |
) |
| 2427 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author") |
| 2428 |
self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher") |
| 2429 |
self.assertOperationAttributes(changes, "testapp", 0, 2, name="publisher") |
| 2430 |
self.assertMigrationDependencies(changes, "testapp", 0, []) |
| 2431 |
|
| 2432 |
def test_same_app_circular_fk_dependency_with_unique_together_and_indexes(self): |
| 2433 |
""" |
| 2434 |
#22275 - A migration with circular FK dependency does not try |
| 2435 |
to create unique together constraint and indexes before creating all |
| 2436 |
required fields first. |
| 2437 |
""" |
| 2438 |
changes = self.get_changes([], [self.knight, self.rabbit]) |
| 2439 |
# Right number/type of migrations? |
| 2440 |
self.assertNumberMigrations(changes, "eggs", 1) |
| 2441 |
self.assertOperationTypes( |
| 2442 |
changes, |
| 2443 |
"eggs", |
| 2444 |
0, |
| 2445 |
["CreateModel", "CreateModel"], |
| 2446 |
) |
| 2447 |
self.assertNotIn("unique_together", changes["eggs"][0].operations[0].options) |
| 2448 |
self.assertMigrationDependencies(changes, "eggs", 0, []) |
| 2449 |
|
| 2450 |
def test_alter_db_table_add(self): |
| 2451 |
"""Tests detection for adding db_table in model's options.""" |
| 2452 |
changes = self.get_changes( |
| 2453 |
[self.author_empty], [self.author_with_db_table_options] |
| 2454 |
) |
| 2455 |
# Right number/type of migrations? |
| 2456 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2457 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTable"]) |
| 2458 |
self.assertOperationAttributes( |
| 2459 |
changes, "testapp", 0, 0, name="author", table="author_one" |
| 2460 |
) |
| 2461 |
|
| 2462 |
def test_alter_db_table_change(self): |
| 2463 |
"""Tests detection for changing db_table in model's options'.""" |
| 2464 |
changes = self.get_changes( |
| 2465 |
[self.author_with_db_table_options], [self.author_with_new_db_table_options] |
| 2466 |
) |
| 2467 |
# Right number/type of migrations? |
| 2468 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2469 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTable"]) |
| 2470 |
self.assertOperationAttributes( |
| 2471 |
changes, "testapp", 0, 0, name="author", table="author_two" |
| 2472 |
) |
| 2473 |
|
| 2474 |
def test_alter_db_table_remove(self): |
| 2475 |
"""Tests detection for removing db_table in model's options.""" |
| 2476 |
changes = self.get_changes( |
| 2477 |
[self.author_with_db_table_options], [self.author_empty] |
| 2478 |
) |
| 2479 |
# Right number/type of migrations? |
| 2480 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2481 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTable"]) |
| 2482 |
self.assertOperationAttributes( |
| 2483 |
changes, "testapp", 0, 0, name="author", table=None |
| 2484 |
) |
| 2485 |
|
| 2486 |
def test_alter_db_table_no_changes(self): |
| 2487 |
""" |
| 2488 |
Alter_db_table doesn't generate a migration if no changes have been made. |
| 2489 |
""" |
| 2490 |
changes = self.get_changes( |
| 2491 |
[self.author_with_db_table_options], [self.author_with_db_table_options] |
| 2492 |
) |
| 2493 |
# Right number of migrations? |
| 2494 |
self.assertEqual(len(changes), 0) |
| 2495 |
|
| 2496 |
def test_keep_db_table_with_model_change(self): |
| 2497 |
""" |
| 2498 |
Tests when model changes but db_table stays as-is, autodetector must not |
| 2499 |
create more than one operation. |
| 2500 |
""" |
| 2501 |
changes = self.get_changes( |
| 2502 |
[self.author_with_db_table_options], |
| 2503 |
[self.author_renamed_with_db_table_options], |
| 2504 |
MigrationQuestioner({"ask_rename_model": True}), |
| 2505 |
) |
| 2506 |
# Right number/type of migrations? |
| 2507 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2508 |
self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"]) |
| 2509 |
self.assertOperationAttributes( |
| 2510 |
changes, "testapp", 0, 0, old_name="Author", new_name="NewAuthor" |
| 2511 |
) |
| 2512 |
|
| 2513 |
def test_alter_db_table_with_model_change(self): |
| 2514 |
""" |
| 2515 |
Tests when model and db_table changes, autodetector must create two |
| 2516 |
operations. |
| 2517 |
""" |
| 2518 |
changes = self.get_changes( |
| 2519 |
[self.author_with_db_table_options], |
| 2520 |
[self.author_renamed_with_new_db_table_options], |
| 2521 |
MigrationQuestioner({"ask_rename_model": True}), |
| 2522 |
) |
| 2523 |
# Right number/type of migrations? |
| 2524 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2525 |
self.assertOperationTypes( |
| 2526 |
changes, "testapp", 0, ["RenameModel", "AlterModelTable"] |
| 2527 |
) |
| 2528 |
self.assertOperationAttributes( |
| 2529 |
changes, "testapp", 0, 0, old_name="Author", new_name="NewAuthor" |
| 2530 |
) |
| 2531 |
self.assertOperationAttributes( |
| 2532 |
changes, "testapp", 0, 1, name="newauthor", table="author_three" |
| 2533 |
) |
| 2534 |
|
| 2535 |
def test_alter_db_table_comment_add(self): |
| 2536 |
changes = self.get_changes( |
| 2537 |
[self.author_empty], [self.author_with_db_table_comment] |
| 2538 |
) |
| 2539 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2540 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTableComment"]) |
| 2541 |
self.assertOperationAttributes( |
| 2542 |
changes, "testapp", 0, 0, name="author", table_comment="Table comment" |
| 2543 |
) |
| 2544 |
|
| 2545 |
def test_alter_db_table_comment_change(self): |
| 2546 |
author_with_new_db_table_comment = ModelState( |
| 2547 |
"testapp", |
| 2548 |
"Author", |
| 2549 |
[ |
| 2550 |
("id", models.AutoField(primary_key=True)), |
| 2551 |
], |
| 2552 |
{"db_table_comment": "New table comment"}, |
| 2553 |
) |
| 2554 |
changes = self.get_changes( |
| 2555 |
[self.author_with_db_table_comment], |
| 2556 |
[author_with_new_db_table_comment], |
| 2557 |
) |
| 2558 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2559 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTableComment"]) |
| 2560 |
self.assertOperationAttributes( |
| 2561 |
changes, |
| 2562 |
"testapp", |
| 2563 |
0, |
| 2564 |
0, |
| 2565 |
name="author", |
| 2566 |
table_comment="New table comment", |
| 2567 |
) |
| 2568 |
|
| 2569 |
def test_alter_db_table_comment_remove(self): |
| 2570 |
changes = self.get_changes( |
| 2571 |
[self.author_with_db_table_comment], |
| 2572 |
[self.author_empty], |
| 2573 |
) |
| 2574 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2575 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTableComment"]) |
| 2576 |
self.assertOperationAttributes( |
| 2577 |
changes, "testapp", 0, 0, name="author", db_table_comment=None |
| 2578 |
) |
| 2579 |
|
| 2580 |
def test_alter_db_table_comment_no_changes(self): |
| 2581 |
changes = self.get_changes( |
| 2582 |
[self.author_with_db_table_comment], |
| 2583 |
[self.author_with_db_table_comment], |
| 2584 |
) |
| 2585 |
self.assertNumberMigrations(changes, "testapp", 0) |
| 2586 |
|
| 2587 |
def test_identical_regex_doesnt_alter(self): |
| 2588 |
from_state = ModelState( |
| 2589 |
"testapp", |
| 2590 |
"model", |
| 2591 |
[ |
| 2592 |
( |
| 2593 |
"id", |
| 2594 |
models.AutoField( |
| 2595 |
primary_key=True, |
| 2596 |
validators=[ |
| 2597 |
RegexValidator( |
| 2598 |
re.compile("^[-a-zA-Z0-9_]+\\Z"), |
| 2599 |
"Enter a valid “slug” consisting of letters, numbers, " |
| 2600 |
"underscores or hyphens.", |
| 2601 |
"invalid", |
| 2602 |
) |
| 2603 |
], |
| 2604 |
), |
| 2605 |
) |
| 2606 |
], |
| 2607 |
) |
| 2608 |
to_state = ModelState( |
| 2609 |
"testapp", |
| 2610 |
"model", |
| 2611 |
[("id", models.AutoField(primary_key=True, validators=[validate_slug]))], |
| 2612 |
) |
| 2613 |
changes = self.get_changes([from_state], [to_state]) |
| 2614 |
# Right number/type of migrations? |
| 2615 |
self.assertNumberMigrations(changes, "testapp", 0) |
| 2616 |
|
| 2617 |
def test_different_regex_does_alter(self): |
| 2618 |
from_state = ModelState( |
| 2619 |
"testapp", |
| 2620 |
"model", |
| 2621 |
[ |
| 2622 |
( |
| 2623 |
"id", |
| 2624 |
models.AutoField( |
| 2625 |
primary_key=True, |
| 2626 |
validators=[ |
| 2627 |
RegexValidator( |
| 2628 |
re.compile("^[a-z]+\\Z", 32), |
| 2629 |
"Enter a valid “slug” consisting of letters, numbers, " |
| 2630 |
"underscores or hyphens.", |
| 2631 |
"invalid", |
| 2632 |
) |
| 2633 |
], |
| 2634 |
), |
| 2635 |
) |
| 2636 |
], |
| 2637 |
) |
| 2638 |
to_state = ModelState( |
| 2639 |
"testapp", |
| 2640 |
"model", |
| 2641 |
[("id", models.AutoField(primary_key=True, validators=[validate_slug]))], |
| 2642 |
) |
| 2643 |
changes = self.get_changes([from_state], [to_state]) |
| 2644 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2645 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterField"]) |
| 2646 |
|
| 2647 |
def test_alter_regex_string_to_compiled_regex(self): |
| 2648 |
regex_string = "^[a-z]+$" |
| 2649 |
from_state = ModelState( |
| 2650 |
"testapp", |
| 2651 |
"model", |
| 2652 |
[ |
| 2653 |
( |
| 2654 |
"id", |
| 2655 |
models.AutoField( |
| 2656 |
primary_key=True, validators=[RegexValidator(regex_string)] |
| 2657 |
), |
| 2658 |
) |
| 2659 |
], |
| 2660 |
) |
| 2661 |
to_state = ModelState( |
| 2662 |
"testapp", |
| 2663 |
"model", |
| 2664 |
[ |
| 2665 |
( |
| 2666 |
"id", |
| 2667 |
models.AutoField( |
| 2668 |
primary_key=True, |
| 2669 |
validators=[RegexValidator(re.compile(regex_string))], |
| 2670 |
), |
| 2671 |
) |
| 2672 |
], |
| 2673 |
) |
| 2674 |
changes = self.get_changes([from_state], [to_state]) |
| 2675 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2676 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterField"]) |
| 2677 |
|
| 2678 |
def test_empty_unique_together(self): |
| 2679 |
"""Empty unique_together shouldn't generate a migration.""" |
| 2680 |
# Explicitly testing for not specified, since this is the case after |
| 2681 |
# a CreateModel operation w/o any definition on the original model |
| 2682 |
model_state_not_specified = ModelState( |
| 2683 |
"a", "model", [("id", models.AutoField(primary_key=True))] |
| 2684 |
) |
| 2685 |
# Explicitly testing for None, since this was the issue in #23452 after |
| 2686 |
# an AlterUniqueTogether operation with e.g. () as value |
| 2687 |
model_state_none = ModelState( |
| 2688 |
"a", |
| 2689 |
"model", |
| 2690 |
[("id", models.AutoField(primary_key=True))], |
| 2691 |
{ |
| 2692 |
"unique_together": None, |
| 2693 |
}, |
| 2694 |
) |
| 2695 |
# Explicitly testing for the empty set, since we now always have sets. |
| 2696 |
# During removal (('col1', 'col2'),) --> () this becomes set([]) |
| 2697 |
model_state_empty = ModelState( |
| 2698 |
"a", |
| 2699 |
"model", |
| 2700 |
[("id", models.AutoField(primary_key=True))], |
| 2701 |
{ |
| 2702 |
"unique_together": set(), |
| 2703 |
}, |
| 2704 |
) |
| 2705 |
|
| 2706 |
def test(from_state, to_state, msg): |
| 2707 |
changes = self.get_changes([from_state], [to_state]) |
| 2708 |
if changes: |
| 2709 |
ops = ", ".join( |
| 2710 |
o.__class__.__name__ for o in changes["a"][0].operations |
| 2711 |
) |
| 2712 |
self.fail("Created operation(s) %s from %s" % (ops, msg)) |
| 2713 |
|
| 2714 |
tests = ( |
| 2715 |
( |
| 2716 |
model_state_not_specified, |
| 2717 |
model_state_not_specified, |
| 2718 |
'"not specified" to "not specified"', |
| 2719 |
), |
| 2720 |
(model_state_not_specified, model_state_none, '"not specified" to "None"'), |
| 2721 |
( |
| 2722 |
model_state_not_specified, |
| 2723 |
model_state_empty, |
| 2724 |
'"not specified" to "empty"', |
| 2725 |
), |
| 2726 |
(model_state_none, model_state_not_specified, '"None" to "not specified"'), |
| 2727 |
(model_state_none, model_state_none, '"None" to "None"'), |
| 2728 |
(model_state_none, model_state_empty, '"None" to "empty"'), |
| 2729 |
( |
| 2730 |
model_state_empty, |
| 2731 |
model_state_not_specified, |
| 2732 |
'"empty" to "not specified"', |
| 2733 |
), |
| 2734 |
(model_state_empty, model_state_none, '"empty" to "None"'), |
| 2735 |
(model_state_empty, model_state_empty, '"empty" to "empty"'), |
| 2736 |
) |
| 2737 |
|
| 2738 |
for t in tests: |
| 2739 |
test(*t) |
| 2740 |
|
| 2741 |
def test_create_model_with_indexes(self): |
| 2742 |
"""Test creation of new model with indexes already defined.""" |
| 2743 |
added_index = models.Index( |
| 2744 |
fields=["name"], name="create_model_with_indexes_idx" |
| 2745 |
) |
| 2746 |
author = ModelState( |
| 2747 |
"otherapp", |
| 2748 |
"Author", |
| 2749 |
[ |
| 2750 |
("id", models.AutoField(primary_key=True)), |
| 2751 |
("name", models.CharField(max_length=200)), |
| 2752 |
], |
| 2753 |
{ |
| 2754 |
"indexes": [added_index], |
| 2755 |
}, |
| 2756 |
) |
| 2757 |
changes = self.get_changes([], [author]) |
| 2758 |
# Right number of migrations? |
| 2759 |
self.assertEqual(len(changes["otherapp"]), 1) |
| 2760 |
# Right number of actions? |
| 2761 |
migration = changes["otherapp"][0] |
| 2762 |
self.assertEqual(len(migration.operations), 1) |
| 2763 |
# Right actions order? |
| 2764 |
self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"]) |
| 2765 |
self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Author") |
| 2766 |
self.assertOperationAttributes( |
| 2767 |
changes, |
| 2768 |
"otherapp", |
| 2769 |
0, |
| 2770 |
0, |
| 2771 |
name="Author", |
| 2772 |
options={"indexes": [added_index]}, |
| 2773 |
) |
| 2774 |
|
| 2775 |
def test_add_indexes(self): |
| 2776 |
"""Test change detection of new indexes.""" |
| 2777 |
changes = self.get_changes( |
| 2778 |
[self.author_empty, self.book], [self.author_empty, self.book_indexes] |
| 2779 |
) |
| 2780 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 2781 |
self.assertOperationTypes(changes, "otherapp", 0, ["AddIndex"]) |
| 2782 |
added_index = models.Index( |
| 2783 |
fields=["author", "title"], name="book_title_author_idx" |
| 2784 |
) |
| 2785 |
self.assertOperationAttributes( |
| 2786 |
changes, "otherapp", 0, 0, model_name="book", index=added_index |
| 2787 |
) |
| 2788 |
|
| 2789 |
def test_remove_indexes(self): |
| 2790 |
"""Test change detection of removed indexes.""" |
| 2791 |
changes = self.get_changes( |
| 2792 |
[self.author_empty, self.book_indexes], [self.author_empty, self.book] |
| 2793 |
) |
| 2794 |
# Right number/type of migrations? |
| 2795 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 2796 |
self.assertOperationTypes(changes, "otherapp", 0, ["RemoveIndex"]) |
| 2797 |
self.assertOperationAttributes( |
| 2798 |
changes, "otherapp", 0, 0, model_name="book", name="book_title_author_idx" |
| 2799 |
) |
| 2800 |
|
| 2801 |
def test_rename_indexes(self): |
| 2802 |
book_renamed_indexes = ModelState( |
| 2803 |
"otherapp", |
| 2804 |
"Book", |
| 2805 |
[ |
| 2806 |
("id", models.AutoField(primary_key=True)), |
| 2807 |
("author", models.ForeignKey("testapp.Author", models.CASCADE)), |
| 2808 |
("title", models.CharField(max_length=200)), |
| 2809 |
], |
| 2810 |
{ |
| 2811 |
"indexes": [ |
| 2812 |
models.Index( |
| 2813 |
fields=["author", "title"], name="renamed_book_title_author_idx" |
| 2814 |
) |
| 2815 |
], |
| 2816 |
}, |
| 2817 |
) |
| 2818 |
changes = self.get_changes( |
| 2819 |
[self.author_empty, self.book_indexes], |
| 2820 |
[self.author_empty, book_renamed_indexes], |
| 2821 |
) |
| 2822 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 2823 |
self.assertOperationTypes(changes, "otherapp", 0, ["RenameIndex"]) |
| 2824 |
self.assertOperationAttributes( |
| 2825 |
changes, |
| 2826 |
"otherapp", |
| 2827 |
0, |
| 2828 |
0, |
| 2829 |
model_name="book", |
| 2830 |
new_name="renamed_book_title_author_idx", |
| 2831 |
old_name="book_title_author_idx", |
| 2832 |
) |
| 2833 |
|
| 2834 |
def test_order_fields_indexes(self): |
| 2835 |
"""Test change detection of reordering of fields in indexes.""" |
| 2836 |
changes = self.get_changes( |
| 2837 |
[self.author_empty, self.book_indexes], |
| 2838 |
[self.author_empty, self.book_unordered_indexes], |
| 2839 |
) |
| 2840 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 2841 |
self.assertOperationTypes(changes, "otherapp", 0, ["RemoveIndex", "AddIndex"]) |
| 2842 |
self.assertOperationAttributes( |
| 2843 |
changes, "otherapp", 0, 0, model_name="book", name="book_title_author_idx" |
| 2844 |
) |
| 2845 |
added_index = models.Index( |
| 2846 |
fields=["title", "author"], name="book_author_title_idx" |
| 2847 |
) |
| 2848 |
self.assertOperationAttributes( |
| 2849 |
changes, "otherapp", 0, 1, model_name="book", index=added_index |
| 2850 |
) |
| 2851 |
|
| 2852 |
def test_create_model_with_check_constraint(self): |
| 2853 |
"""Test creation of new model with constraints already defined.""" |
| 2854 |
author = ModelState( |
| 2855 |
"otherapp", |
| 2856 |
"Author", |
| 2857 |
[ |
| 2858 |
("id", models.AutoField(primary_key=True)), |
| 2859 |
("name", models.CharField(max_length=200)), |
| 2860 |
], |
| 2861 |
{ |
| 2862 |
"constraints": [ |
| 2863 |
models.CheckConstraint( |
| 2864 |
condition=models.Q(name__contains="Bob"), |
| 2865 |
name="name_contains_bob", |
| 2866 |
) |
| 2867 |
] |
| 2868 |
}, |
| 2869 |
) |
| 2870 |
changes = self.get_changes([], [author]) |
| 2871 |
constraint = models.CheckConstraint( |
| 2872 |
condition=models.Q(name__contains="Bob"), name="name_contains_bob" |
| 2873 |
) |
| 2874 |
# Right number of migrations? |
| 2875 |
self.assertEqual(len(changes["otherapp"]), 1) |
| 2876 |
# Right number of actions? |
| 2877 |
migration = changes["otherapp"][0] |
| 2878 |
self.assertEqual(len(migration.operations), 1) |
| 2879 |
# Right actions order? |
| 2880 |
self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"]) |
| 2881 |
self.assertOperationAttributes( |
| 2882 |
changes, |
| 2883 |
"otherapp", |
| 2884 |
0, |
| 2885 |
0, |
| 2886 |
name="Author", |
| 2887 |
options={"constraints": [constraint]}, |
| 2888 |
) |
| 2889 |
|
| 2890 |
def test_add_constraints(self): |
| 2891 |
"""Test change detection of new constraints.""" |
| 2892 |
changes = self.get_changes( |
| 2893 |
[self.author_name], [self.author_name_check_constraint] |
| 2894 |
) |
| 2895 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 2896 |
self.assertOperationTypes(changes, "testapp", 0, ["AddConstraint"]) |
| 2897 |
added_constraint = models.CheckConstraint( |
| 2898 |
condition=models.Q(name__contains="Bob"), name="name_contains_bob" |
| 2899 |
) |
| 2900 |
self.assertOperationAttributes( |
| 2901 |
changes, "testapp", 0, 0, model_name="author", constraint=added_constraint |
| 2902 |
) |
| 2903 |
|
| 2904 |
def test_add_constraints_with_new_model(self): |
| 2905 |
book_with_unique_title_and_pony = ModelState( |
| 2906 |
"otherapp", |
| 2907 |
"Book", |
| 2908 |
[ |
| 2909 |
("id", models.AutoField(primary_key=True)), |
| 2910 |
("title", models.CharField(max_length=200)), |
| 2911 |
("pony", models.ForeignKey("otherapp.Pony", models.CASCADE)), |
| 2912 |
], |
| 2913 |
{ |
| 2914 |
"constraints": [ |
| 2915 |
models.UniqueConstraint( |
| 2916 |
fields=["title", "pony"], |
| 2917 |
name="unique_title_pony", |
| 2918 |
) |
| 2919 |
] |
| 2920 |
}, |
| 2921 |
) |
| 2922 |
changes = self.get_changes( |
| 2923 |
[self.book_with_no_author], |
| 2924 |
[book_with_unique_title_and_pony, self.other_pony], |
| 2925 |
) |
| 2926 |
|
| 2927 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 2928 |
self.assertOperationTypes( |
| 2929 |
changes, |
| 2930 |
"otherapp", |
| 2931 |
0, |
| 2932 |
["CreateModel", "AddField", "AddConstraint"], |
| 2933 |
) |
| 2934 |
|
| 2935 |
def test_add_constraints_with_dict_keys(self): |
| 2936 |
book_types = {"F": "Fantasy", "M": "Mystery"} |
| 2937 |
book_with_type = ModelState( |
| 2938 |
"testapp", |
| 2939 |
"Book", |
| 2940 |
[ |
| 2941 |
("id", models.AutoField(primary_key=True)), |
| 2942 |
("type", models.CharField(max_length=1)), |
| 2943 |
], |
| 2944 |
{ |
| 2945 |
"constraints": [ |
| 2946 |
models.CheckConstraint( |
| 2947 |
condition=models.Q(type__in=book_types.keys()), |
| 2948 |
name="book_type_check", |
| 2949 |
), |
| 2950 |
], |
| 2951 |
}, |
| 2952 |
) |
| 2953 |
book_with_resolved_type = ModelState( |
| 2954 |
"testapp", |
| 2955 |
"Book", |
| 2956 |
[ |
| 2957 |
("id", models.AutoField(primary_key=True)), |
| 2958 |
("type", models.CharField(max_length=1)), |
| 2959 |
], |
| 2960 |
{ |
| 2961 |
"constraints": [ |
| 2962 |
models.CheckConstraint( |
| 2963 |
condition=models.Q(("type__in", tuple(book_types))), |
| 2964 |
name="book_type_check", |
| 2965 |
), |
| 2966 |
], |
| 2967 |
}, |
| 2968 |
) |
| 2969 |
changes = self.get_changes([book_with_type], [book_with_resolved_type]) |
| 2970 |
self.assertEqual(len(changes), 0) |
| 2971 |
|
| 2972 |
def test_add_index_with_new_model(self): |
| 2973 |
book_with_index_title_and_pony = ModelState( |
| 2974 |
"otherapp", |
| 2975 |
"Book", |
| 2976 |
[ |
| 2977 |
("id", models.AutoField(primary_key=True)), |
| 2978 |
("title", models.CharField(max_length=200)), |
| 2979 |
("pony", models.ForeignKey("otherapp.Pony", models.CASCADE)), |
| 2980 |
], |
| 2981 |
{ |
| 2982 |
"indexes": [ |
| 2983 |
models.Index(fields=["title", "pony"], name="index_title_pony"), |
| 2984 |
] |
| 2985 |
}, |
| 2986 |
) |
| 2987 |
changes = self.get_changes( |
| 2988 |
[self.book_with_no_author], |
| 2989 |
[book_with_index_title_and_pony, self.other_pony], |
| 2990 |
) |
| 2991 |
|
| 2992 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 2993 |
self.assertOperationTypes( |
| 2994 |
changes, |
| 2995 |
"otherapp", |
| 2996 |
0, |
| 2997 |
["CreateModel", "AddField", "AddIndex"], |
| 2998 |
) |
| 2999 |
|
| 3000 |
def test_alter_constraint(self): |
| 3001 |
book_constraint = models.CheckConstraint( |
| 3002 |
condition=models.Q(title__contains="title"), |
| 3003 |
name="title_contains_title", |
| 3004 |
) |
| 3005 |
book_altered_constraint = models.CheckConstraint( |
| 3006 |
condition=models.Q(title__contains="title"), |
| 3007 |
name="title_contains_title", |
| 3008 |
violation_error_code="error_code", |
| 3009 |
) |
| 3010 |
author_altered_constraint = models.CheckConstraint( |
| 3011 |
condition=models.Q(name__contains="Bob"), |
| 3012 |
name="name_contains_bob", |
| 3013 |
violation_error_message="Name doesn't contain Bob", |
| 3014 |
) |
| 3015 |
|
| 3016 |
book_check_constraint = copy.deepcopy(self.book) |
| 3017 |
book_check_constraint_with_error_message = copy.deepcopy(self.book) |
| 3018 |
author_name_check_constraint_with_error_message = copy.deepcopy( |
| 3019 |
self.author_name_check_constraint |
| 3020 |
) |
| 3021 |
|
| 3022 |
book_check_constraint.options = {"constraints": [book_constraint]} |
| 3023 |
book_check_constraint_with_error_message.options = { |
| 3024 |
"constraints": [book_altered_constraint] |
| 3025 |
} |
| 3026 |
author_name_check_constraint_with_error_message.options = { |
| 3027 |
"constraints": [author_altered_constraint] |
| 3028 |
} |
| 3029 |
|
| 3030 |
changes = self.get_changes( |
| 3031 |
[self.author_name_check_constraint, book_check_constraint], |
| 3032 |
[ |
| 3033 |
author_name_check_constraint_with_error_message, |
| 3034 |
book_check_constraint_with_error_message, |
| 3035 |
], |
| 3036 |
) |
| 3037 |
|
| 3038 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3039 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterConstraint"]) |
| 3040 |
self.assertOperationAttributes( |
| 3041 |
changes, |
| 3042 |
"testapp", |
| 3043 |
0, |
| 3044 |
0, |
| 3045 |
model_name="author", |
| 3046 |
name="name_contains_bob", |
| 3047 |
constraint=author_altered_constraint, |
| 3048 |
) |
| 3049 |
|
| 3050 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 3051 |
self.assertOperationTypes(changes, "otherapp", 0, ["AlterConstraint"]) |
| 3052 |
self.assertOperationAttributes( |
| 3053 |
changes, |
| 3054 |
"otherapp", |
| 3055 |
0, |
| 3056 |
0, |
| 3057 |
model_name="book", |
| 3058 |
name="title_contains_title", |
| 3059 |
constraint=book_altered_constraint, |
| 3060 |
) |
| 3061 |
self.assertMigrationDependencies( |
| 3062 |
changes, "otherapp", 0, [("testapp", "auto_1")] |
| 3063 |
) |
| 3064 |
|
| 3065 |
def test_remove_constraints(self): |
| 3066 |
"""Test change detection of removed constraints.""" |
| 3067 |
changes = self.get_changes( |
| 3068 |
[self.author_name_check_constraint], [self.author_name] |
| 3069 |
) |
| 3070 |
# Right number/type of migrations? |
| 3071 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3072 |
self.assertOperationTypes(changes, "testapp", 0, ["RemoveConstraint"]) |
| 3073 |
self.assertOperationAttributes( |
| 3074 |
changes, "testapp", 0, 0, model_name="author", name="name_contains_bob" |
| 3075 |
) |
| 3076 |
|
| 3077 |
def test_constraint_dropped_and_recreated(self): |
| 3078 |
altered_constraint = models.CheckConstraint( |
| 3079 |
condition=models.Q(name__contains="bob"), |
| 3080 |
name="name_contains_bob", |
| 3081 |
) |
| 3082 |
author_name_check_constraint_lowercased = copy.deepcopy( |
| 3083 |
self.author_name_check_constraint |
| 3084 |
) |
| 3085 |
author_name_check_constraint_lowercased.options = { |
| 3086 |
"constraints": [altered_constraint] |
| 3087 |
} |
| 3088 |
changes = self.get_changes( |
| 3089 |
[self.author_name_check_constraint], |
| 3090 |
[author_name_check_constraint_lowercased], |
| 3091 |
) |
| 3092 |
|
| 3093 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3094 |
self.assertOperationTypes( |
| 3095 |
changes, "testapp", 0, ["RemoveConstraint", "AddConstraint"] |
| 3096 |
) |
| 3097 |
self.assertOperationAttributes( |
| 3098 |
changes, |
| 3099 |
"testapp", |
| 3100 |
0, |
| 3101 |
0, |
| 3102 |
model_name="author", |
| 3103 |
name="name_contains_bob", |
| 3104 |
) |
| 3105 |
self.assertOperationAttributes( |
| 3106 |
changes, |
| 3107 |
"testapp", |
| 3108 |
0, |
| 3109 |
1, |
| 3110 |
model_name="author", |
| 3111 |
constraint=altered_constraint, |
| 3112 |
) |
| 3113 |
|
| 3114 |
def test_add_unique_together(self): |
| 3115 |
"""Tests unique_together detection.""" |
| 3116 |
changes = self.get_changes( |
| 3117 |
[self.author_empty, self.book], |
| 3118 |
[self.author_empty, self.book_unique_together], |
| 3119 |
) |
| 3120 |
# Right number/type of migrations? |
| 3121 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 3122 |
self.assertOperationTypes(changes, "otherapp", 0, ["AlterUniqueTogether"]) |
| 3123 |
self.assertOperationAttributes( |
| 3124 |
changes, |
| 3125 |
"otherapp", |
| 3126 |
0, |
| 3127 |
0, |
| 3128 |
name="book", |
| 3129 |
unique_together={("author", "title")}, |
| 3130 |
) |
| 3131 |
|
| 3132 |
def test_remove_unique_together(self): |
| 3133 |
"""Tests unique_together detection.""" |
| 3134 |
changes = self.get_changes( |
| 3135 |
[self.author_empty, self.book_unique_together], |
| 3136 |
[self.author_empty, self.book], |
| 3137 |
) |
| 3138 |
# Right number/type of migrations? |
| 3139 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 3140 |
self.assertOperationTypes(changes, "otherapp", 0, ["AlterUniqueTogether"]) |
| 3141 |
self.assertOperationAttributes( |
| 3142 |
changes, "otherapp", 0, 0, name="book", unique_together=set() |
| 3143 |
) |
| 3144 |
|
| 3145 |
def test_unique_together_remove_fk(self): |
| 3146 |
"""Tests unique_together and field removal detection & ordering""" |
| 3147 |
changes = self.get_changes( |
| 3148 |
[self.author_empty, self.book_unique_together], |
| 3149 |
[self.author_empty, self.book_with_no_author], |
| 3150 |
) |
| 3151 |
# Right number/type of migrations? |
| 3152 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 3153 |
self.assertOperationTypes( |
| 3154 |
changes, |
| 3155 |
"otherapp", |
| 3156 |
0, |
| 3157 |
["AlterUniqueTogether", "RemoveField"], |
| 3158 |
) |
| 3159 |
self.assertOperationAttributes( |
| 3160 |
changes, "otherapp", 0, 0, name="book", unique_together=set() |
| 3161 |
) |
| 3162 |
self.assertOperationAttributes( |
| 3163 |
changes, "otherapp", 0, 1, model_name="book", name="author" |
| 3164 |
) |
| 3165 |
|
| 3166 |
def test_unique_together_no_changes(self): |
| 3167 |
""" |
| 3168 |
unique_together doesn't generate a migration if no |
| 3169 |
changes have been made. |
| 3170 |
""" |
| 3171 |
changes = self.get_changes( |
| 3172 |
[self.author_empty, self.book_unique_together], |
| 3173 |
[self.author_empty, self.book_unique_together], |
| 3174 |
) |
| 3175 |
# Right number of migrations? |
| 3176 |
self.assertEqual(len(changes), 0) |
| 3177 |
|
| 3178 |
def test_unique_together_ordering(self): |
| 3179 |
""" |
| 3180 |
unique_together also triggers on ordering changes. |
| 3181 |
""" |
| 3182 |
changes = self.get_changes( |
| 3183 |
[self.author_empty, self.book_unique_together], |
| 3184 |
[self.author_empty, self.book_unique_together_2], |
| 3185 |
) |
| 3186 |
# Right number/type of migrations? |
| 3187 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 3188 |
self.assertOperationTypes( |
| 3189 |
changes, |
| 3190 |
"otherapp", |
| 3191 |
0, |
| 3192 |
["AlterUniqueTogether"], |
| 3193 |
) |
| 3194 |
self.assertOperationAttributes( |
| 3195 |
changes, |
| 3196 |
"otherapp", |
| 3197 |
0, |
| 3198 |
0, |
| 3199 |
name="book", |
| 3200 |
unique_together={("title", "author")}, |
| 3201 |
) |
| 3202 |
|
| 3203 |
def test_add_field_and_unique_together(self): |
| 3204 |
""" |
| 3205 |
Added fields will be created before using them in unique_together. |
| 3206 |
""" |
| 3207 |
changes = self.get_changes( |
| 3208 |
[self.author_empty, self.book], |
| 3209 |
[self.author_empty, self.book_unique_together_3], |
| 3210 |
) |
| 3211 |
# Right number/type of migrations? |
| 3212 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 3213 |
self.assertOperationTypes( |
| 3214 |
changes, |
| 3215 |
"otherapp", |
| 3216 |
0, |
| 3217 |
["AddField", "AlterUniqueTogether"], |
| 3218 |
) |
| 3219 |
self.assertOperationAttributes( |
| 3220 |
changes, |
| 3221 |
"otherapp", |
| 3222 |
0, |
| 3223 |
1, |
| 3224 |
name="book", |
| 3225 |
unique_together={("title", "newfield")}, |
| 3226 |
) |
| 3227 |
|
| 3228 |
def test_create_model_and_unique_together(self): |
| 3229 |
author = ModelState( |
| 3230 |
"otherapp", |
| 3231 |
"Author", |
| 3232 |
[ |
| 3233 |
("id", models.AutoField(primary_key=True)), |
| 3234 |
("name", models.CharField(max_length=200)), |
| 3235 |
], |
| 3236 |
) |
| 3237 |
book_with_author = ModelState( |
| 3238 |
"otherapp", |
| 3239 |
"Book", |
| 3240 |
[ |
| 3241 |
("id", models.AutoField(primary_key=True)), |
| 3242 |
("author", models.ForeignKey("otherapp.Author", models.CASCADE)), |
| 3243 |
("title", models.CharField(max_length=200)), |
| 3244 |
], |
| 3245 |
{ |
| 3246 |
"unique_together": {("title", "author")}, |
| 3247 |
}, |
| 3248 |
) |
| 3249 |
changes = self.get_changes( |
| 3250 |
[self.book_with_no_author], [author, book_with_author] |
| 3251 |
) |
| 3252 |
# Right number of migrations? |
| 3253 |
self.assertEqual(len(changes["otherapp"]), 1) |
| 3254 |
# Right number of actions? |
| 3255 |
migration = changes["otherapp"][0] |
| 3256 |
self.assertEqual(len(migration.operations), 3) |
| 3257 |
# Right actions order? |
| 3258 |
self.assertOperationTypes( |
| 3259 |
changes, |
| 3260 |
"otherapp", |
| 3261 |
0, |
| 3262 |
["CreateModel", "AddField", "AlterUniqueTogether"], |
| 3263 |
) |
| 3264 |
|
| 3265 |
def test_remove_field_and_unique_together(self): |
| 3266 |
""" |
| 3267 |
Removed fields will be removed after updating unique_together. |
| 3268 |
""" |
| 3269 |
changes = self.get_changes( |
| 3270 |
[self.author_empty, self.book_unique_together_3], |
| 3271 |
[self.author_empty, self.book_unique_together], |
| 3272 |
) |
| 3273 |
# Right number/type of migrations? |
| 3274 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 3275 |
self.assertOperationTypes( |
| 3276 |
changes, |
| 3277 |
"otherapp", |
| 3278 |
0, |
| 3279 |
["AlterUniqueTogether", "RemoveField"], |
| 3280 |
) |
| 3281 |
self.assertOperationAttributes( |
| 3282 |
changes, |
| 3283 |
"otherapp", |
| 3284 |
0, |
| 3285 |
0, |
| 3286 |
name="book", |
| 3287 |
unique_together={("author", "title")}, |
| 3288 |
) |
| 3289 |
self.assertOperationAttributes( |
| 3290 |
changes, |
| 3291 |
"otherapp", |
| 3292 |
0, |
| 3293 |
1, |
| 3294 |
model_name="book", |
| 3295 |
name="newfield", |
| 3296 |
) |
| 3297 |
|
| 3298 |
def test_alter_field_and_unique_together(self): |
| 3299 |
"""Fields are altered after deleting some unique_together.""" |
| 3300 |
initial_author = ModelState( |
| 3301 |
"testapp", |
| 3302 |
"Author", |
| 3303 |
[ |
| 3304 |
("id", models.AutoField(primary_key=True)), |
| 3305 |
("name", models.CharField(max_length=200)), |
| 3306 |
("age", models.IntegerField(db_index=True)), |
| 3307 |
], |
| 3308 |
{ |
| 3309 |
"unique_together": {("name",)}, |
| 3310 |
}, |
| 3311 |
) |
| 3312 |
author_reversed_constraints = ModelState( |
| 3313 |
"testapp", |
| 3314 |
"Author", |
| 3315 |
[ |
| 3316 |
("id", models.AutoField(primary_key=True)), |
| 3317 |
("name", models.CharField(max_length=200, unique=True)), |
| 3318 |
("age", models.IntegerField()), |
| 3319 |
], |
| 3320 |
{ |
| 3321 |
"unique_together": {("age",)}, |
| 3322 |
}, |
| 3323 |
) |
| 3324 |
changes = self.get_changes([initial_author], [author_reversed_constraints]) |
| 3325 |
|
| 3326 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3327 |
self.assertOperationTypes( |
| 3328 |
changes, |
| 3329 |
"testapp", |
| 3330 |
0, |
| 3331 |
[ |
| 3332 |
"AlterUniqueTogether", |
| 3333 |
"AlterField", |
| 3334 |
"AlterField", |
| 3335 |
"AlterUniqueTogether", |
| 3336 |
], |
| 3337 |
) |
| 3338 |
self.assertOperationAttributes( |
| 3339 |
changes, |
| 3340 |
"testapp", |
| 3341 |
0, |
| 3342 |
0, |
| 3343 |
name="author", |
| 3344 |
unique_together=set(), |
| 3345 |
) |
| 3346 |
self.assertOperationAttributes( |
| 3347 |
changes, |
| 3348 |
"testapp", |
| 3349 |
0, |
| 3350 |
1, |
| 3351 |
model_name="author", |
| 3352 |
name="age", |
| 3353 |
) |
| 3354 |
self.assertOperationAttributes( |
| 3355 |
changes, |
| 3356 |
"testapp", |
| 3357 |
0, |
| 3358 |
2, |
| 3359 |
model_name="author", |
| 3360 |
name="name", |
| 3361 |
) |
| 3362 |
self.assertOperationAttributes( |
| 3363 |
changes, |
| 3364 |
"testapp", |
| 3365 |
0, |
| 3366 |
3, |
| 3367 |
name="author", |
| 3368 |
unique_together={("age",)}, |
| 3369 |
) |
| 3370 |
|
| 3371 |
def test_partly_alter_unique_together_increase(self): |
| 3372 |
initial_author = ModelState( |
| 3373 |
"testapp", |
| 3374 |
"Author", |
| 3375 |
[ |
| 3376 |
("id", models.AutoField(primary_key=True)), |
| 3377 |
("name", models.CharField(max_length=200)), |
| 3378 |
("age", models.IntegerField()), |
| 3379 |
], |
| 3380 |
{ |
| 3381 |
"unique_together": {("name",)}, |
| 3382 |
}, |
| 3383 |
) |
| 3384 |
author_new_constraints = ModelState( |
| 3385 |
"testapp", |
| 3386 |
"Author", |
| 3387 |
[ |
| 3388 |
("id", models.AutoField(primary_key=True)), |
| 3389 |
("name", models.CharField(max_length=200)), |
| 3390 |
("age", models.IntegerField()), |
| 3391 |
], |
| 3392 |
{ |
| 3393 |
"unique_together": {("name",), ("age",)}, |
| 3394 |
}, |
| 3395 |
) |
| 3396 |
changes = self.get_changes([initial_author], [author_new_constraints]) |
| 3397 |
|
| 3398 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3399 |
self.assertOperationTypes( |
| 3400 |
changes, |
| 3401 |
"testapp", |
| 3402 |
0, |
| 3403 |
["AlterUniqueTogether"], |
| 3404 |
) |
| 3405 |
self.assertOperationAttributes( |
| 3406 |
changes, |
| 3407 |
"testapp", |
| 3408 |
0, |
| 3409 |
0, |
| 3410 |
name="author", |
| 3411 |
unique_together={("name",), ("age",)}, |
| 3412 |
) |
| 3413 |
|
| 3414 |
def test_partly_alter_unique_together_decrease(self): |
| 3415 |
initial_author = ModelState( |
| 3416 |
"testapp", |
| 3417 |
"Author", |
| 3418 |
[ |
| 3419 |
("id", models.AutoField(primary_key=True)), |
| 3420 |
("name", models.CharField(max_length=200)), |
| 3421 |
("age", models.IntegerField()), |
| 3422 |
], |
| 3423 |
{ |
| 3424 |
"unique_together": {("name",), ("age",)}, |
| 3425 |
}, |
| 3426 |
) |
| 3427 |
author_new_constraints = ModelState( |
| 3428 |
"testapp", |
| 3429 |
"Author", |
| 3430 |
[ |
| 3431 |
("id", models.AutoField(primary_key=True)), |
| 3432 |
("name", models.CharField(max_length=200)), |
| 3433 |
("age", models.IntegerField()), |
| 3434 |
], |
| 3435 |
{ |
| 3436 |
"unique_together": {("name",)}, |
| 3437 |
}, |
| 3438 |
) |
| 3439 |
changes = self.get_changes([initial_author], [author_new_constraints]) |
| 3440 |
|
| 3441 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3442 |
self.assertOperationTypes( |
| 3443 |
changes, |
| 3444 |
"testapp", |
| 3445 |
0, |
| 3446 |
["AlterUniqueTogether"], |
| 3447 |
) |
| 3448 |
self.assertOperationAttributes( |
| 3449 |
changes, |
| 3450 |
"testapp", |
| 3451 |
0, |
| 3452 |
0, |
| 3453 |
name="author", |
| 3454 |
unique_together={("name",)}, |
| 3455 |
) |
| 3456 |
|
| 3457 |
def test_rename_field_and_unique_together(self): |
| 3458 |
"""Fields are renamed before updating unique_together.""" |
| 3459 |
changes = self.get_changes( |
| 3460 |
[self.author_empty, self.book_unique_together_3], |
| 3461 |
[self.author_empty, self.book_unique_together_4], |
| 3462 |
MigrationQuestioner({"ask_rename": True}), |
| 3463 |
) |
| 3464 |
# Right number/type of migrations? |
| 3465 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 3466 |
self.assertOperationTypes( |
| 3467 |
changes, |
| 3468 |
"otherapp", |
| 3469 |
0, |
| 3470 |
["RenameField", "AlterUniqueTogether"], |
| 3471 |
) |
| 3472 |
self.assertOperationAttributes( |
| 3473 |
changes, |
| 3474 |
"otherapp", |
| 3475 |
0, |
| 3476 |
1, |
| 3477 |
name="book", |
| 3478 |
unique_together={("title", "newfield2")}, |
| 3479 |
) |
| 3480 |
|
| 3481 |
def test_proxy(self): |
| 3482 |
"""The autodetector correctly deals with proxy models.""" |
| 3483 |
# First, we test adding a proxy model |
| 3484 |
changes = self.get_changes( |
| 3485 |
[self.author_empty], [self.author_empty, self.author_proxy] |
| 3486 |
) |
| 3487 |
# Right number/type of migrations? |
| 3488 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3489 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"]) |
| 3490 |
self.assertOperationAttributes( |
| 3491 |
changes, |
| 3492 |
"testapp", |
| 3493 |
0, |
| 3494 |
0, |
| 3495 |
name="AuthorProxy", |
| 3496 |
options={"proxy": True, "indexes": [], "constraints": []}, |
| 3497 |
) |
| 3498 |
# Now, we test turning a proxy model into a non-proxy model |
| 3499 |
# It should delete the proxy then make the real one |
| 3500 |
changes = self.get_changes( |
| 3501 |
[self.author_empty, self.author_proxy], |
| 3502 |
[self.author_empty, self.author_proxy_notproxy], |
| 3503 |
) |
| 3504 |
# Right number/type of migrations? |
| 3505 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3506 |
self.assertOperationTypes(changes, "testapp", 0, ["DeleteModel", "CreateModel"]) |
| 3507 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="AuthorProxy") |
| 3508 |
self.assertOperationAttributes( |
| 3509 |
changes, "testapp", 0, 1, name="AuthorProxy", options={} |
| 3510 |
) |
| 3511 |
|
| 3512 |
def test_proxy_non_model_parent(self): |
| 3513 |
class Mixin: |
| 3514 |
pass |
| 3515 |
|
| 3516 |
author_proxy_non_model_parent = ModelState( |
| 3517 |
"testapp", |
| 3518 |
"AuthorProxy", |
| 3519 |
[], |
| 3520 |
{"proxy": True}, |
| 3521 |
(Mixin, "testapp.author"), |
| 3522 |
) |
| 3523 |
changes = self.get_changes( |
| 3524 |
[self.author_empty], |
| 3525 |
[self.author_empty, author_proxy_non_model_parent], |
| 3526 |
) |
| 3527 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3528 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"]) |
| 3529 |
self.assertOperationAttributes( |
| 3530 |
changes, |
| 3531 |
"testapp", |
| 3532 |
0, |
| 3533 |
0, |
| 3534 |
name="AuthorProxy", |
| 3535 |
options={"proxy": True, "indexes": [], "constraints": []}, |
| 3536 |
bases=(Mixin, "testapp.author"), |
| 3537 |
) |
| 3538 |
|
| 3539 |
def test_proxy_custom_pk(self): |
| 3540 |
""" |
| 3541 |
#23415 - The autodetector must correctly deal with custom FK on proxy |
| 3542 |
models. |
| 3543 |
""" |
| 3544 |
# First, we test the default pk field name |
| 3545 |
changes = self.get_changes( |
| 3546 |
[], [self.author_empty, self.author_proxy_third, self.book_proxy_fk] |
| 3547 |
) |
| 3548 |
# The model the FK is pointing from and to. |
| 3549 |
self.assertEqual( |
| 3550 |
changes["otherapp"][0].operations[0].fields[2][1].remote_field.model, |
| 3551 |
"thirdapp.AuthorProxy", |
| 3552 |
) |
| 3553 |
# Now, we test the custom pk field name |
| 3554 |
changes = self.get_changes( |
| 3555 |
[], [self.author_custom_pk, self.author_proxy_third, self.book_proxy_fk] |
| 3556 |
) |
| 3557 |
# The model the FK is pointing from and to. |
| 3558 |
self.assertEqual( |
| 3559 |
changes["otherapp"][0].operations[0].fields[2][1].remote_field.model, |
| 3560 |
"thirdapp.AuthorProxy", |
| 3561 |
) |
| 3562 |
|
| 3563 |
def test_proxy_to_mti_with_fk_to_proxy(self): |
| 3564 |
# First, test the pk table and field name. |
| 3565 |
to_state = self.make_project_state( |
| 3566 |
[self.author_empty, self.author_proxy_third, self.book_proxy_fk], |
| 3567 |
) |
| 3568 |
changes = self.get_changes([], to_state) |
| 3569 |
fk_field = changes["otherapp"][0].operations[0].fields[2][1] |
| 3570 |
self.assertEqual( |
| 3571 |
to_state.get_concrete_model_key(fk_field.remote_field.model), |
| 3572 |
("testapp", "author"), |
| 3573 |
) |
| 3574 |
self.assertEqual(fk_field.remote_field.model, "thirdapp.AuthorProxy") |
| 3575 |
|
| 3576 |
# Change AuthorProxy to use MTI. |
| 3577 |
from_state = to_state.clone() |
| 3578 |
to_state = self.make_project_state( |
| 3579 |
[self.author_empty, self.author_proxy_third_notproxy, self.book_proxy_fk], |
| 3580 |
) |
| 3581 |
changes = self.get_changes(from_state, to_state) |
| 3582 |
# Right number/type of migrations for the AuthorProxy model? |
| 3583 |
self.assertNumberMigrations(changes, "thirdapp", 1) |
| 3584 |
self.assertOperationTypes( |
| 3585 |
changes, "thirdapp", 0, ["DeleteModel", "CreateModel"] |
| 3586 |
) |
| 3587 |
# Right number/type of migrations for the Book model with a FK to |
| 3588 |
# AuthorProxy? |
| 3589 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 3590 |
self.assertOperationTypes(changes, "otherapp", 0, ["AlterField"]) |
| 3591 |
# otherapp should depend on thirdapp. |
| 3592 |
self.assertMigrationDependencies( |
| 3593 |
changes, "otherapp", 0, [("thirdapp", "auto_1")] |
| 3594 |
) |
| 3595 |
# Now, test the pk table and field name. |
| 3596 |
fk_field = changes["otherapp"][0].operations[0].field |
| 3597 |
self.assertEqual( |
| 3598 |
to_state.get_concrete_model_key(fk_field.remote_field.model), |
| 3599 |
("thirdapp", "authorproxy"), |
| 3600 |
) |
| 3601 |
self.assertEqual(fk_field.remote_field.model, "thirdapp.AuthorProxy") |
| 3602 |
|
| 3603 |
def test_proxy_to_mti_with_fk_to_proxy_proxy(self): |
| 3604 |
# First, test the pk table and field name. |
| 3605 |
to_state = self.make_project_state( |
| 3606 |
[ |
| 3607 |
self.author_empty, |
| 3608 |
self.author_proxy, |
| 3609 |
self.author_proxy_proxy, |
| 3610 |
self.book_proxy_proxy_fk, |
| 3611 |
] |
| 3612 |
) |
| 3613 |
changes = self.get_changes([], to_state) |
| 3614 |
fk_field = changes["otherapp"][0].operations[0].fields[1][1] |
| 3615 |
self.assertEqual( |
| 3616 |
to_state.get_concrete_model_key(fk_field.remote_field.model), |
| 3617 |
("testapp", "author"), |
| 3618 |
) |
| 3619 |
self.assertEqual(fk_field.remote_field.model, "testapp.AAuthorProxyProxy") |
| 3620 |
|
| 3621 |
# Change AuthorProxy to use MTI. FK still points to AAuthorProxyProxy, |
| 3622 |
# a proxy of AuthorProxy. |
| 3623 |
from_state = to_state.clone() |
| 3624 |
to_state = self.make_project_state( |
| 3625 |
[ |
| 3626 |
self.author_empty, |
| 3627 |
self.author_proxy_notproxy, |
| 3628 |
self.author_proxy_proxy, |
| 3629 |
self.book_proxy_proxy_fk, |
| 3630 |
] |
| 3631 |
) |
| 3632 |
changes = self.get_changes(from_state, to_state) |
| 3633 |
# Right number/type of migrations for the AuthorProxy model? |
| 3634 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3635 |
self.assertOperationTypes(changes, "testapp", 0, ["DeleteModel", "CreateModel"]) |
| 3636 |
# Right number/type of migrations for the Book model with a FK to |
| 3637 |
# AAuthorProxyProxy? |
| 3638 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 3639 |
self.assertOperationTypes(changes, "otherapp", 0, ["AlterField"]) |
| 3640 |
# otherapp should depend on testapp. |
| 3641 |
self.assertMigrationDependencies( |
| 3642 |
changes, "otherapp", 0, [("testapp", "auto_1")] |
| 3643 |
) |
| 3644 |
# Now, test the pk table and field name. |
| 3645 |
fk_field = changes["otherapp"][0].operations[0].field |
| 3646 |
self.assertEqual( |
| 3647 |
to_state.get_concrete_model_key(fk_field.remote_field.model), |
| 3648 |
("testapp", "authorproxy"), |
| 3649 |
) |
| 3650 |
self.assertEqual(fk_field.remote_field.model, "testapp.AAuthorProxyProxy") |
| 3651 |
|
| 3652 |
def test_unmanaged_create(self): |
| 3653 |
"""The autodetector correctly deals with managed models.""" |
| 3654 |
# First, we test adding an unmanaged model |
| 3655 |
changes = self.get_changes( |
| 3656 |
[self.author_empty], [self.author_empty, self.author_unmanaged] |
| 3657 |
) |
| 3658 |
# Right number/type of migrations? |
| 3659 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3660 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"]) |
| 3661 |
self.assertOperationAttributes( |
| 3662 |
changes, "testapp", 0, 0, name="AuthorUnmanaged", options={"managed": False} |
| 3663 |
) |
| 3664 |
|
| 3665 |
def test_unmanaged_delete(self): |
| 3666 |
changes = self.get_changes( |
| 3667 |
[self.author_empty, self.author_unmanaged], [self.author_empty] |
| 3668 |
) |
| 3669 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3670 |
self.assertOperationTypes(changes, "testapp", 0, ["DeleteModel"]) |
| 3671 |
|
| 3672 |
def test_unmanaged_to_managed(self): |
| 3673 |
# Now, we test turning an unmanaged model into a managed model |
| 3674 |
changes = self.get_changes( |
| 3675 |
[self.author_empty, self.author_unmanaged], |
| 3676 |
[self.author_empty, self.author_unmanaged_managed], |
| 3677 |
) |
| 3678 |
# Right number/type of migrations? |
| 3679 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3680 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"]) |
| 3681 |
self.assertOperationAttributes( |
| 3682 |
changes, "testapp", 0, 0, name="authorunmanaged", options={} |
| 3683 |
) |
| 3684 |
|
| 3685 |
def test_managed_to_unmanaged(self): |
| 3686 |
# Now, we turn managed to unmanaged. |
| 3687 |
changes = self.get_changes( |
| 3688 |
[self.author_empty, self.author_unmanaged_managed], |
| 3689 |
[self.author_empty, self.author_unmanaged], |
| 3690 |
) |
| 3691 |
# Right number/type of migrations? |
| 3692 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3693 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"]) |
| 3694 |
self.assertOperationAttributes( |
| 3695 |
changes, "testapp", 0, 0, name="authorunmanaged", options={"managed": False} |
| 3696 |
) |
| 3697 |
|
| 3698 |
def test_unmanaged_custom_pk(self): |
| 3699 |
""" |
| 3700 |
#23415 - The autodetector must correctly deal with custom FK on |
| 3701 |
unmanaged models. |
| 3702 |
""" |
| 3703 |
# First, we test the default pk field name |
| 3704 |
changes = self.get_changes([], [self.author_unmanaged_default_pk, self.book]) |
| 3705 |
# The model the FK on the book model points to. |
| 3706 |
fk_field = changes["otherapp"][0].operations[0].fields[2][1] |
| 3707 |
self.assertEqual(fk_field.remote_field.model, "testapp.Author") |
| 3708 |
# Now, we test the custom pk field name |
| 3709 |
changes = self.get_changes([], [self.author_unmanaged_custom_pk, self.book]) |
| 3710 |
# The model the FK on the book model points to. |
| 3711 |
fk_field = changes["otherapp"][0].operations[0].fields[2][1] |
| 3712 |
self.assertEqual(fk_field.remote_field.model, "testapp.Author") |
| 3713 |
|
| 3714 |
@override_settings(AUTH_USER_MODEL="thirdapp.CustomUser") |
| 3715 |
def test_swappable(self): |
| 3716 |
with isolate_lru_cache(apps.get_swappable_settings_name): |
| 3717 |
changes = self.get_changes( |
| 3718 |
[self.custom_user], [self.custom_user, self.author_with_custom_user] |
| 3719 |
) |
| 3720 |
# Right number/type of migrations? |
| 3721 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3722 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"]) |
| 3723 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author") |
| 3724 |
self.assertMigrationDependencies( |
| 3725 |
changes, "testapp", 0, [("__setting__", "AUTH_USER_MODEL")] |
| 3726 |
) |
| 3727 |
|
| 3728 |
def test_swappable_lowercase(self): |
| 3729 |
model_state = ModelState( |
| 3730 |
"testapp", |
| 3731 |
"Document", |
| 3732 |
[ |
| 3733 |
("id", models.AutoField(primary_key=True)), |
| 3734 |
( |
| 3735 |
"owner", |
| 3736 |
models.ForeignKey( |
| 3737 |
settings.AUTH_USER_MODEL.lower(), |
| 3738 |
models.CASCADE, |
| 3739 |
), |
| 3740 |
), |
| 3741 |
], |
| 3742 |
) |
| 3743 |
with isolate_lru_cache(apps.get_swappable_settings_name): |
| 3744 |
changes = self.get_changes([], [model_state]) |
| 3745 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3746 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"]) |
| 3747 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Document") |
| 3748 |
self.assertMigrationDependencies( |
| 3749 |
changes, |
| 3750 |
"testapp", |
| 3751 |
0, |
| 3752 |
[("__setting__", "AUTH_USER_MODEL")], |
| 3753 |
) |
| 3754 |
|
| 3755 |
@override_settings(AUTH_USER_MODEL="thirdapp.CustomUser") |
| 3756 |
def test_swappable_many_to_many_model_case(self): |
| 3757 |
document_lowercase = ModelState( |
| 3758 |
"testapp", |
| 3759 |
"Document", |
| 3760 |
[ |
| 3761 |
("id", models.AutoField(primary_key=True)), |
| 3762 |
("owners", models.ManyToManyField(settings.AUTH_USER_MODEL.lower())), |
| 3763 |
], |
| 3764 |
) |
| 3765 |
document = ModelState( |
| 3766 |
"testapp", |
| 3767 |
"Document", |
| 3768 |
[ |
| 3769 |
("id", models.AutoField(primary_key=True)), |
| 3770 |
("owners", models.ManyToManyField(settings.AUTH_USER_MODEL)), |
| 3771 |
], |
| 3772 |
) |
| 3773 |
with isolate_lru_cache(apps.get_swappable_settings_name): |
| 3774 |
changes = self.get_changes( |
| 3775 |
[self.custom_user, document_lowercase], |
| 3776 |
[self.custom_user, document], |
| 3777 |
) |
| 3778 |
self.assertEqual(len(changes), 0) |
| 3779 |
|
| 3780 |
def test_swappable_changed(self): |
| 3781 |
with isolate_lru_cache(apps.get_swappable_settings_name): |
| 3782 |
before = self.make_project_state([self.custom_user, self.author_with_user]) |
| 3783 |
with override_settings(AUTH_USER_MODEL="thirdapp.CustomUser"): |
| 3784 |
after = self.make_project_state( |
| 3785 |
[self.custom_user, self.author_with_custom_user] |
| 3786 |
) |
| 3787 |
autodetector = MigrationAutodetector(before, after) |
| 3788 |
changes = autodetector._detect_changes() |
| 3789 |
# Right number/type of migrations? |
| 3790 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3791 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterField"]) |
| 3792 |
self.assertOperationAttributes( |
| 3793 |
changes, "testapp", 0, 0, model_name="author", name="user" |
| 3794 |
) |
| 3795 |
fk_field = changes["testapp"][0].operations[0].field |
| 3796 |
self.assertEqual(fk_field.remote_field.model, "thirdapp.CustomUser") |
| 3797 |
|
| 3798 |
def test_add_field_with_default(self): |
| 3799 |
"""#22030 - Adding a field with a default should work.""" |
| 3800 |
changes = self.get_changes([self.author_empty], [self.author_name_default]) |
| 3801 |
# Right number/type of migrations? |
| 3802 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3803 |
self.assertOperationTypes(changes, "testapp", 0, ["AddField"]) |
| 3804 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="name") |
| 3805 |
|
| 3806 |
def test_custom_deconstructible(self): |
| 3807 |
""" |
| 3808 |
Two instances which deconstruct to the same value aren't considered a |
| 3809 |
change. |
| 3810 |
""" |
| 3811 |
changes = self.get_changes( |
| 3812 |
[self.author_name_deconstructible_1], [self.author_name_deconstructible_2] |
| 3813 |
) |
| 3814 |
# Right number of migrations? |
| 3815 |
self.assertEqual(len(changes), 0) |
| 3816 |
|
| 3817 |
def test_deconstruct_field_kwarg(self): |
| 3818 |
"""Field instances are handled correctly by nested deconstruction.""" |
| 3819 |
changes = self.get_changes( |
| 3820 |
[self.author_name_deconstructible_3], [self.author_name_deconstructible_4] |
| 3821 |
) |
| 3822 |
self.assertEqual(changes, {}) |
| 3823 |
|
| 3824 |
def test_deconstructible_list(self): |
| 3825 |
"""Nested deconstruction descends into lists.""" |
| 3826 |
# When lists contain items that deconstruct to identical values, those lists |
| 3827 |
# should be considered equal for the purpose of detecting state changes |
| 3828 |
# (even if the original items are unequal). |
| 3829 |
changes = self.get_changes( |
| 3830 |
[self.author_name_deconstructible_list_1], |
| 3831 |
[self.author_name_deconstructible_list_2], |
| 3832 |
) |
| 3833 |
self.assertEqual(changes, {}) |
| 3834 |
# Legitimate differences within the deconstructed lists should be reported |
| 3835 |
# as a change |
| 3836 |
changes = self.get_changes( |
| 3837 |
[self.author_name_deconstructible_list_1], |
| 3838 |
[self.author_name_deconstructible_list_3], |
| 3839 |
) |
| 3840 |
self.assertEqual(len(changes), 1) |
| 3841 |
|
| 3842 |
def test_deconstructible_tuple(self): |
| 3843 |
"""Nested deconstruction descends into tuples.""" |
| 3844 |
# When tuples contain items that deconstruct to identical values, those tuples |
| 3845 |
# should be considered equal for the purpose of detecting state changes |
| 3846 |
# (even if the original items are unequal). |
| 3847 |
changes = self.get_changes( |
| 3848 |
[self.author_name_deconstructible_tuple_1], |
| 3849 |
[self.author_name_deconstructible_tuple_2], |
| 3850 |
) |
| 3851 |
self.assertEqual(changes, {}) |
| 3852 |
# Legitimate differences within the deconstructed tuples should be reported |
| 3853 |
# as a change |
| 3854 |
changes = self.get_changes( |
| 3855 |
[self.author_name_deconstructible_tuple_1], |
| 3856 |
[self.author_name_deconstructible_tuple_3], |
| 3857 |
) |
| 3858 |
self.assertEqual(len(changes), 1) |
| 3859 |
|
| 3860 |
def test_deconstructible_dict(self): |
| 3861 |
"""Nested deconstruction descends into dict values.""" |
| 3862 |
# When dicts contain items whose values deconstruct to identical values, |
| 3863 |
# those dicts should be considered equal for the purpose of detecting |
| 3864 |
# state changes (even if the original values are unequal). |
| 3865 |
changes = self.get_changes( |
| 3866 |
[self.author_name_deconstructible_dict_1], |
| 3867 |
[self.author_name_deconstructible_dict_2], |
| 3868 |
) |
| 3869 |
self.assertEqual(changes, {}) |
| 3870 |
# Legitimate differences within the deconstructed dicts should be reported |
| 3871 |
# as a change |
| 3872 |
changes = self.get_changes( |
| 3873 |
[self.author_name_deconstructible_dict_1], |
| 3874 |
[self.author_name_deconstructible_dict_3], |
| 3875 |
) |
| 3876 |
self.assertEqual(len(changes), 1) |
| 3877 |
|
| 3878 |
def test_nested_deconstructible_objects(self): |
| 3879 |
""" |
| 3880 |
Nested deconstruction is applied recursively to the args/kwargs of |
| 3881 |
deconstructed objects. |
| 3882 |
""" |
| 3883 |
# If the items within a deconstructed object's args/kwargs have the same |
| 3884 |
# deconstructed values - whether or not the items themselves are different |
| 3885 |
# instances - then the object as a whole is regarded as unchanged. |
| 3886 |
changes = self.get_changes( |
| 3887 |
[self.author_name_nested_deconstructible_1], |
| 3888 |
[self.author_name_nested_deconstructible_2], |
| 3889 |
) |
| 3890 |
self.assertEqual(changes, {}) |
| 3891 |
# Differences that exist solely within the args list of a deconstructed object |
| 3892 |
# should be reported as changes |
| 3893 |
changes = self.get_changes( |
| 3894 |
[self.author_name_nested_deconstructible_1], |
| 3895 |
[self.author_name_nested_deconstructible_changed_arg], |
| 3896 |
) |
| 3897 |
self.assertEqual(len(changes), 1) |
| 3898 |
# Additional args should also be reported as a change |
| 3899 |
changes = self.get_changes( |
| 3900 |
[self.author_name_nested_deconstructible_1], |
| 3901 |
[self.author_name_nested_deconstructible_extra_arg], |
| 3902 |
) |
| 3903 |
self.assertEqual(len(changes), 1) |
| 3904 |
# Differences that exist solely within the kwargs dict of a deconstructed object |
| 3905 |
# should be reported as changes |
| 3906 |
changes = self.get_changes( |
| 3907 |
[self.author_name_nested_deconstructible_1], |
| 3908 |
[self.author_name_nested_deconstructible_changed_kwarg], |
| 3909 |
) |
| 3910 |
self.assertEqual(len(changes), 1) |
| 3911 |
# Additional kwargs should also be reported as a change |
| 3912 |
changes = self.get_changes( |
| 3913 |
[self.author_name_nested_deconstructible_1], |
| 3914 |
[self.author_name_nested_deconstructible_extra_kwarg], |
| 3915 |
) |
| 3916 |
self.assertEqual(len(changes), 1) |
| 3917 |
|
| 3918 |
def test_deconstruct_type(self): |
| 3919 |
""" |
| 3920 |
#22951 -- Uninstantiated classes with deconstruct are correctly returned |
| 3921 |
by deep_deconstruct during serialization. |
| 3922 |
""" |
| 3923 |
author = ModelState( |
| 3924 |
"testapp", |
| 3925 |
"Author", |
| 3926 |
[ |
| 3927 |
("id", models.AutoField(primary_key=True)), |
| 3928 |
( |
| 3929 |
"name", |
| 3930 |
models.CharField( |
| 3931 |
max_length=200, |
| 3932 |
# IntegerField intentionally not instantiated. |
| 3933 |
default=models.IntegerField, |
| 3934 |
), |
| 3935 |
), |
| 3936 |
], |
| 3937 |
) |
| 3938 |
changes = self.get_changes([], [author]) |
| 3939 |
# Right number/type of migrations? |
| 3940 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3941 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"]) |
| 3942 |
|
| 3943 |
def test_replace_string_with_foreignkey(self): |
| 3944 |
""" |
| 3945 |
#22300 - Adding an FK in the same "spot" as a deleted CharField should |
| 3946 |
work. |
| 3947 |
""" |
| 3948 |
changes = self.get_changes( |
| 3949 |
[self.author_with_publisher_string], |
| 3950 |
[self.author_with_publisher, self.publisher], |
| 3951 |
) |
| 3952 |
# Right number/type of migrations? |
| 3953 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3954 |
self.assertOperationTypes( |
| 3955 |
changes, "testapp", 0, ["CreateModel", "RemoveField", "AddField"] |
| 3956 |
) |
| 3957 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Publisher") |
| 3958 |
self.assertOperationAttributes(changes, "testapp", 0, 1, name="publisher_name") |
| 3959 |
self.assertOperationAttributes(changes, "testapp", 0, 2, name="publisher") |
| 3960 |
|
| 3961 |
def test_foreign_key_removed_before_target_model(self): |
| 3962 |
""" |
| 3963 |
Removing an FK and the model it targets in the same change must remove |
| 3964 |
the FK field before the model to maintain consistency. |
| 3965 |
""" |
| 3966 |
changes = self.get_changes( |
| 3967 |
[self.author_with_publisher, self.publisher], [self.author_name] |
| 3968 |
) # removes both the model and FK |
| 3969 |
# Right number/type of migrations? |
| 3970 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3971 |
self.assertOperationTypes(changes, "testapp", 0, ["RemoveField", "DeleteModel"]) |
| 3972 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="publisher") |
| 3973 |
self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher") |
| 3974 |
|
| 3975 |
@mock.patch( |
| 3976 |
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition", |
| 3977 |
side_effect=AssertionError("Should not have prompted for not null addition"), |
| 3978 |
) |
| 3979 |
def test_add_many_to_many(self, mocked_ask_method): |
| 3980 |
"""#22435 - Adding a ManyToManyField should not prompt for a default.""" |
| 3981 |
changes = self.get_changes( |
| 3982 |
[self.author_empty, self.publisher], [self.author_with_m2m, self.publisher] |
| 3983 |
) |
| 3984 |
# Right number/type of migrations? |
| 3985 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3986 |
self.assertOperationTypes(changes, "testapp", 0, ["AddField"]) |
| 3987 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="publishers") |
| 3988 |
|
| 3989 |
def test_alter_many_to_many(self): |
| 3990 |
changes = self.get_changes( |
| 3991 |
[self.author_with_m2m, self.publisher], |
| 3992 |
[self.author_with_m2m_blank, self.publisher], |
| 3993 |
) |
| 3994 |
# Right number/type of migrations? |
| 3995 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 3996 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterField"]) |
| 3997 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="publishers") |
| 3998 |
|
| 3999 |
def test_create_with_through_model(self): |
| 4000 |
""" |
| 4001 |
Adding a m2m with a through model and the models that use it should be |
| 4002 |
ordered correctly. |
| 4003 |
""" |
| 4004 |
changes = self.get_changes( |
| 4005 |
[], [self.author_with_m2m_through, self.publisher, self.contract] |
| 4006 |
) |
| 4007 |
# Right number/type of migrations? |
| 4008 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4009 |
self.assertOperationTypes( |
| 4010 |
changes, |
| 4011 |
"testapp", |
| 4012 |
0, |
| 4013 |
[ |
| 4014 |
"CreateModel", |
| 4015 |
"CreateModel", |
| 4016 |
"CreateModel", |
| 4017 |
"AddField", |
| 4018 |
], |
| 4019 |
) |
| 4020 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author") |
| 4021 |
self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher") |
| 4022 |
self.assertOperationAttributes(changes, "testapp", 0, 2, name="Contract") |
| 4023 |
self.assertOperationAttributes( |
| 4024 |
changes, "testapp", 0, 3, model_name="author", name="publishers" |
| 4025 |
) |
| 4026 |
|
| 4027 |
def test_create_with_through_model_separate_apps(self): |
| 4028 |
author_with_m2m_through = ModelState( |
| 4029 |
"authors", |
| 4030 |
"Author", |
| 4031 |
[ |
| 4032 |
("id", models.AutoField(primary_key=True)), |
| 4033 |
( |
| 4034 |
"publishers", |
| 4035 |
models.ManyToManyField( |
| 4036 |
"testapp.Publisher", through="contract.Contract" |
| 4037 |
), |
| 4038 |
), |
| 4039 |
], |
| 4040 |
) |
| 4041 |
contract = ModelState( |
| 4042 |
"contract", |
| 4043 |
"Contract", |
| 4044 |
[ |
| 4045 |
("id", models.AutoField(primary_key=True)), |
| 4046 |
("author", models.ForeignKey("authors.Author", models.CASCADE)), |
| 4047 |
("publisher", models.ForeignKey("testapp.Publisher", models.CASCADE)), |
| 4048 |
], |
| 4049 |
) |
| 4050 |
changes = self.get_changes( |
| 4051 |
[], [author_with_m2m_through, self.publisher, contract] |
| 4052 |
) |
| 4053 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4054 |
self.assertNumberMigrations(changes, "contract", 1) |
| 4055 |
self.assertNumberMigrations(changes, "authors", 2) |
| 4056 |
self.assertMigrationDependencies( |
| 4057 |
changes, |
| 4058 |
"authors", |
| 4059 |
1, |
| 4060 |
{("authors", "auto_1"), ("contract", "auto_1"), ("testapp", "auto_1")}, |
| 4061 |
) |
| 4062 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"]) |
| 4063 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Publisher") |
| 4064 |
self.assertOperationTypes(changes, "contract", 0, ["CreateModel"]) |
| 4065 |
self.assertOperationAttributes(changes, "contract", 0, 0, name="Contract") |
| 4066 |
self.assertOperationTypes(changes, "authors", 0, ["CreateModel"]) |
| 4067 |
self.assertOperationTypes(changes, "authors", 1, ["AddField"]) |
| 4068 |
self.assertOperationAttributes(changes, "authors", 0, 0, name="Author") |
| 4069 |
self.assertOperationAttributes( |
| 4070 |
changes, "authors", 1, 0, model_name="author", name="publishers" |
| 4071 |
) |
| 4072 |
|
| 4073 |
def test_many_to_many_removed_before_through_model(self): |
| 4074 |
""" |
| 4075 |
Removing a ManyToManyField and the "through" model in the same change |
| 4076 |
must remove the field before the model to maintain consistency. |
| 4077 |
""" |
| 4078 |
changes = self.get_changes( |
| 4079 |
[ |
| 4080 |
self.book_with_multiple_authors_through_attribution, |
| 4081 |
self.author_name, |
| 4082 |
self.attribution, |
| 4083 |
], |
| 4084 |
[self.book_with_no_author, self.author_name], |
| 4085 |
) |
| 4086 |
# Remove both the through model and ManyToMany |
| 4087 |
# Right number/type of migrations? |
| 4088 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 4089 |
self.assertOperationTypes( |
| 4090 |
changes, "otherapp", 0, ["RemoveField", "DeleteModel"] |
| 4091 |
) |
| 4092 |
self.assertOperationAttributes( |
| 4093 |
changes, "otherapp", 0, 0, name="authors", model_name="book" |
| 4094 |
) |
| 4095 |
self.assertOperationAttributes(changes, "otherapp", 0, 1, name="Attribution") |
| 4096 |
|
| 4097 |
def test_many_to_many_removed_before_through_model_2(self): |
| 4098 |
""" |
| 4099 |
Removing a model that contains a ManyToManyField and the "through" model |
| 4100 |
in the same change must remove the field before the model to maintain |
| 4101 |
consistency. |
| 4102 |
""" |
| 4103 |
changes = self.get_changes( |
| 4104 |
[ |
| 4105 |
self.book_with_multiple_authors_through_attribution, |
| 4106 |
self.author_name, |
| 4107 |
self.attribution, |
| 4108 |
], |
| 4109 |
[self.author_name], |
| 4110 |
) |
| 4111 |
# Remove both the through model and ManyToMany |
| 4112 |
# Right number/type of migrations? |
| 4113 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 4114 |
self.assertOperationTypes( |
| 4115 |
changes, "otherapp", 0, ["RemoveField", "DeleteModel", "DeleteModel"] |
| 4116 |
) |
| 4117 |
self.assertOperationAttributes( |
| 4118 |
changes, "otherapp", 0, 0, name="authors", model_name="book" |
| 4119 |
) |
| 4120 |
self.assertOperationAttributes(changes, "otherapp", 0, 1, name="Attribution") |
| 4121 |
self.assertOperationAttributes(changes, "otherapp", 0, 2, name="Book") |
| 4122 |
|
| 4123 |
def test_m2m_w_through_multistep_remove(self): |
| 4124 |
""" |
| 4125 |
A model with a m2m field that specifies a "through" model cannot be |
| 4126 |
removed in the same migration as that through model as the schema will |
| 4127 |
pass through an inconsistent state. The autodetector should produce two |
| 4128 |
migrations to avoid this issue. |
| 4129 |
""" |
| 4130 |
changes = self.get_changes( |
| 4131 |
[self.author_with_m2m_through, self.publisher, self.contract], |
| 4132 |
[self.publisher], |
| 4133 |
) |
| 4134 |
# Right number/type of migrations? |
| 4135 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4136 |
self.assertOperationTypes( |
| 4137 |
changes, |
| 4138 |
"testapp", |
| 4139 |
0, |
| 4140 |
["RemoveField", "RemoveField", "DeleteModel", "DeleteModel"], |
| 4141 |
) |
| 4142 |
self.assertOperationAttributes( |
| 4143 |
changes, "testapp", 0, 0, name="author", model_name="contract" |
| 4144 |
) |
| 4145 |
self.assertOperationAttributes( |
| 4146 |
changes, "testapp", 0, 1, name="publisher", model_name="contract" |
| 4147 |
) |
| 4148 |
self.assertOperationAttributes(changes, "testapp", 0, 2, name="Author") |
| 4149 |
self.assertOperationAttributes(changes, "testapp", 0, 3, name="Contract") |
| 4150 |
|
| 4151 |
def test_concrete_field_changed_to_many_to_many(self): |
| 4152 |
""" |
| 4153 |
#23938 - Changing a concrete field into a ManyToManyField |
| 4154 |
first removes the concrete field and then adds the m2m field. |
| 4155 |
""" |
| 4156 |
changes = self.get_changes( |
| 4157 |
[self.author_with_former_m2m], [self.author_with_m2m, self.publisher] |
| 4158 |
) |
| 4159 |
# Right number/type of migrations? |
| 4160 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4161 |
self.assertOperationTypes( |
| 4162 |
changes, "testapp", 0, ["CreateModel", "RemoveField", "AddField"] |
| 4163 |
) |
| 4164 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Publisher") |
| 4165 |
self.assertOperationAttributes( |
| 4166 |
changes, "testapp", 0, 1, name="publishers", model_name="author" |
| 4167 |
) |
| 4168 |
self.assertOperationAttributes( |
| 4169 |
changes, "testapp", 0, 2, name="publishers", model_name="author" |
| 4170 |
) |
| 4171 |
|
| 4172 |
def test_many_to_many_changed_to_concrete_field(self): |
| 4173 |
""" |
| 4174 |
#23938 - Changing a ManyToManyField into a concrete field |
| 4175 |
first removes the m2m field and then adds the concrete field. |
| 4176 |
""" |
| 4177 |
changes = self.get_changes( |
| 4178 |
[self.author_with_m2m, self.publisher], [self.author_with_former_m2m] |
| 4179 |
) |
| 4180 |
# Right number/type of migrations? |
| 4181 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4182 |
self.assertOperationTypes( |
| 4183 |
changes, "testapp", 0, ["RemoveField", "DeleteModel", "AddField"] |
| 4184 |
) |
| 4185 |
self.assertOperationAttributes( |
| 4186 |
changes, "testapp", 0, 0, name="publishers", model_name="author" |
| 4187 |
) |
| 4188 |
self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher") |
| 4189 |
self.assertOperationAttributes( |
| 4190 |
changes, "testapp", 0, 2, name="publishers", model_name="author" |
| 4191 |
) |
| 4192 |
self.assertOperationFieldAttributes(changes, "testapp", 0, 2, max_length=100) |
| 4193 |
|
| 4194 |
def test_non_circular_foreignkey_dependency_removal(self): |
| 4195 |
""" |
| 4196 |
If two models with a ForeignKey from one to the other are removed at the |
| 4197 |
same time, the autodetector should remove them in the correct order. |
| 4198 |
""" |
| 4199 |
changes = self.get_changes( |
| 4200 |
[self.author_with_publisher, self.publisher_with_author], [] |
| 4201 |
) |
| 4202 |
# Right number/type of migrations? |
| 4203 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4204 |
self.assertOperationTypes( |
| 4205 |
changes, "testapp", 0, ["RemoveField", "DeleteModel", "DeleteModel"] |
| 4206 |
) |
| 4207 |
self.assertOperationAttributes( |
| 4208 |
changes, "testapp", 0, 0, name="author", model_name="publisher" |
| 4209 |
) |
| 4210 |
self.assertOperationAttributes(changes, "testapp", 0, 1, name="Author") |
| 4211 |
self.assertOperationAttributes(changes, "testapp", 0, 2, name="Publisher") |
| 4212 |
|
| 4213 |
def test_alter_model_options(self): |
| 4214 |
"""Changing a model's options should make a change.""" |
| 4215 |
changes = self.get_changes([self.author_empty], [self.author_with_options]) |
| 4216 |
# Right number/type of migrations? |
| 4217 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4218 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"]) |
| 4219 |
self.assertOperationAttributes( |
| 4220 |
changes, |
| 4221 |
"testapp", |
| 4222 |
0, |
| 4223 |
0, |
| 4224 |
options={ |
| 4225 |
"permissions": [("can_hire", "Can hire")], |
| 4226 |
"verbose_name": "Authi", |
| 4227 |
}, |
| 4228 |
) |
| 4229 |
|
| 4230 |
# Changing them back to empty should also make a change |
| 4231 |
changes = self.get_changes([self.author_with_options], [self.author_empty]) |
| 4232 |
# Right number/type of migrations? |
| 4233 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4234 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"]) |
| 4235 |
self.assertOperationAttributes( |
| 4236 |
changes, "testapp", 0, 0, name="author", options={} |
| 4237 |
) |
| 4238 |
|
| 4239 |
def test_alter_model_options_proxy(self): |
| 4240 |
"""Changing a proxy model's options should also make a change.""" |
| 4241 |
changes = self.get_changes( |
| 4242 |
[self.author_proxy, self.author_empty], |
| 4243 |
[self.author_proxy_options, self.author_empty], |
| 4244 |
) |
| 4245 |
# Right number/type of migrations? |
| 4246 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4247 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"]) |
| 4248 |
self.assertOperationAttributes( |
| 4249 |
changes, |
| 4250 |
"testapp", |
| 4251 |
0, |
| 4252 |
0, |
| 4253 |
name="authorproxy", |
| 4254 |
options={"verbose_name": "Super Author"}, |
| 4255 |
) |
| 4256 |
|
| 4257 |
def test_set_alter_order_with_respect_to(self): |
| 4258 |
"""Setting order_with_respect_to adds a field.""" |
| 4259 |
changes = self.get_changes( |
| 4260 |
[self.book, self.author_with_book], |
| 4261 |
[self.book, self.author_with_book_order_wrt], |
| 4262 |
) |
| 4263 |
# Right number/type of migrations? |
| 4264 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4265 |
self.assertOperationTypes(changes, "testapp", 0, ["AlterOrderWithRespectTo"]) |
| 4266 |
self.assertOperationAttributes( |
| 4267 |
changes, "testapp", 0, 0, name="author", order_with_respect_to="book" |
| 4268 |
) |
| 4269 |
|
| 4270 |
def test_add_alter_order_with_respect_to(self): |
| 4271 |
""" |
| 4272 |
Setting order_with_respect_to when adding the FK too does |
| 4273 |
things in the right order. |
| 4274 |
""" |
| 4275 |
changes = self.get_changes( |
| 4276 |
[self.author_name], [self.book, self.author_with_book_order_wrt] |
| 4277 |
) |
| 4278 |
# Right number/type of migrations? |
| 4279 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4280 |
self.assertOperationTypes( |
| 4281 |
changes, "testapp", 0, ["AddField", "AlterOrderWithRespectTo"] |
| 4282 |
) |
| 4283 |
self.assertOperationAttributes( |
| 4284 |
changes, "testapp", 0, 0, model_name="author", name="book" |
| 4285 |
) |
| 4286 |
self.assertOperationAttributes( |
| 4287 |
changes, "testapp", 0, 1, name="author", order_with_respect_to="book" |
| 4288 |
) |
| 4289 |
|
| 4290 |
def test_remove_alter_order_with_respect_to(self): |
| 4291 |
""" |
| 4292 |
Removing order_with_respect_to when removing the FK too does |
| 4293 |
things in the right order. |
| 4294 |
""" |
| 4295 |
changes = self.get_changes( |
| 4296 |
[self.book, self.author_with_book_order_wrt], [self.author_name] |
| 4297 |
) |
| 4298 |
# Right number/type of migrations? |
| 4299 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4300 |
self.assertOperationTypes( |
| 4301 |
changes, "testapp", 0, ["AlterOrderWithRespectTo", "RemoveField"] |
| 4302 |
) |
| 4303 |
self.assertOperationAttributes( |
| 4304 |
changes, "testapp", 0, 0, name="author", order_with_respect_to=None |
| 4305 |
) |
| 4306 |
self.assertOperationAttributes( |
| 4307 |
changes, "testapp", 0, 1, model_name="author", name="book" |
| 4308 |
) |
| 4309 |
|
| 4310 |
def test_add_model_order_with_respect_to(self): |
| 4311 |
""" |
| 4312 |
Setting order_with_respect_to when adding the whole model |
| 4313 |
does things in the right order. |
| 4314 |
""" |
| 4315 |
changes = self.get_changes([], [self.book, self.author_with_book_order_wrt]) |
| 4316 |
# Right number/type of migrations? |
| 4317 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4318 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"]) |
| 4319 |
self.assertOperationAttributes( |
| 4320 |
changes, |
| 4321 |
"testapp", |
| 4322 |
0, |
| 4323 |
0, |
| 4324 |
name="Author", |
| 4325 |
options={"order_with_respect_to": "book"}, |
| 4326 |
) |
| 4327 |
self.assertNotIn( |
| 4328 |
"_order", |
| 4329 |
[name for name, field in changes["testapp"][0].operations[0].fields], |
| 4330 |
) |
| 4331 |
|
| 4332 |
def test_add_model_order_with_respect_to_unique_together(self): |
| 4333 |
changes = self.get_changes( |
| 4334 |
[], |
| 4335 |
[ |
| 4336 |
self.book, |
| 4337 |
ModelState( |
| 4338 |
"testapp", |
| 4339 |
"Author", |
| 4340 |
[ |
| 4341 |
("id", models.AutoField(primary_key=True)), |
| 4342 |
("name", models.CharField(max_length=200)), |
| 4343 |
("book", models.ForeignKey("otherapp.Book", models.CASCADE)), |
| 4344 |
], |
| 4345 |
options={ |
| 4346 |
"order_with_respect_to": "book", |
| 4347 |
"unique_together": {("id", "_order")}, |
| 4348 |
}, |
| 4349 |
), |
| 4350 |
], |
| 4351 |
) |
| 4352 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4353 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"]) |
| 4354 |
self.assertOperationAttributes( |
| 4355 |
changes, |
| 4356 |
"testapp", |
| 4357 |
0, |
| 4358 |
0, |
| 4359 |
name="Author", |
| 4360 |
options={ |
| 4361 |
"order_with_respect_to": "book", |
| 4362 |
"unique_together": {("id", "_order")}, |
| 4363 |
}, |
| 4364 |
) |
| 4365 |
|
| 4366 |
def test_add_model_order_with_respect_to_constraint(self): |
| 4367 |
after = ModelState( |
| 4368 |
"testapp", |
| 4369 |
"Author", |
| 4370 |
[ |
| 4371 |
("id", models.AutoField(primary_key=True)), |
| 4372 |
("name", models.CharField(max_length=200)), |
| 4373 |
("book", models.ForeignKey("otherapp.Book", models.CASCADE)), |
| 4374 |
], |
| 4375 |
options={ |
| 4376 |
"order_with_respect_to": "book", |
| 4377 |
"constraints": [ |
| 4378 |
models.CheckConstraint( |
| 4379 |
condition=models.Q(_order__gt=1), name="book_order_gt_1" |
| 4380 |
), |
| 4381 |
], |
| 4382 |
}, |
| 4383 |
) |
| 4384 |
changes = self.get_changes([], [self.book, after]) |
| 4385 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4386 |
self.assertOperationTypes( |
| 4387 |
changes, |
| 4388 |
"testapp", |
| 4389 |
0, |
| 4390 |
["CreateModel"], |
| 4391 |
) |
| 4392 |
self.assertOperationAttributes( |
| 4393 |
changes, |
| 4394 |
"testapp", |
| 4395 |
0, |
| 4396 |
0, |
| 4397 |
name="Author", |
| 4398 |
options={ |
| 4399 |
"order_with_respect_to": "book", |
| 4400 |
"constraints": [ |
| 4401 |
models.CheckConstraint( |
| 4402 |
condition=models.Q(_order__gt=1), name="book_order_gt_1" |
| 4403 |
) |
| 4404 |
], |
| 4405 |
}, |
| 4406 |
) |
| 4407 |
|
| 4408 |
def test_add_model_order_with_respect_to_index(self): |
| 4409 |
after = ModelState( |
| 4410 |
"testapp", |
| 4411 |
"Author", |
| 4412 |
[ |
| 4413 |
("id", models.AutoField(primary_key=True)), |
| 4414 |
("name", models.CharField(max_length=200)), |
| 4415 |
("book", models.ForeignKey("otherapp.Book", models.CASCADE)), |
| 4416 |
], |
| 4417 |
options={ |
| 4418 |
"order_with_respect_to": "book", |
| 4419 |
"indexes": [models.Index(fields=["_order"], name="book_order_idx")], |
| 4420 |
}, |
| 4421 |
) |
| 4422 |
changes = self.get_changes([], [self.book, after]) |
| 4423 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4424 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"]) |
| 4425 |
self.assertOperationAttributes( |
| 4426 |
changes, |
| 4427 |
"testapp", |
| 4428 |
0, |
| 4429 |
0, |
| 4430 |
name="Author", |
| 4431 |
options={ |
| 4432 |
"order_with_respect_to": "book", |
| 4433 |
"indexes": [models.Index(fields=["_order"], name="book_order_idx")], |
| 4434 |
}, |
| 4435 |
) |
| 4436 |
|
| 4437 |
def test_set_alter_order_with_respect_to_index_constraint_unique_together(self): |
| 4438 |
tests = [ |
| 4439 |
( |
| 4440 |
"AddIndex", |
| 4441 |
{ |
| 4442 |
"indexes": [ |
| 4443 |
models.Index(fields=["_order"], name="book_order_idx"), |
| 4444 |
] |
| 4445 |
}, |
| 4446 |
), |
| 4447 |
( |
| 4448 |
"AddConstraint", |
| 4449 |
{ |
| 4450 |
"constraints": [ |
| 4451 |
models.CheckConstraint( |
| 4452 |
condition=models.Q(_order__gt=1), |
| 4453 |
name="book_order_gt_1", |
| 4454 |
), |
| 4455 |
] |
| 4456 |
}, |
| 4457 |
), |
| 4458 |
("AlterUniqueTogether", {"unique_together": {("id", "_order")}}), |
| 4459 |
] |
| 4460 |
for operation, extra_option in tests: |
| 4461 |
with self.subTest(operation=operation): |
| 4462 |
after = ModelState( |
| 4463 |
"testapp", |
| 4464 |
"Author", |
| 4465 |
[ |
| 4466 |
("id", models.AutoField(primary_key=True)), |
| 4467 |
("name", models.CharField(max_length=200)), |
| 4468 |
("book", models.ForeignKey("otherapp.Book", models.CASCADE)), |
| 4469 |
], |
| 4470 |
options={ |
| 4471 |
"order_with_respect_to": "book", |
| 4472 |
**extra_option, |
| 4473 |
}, |
| 4474 |
) |
| 4475 |
changes = self.get_changes( |
| 4476 |
[self.book, self.author_with_book], |
| 4477 |
[self.book, after], |
| 4478 |
) |
| 4479 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4480 |
self.assertOperationTypes( |
| 4481 |
changes, |
| 4482 |
"testapp", |
| 4483 |
0, |
| 4484 |
[ |
| 4485 |
"AlterOrderWithRespectTo", |
| 4486 |
operation, |
| 4487 |
], |
| 4488 |
) |
| 4489 |
|
| 4490 |
def test_alter_model_managers(self): |
| 4491 |
""" |
| 4492 |
Changing the model managers adds a new operation. |
| 4493 |
""" |
| 4494 |
changes = self.get_changes([self.other_pony], [self.other_pony_food]) |
| 4495 |
# Right number/type of migrations? |
| 4496 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 4497 |
self.assertOperationTypes(changes, "otherapp", 0, ["AlterModelManagers"]) |
| 4498 |
self.assertOperationAttributes(changes, "otherapp", 0, 0, name="pony") |
| 4499 |
self.assertEqual( |
| 4500 |
[name for name, mgr in changes["otherapp"][0].operations[0].managers], |
| 4501 |
["food_qs", "food_mgr", "food_mgr_kwargs"], |
| 4502 |
) |
| 4503 |
self.assertEqual( |
| 4504 |
changes["otherapp"][0].operations[0].managers[1][1].args, ("a", "b", 1, 2) |
| 4505 |
) |
| 4506 |
self.assertEqual( |
| 4507 |
changes["otherapp"][0].operations[0].managers[2][1].args, ("x", "y", 3, 4) |
| 4508 |
) |
| 4509 |
|
| 4510 |
def test_swappable_first_inheritance(self): |
| 4511 |
"""Swappable models get their CreateModel first.""" |
| 4512 |
changes = self.get_changes([], [self.custom_user, self.aardvark]) |
| 4513 |
# Right number/type of migrations? |
| 4514 |
self.assertNumberMigrations(changes, "thirdapp", 1) |
| 4515 |
self.assertOperationTypes( |
| 4516 |
changes, "thirdapp", 0, ["CreateModel", "CreateModel"] |
| 4517 |
) |
| 4518 |
self.assertOperationAttributes(changes, "thirdapp", 0, 0, name="CustomUser") |
| 4519 |
self.assertOperationAttributes(changes, "thirdapp", 0, 1, name="Aardvark") |
| 4520 |
|
| 4521 |
def test_default_related_name_option(self): |
| 4522 |
model_state = ModelState( |
| 4523 |
"app", |
| 4524 |
"model", |
| 4525 |
[ |
| 4526 |
("id", models.AutoField(primary_key=True)), |
| 4527 |
], |
| 4528 |
options={"default_related_name": "related_name"}, |
| 4529 |
) |
| 4530 |
changes = self.get_changes([], [model_state]) |
| 4531 |
self.assertNumberMigrations(changes, "app", 1) |
| 4532 |
self.assertOperationTypes(changes, "app", 0, ["CreateModel"]) |
| 4533 |
self.assertOperationAttributes( |
| 4534 |
changes, |
| 4535 |
"app", |
| 4536 |
0, |
| 4537 |
0, |
| 4538 |
name="model", |
| 4539 |
options={"default_related_name": "related_name"}, |
| 4540 |
) |
| 4541 |
altered_model_state = ModelState( |
| 4542 |
"app", |
| 4543 |
"Model", |
| 4544 |
[ |
| 4545 |
("id", models.AutoField(primary_key=True)), |
| 4546 |
], |
| 4547 |
) |
| 4548 |
changes = self.get_changes([model_state], [altered_model_state]) |
| 4549 |
self.assertNumberMigrations(changes, "app", 1) |
| 4550 |
self.assertOperationTypes(changes, "app", 0, ["AlterModelOptions"]) |
| 4551 |
self.assertOperationAttributes(changes, "app", 0, 0, name="model", options={}) |
| 4552 |
|
| 4553 |
@override_settings(AUTH_USER_MODEL="thirdapp.CustomUser") |
| 4554 |
def test_swappable_first_setting(self): |
| 4555 |
"""Swappable models get their CreateModel first.""" |
| 4556 |
with isolate_lru_cache(apps.get_swappable_settings_name): |
| 4557 |
changes = self.get_changes([], [self.custom_user_no_inherit, self.aardvark]) |
| 4558 |
# Right number/type of migrations? |
| 4559 |
self.assertNumberMigrations(changes, "thirdapp", 1) |
| 4560 |
self.assertOperationTypes( |
| 4561 |
changes, "thirdapp", 0, ["CreateModel", "CreateModel"] |
| 4562 |
) |
| 4563 |
self.assertOperationAttributes(changes, "thirdapp", 0, 0, name="CustomUser") |
| 4564 |
self.assertOperationAttributes(changes, "thirdapp", 0, 1, name="Aardvark") |
| 4565 |
|
| 4566 |
def test_bases_first(self): |
| 4567 |
"""Bases of other models come first.""" |
| 4568 |
changes = self.get_changes( |
| 4569 |
[], [self.aardvark_based_on_author, self.author_name] |
| 4570 |
) |
| 4571 |
# Right number/type of migrations? |
| 4572 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4573 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel", "CreateModel"]) |
| 4574 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author") |
| 4575 |
self.assertOperationAttributes(changes, "testapp", 0, 1, name="Aardvark") |
| 4576 |
|
| 4577 |
def test_bases_first_mixed_case_app_label(self): |
| 4578 |
app_label = "MiXedCaseApp" |
| 4579 |
changes = self.get_changes( |
| 4580 |
[], |
| 4581 |
[ |
| 4582 |
ModelState( |
| 4583 |
app_label, |
| 4584 |
"owner", |
| 4585 |
[ |
| 4586 |
("id", models.AutoField(primary_key=True)), |
| 4587 |
], |
| 4588 |
), |
| 4589 |
ModelState( |
| 4590 |
app_label, |
| 4591 |
"place", |
| 4592 |
[ |
| 4593 |
("id", models.AutoField(primary_key=True)), |
| 4594 |
( |
| 4595 |
"owner", |
| 4596 |
models.ForeignKey("MiXedCaseApp.owner", models.CASCADE), |
| 4597 |
), |
| 4598 |
], |
| 4599 |
), |
| 4600 |
ModelState(app_label, "restaurant", [], bases=("MiXedCaseApp.place",)), |
| 4601 |
], |
| 4602 |
) |
| 4603 |
self.assertNumberMigrations(changes, app_label, 1) |
| 4604 |
self.assertOperationTypes( |
| 4605 |
changes, |
| 4606 |
app_label, |
| 4607 |
0, |
| 4608 |
[ |
| 4609 |
"CreateModel", |
| 4610 |
"CreateModel", |
| 4611 |
"CreateModel", |
| 4612 |
], |
| 4613 |
) |
| 4614 |
self.assertOperationAttributes(changes, app_label, 0, 0, name="owner") |
| 4615 |
self.assertOperationAttributes(changes, app_label, 0, 1, name="place") |
| 4616 |
self.assertOperationAttributes(changes, app_label, 0, 2, name="restaurant") |
| 4617 |
|
| 4618 |
def test_multiple_bases(self): |
| 4619 |
""" |
| 4620 |
Inheriting models doesn't move *_ptr fields into AddField operations. |
| 4621 |
""" |
| 4622 |
A = ModelState("app", "A", [("a_id", models.AutoField(primary_key=True))]) |
| 4623 |
B = ModelState("app", "B", [("b_id", models.AutoField(primary_key=True))]) |
| 4624 |
C = ModelState("app", "C", [], bases=("app.A", "app.B")) |
| 4625 |
D = ModelState("app", "D", [], bases=("app.A", "app.B")) |
| 4626 |
E = ModelState("app", "E", [], bases=("app.A", "app.B")) |
| 4627 |
changes = self.get_changes([], [A, B, C, D, E]) |
| 4628 |
# Right number/type of migrations? |
| 4629 |
self.assertNumberMigrations(changes, "app", 1) |
| 4630 |
self.assertOperationTypes( |
| 4631 |
changes, |
| 4632 |
"app", |
| 4633 |
0, |
| 4634 |
["CreateModel", "CreateModel", "CreateModel", "CreateModel", "CreateModel"], |
| 4635 |
) |
| 4636 |
self.assertOperationAttributes(changes, "app", 0, 0, name="A") |
| 4637 |
self.assertOperationAttributes(changes, "app", 0, 1, name="B") |
| 4638 |
self.assertOperationAttributes(changes, "app", 0, 2, name="C") |
| 4639 |
self.assertOperationAttributes(changes, "app", 0, 3, name="D") |
| 4640 |
self.assertOperationAttributes(changes, "app", 0, 4, name="E") |
| 4641 |
|
| 4642 |
def test_proxy_bases_first(self): |
| 4643 |
"""Bases of proxies come first.""" |
| 4644 |
changes = self.get_changes( |
| 4645 |
[], [self.author_empty, self.author_proxy, self.author_proxy_proxy] |
| 4646 |
) |
| 4647 |
# Right number/type of migrations? |
| 4648 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4649 |
self.assertOperationTypes( |
| 4650 |
changes, "testapp", 0, ["CreateModel", "CreateModel", "CreateModel"] |
| 4651 |
) |
| 4652 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author") |
| 4653 |
self.assertOperationAttributes(changes, "testapp", 0, 1, name="AuthorProxy") |
| 4654 |
self.assertOperationAttributes( |
| 4655 |
changes, "testapp", 0, 2, name="AAuthorProxyProxy" |
| 4656 |
) |
| 4657 |
|
| 4658 |
def test_pk_fk_included(self): |
| 4659 |
""" |
| 4660 |
A relation used as the primary key is kept as part of CreateModel. |
| 4661 |
""" |
| 4662 |
changes = self.get_changes([], [self.aardvark_pk_fk_author, self.author_name]) |
| 4663 |
# Right number/type of migrations? |
| 4664 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4665 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel", "CreateModel"]) |
| 4666 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author") |
| 4667 |
self.assertOperationAttributes(changes, "testapp", 0, 1, name="Aardvark") |
| 4668 |
|
| 4669 |
def test_first_dependency(self): |
| 4670 |
""" |
| 4671 |
A dependency to an app with no migrations uses __first__. |
| 4672 |
""" |
| 4673 |
# Load graph |
| 4674 |
loader = MigrationLoader(connection) |
| 4675 |
before = self.make_project_state([]) |
| 4676 |
after = self.make_project_state([self.book_migrations_fk]) |
| 4677 |
after.real_apps = {"migrations"} |
| 4678 |
autodetector = MigrationAutodetector(before, after) |
| 4679 |
changes = autodetector._detect_changes(graph=loader.graph) |
| 4680 |
# Right number/type of migrations? |
| 4681 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 4682 |
self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"]) |
| 4683 |
self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Book") |
| 4684 |
self.assertMigrationDependencies( |
| 4685 |
changes, "otherapp", 0, [("migrations", "__first__")] |
| 4686 |
) |
| 4687 |
|
| 4688 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 4689 |
def test_last_dependency(self): |
| 4690 |
""" |
| 4691 |
A dependency to an app with existing migrations uses the |
| 4692 |
last migration of that app. |
| 4693 |
""" |
| 4694 |
# Load graph |
| 4695 |
loader = MigrationLoader(connection) |
| 4696 |
before = self.make_project_state([]) |
| 4697 |
after = self.make_project_state([self.book_migrations_fk]) |
| 4698 |
after.real_apps = {"migrations"} |
| 4699 |
autodetector = MigrationAutodetector(before, after) |
| 4700 |
changes = autodetector._detect_changes(graph=loader.graph) |
| 4701 |
# Right number/type of migrations? |
| 4702 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 4703 |
self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"]) |
| 4704 |
self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Book") |
| 4705 |
self.assertMigrationDependencies( |
| 4706 |
changes, "otherapp", 0, [("migrations", "0002_second")] |
| 4707 |
) |
| 4708 |
|
| 4709 |
def test_alter_fk_before_model_deletion(self): |
| 4710 |
""" |
| 4711 |
ForeignKeys are altered _before_ the model they used to |
| 4712 |
refer to are deleted. |
| 4713 |
""" |
| 4714 |
changes = self.get_changes( |
| 4715 |
[self.author_name, self.publisher_with_author], |
| 4716 |
[self.aardvark_testapp, self.publisher_with_aardvark_author], |
| 4717 |
) |
| 4718 |
# Right number/type of migrations? |
| 4719 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4720 |
self.assertOperationTypes( |
| 4721 |
changes, "testapp", 0, ["CreateModel", "AlterField", "DeleteModel"] |
| 4722 |
) |
| 4723 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Aardvark") |
| 4724 |
self.assertOperationAttributes(changes, "testapp", 0, 1, name="author") |
| 4725 |
self.assertOperationAttributes(changes, "testapp", 0, 2, name="Author") |
| 4726 |
|
| 4727 |
def test_fk_dependency_other_app(self): |
| 4728 |
""" |
| 4729 |
#23100 - ForeignKeys correctly depend on other apps' models. |
| 4730 |
""" |
| 4731 |
changes = self.get_changes( |
| 4732 |
[self.author_name, self.book], [self.author_with_book, self.book] |
| 4733 |
) |
| 4734 |
# Right number/type of migrations? |
| 4735 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4736 |
self.assertOperationTypes(changes, "testapp", 0, ["AddField"]) |
| 4737 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="book") |
| 4738 |
self.assertMigrationDependencies( |
| 4739 |
changes, "testapp", 0, [("otherapp", "__first__")] |
| 4740 |
) |
| 4741 |
|
| 4742 |
def test_alter_unique_together_fk_to_m2m(self): |
| 4743 |
changes = self.get_changes( |
| 4744 |
[self.author_name, self.book_unique_together], |
| 4745 |
[ |
| 4746 |
self.author_name, |
| 4747 |
ModelState( |
| 4748 |
"otherapp", |
| 4749 |
"Book", |
| 4750 |
[ |
| 4751 |
("id", models.AutoField(primary_key=True)), |
| 4752 |
("author", models.ManyToManyField("testapp.Author")), |
| 4753 |
("title", models.CharField(max_length=200)), |
| 4754 |
], |
| 4755 |
), |
| 4756 |
], |
| 4757 |
) |
| 4758 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 4759 |
self.assertOperationTypes( |
| 4760 |
changes, "otherapp", 0, ["AlterUniqueTogether", "RemoveField", "AddField"] |
| 4761 |
) |
| 4762 |
self.assertOperationAttributes( |
| 4763 |
changes, "otherapp", 0, 0, name="book", unique_together=set() |
| 4764 |
) |
| 4765 |
self.assertOperationAttributes( |
| 4766 |
changes, "otherapp", 0, 1, model_name="book", name="author" |
| 4767 |
) |
| 4768 |
self.assertOperationAttributes( |
| 4769 |
changes, "otherapp", 0, 2, model_name="book", name="author" |
| 4770 |
) |
| 4771 |
|
| 4772 |
def test_alter_field_to_fk_dependency_other_app(self): |
| 4773 |
changes = self.get_changes( |
| 4774 |
[self.author_empty, self.book_with_no_author_fk], |
| 4775 |
[self.author_empty, self.book], |
| 4776 |
) |
| 4777 |
self.assertNumberMigrations(changes, "otherapp", 1) |
| 4778 |
self.assertOperationTypes(changes, "otherapp", 0, ["AlterField"]) |
| 4779 |
self.assertMigrationDependencies( |
| 4780 |
changes, "otherapp", 0, [("testapp", "__first__")] |
| 4781 |
) |
| 4782 |
|
| 4783 |
def test_circular_dependency_mixed_addcreate(self): |
| 4784 |
""" |
| 4785 |
#23315 - The dependency resolver knows to put all CreateModel |
| 4786 |
before AddField and not become unsolvable. |
| 4787 |
""" |
| 4788 |
address = ModelState( |
| 4789 |
"a", |
| 4790 |
"Address", |
| 4791 |
[ |
| 4792 |
("id", models.AutoField(primary_key=True)), |
| 4793 |
("country", models.ForeignKey("b.DeliveryCountry", models.CASCADE)), |
| 4794 |
], |
| 4795 |
) |
| 4796 |
person = ModelState( |
| 4797 |
"a", |
| 4798 |
"Person", |
| 4799 |
[ |
| 4800 |
("id", models.AutoField(primary_key=True)), |
| 4801 |
], |
| 4802 |
) |
| 4803 |
apackage = ModelState( |
| 4804 |
"b", |
| 4805 |
"APackage", |
| 4806 |
[ |
| 4807 |
("id", models.AutoField(primary_key=True)), |
| 4808 |
("person", models.ForeignKey("a.Person", models.CASCADE)), |
| 4809 |
], |
| 4810 |
) |
| 4811 |
country = ModelState( |
| 4812 |
"b", |
| 4813 |
"DeliveryCountry", |
| 4814 |
[ |
| 4815 |
("id", models.AutoField(primary_key=True)), |
| 4816 |
], |
| 4817 |
) |
| 4818 |
changes = self.get_changes([], [address, person, apackage, country]) |
| 4819 |
# Right number/type of migrations? |
| 4820 |
self.assertNumberMigrations(changes, "a", 2) |
| 4821 |
self.assertNumberMigrations(changes, "b", 1) |
| 4822 |
self.assertOperationTypes(changes, "a", 0, ["CreateModel", "CreateModel"]) |
| 4823 |
self.assertOperationTypes(changes, "a", 1, ["AddField"]) |
| 4824 |
self.assertOperationTypes(changes, "b", 0, ["CreateModel", "CreateModel"]) |
| 4825 |
|
| 4826 |
@override_settings(AUTH_USER_MODEL="a.Tenant") |
| 4827 |
def test_circular_dependency_swappable(self): |
| 4828 |
""" |
| 4829 |
#23322 - The dependency resolver knows to explicitly resolve |
| 4830 |
swappable models. |
| 4831 |
""" |
| 4832 |
with isolate_lru_cache(apps.get_swappable_settings_name): |
| 4833 |
tenant = ModelState( |
| 4834 |
"a", |
| 4835 |
"Tenant", |
| 4836 |
[ |
| 4837 |
("id", models.AutoField(primary_key=True)), |
| 4838 |
("primary_address", models.ForeignKey("b.Address", models.CASCADE)), |
| 4839 |
], |
| 4840 |
bases=(AbstractBaseUser,), |
| 4841 |
) |
| 4842 |
address = ModelState( |
| 4843 |
"b", |
| 4844 |
"Address", |
| 4845 |
[ |
| 4846 |
("id", models.AutoField(primary_key=True)), |
| 4847 |
( |
| 4848 |
"tenant", |
| 4849 |
models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE), |
| 4850 |
), |
| 4851 |
], |
| 4852 |
) |
| 4853 |
changes = self.get_changes([], [address, tenant]) |
| 4854 |
|
| 4855 |
# Right number/type of migrations? |
| 4856 |
self.assertNumberMigrations(changes, "a", 2) |
| 4857 |
self.assertOperationTypes(changes, "a", 0, ["CreateModel"]) |
| 4858 |
self.assertOperationTypes(changes, "a", 1, ["AddField"]) |
| 4859 |
self.assertMigrationDependencies(changes, "a", 0, []) |
| 4860 |
self.assertMigrationDependencies( |
| 4861 |
changes, "a", 1, [("a", "auto_1"), ("b", "auto_1")] |
| 4862 |
) |
| 4863 |
# Right number/type of migrations? |
| 4864 |
self.assertNumberMigrations(changes, "b", 1) |
| 4865 |
self.assertOperationTypes(changes, "b", 0, ["CreateModel"]) |
| 4866 |
self.assertMigrationDependencies( |
| 4867 |
changes, "b", 0, [("__setting__", "AUTH_USER_MODEL")] |
| 4868 |
) |
| 4869 |
|
| 4870 |
@override_settings(AUTH_USER_MODEL="b.Tenant") |
| 4871 |
def test_circular_dependency_swappable2(self): |
| 4872 |
""" |
| 4873 |
#23322 - The dependency resolver knows to explicitly resolve |
| 4874 |
swappable models but with the swappable not being the first migrated |
| 4875 |
model. |
| 4876 |
""" |
| 4877 |
with isolate_lru_cache(apps.get_swappable_settings_name): |
| 4878 |
address = ModelState( |
| 4879 |
"a", |
| 4880 |
"Address", |
| 4881 |
[ |
| 4882 |
("id", models.AutoField(primary_key=True)), |
| 4883 |
( |
| 4884 |
"tenant", |
| 4885 |
models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE), |
| 4886 |
), |
| 4887 |
], |
| 4888 |
) |
| 4889 |
tenant = ModelState( |
| 4890 |
"b", |
| 4891 |
"Tenant", |
| 4892 |
[ |
| 4893 |
("id", models.AutoField(primary_key=True)), |
| 4894 |
("primary_address", models.ForeignKey("a.Address", models.CASCADE)), |
| 4895 |
], |
| 4896 |
bases=(AbstractBaseUser,), |
| 4897 |
) |
| 4898 |
changes = self.get_changes([], [address, tenant]) |
| 4899 |
# Right number/type of migrations? |
| 4900 |
self.assertNumberMigrations(changes, "a", 2) |
| 4901 |
self.assertOperationTypes(changes, "a", 0, ["CreateModel"]) |
| 4902 |
self.assertOperationTypes(changes, "a", 1, ["AddField"]) |
| 4903 |
self.assertMigrationDependencies(changes, "a", 0, []) |
| 4904 |
self.assertMigrationDependencies( |
| 4905 |
changes, "a", 1, [("__setting__", "AUTH_USER_MODEL"), ("a", "auto_1")] |
| 4906 |
) |
| 4907 |
# Right number/type of migrations? |
| 4908 |
self.assertNumberMigrations(changes, "b", 1) |
| 4909 |
self.assertOperationTypes(changes, "b", 0, ["CreateModel"]) |
| 4910 |
self.assertMigrationDependencies(changes, "b", 0, [("a", "auto_1")]) |
| 4911 |
|
| 4912 |
@override_settings(AUTH_USER_MODEL="a.Person") |
| 4913 |
def test_circular_dependency_swappable_self(self): |
| 4914 |
""" |
| 4915 |
#23322 - The dependency resolver knows to explicitly resolve |
| 4916 |
swappable models. |
| 4917 |
""" |
| 4918 |
with isolate_lru_cache(apps.get_swappable_settings_name): |
| 4919 |
person = ModelState( |
| 4920 |
"a", |
| 4921 |
"Person", |
| 4922 |
[ |
| 4923 |
("id", models.AutoField(primary_key=True)), |
| 4924 |
( |
| 4925 |
"parent1", |
| 4926 |
models.ForeignKey( |
| 4927 |
settings.AUTH_USER_MODEL, |
| 4928 |
models.CASCADE, |
| 4929 |
related_name="children", |
| 4930 |
), |
| 4931 |
), |
| 4932 |
], |
| 4933 |
) |
| 4934 |
changes = self.get_changes([], [person]) |
| 4935 |
# Right number/type of migrations? |
| 4936 |
self.assertNumberMigrations(changes, "a", 1) |
| 4937 |
self.assertOperationTypes(changes, "a", 0, ["CreateModel"]) |
| 4938 |
self.assertMigrationDependencies(changes, "a", 0, []) |
| 4939 |
|
| 4940 |
@override_settings(AUTH_USER_MODEL="a.User") |
| 4941 |
def test_swappable_circular_multi_mti(self): |
| 4942 |
with isolate_lru_cache(apps.get_swappable_settings_name): |
| 4943 |
parent = ModelState( |
| 4944 |
"a", |
| 4945 |
"Parent", |
| 4946 |
[("user", models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE))], |
| 4947 |
) |
| 4948 |
child = ModelState("a", "Child", [], bases=("a.Parent",)) |
| 4949 |
user = ModelState("a", "User", [], bases=(AbstractBaseUser, "a.Child")) |
| 4950 |
changes = self.get_changes([], [parent, child, user]) |
| 4951 |
self.assertNumberMigrations(changes, "a", 1) |
| 4952 |
self.assertOperationTypes( |
| 4953 |
changes, "a", 0, ["CreateModel", "CreateModel", "CreateModel", "AddField"] |
| 4954 |
) |
| 4955 |
|
| 4956 |
@mock.patch( |
| 4957 |
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition", |
| 4958 |
side_effect=AssertionError("Should not have prompted for not null addition"), |
| 4959 |
) |
| 4960 |
def test_add_blank_textfield_and_charfield(self, mocked_ask_method): |
| 4961 |
""" |
| 4962 |
#23405 - Adding a NOT NULL and blank `CharField` or `TextField` |
| 4963 |
without default should not prompt for a default. |
| 4964 |
""" |
| 4965 |
changes = self.get_changes( |
| 4966 |
[self.author_empty], [self.author_with_biography_blank] |
| 4967 |
) |
| 4968 |
# Right number/type of migrations? |
| 4969 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4970 |
self.assertOperationTypes(changes, "testapp", 0, ["AddField", "AddField"]) |
| 4971 |
self.assertOperationAttributes(changes, "testapp", 0, 0) |
| 4972 |
|
| 4973 |
@mock.patch( |
| 4974 |
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition" |
| 4975 |
) |
| 4976 |
def test_add_non_blank_textfield_and_charfield(self, mocked_ask_method): |
| 4977 |
""" |
| 4978 |
#23405 - Adding a NOT NULL and non-blank `CharField` or `TextField` |
| 4979 |
without default should prompt for a default. |
| 4980 |
""" |
| 4981 |
changes = self.get_changes( |
| 4982 |
[self.author_empty], [self.author_with_biography_non_blank] |
| 4983 |
) |
| 4984 |
self.assertEqual(mocked_ask_method.call_count, 2) |
| 4985 |
# Right number/type of migrations? |
| 4986 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 4987 |
self.assertOperationTypes(changes, "testapp", 0, ["AddField", "AddField"]) |
| 4988 |
self.assertOperationAttributes(changes, "testapp", 0, 0) |
| 4989 |
|
| 4990 |
def test_mti_inheritance_model_removal(self): |
| 4991 |
Animal = ModelState( |
| 4992 |
"app", |
| 4993 |
"Animal", |
| 4994 |
[ |
| 4995 |
("id", models.AutoField(primary_key=True)), |
| 4996 |
], |
| 4997 |
) |
| 4998 |
Dog = ModelState("app", "Dog", [], bases=("app.Animal",)) |
| 4999 |
changes = self.get_changes([Animal, Dog], [Animal]) |
| 5000 |
self.assertNumberMigrations(changes, "app", 1) |
| 5001 |
self.assertOperationTypes(changes, "app", 0, ["DeleteModel"]) |
| 5002 |
self.assertOperationAttributes(changes, "app", 0, 0, name="Dog") |
| 5003 |
|
| 5004 |
def test_add_model_with_field_removed_from_base_model(self): |
| 5005 |
""" |
| 5006 |
Removing a base field takes place before adding a new inherited model |
| 5007 |
that has a field with the same name. |
| 5008 |
""" |
| 5009 |
before = [ |
| 5010 |
ModelState( |
| 5011 |
"app", |
| 5012 |
"readable", |
| 5013 |
[ |
| 5014 |
("id", models.AutoField(primary_key=True)), |
| 5015 |
("title", models.CharField(max_length=200)), |
| 5016 |
], |
| 5017 |
), |
| 5018 |
] |
| 5019 |
after = [ |
| 5020 |
ModelState( |
| 5021 |
"app", |
| 5022 |
"readable", |
| 5023 |
[ |
| 5024 |
("id", models.AutoField(primary_key=True)), |
| 5025 |
], |
| 5026 |
), |
| 5027 |
ModelState( |
| 5028 |
"app", |
| 5029 |
"book", |
| 5030 |
[ |
| 5031 |
("title", models.CharField(max_length=200)), |
| 5032 |
], |
| 5033 |
bases=("app.readable",), |
| 5034 |
), |
| 5035 |
] |
| 5036 |
changes = self.get_changes(before, after) |
| 5037 |
self.assertNumberMigrations(changes, "app", 1) |
| 5038 |
self.assertOperationTypes(changes, "app", 0, ["RemoveField", "CreateModel"]) |
| 5039 |
self.assertOperationAttributes( |
| 5040 |
changes, "app", 0, 0, name="title", model_name="readable" |
| 5041 |
) |
| 5042 |
self.assertOperationAttributes(changes, "app", 0, 1, name="book") |
| 5043 |
|
| 5044 |
def test_parse_number(self): |
| 5045 |
tests = [ |
| 5046 |
("no_number", None), |
| 5047 |
("0001_initial", 1), |
| 5048 |
("0002_model3", 2), |
| 5049 |
("0002_auto_20380101_1112", 2), |
| 5050 |
("0002_squashed_0003", 3), |
| 5051 |
("0002_model2_squashed_0003_other4", 3), |
| 5052 |
("0002_squashed_0003_squashed_0004", 4), |
| 5053 |
("0002_model2_squashed_0003_other4_squashed_0005_other6", 5), |
| 5054 |
("0002_custom_name_20380101_1112_squashed_0003_model", 3), |
| 5055 |
("2_squashed_4", 4), |
| 5056 |
] |
| 5057 |
for migration_name, expected_number in tests: |
| 5058 |
with self.subTest(migration_name=migration_name): |
| 5059 |
self.assertEqual( |
| 5060 |
MigrationAutodetector.parse_number(migration_name), |
| 5061 |
expected_number, |
| 5062 |
) |
| 5063 |
|
| 5064 |
def test_add_custom_fk_with_hardcoded_to(self): |
| 5065 |
class HardcodedForeignKey(models.ForeignKey): |
| 5066 |
def __init__(self, *args, **kwargs): |
| 5067 |
kwargs["to"] = "testapp.Author" |
| 5068 |
super().__init__(*args, **kwargs) |
| 5069 |
|
| 5070 |
def deconstruct(self): |
| 5071 |
name, path, args, kwargs = super().deconstruct() |
| 5072 |
del kwargs["to"] |
| 5073 |
return name, path, args, kwargs |
| 5074 |
|
| 5075 |
book_hardcoded_fk_to = ModelState( |
| 5076 |
"testapp", |
| 5077 |
"Book", |
| 5078 |
[ |
| 5079 |
("author", HardcodedForeignKey(on_delete=models.CASCADE)), |
| 5080 |
], |
| 5081 |
) |
| 5082 |
changes = self.get_changes( |
| 5083 |
[self.author_empty], |
| 5084 |
[self.author_empty, book_hardcoded_fk_to], |
| 5085 |
) |
| 5086 |
self.assertNumberMigrations(changes, "testapp", 1) |
| 5087 |
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"]) |
| 5088 |
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Book") |
| 5089 |
|
| 5090 |
@mock.patch( |
| 5091 |
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition" |
| 5092 |
) |
| 5093 |
def test_add_composite_pk(self, mocked_ask_method): |
| 5094 |
before = [ |
| 5095 |
ModelState( |
| 5096 |
"app", |
| 5097 |
"foo", |
| 5098 |
[ |
| 5099 |
("id", models.AutoField(primary_key=True)), |
| 5100 |
], |
| 5101 |
), |
| 5102 |
] |
| 5103 |
after = [ |
| 5104 |
ModelState( |
| 5105 |
"app", |
| 5106 |
"foo", |
| 5107 |
[ |
| 5108 |
("pk", models.CompositePrimaryKey("foo_id", "bar_id")), |
| 5109 |
("id", models.IntegerField()), |
| 5110 |
], |
| 5111 |
), |
| 5112 |
] |
| 5113 |
|
| 5114 |
changes = self.get_changes(before, after) |
| 5115 |
self.assertEqual(mocked_ask_method.call_count, 0) |
| 5116 |
self.assertNumberMigrations(changes, "app", 1) |
| 5117 |
self.assertOperationTypes(changes, "app", 0, ["AddField", "AlterField"]) |
| 5118 |
self.assertOperationAttributes( |
| 5119 |
changes, |
| 5120 |
"app", |
| 5121 |
0, |
| 5122 |
0, |
| 5123 |
name="pk", |
| 5124 |
model_name="foo", |
| 5125 |
preserve_default=True, |
| 5126 |
) |
| 5127 |
self.assertOperationAttributes( |
| 5128 |
changes, |
| 5129 |
"app", |
| 5130 |
0, |
| 5131 |
1, |
| 5132 |
name="id", |
| 5133 |
model_name="foo", |
| 5134 |
preserve_default=True, |
| 5135 |
) |
| 5136 |
|
| 5137 |
def test_remove_composite_pk(self): |
| 5138 |
before = [ |
| 5139 |
ModelState( |
| 5140 |
"app", |
| 5141 |
"foo", |
| 5142 |
[ |
| 5143 |
("pk", models.CompositePrimaryKey("foo_id", "bar_id")), |
| 5144 |
("id", models.IntegerField()), |
| 5145 |
], |
| 5146 |
), |
| 5147 |
] |
| 5148 |
after = [ |
| 5149 |
ModelState( |
| 5150 |
"app", |
| 5151 |
"foo", |
| 5152 |
[ |
| 5153 |
("id", models.AutoField(primary_key=True)), |
| 5154 |
], |
| 5155 |
), |
| 5156 |
] |
| 5157 |
|
| 5158 |
changes = self.get_changes(before, after) |
| 5159 |
self.assertNumberMigrations(changes, "app", 1) |
| 5160 |
self.assertOperationTypes(changes, "app", 0, ["RemoveField", "AlterField"]) |
| 5161 |
self.assertOperationAttributes( |
| 5162 |
changes, |
| 5163 |
"app", |
| 5164 |
0, |
| 5165 |
0, |
| 5166 |
name="pk", |
| 5167 |
model_name="foo", |
| 5168 |
) |
| 5169 |
self.assertOperationAttributes( |
| 5170 |
changes, |
| 5171 |
"app", |
| 5172 |
0, |
| 5173 |
1, |
| 5174 |
name="id", |
| 5175 |
model_name="foo", |
| 5176 |
preserve_default=True, |
| 5177 |
) |
| 5178 |
|
| 5179 |
|
| 5180 |
class MigrationSuggestNameTests(SimpleTestCase): |
| 5181 |
def test_no_operations(self): |
| 5182 |
class Migration(migrations.Migration): |
| 5183 |
operations = [] |
| 5184 |
|
| 5185 |
migration = Migration("some_migration", "test_app") |
| 5186 |
self.assertIs(migration.suggest_name().startswith("auto_"), True) |
| 5187 |
|
| 5188 |
def test_no_operations_initial(self): |
| 5189 |
class Migration(migrations.Migration): |
| 5190 |
initial = True |
| 5191 |
operations = [] |
| 5192 |
|
| 5193 |
migration = Migration("some_migration", "test_app") |
| 5194 |
self.assertEqual(migration.suggest_name(), "initial") |
| 5195 |
|
| 5196 |
def test_single_operation(self): |
| 5197 |
class Migration(migrations.Migration): |
| 5198 |
operations = [migrations.CreateModel("Person", fields=[])] |
| 5199 |
|
| 5200 |
migration = Migration("0001_initial", "test_app") |
| 5201 |
self.assertEqual(migration.suggest_name(), "person") |
| 5202 |
|
| 5203 |
class Migration(migrations.Migration): |
| 5204 |
operations = [migrations.DeleteModel("Person")] |
| 5205 |
|
| 5206 |
migration = Migration("0002_initial", "test_app") |
| 5207 |
self.assertEqual(migration.suggest_name(), "delete_person") |
| 5208 |
|
| 5209 |
def test_single_operation_long_name(self): |
| 5210 |
class Migration(migrations.Migration): |
| 5211 |
operations = [migrations.CreateModel("A" * 53, fields=[])] |
| 5212 |
|
| 5213 |
migration = Migration("some_migration", "test_app") |
| 5214 |
self.assertEqual(migration.suggest_name(), "a" * 53) |
| 5215 |
|
| 5216 |
def test_two_operations(self): |
| 5217 |
class Migration(migrations.Migration): |
| 5218 |
operations = [ |
| 5219 |
migrations.CreateModel("Person", fields=[]), |
| 5220 |
migrations.DeleteModel("Animal"), |
| 5221 |
] |
| 5222 |
|
| 5223 |
migration = Migration("some_migration", "test_app") |
| 5224 |
self.assertEqual(migration.suggest_name(), "person_delete_animal") |
| 5225 |
|
| 5226 |
def test_two_create_models(self): |
| 5227 |
class Migration(migrations.Migration): |
| 5228 |
operations = [ |
| 5229 |
migrations.CreateModel("Person", fields=[]), |
| 5230 |
migrations.CreateModel("Animal", fields=[]), |
| 5231 |
] |
| 5232 |
|
| 5233 |
migration = Migration("0001_initial", "test_app") |
| 5234 |
self.assertEqual(migration.suggest_name(), "person_animal") |
| 5235 |
|
| 5236 |
def test_two_create_models_with_initial_true(self): |
| 5237 |
class Migration(migrations.Migration): |
| 5238 |
initial = True |
| 5239 |
operations = [ |
| 5240 |
migrations.CreateModel("Person", fields=[]), |
| 5241 |
migrations.CreateModel("Animal", fields=[]), |
| 5242 |
] |
| 5243 |
|
| 5244 |
migration = Migration("0001_initial", "test_app") |
| 5245 |
self.assertEqual(migration.suggest_name(), "initial") |
| 5246 |
|
| 5247 |
def test_many_operations_suffix(self): |
| 5248 |
class Migration(migrations.Migration): |
| 5249 |
operations = [ |
| 5250 |
migrations.CreateModel("Person1", fields=[]), |
| 5251 |
migrations.CreateModel("Person2", fields=[]), |
| 5252 |
migrations.CreateModel("Person3", fields=[]), |
| 5253 |
migrations.DeleteModel("Person4"), |
| 5254 |
migrations.DeleteModel("Person5"), |
| 5255 |
] |
| 5256 |
|
| 5257 |
migration = Migration("some_migration", "test_app") |
| 5258 |
self.assertEqual( |
| 5259 |
migration.suggest_name(), |
| 5260 |
"person1_person2_person3_delete_person4_and_more", |
| 5261 |
) |
| 5262 |
|
| 5263 |
def test_operation_with_no_suggested_name(self): |
| 5264 |
class Migration(migrations.Migration): |
| 5265 |
operations = [ |
| 5266 |
migrations.CreateModel("Person", fields=[]), |
| 5267 |
migrations.RunSQL("SELECT 1 FROM person;"), |
| 5268 |
] |
| 5269 |
|
| 5270 |
migration = Migration("some_migration", "test_app") |
| 5271 |
self.assertIs(migration.suggest_name().startswith("auto_"), True) |
| 5272 |
|
| 5273 |
def test_operation_with_invalid_chars_in_suggested_name(self): |
| 5274 |
class Migration(migrations.Migration): |
| 5275 |
operations = [ |
| 5276 |
migrations.AddConstraint( |
| 5277 |
"Person", |
| 5278 |
models.UniqueConstraint( |
| 5279 |
fields=["name"], name="person.name-*~unique!" |
| 5280 |
), |
| 5281 |
), |
| 5282 |
] |
| 5283 |
|
| 5284 |
migration = Migration("some_migration", "test_app") |
| 5285 |
self.assertEqual(migration.suggest_name(), "person_person_name_unique_") |
| 5286 |
|
| 5287 |
def test_none_name(self): |
| 5288 |
class Migration(migrations.Migration): |
| 5289 |
operations = [migrations.RunSQL("SELECT 1 FROM person;")] |
| 5290 |
|
| 5291 |
migration = Migration("0001_initial", "test_app") |
| 5292 |
suggest_name = migration.suggest_name() |
| 5293 |
self.assertIs(suggest_name.startswith("auto_"), True) |
| 5294 |
|
| 5295 |
def test_none_name_with_initial_true(self): |
| 5296 |
class Migration(migrations.Migration): |
| 5297 |
initial = True |
| 5298 |
operations = [migrations.RunSQL("SELECT 1 FROM person;")] |
| 5299 |
|
| 5300 |
migration = Migration("0001_initial", "test_app") |
| 5301 |
self.assertEqual(migration.suggest_name(), "initial") |
| 5302 |
|
| 5303 |
def test_auto(self): |
| 5304 |
migration = migrations.Migration("0001_initial", "test_app") |
| 5305 |
suggest_name = migration.suggest_name() |
| 5306 |
self.assertIs(suggest_name.startswith("auto_"), True) |