| 1 |
import datetime |
| 2 |
import decimal |
| 3 |
import enum |
| 4 |
import functools |
| 5 |
import math |
| 6 |
import os |
| 7 |
import pathlib |
| 8 |
import re |
| 9 |
import sys |
| 10 |
import time |
| 11 |
import uuid |
| 12 |
import zoneinfo |
| 13 |
from types import NoneType |
| 14 |
from unittest import mock |
| 15 |
|
| 16 |
import custom_migration_operations.more_operations |
| 17 |
import custom_migration_operations.operations |
| 18 |
|
| 19 |
from django import get_version |
| 20 |
from django.conf import SettingsReference, settings |
| 21 |
from django.core.validators import EmailValidator, RegexValidator |
| 22 |
from django.db import migrations, models |
| 23 |
from django.db.migrations.serializer import BaseSerializer |
| 24 |
from django.db.migrations.writer import MigrationWriter, OperationWriter |
| 25 |
from django.test import SimpleTestCase, override_settings |
| 26 |
from django.test.utils import extend_sys_path |
| 27 |
from django.utils.deconstruct import deconstructible |
| 28 |
from django.utils.functional import SimpleLazyObject |
| 29 |
from django.utils.timezone import get_default_timezone, get_fixed_timezone |
| 30 |
from django.utils.translation import gettext_lazy as _ |
| 31 |
|
| 32 |
from .models import FoodManager, FoodQuerySet |
| 33 |
|
| 34 |
|
| 35 |
def get_choices(): |
| 36 |
return [(i, str(i)) for i in range(3)] |
| 37 |
|
| 38 |
|
| 39 |
class DeconstructibleInstances: |
| 40 |
def deconstruct(self): |
| 41 |
return ("DeconstructibleInstances", [], {}) |
| 42 |
|
| 43 |
|
| 44 |
class Money(decimal.Decimal): |
| 45 |
def deconstruct(self): |
| 46 |
return ( |
| 47 |
"%s.%s" % (self.__class__.__module__, self.__class__.__name__), |
| 48 |
[str(self)], |
| 49 |
{}, |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
class TestModel1: |
| 54 |
def upload_to(self): |
| 55 |
return "/somewhere/dynamic/" |
| 56 |
|
| 57 |
thing = models.FileField(upload_to=upload_to) |
| 58 |
|
| 59 |
|
| 60 |
class TextEnum(enum.Enum): |
| 61 |
A = "a-value" |
| 62 |
B = "value-b" |
| 63 |
|
| 64 |
|
| 65 |
class TextTranslatedEnum(enum.Enum): |
| 66 |
A = _("a-value") |
| 67 |
B = _("value-b") |
| 68 |
|
| 69 |
|
| 70 |
class BinaryEnum(enum.Enum): |
| 71 |
A = b"a-value" |
| 72 |
B = b"value-b" |
| 73 |
|
| 74 |
|
| 75 |
class IntEnum(enum.IntEnum): |
| 76 |
A = 1 |
| 77 |
B = 2 |
| 78 |
|
| 79 |
|
| 80 |
class IntFlagEnum(enum.IntFlag): |
| 81 |
A = 1 |
| 82 |
B = 2 |
| 83 |
|
| 84 |
|
| 85 |
def decorator(f): |
| 86 |
@functools.wraps(f) |
| 87 |
def wrapper(*args, **kwargs): |
| 88 |
return f(*args, **kwargs) |
| 89 |
|
| 90 |
return wrapper |
| 91 |
|
| 92 |
|
| 93 |
@decorator |
| 94 |
def function_with_decorator(): |
| 95 |
pass |
| 96 |
|
| 97 |
|
| 98 |
@functools.cache |
| 99 |
def function_with_cache(): |
| 100 |
pass |
| 101 |
|
| 102 |
|
| 103 |
@functools.lru_cache(maxsize=10) |
| 104 |
def function_with_lru_cache(): |
| 105 |
pass |
| 106 |
|
| 107 |
|
| 108 |
class OperationWriterTests(SimpleTestCase): |
| 109 |
def test_empty_signature(self): |
| 110 |
operation = custom_migration_operations.operations.TestOperation() |
| 111 |
buff, imports = OperationWriter(operation, indentation=0).serialize() |
| 112 |
self.assertEqual(imports, {"import custom_migration_operations.operations"}) |
| 113 |
self.assertEqual( |
| 114 |
buff, |
| 115 |
"custom_migration_operations.operations.TestOperation(\n),", |
| 116 |
) |
| 117 |
|
| 118 |
def test_args_signature(self): |
| 119 |
operation = custom_migration_operations.operations.ArgsOperation(1, 2) |
| 120 |
buff, imports = OperationWriter(operation, indentation=0).serialize() |
| 121 |
self.assertEqual(imports, {"import custom_migration_operations.operations"}) |
| 122 |
self.assertEqual( |
| 123 |
buff, |
| 124 |
"custom_migration_operations.operations.ArgsOperation(\n" |
| 125 |
" arg1=1,\n" |
| 126 |
" arg2=2,\n" |
| 127 |
"),", |
| 128 |
) |
| 129 |
|
| 130 |
def test_kwargs_signature(self): |
| 131 |
operation = custom_migration_operations.operations.KwargsOperation(kwarg1=1) |
| 132 |
buff, imports = OperationWriter(operation, indentation=0).serialize() |
| 133 |
self.assertEqual(imports, {"import custom_migration_operations.operations"}) |
| 134 |
self.assertEqual( |
| 135 |
buff, |
| 136 |
"custom_migration_operations.operations.KwargsOperation(\n" |
| 137 |
" kwarg1=1,\n" |
| 138 |
"),", |
| 139 |
) |
| 140 |
|
| 141 |
def test_args_kwargs_signature(self): |
| 142 |
operation = custom_migration_operations.operations.ArgsKwargsOperation( |
| 143 |
1, 2, kwarg2=4 |
| 144 |
) |
| 145 |
buff, imports = OperationWriter(operation, indentation=0).serialize() |
| 146 |
self.assertEqual(imports, {"import custom_migration_operations.operations"}) |
| 147 |
self.assertEqual( |
| 148 |
buff, |
| 149 |
"custom_migration_operations.operations.ArgsKwargsOperation(\n" |
| 150 |
" arg1=1,\n" |
| 151 |
" arg2=2,\n" |
| 152 |
" kwarg2=4,\n" |
| 153 |
"),", |
| 154 |
) |
| 155 |
|
| 156 |
def test_keyword_only_args_signature(self): |
| 157 |
operation = ( |
| 158 |
custom_migration_operations.operations.ArgsAndKeywordOnlyArgsOperation( |
| 159 |
1, 2, kwarg1=3, kwarg2=4 |
| 160 |
) |
| 161 |
) |
| 162 |
buff, imports = OperationWriter(operation, indentation=0).serialize() |
| 163 |
self.assertEqual(imports, {"import custom_migration_operations.operations"}) |
| 164 |
self.assertEqual( |
| 165 |
buff, |
| 166 |
"custom_migration_operations.operations.ArgsAndKeywordOnlyArgsOperation(\n" |
| 167 |
" arg1=1,\n" |
| 168 |
" arg2=2,\n" |
| 169 |
" kwarg1=3,\n" |
| 170 |
" kwarg2=4,\n" |
| 171 |
"),", |
| 172 |
) |
| 173 |
|
| 174 |
def test_nested_args_signature(self): |
| 175 |
operation = custom_migration_operations.operations.ArgsOperation( |
| 176 |
custom_migration_operations.operations.ArgsOperation(1, 2), |
| 177 |
custom_migration_operations.operations.KwargsOperation(kwarg1=3, kwarg2=4), |
| 178 |
) |
| 179 |
buff, imports = OperationWriter(operation, indentation=0).serialize() |
| 180 |
self.assertEqual(imports, {"import custom_migration_operations.operations"}) |
| 181 |
self.assertEqual( |
| 182 |
buff, |
| 183 |
"custom_migration_operations.operations.ArgsOperation(\n" |
| 184 |
" arg1=custom_migration_operations.operations.ArgsOperation(\n" |
| 185 |
" arg1=1,\n" |
| 186 |
" arg2=2,\n" |
| 187 |
" ),\n" |
| 188 |
" arg2=custom_migration_operations.operations.KwargsOperation(\n" |
| 189 |
" kwarg1=3,\n" |
| 190 |
" kwarg2=4,\n" |
| 191 |
" ),\n" |
| 192 |
"),", |
| 193 |
) |
| 194 |
|
| 195 |
def test_multiline_args_signature(self): |
| 196 |
operation = custom_migration_operations.operations.ArgsOperation( |
| 197 |
"test\n arg1", "test\narg2" |
| 198 |
) |
| 199 |
buff, imports = OperationWriter(operation, indentation=0).serialize() |
| 200 |
self.assertEqual(imports, {"import custom_migration_operations.operations"}) |
| 201 |
self.assertEqual( |
| 202 |
buff, |
| 203 |
"custom_migration_operations.operations.ArgsOperation(\n" |
| 204 |
" arg1='test\\n arg1',\n" |
| 205 |
" arg2='test\\narg2',\n" |
| 206 |
"),", |
| 207 |
) |
| 208 |
|
| 209 |
def test_expand_args_signature(self): |
| 210 |
operation = custom_migration_operations.operations.ExpandArgsOperation([1, 2]) |
| 211 |
buff, imports = OperationWriter(operation, indentation=0).serialize() |
| 212 |
self.assertEqual(imports, {"import custom_migration_operations.operations"}) |
| 213 |
self.assertEqual( |
| 214 |
buff, |
| 215 |
"custom_migration_operations.operations.ExpandArgsOperation(\n" |
| 216 |
" arg=[\n" |
| 217 |
" 1,\n" |
| 218 |
" 2,\n" |
| 219 |
" ],\n" |
| 220 |
"),", |
| 221 |
) |
| 222 |
|
| 223 |
def test_nested_operation_expand_args_signature(self): |
| 224 |
operation = custom_migration_operations.operations.ExpandArgsOperation( |
| 225 |
arg=[ |
| 226 |
custom_migration_operations.operations.KwargsOperation( |
| 227 |
kwarg1=1, |
| 228 |
kwarg2=2, |
| 229 |
), |
| 230 |
] |
| 231 |
) |
| 232 |
buff, imports = OperationWriter(operation, indentation=0).serialize() |
| 233 |
self.assertEqual(imports, {"import custom_migration_operations.operations"}) |
| 234 |
self.assertEqual( |
| 235 |
buff, |
| 236 |
"custom_migration_operations.operations.ExpandArgsOperation(\n" |
| 237 |
" arg=[\n" |
| 238 |
" custom_migration_operations.operations.KwargsOperation(\n" |
| 239 |
" kwarg1=1,\n" |
| 240 |
" kwarg2=2,\n" |
| 241 |
" ),\n" |
| 242 |
" ],\n" |
| 243 |
"),", |
| 244 |
) |
| 245 |
|
| 246 |
|
| 247 |
class WriterTests(SimpleTestCase): |
| 248 |
""" |
| 249 |
Tests the migration writer (makes migration files from Migration instances) |
| 250 |
""" |
| 251 |
|
| 252 |
class NestedEnum(enum.IntEnum): |
| 253 |
A = 1 |
| 254 |
B = 2 |
| 255 |
|
| 256 |
class NestedChoices(models.TextChoices): |
| 257 |
X = "X", "X value" |
| 258 |
Y = "Y", "Y value" |
| 259 |
|
| 260 |
@classmethod |
| 261 |
def method(cls): |
| 262 |
return cls.X |
| 263 |
|
| 264 |
def safe_exec(self, string, value=None): |
| 265 |
d = {} |
| 266 |
try: |
| 267 |
exec(string, globals(), d) |
| 268 |
except Exception as e: |
| 269 |
if value: |
| 270 |
self.fail( |
| 271 |
"Could not exec %r (from value %r): %s" % (string.strip(), value, e) |
| 272 |
) |
| 273 |
else: |
| 274 |
self.fail("Could not exec %r: %s" % (string.strip(), e)) |
| 275 |
return d |
| 276 |
|
| 277 |
def serialize_round_trip(self, value): |
| 278 |
string, imports = MigrationWriter.serialize(value) |
| 279 |
return self.safe_exec( |
| 280 |
"%s\ntest_value_result = %s" % ("\n".join(imports), string), value |
| 281 |
)["test_value_result"] |
| 282 |
|
| 283 |
def assertSerializedEqual(self, value): |
| 284 |
self.assertEqual(self.serialize_round_trip(value), value) |
| 285 |
|
| 286 |
def assertSerializedResultEqual(self, value, target): |
| 287 |
self.assertEqual(MigrationWriter.serialize(value), target) |
| 288 |
|
| 289 |
def assertSerializedFieldEqual(self, value): |
| 290 |
new_value = self.serialize_round_trip(value) |
| 291 |
self.assertEqual(value.__class__, new_value.__class__) |
| 292 |
self.assertEqual(value.max_length, new_value.max_length) |
| 293 |
self.assertEqual(value.null, new_value.null) |
| 294 |
self.assertEqual(value.unique, new_value.unique) |
| 295 |
|
| 296 |
def test_serialize_numbers(self): |
| 297 |
self.assertSerializedEqual(1) |
| 298 |
self.assertSerializedEqual(1.2) |
| 299 |
self.assertTrue(math.isinf(self.serialize_round_trip(float("inf")))) |
| 300 |
self.assertTrue(math.isinf(self.serialize_round_trip(float("-inf")))) |
| 301 |
self.assertTrue(math.isnan(self.serialize_round_trip(float("nan")))) |
| 302 |
|
| 303 |
self.assertSerializedEqual(decimal.Decimal("1.3")) |
| 304 |
self.assertSerializedResultEqual( |
| 305 |
decimal.Decimal("1.3"), ("Decimal('1.3')", {"from decimal import Decimal"}) |
| 306 |
) |
| 307 |
|
| 308 |
self.assertSerializedEqual(Money("1.3")) |
| 309 |
self.assertSerializedResultEqual( |
| 310 |
Money("1.3"), |
| 311 |
("migrations.test_writer.Money('1.3')", {"import migrations.test_writer"}), |
| 312 |
) |
| 313 |
|
| 314 |
def test_serialize_constants(self): |
| 315 |
self.assertSerializedEqual(None) |
| 316 |
self.assertSerializedEqual(True) |
| 317 |
self.assertSerializedEqual(False) |
| 318 |
|
| 319 |
def test_serialize_strings(self): |
| 320 |
self.assertSerializedEqual(b"foobar") |
| 321 |
string, imports = MigrationWriter.serialize(b"foobar") |
| 322 |
self.assertEqual(string, "b'foobar'") |
| 323 |
self.assertSerializedEqual("föobár") |
| 324 |
string, imports = MigrationWriter.serialize("foobar") |
| 325 |
self.assertEqual(string, "'foobar'") |
| 326 |
|
| 327 |
def test_serialize_multiline_strings(self): |
| 328 |
self.assertSerializedEqual(b"foo\nbar") |
| 329 |
string, imports = MigrationWriter.serialize(b"foo\nbar") |
| 330 |
self.assertEqual(string, "b'foo\\nbar'") |
| 331 |
self.assertSerializedEqual("föo\nbár") |
| 332 |
string, imports = MigrationWriter.serialize("foo\nbar") |
| 333 |
self.assertEqual(string, "'foo\\nbar'") |
| 334 |
|
| 335 |
def test_serialize_collections(self): |
| 336 |
self.assertSerializedEqual({1: 2}) |
| 337 |
self.assertSerializedEqual(["a", 2, True, None]) |
| 338 |
self.assertSerializedEqual({2, 3, "eighty"}) |
| 339 |
self.assertSerializedEqual({"lalalala": ["yeah", "no", "maybe"]}) |
| 340 |
self.assertSerializedEqual(_("Hello")) |
| 341 |
|
| 342 |
def test_serialize_builtin_types(self): |
| 343 |
self.assertSerializedEqual([list, tuple, dict, set, frozenset]) |
| 344 |
self.assertSerializedResultEqual( |
| 345 |
[list, tuple, dict, set, frozenset], |
| 346 |
("[list, tuple, dict, set, frozenset]", set()), |
| 347 |
) |
| 348 |
|
| 349 |
def test_serialize_lazy_objects(self): |
| 350 |
pattern = re.compile(r"^foo$") |
| 351 |
lazy_pattern = SimpleLazyObject(lambda: pattern) |
| 352 |
self.assertEqual(self.serialize_round_trip(lazy_pattern), pattern) |
| 353 |
|
| 354 |
def test_serialize_enums(self): |
| 355 |
self.assertSerializedResultEqual( |
| 356 |
TextEnum.A, |
| 357 |
("migrations.test_writer.TextEnum['A']", {"import migrations.test_writer"}), |
| 358 |
) |
| 359 |
self.assertSerializedResultEqual( |
| 360 |
TextTranslatedEnum.A, |
| 361 |
( |
| 362 |
"migrations.test_writer.TextTranslatedEnum['A']", |
| 363 |
{"import migrations.test_writer"}, |
| 364 |
), |
| 365 |
) |
| 366 |
self.assertSerializedResultEqual( |
| 367 |
BinaryEnum.A, |
| 368 |
( |
| 369 |
"migrations.test_writer.BinaryEnum['A']", |
| 370 |
{"import migrations.test_writer"}, |
| 371 |
), |
| 372 |
) |
| 373 |
self.assertSerializedResultEqual( |
| 374 |
IntEnum.B, |
| 375 |
("migrations.test_writer.IntEnum['B']", {"import migrations.test_writer"}), |
| 376 |
) |
| 377 |
self.assertSerializedResultEqual( |
| 378 |
self.NestedEnum.A, |
| 379 |
( |
| 380 |
"migrations.test_writer.WriterTests.NestedEnum['A']", |
| 381 |
{"import migrations.test_writer"}, |
| 382 |
), |
| 383 |
) |
| 384 |
self.assertSerializedEqual(self.NestedEnum.A) |
| 385 |
|
| 386 |
field = models.CharField( |
| 387 |
default=TextEnum.B, choices=[(m.value, m) for m in TextEnum] |
| 388 |
) |
| 389 |
string = MigrationWriter.serialize(field)[0] |
| 390 |
self.assertEqual( |
| 391 |
string, |
| 392 |
"models.CharField(choices=[" |
| 393 |
"('a-value', migrations.test_writer.TextEnum['A']), " |
| 394 |
"('value-b', migrations.test_writer.TextEnum['B'])], " |
| 395 |
"default=migrations.test_writer.TextEnum['B'])", |
| 396 |
) |
| 397 |
field = models.CharField( |
| 398 |
default=TextTranslatedEnum.A, |
| 399 |
choices=[(m.value, m) for m in TextTranslatedEnum], |
| 400 |
) |
| 401 |
string = MigrationWriter.serialize(field)[0] |
| 402 |
self.assertEqual( |
| 403 |
string, |
| 404 |
"models.CharField(choices=[" |
| 405 |
"('a-value', migrations.test_writer.TextTranslatedEnum['A']), " |
| 406 |
"('value-b', migrations.test_writer.TextTranslatedEnum['B'])], " |
| 407 |
"default=migrations.test_writer.TextTranslatedEnum['A'])", |
| 408 |
) |
| 409 |
field = models.CharField( |
| 410 |
default=BinaryEnum.B, choices=[(m.value, m) for m in BinaryEnum] |
| 411 |
) |
| 412 |
string = MigrationWriter.serialize(field)[0] |
| 413 |
self.assertEqual( |
| 414 |
string, |
| 415 |
"models.CharField(choices=[" |
| 416 |
"(b'a-value', migrations.test_writer.BinaryEnum['A']), " |
| 417 |
"(b'value-b', migrations.test_writer.BinaryEnum['B'])], " |
| 418 |
"default=migrations.test_writer.BinaryEnum['B'])", |
| 419 |
) |
| 420 |
field = models.IntegerField( |
| 421 |
default=IntEnum.A, choices=[(m.value, m) for m in IntEnum] |
| 422 |
) |
| 423 |
string = MigrationWriter.serialize(field)[0] |
| 424 |
self.assertEqual( |
| 425 |
string, |
| 426 |
"models.IntegerField(choices=[" |
| 427 |
"(1, migrations.test_writer.IntEnum['A']), " |
| 428 |
"(2, migrations.test_writer.IntEnum['B'])], " |
| 429 |
"default=migrations.test_writer.IntEnum['A'])", |
| 430 |
) |
| 431 |
|
| 432 |
def test_serialize_enum_flags(self): |
| 433 |
self.assertSerializedResultEqual( |
| 434 |
IntFlagEnum.A, |
| 435 |
( |
| 436 |
"migrations.test_writer.IntFlagEnum['A']", |
| 437 |
{"import migrations.test_writer"}, |
| 438 |
), |
| 439 |
) |
| 440 |
self.assertSerializedResultEqual( |
| 441 |
IntFlagEnum.B, |
| 442 |
( |
| 443 |
"migrations.test_writer.IntFlagEnum['B']", |
| 444 |
{"import migrations.test_writer"}, |
| 445 |
), |
| 446 |
) |
| 447 |
field = models.IntegerField( |
| 448 |
default=IntFlagEnum.A, choices=[(m.value, m) for m in IntFlagEnum] |
| 449 |
) |
| 450 |
string = MigrationWriter.serialize(field)[0] |
| 451 |
self.assertEqual( |
| 452 |
string, |
| 453 |
"models.IntegerField(choices=[" |
| 454 |
"(1, migrations.test_writer.IntFlagEnum['A']), " |
| 455 |
"(2, migrations.test_writer.IntFlagEnum['B'])], " |
| 456 |
"default=migrations.test_writer.IntFlagEnum['A'])", |
| 457 |
) |
| 458 |
self.assertSerializedResultEqual( |
| 459 |
IntFlagEnum.A | IntFlagEnum.B, |
| 460 |
( |
| 461 |
"migrations.test_writer.IntFlagEnum['A'] | " |
| 462 |
"migrations.test_writer.IntFlagEnum['B']", |
| 463 |
{"import migrations.test_writer"}, |
| 464 |
), |
| 465 |
) |
| 466 |
|
| 467 |
def test_serialize_choices(self): |
| 468 |
class TextChoices(models.TextChoices): |
| 469 |
A = "A", "A value" |
| 470 |
B = "B", "B value" |
| 471 |
|
| 472 |
class IntegerChoices(models.IntegerChoices): |
| 473 |
A = 1, "One" |
| 474 |
B = 2, "Two" |
| 475 |
|
| 476 |
class DateChoices(datetime.date, models.Choices): |
| 477 |
DATE_1 = 1969, 7, 20, "First date" |
| 478 |
DATE_2 = 1969, 11, 19, "Second date" |
| 479 |
|
| 480 |
self.assertSerializedResultEqual(TextChoices.A, ("'A'", set())) |
| 481 |
self.assertSerializedResultEqual(IntegerChoices.A, ("1", set())) |
| 482 |
self.assertSerializedResultEqual( |
| 483 |
DateChoices.DATE_1, |
| 484 |
("datetime.date(1969, 7, 20)", {"import datetime"}), |
| 485 |
) |
| 486 |
field = models.CharField(default=TextChoices.B, choices=TextChoices) |
| 487 |
string = MigrationWriter.serialize(field)[0] |
| 488 |
self.assertEqual( |
| 489 |
string, |
| 490 |
"models.CharField(choices=[('A', 'A value'), ('B', 'B value')], " |
| 491 |
"default='B')", |
| 492 |
) |
| 493 |
field = models.IntegerField(default=IntegerChoices.B, choices=IntegerChoices) |
| 494 |
string = MigrationWriter.serialize(field)[0] |
| 495 |
self.assertEqual( |
| 496 |
string, |
| 497 |
"models.IntegerField(choices=[(1, 'One'), (2, 'Two')], default=2)", |
| 498 |
) |
| 499 |
field = models.DateField(default=DateChoices.DATE_2, choices=DateChoices) |
| 500 |
string = MigrationWriter.serialize(field)[0] |
| 501 |
self.assertEqual( |
| 502 |
string, |
| 503 |
"models.DateField(choices=[" |
| 504 |
"(datetime.date(1969, 7, 20), 'First date'), " |
| 505 |
"(datetime.date(1969, 11, 19), 'Second date')], " |
| 506 |
"default=datetime.date(1969, 11, 19))", |
| 507 |
) |
| 508 |
|
| 509 |
def test_serialize_dictionary_choices(self): |
| 510 |
for choices in ({"Group": [(2, "2"), (1, "1")]}, {"Group": {2: "2", 1: "1"}}): |
| 511 |
with self.subTest(choices): |
| 512 |
field = models.IntegerField(choices=choices) |
| 513 |
string = MigrationWriter.serialize(field)[0] |
| 514 |
self.assertEqual( |
| 515 |
string, |
| 516 |
"models.IntegerField(choices=[('Group', [(2, '2'), (1, '1')])])", |
| 517 |
) |
| 518 |
|
| 519 |
def test_serialize_callable_choices(self): |
| 520 |
field = models.IntegerField(choices=get_choices) |
| 521 |
string = MigrationWriter.serialize(field)[0] |
| 522 |
self.assertEqual( |
| 523 |
string, |
| 524 |
"models.IntegerField(choices=migrations.test_writer.get_choices)", |
| 525 |
) |
| 526 |
|
| 527 |
def test_serialize_nested_class(self): |
| 528 |
for nested_cls in [self.NestedEnum, self.NestedChoices]: |
| 529 |
cls_name = nested_cls.__name__ |
| 530 |
with self.subTest(cls_name): |
| 531 |
self.assertSerializedResultEqual( |
| 532 |
nested_cls, |
| 533 |
( |
| 534 |
"migrations.test_writer.WriterTests.%s" % cls_name, |
| 535 |
{"import migrations.test_writer"}, |
| 536 |
), |
| 537 |
) |
| 538 |
|
| 539 |
def test_serialize_nested_class_method(self): |
| 540 |
self.assertSerializedResultEqual( |
| 541 |
self.NestedChoices.method, |
| 542 |
( |
| 543 |
"migrations.test_writer.WriterTests.NestedChoices.method", |
| 544 |
{"import migrations.test_writer"}, |
| 545 |
), |
| 546 |
) |
| 547 |
|
| 548 |
def test_serialize_uuid(self): |
| 549 |
self.assertSerializedEqual(uuid.uuid1()) |
| 550 |
self.assertSerializedEqual(uuid.uuid4()) |
| 551 |
|
| 552 |
uuid_a = uuid.UUID("5c859437-d061-4847-b3f7-e6b78852f8c8") |
| 553 |
uuid_b = uuid.UUID("c7853ec1-2ea3-4359-b02d-b54e8f1bcee2") |
| 554 |
self.assertSerializedResultEqual( |
| 555 |
uuid_a, |
| 556 |
("uuid.UUID('5c859437-d061-4847-b3f7-e6b78852f8c8')", {"import uuid"}), |
| 557 |
) |
| 558 |
self.assertSerializedResultEqual( |
| 559 |
uuid_b, |
| 560 |
("uuid.UUID('c7853ec1-2ea3-4359-b02d-b54e8f1bcee2')", {"import uuid"}), |
| 561 |
) |
| 562 |
|
| 563 |
field = models.UUIDField( |
| 564 |
choices=((uuid_a, "UUID A"), (uuid_b, "UUID B")), default=uuid_a |
| 565 |
) |
| 566 |
string = MigrationWriter.serialize(field)[0] |
| 567 |
self.assertEqual( |
| 568 |
string, |
| 569 |
"models.UUIDField(choices=[" |
| 570 |
"(uuid.UUID('5c859437-d061-4847-b3f7-e6b78852f8c8'), 'UUID A'), " |
| 571 |
"(uuid.UUID('c7853ec1-2ea3-4359-b02d-b54e8f1bcee2'), 'UUID B')], " |
| 572 |
"default=uuid.UUID('5c859437-d061-4847-b3f7-e6b78852f8c8'))", |
| 573 |
) |
| 574 |
|
| 575 |
def test_serialize_pathlib(self): |
| 576 |
# Pure path objects work in all platforms. |
| 577 |
self.assertSerializedEqual(pathlib.PurePosixPath()) |
| 578 |
self.assertSerializedEqual(pathlib.PureWindowsPath()) |
| 579 |
path = pathlib.PurePosixPath("/path/file.txt") |
| 580 |
expected = ("pathlib.PurePosixPath('/path/file.txt')", {"import pathlib"}) |
| 581 |
self.assertSerializedResultEqual(path, expected) |
| 582 |
path = pathlib.PureWindowsPath("A:\\File.txt") |
| 583 |
expected = ("pathlib.PureWindowsPath('A:/File.txt')", {"import pathlib"}) |
| 584 |
self.assertSerializedResultEqual(path, expected) |
| 585 |
# Concrete path objects work on supported platforms. |
| 586 |
if sys.platform == "win32": |
| 587 |
self.assertSerializedEqual(pathlib.WindowsPath.cwd()) |
| 588 |
path = pathlib.WindowsPath("A:\\File.txt") |
| 589 |
expected = ("pathlib.PureWindowsPath('A:/File.txt')", {"import pathlib"}) |
| 590 |
self.assertSerializedResultEqual(path, expected) |
| 591 |
else: |
| 592 |
self.assertSerializedEqual(pathlib.PosixPath.cwd()) |
| 593 |
path = pathlib.PosixPath("/path/file.txt") |
| 594 |
expected = ("pathlib.PurePosixPath('/path/file.txt')", {"import pathlib"}) |
| 595 |
self.assertSerializedResultEqual(path, expected) |
| 596 |
|
| 597 |
field = models.FilePathField(path=pathlib.PurePosixPath("/home/user")) |
| 598 |
string, imports = MigrationWriter.serialize(field) |
| 599 |
self.assertEqual( |
| 600 |
string, |
| 601 |
"models.FilePathField(path=pathlib.PurePosixPath('/home/user'))", |
| 602 |
) |
| 603 |
self.assertIn("import pathlib", imports) |
| 604 |
|
| 605 |
def test_serialize_path_like(self): |
| 606 |
with os.scandir(os.path.dirname(__file__)) as entries: |
| 607 |
path_like = list(entries)[0] |
| 608 |
expected = (repr(path_like.path), {}) |
| 609 |
self.assertSerializedResultEqual(path_like, expected) |
| 610 |
|
| 611 |
field = models.FilePathField(path=path_like) |
| 612 |
string = MigrationWriter.serialize(field)[0] |
| 613 |
self.assertEqual(string, "models.FilePathField(path=%r)" % path_like.path) |
| 614 |
|
| 615 |
def test_serialize_functions(self): |
| 616 |
with self.assertRaisesMessage(ValueError, "Cannot serialize function: lambda"): |
| 617 |
self.assertSerializedEqual(lambda x: 42) |
| 618 |
self.assertSerializedEqual(models.SET_NULL) |
| 619 |
string, imports = MigrationWriter.serialize(models.SET(42)) |
| 620 |
self.assertEqual(string, "models.SET(42)") |
| 621 |
self.serialize_round_trip(models.SET(42)) |
| 622 |
|
| 623 |
def test_serialize_decorated_functions(self): |
| 624 |
self.assertSerializedEqual(function_with_decorator) |
| 625 |
self.assertSerializedEqual(function_with_cache) |
| 626 |
self.assertSerializedEqual(function_with_lru_cache) |
| 627 |
|
| 628 |
def test_serialize_datetime(self): |
| 629 |
self.assertSerializedEqual(datetime.datetime.now()) |
| 630 |
self.assertSerializedEqual(datetime.datetime.now) |
| 631 |
self.assertSerializedEqual(datetime.datetime.today()) |
| 632 |
self.assertSerializedEqual(datetime.datetime.today) |
| 633 |
self.assertSerializedEqual(datetime.date.today()) |
| 634 |
self.assertSerializedEqual(datetime.date.today) |
| 635 |
self.assertSerializedEqual(datetime.datetime.now().time()) |
| 636 |
self.assertSerializedEqual( |
| 637 |
datetime.datetime(2014, 1, 1, 1, 1, tzinfo=get_default_timezone()) |
| 638 |
) |
| 639 |
self.assertSerializedEqual( |
| 640 |
datetime.datetime(2013, 12, 31, 22, 1, tzinfo=get_fixed_timezone(180)) |
| 641 |
) |
| 642 |
self.assertSerializedResultEqual( |
| 643 |
datetime.datetime(2014, 1, 1, 1, 1), |
| 644 |
("datetime.datetime(2014, 1, 1, 1, 1)", {"import datetime"}), |
| 645 |
) |
| 646 |
self.assertSerializedResultEqual( |
| 647 |
datetime.datetime(2012, 1, 1, 1, 1, tzinfo=datetime.timezone.utc), |
| 648 |
( |
| 649 |
"datetime.datetime(2012, 1, 1, 1, 1, tzinfo=datetime.timezone.utc)", |
| 650 |
{"import datetime"}, |
| 651 |
), |
| 652 |
) |
| 653 |
self.assertSerializedResultEqual( |
| 654 |
datetime.datetime( |
| 655 |
2012, 1, 1, 2, 1, tzinfo=zoneinfo.ZoneInfo("Europe/Paris") |
| 656 |
), |
| 657 |
( |
| 658 |
"datetime.datetime(2012, 1, 1, 1, 1, tzinfo=datetime.timezone.utc)", |
| 659 |
{"import datetime"}, |
| 660 |
), |
| 661 |
) |
| 662 |
|
| 663 |
def test_serialize_fields(self): |
| 664 |
self.assertSerializedFieldEqual(models.CharField(max_length=255)) |
| 665 |
self.assertSerializedResultEqual( |
| 666 |
models.CharField(max_length=255), |
| 667 |
("models.CharField(max_length=255)", {"from django.db import models"}), |
| 668 |
) |
| 669 |
self.assertSerializedFieldEqual(models.TextField(null=True, blank=True)) |
| 670 |
self.assertSerializedResultEqual( |
| 671 |
models.TextField(null=True, blank=True), |
| 672 |
( |
| 673 |
"models.TextField(blank=True, null=True)", |
| 674 |
{"from django.db import models"}, |
| 675 |
), |
| 676 |
) |
| 677 |
|
| 678 |
def test_serialize_settings(self): |
| 679 |
self.assertSerializedEqual( |
| 680 |
SettingsReference(settings.AUTH_USER_MODEL, "AUTH_USER_MODEL") |
| 681 |
) |
| 682 |
self.assertSerializedResultEqual( |
| 683 |
SettingsReference("someapp.model", "AUTH_USER_MODEL"), |
| 684 |
("settings.AUTH_USER_MODEL", {"from django.conf import settings"}), |
| 685 |
) |
| 686 |
|
| 687 |
def test_serialize_iterators(self): |
| 688 |
self.assertSerializedResultEqual( |
| 689 |
((x, x * x) for x in range(3)), ("((0, 0), (1, 1), (2, 4))", set()) |
| 690 |
) |
| 691 |
|
| 692 |
def test_serialize_compiled_regex(self): |
| 693 |
""" |
| 694 |
Make sure compiled regex can be serialized. |
| 695 |
""" |
| 696 |
regex = re.compile(r"^\w+$") |
| 697 |
self.assertSerializedEqual(regex) |
| 698 |
|
| 699 |
def test_serialize_class_based_validators(self): |
| 700 |
""" |
| 701 |
Ticket #22943: Test serialization of class-based validators, including |
| 702 |
compiled regexes. |
| 703 |
""" |
| 704 |
validator = RegexValidator(message="hello") |
| 705 |
string = MigrationWriter.serialize(validator)[0] |
| 706 |
self.assertEqual( |
| 707 |
string, "django.core.validators.RegexValidator(message='hello')" |
| 708 |
) |
| 709 |
self.serialize_round_trip(validator) |
| 710 |
|
| 711 |
# Test with a compiled regex. |
| 712 |
validator = RegexValidator(regex=re.compile(r"^\w+$")) |
| 713 |
string = MigrationWriter.serialize(validator)[0] |
| 714 |
self.assertEqual( |
| 715 |
string, |
| 716 |
"django.core.validators.RegexValidator(regex=re.compile('^\\\\w+$'))", |
| 717 |
) |
| 718 |
self.serialize_round_trip(validator) |
| 719 |
|
| 720 |
# Test a string regex with flag |
| 721 |
validator = RegexValidator(r"^[0-9]+$", flags=re.S) |
| 722 |
string = MigrationWriter.serialize(validator)[0] |
| 723 |
self.assertEqual( |
| 724 |
string, |
| 725 |
"django.core.validators.RegexValidator('^[0-9]+$', " |
| 726 |
"flags=re.RegexFlag['DOTALL'])", |
| 727 |
) |
| 728 |
self.serialize_round_trip(validator) |
| 729 |
|
| 730 |
# Test message and code |
| 731 |
validator = RegexValidator("^[-a-zA-Z0-9_]+$", "Invalid", "invalid") |
| 732 |
string = MigrationWriter.serialize(validator)[0] |
| 733 |
self.assertEqual( |
| 734 |
string, |
| 735 |
"django.core.validators.RegexValidator('^[-a-zA-Z0-9_]+$', 'Invalid', " |
| 736 |
"'invalid')", |
| 737 |
) |
| 738 |
self.serialize_round_trip(validator) |
| 739 |
|
| 740 |
# Test with a subclass. |
| 741 |
validator = EmailValidator(message="hello") |
| 742 |
string = MigrationWriter.serialize(validator)[0] |
| 743 |
self.assertEqual( |
| 744 |
string, "django.core.validators.EmailValidator(message='hello')" |
| 745 |
) |
| 746 |
self.serialize_round_trip(validator) |
| 747 |
|
| 748 |
validator = deconstructible(path="migrations.test_writer.EmailValidator")( |
| 749 |
EmailValidator |
| 750 |
)(message="hello") |
| 751 |
string = MigrationWriter.serialize(validator)[0] |
| 752 |
self.assertEqual( |
| 753 |
string, "migrations.test_writer.EmailValidator(message='hello')" |
| 754 |
) |
| 755 |
|
| 756 |
validator = deconstructible(path="custom.EmailValidator")(EmailValidator)( |
| 757 |
message="hello" |
| 758 |
) |
| 759 |
with self.assertRaisesMessage(ImportError, "No module named 'custom'"): |
| 760 |
MigrationWriter.serialize(validator) |
| 761 |
|
| 762 |
validator = deconstructible(path="django.core.validators.EmailValidator2")( |
| 763 |
EmailValidator |
| 764 |
)(message="hello") |
| 765 |
with self.assertRaisesMessage( |
| 766 |
ValueError, |
| 767 |
"Could not find object EmailValidator2 in django.core.validators.", |
| 768 |
): |
| 769 |
MigrationWriter.serialize(validator) |
| 770 |
|
| 771 |
def test_serialize_complex_func_index(self): |
| 772 |
index = models.Index( |
| 773 |
models.Func("rating", function="ABS"), |
| 774 |
models.Case( |
| 775 |
models.When(name="special", then=models.Value("X")), |
| 776 |
default=models.Value("other"), |
| 777 |
), |
| 778 |
models.ExpressionWrapper( |
| 779 |
models.F("pages"), |
| 780 |
output_field=models.IntegerField(), |
| 781 |
), |
| 782 |
models.OrderBy(models.F("name").desc()), |
| 783 |
name="complex_func_index", |
| 784 |
) |
| 785 |
string, imports = MigrationWriter.serialize(index) |
| 786 |
self.assertEqual( |
| 787 |
string, |
| 788 |
"models.Index(models.Func('rating', function='ABS'), " |
| 789 |
"models.Case(models.When(name='special', then=models.Value('X')), " |
| 790 |
"default=models.Value('other')), " |
| 791 |
"models.ExpressionWrapper(" |
| 792 |
"models.F('pages'), output_field=models.IntegerField()), " |
| 793 |
"models.OrderBy(models.OrderBy(models.F('name'), descending=True)), " |
| 794 |
"name='complex_func_index')", |
| 795 |
) |
| 796 |
self.assertEqual(imports, {"from django.db import models"}) |
| 797 |
|
| 798 |
def test_serialize_empty_nonempty_tuple(self): |
| 799 |
""" |
| 800 |
Ticket #22679: makemigrations generates invalid code for (an empty |
| 801 |
tuple) default_permissions = () |
| 802 |
""" |
| 803 |
empty_tuple = () |
| 804 |
one_item_tuple = ("a",) |
| 805 |
many_items_tuple = ("a", "b", "c") |
| 806 |
self.assertSerializedEqual(empty_tuple) |
| 807 |
self.assertSerializedEqual(one_item_tuple) |
| 808 |
self.assertSerializedEqual(many_items_tuple) |
| 809 |
|
| 810 |
def test_serialize_range(self): |
| 811 |
string, imports = MigrationWriter.serialize(range(1, 5)) |
| 812 |
self.assertEqual(string, "range(1, 5)") |
| 813 |
self.assertEqual(imports, set()) |
| 814 |
|
| 815 |
def test_serialize_builtins(self): |
| 816 |
string, imports = MigrationWriter.serialize(range) |
| 817 |
self.assertEqual(string, "range") |
| 818 |
self.assertEqual(imports, set()) |
| 819 |
|
| 820 |
def test_serialize_unbound_method_reference(self): |
| 821 |
"""An unbound method used within a class body can be serialized.""" |
| 822 |
self.serialize_round_trip(TestModel1.thing) |
| 823 |
|
| 824 |
def test_serialize_local_function_reference(self): |
| 825 |
"""A reference in a local scope can't be serialized.""" |
| 826 |
|
| 827 |
class TestModel2: |
| 828 |
def upload_to(self): |
| 829 |
return "somewhere dynamic" |
| 830 |
|
| 831 |
thing = models.FileField(upload_to=upload_to) |
| 832 |
|
| 833 |
with self.assertRaisesMessage( |
| 834 |
ValueError, "Could not find function upload_to in migrations.test_writer" |
| 835 |
): |
| 836 |
self.serialize_round_trip(TestModel2.thing) |
| 837 |
|
| 838 |
def test_serialize_managers(self): |
| 839 |
self.assertSerializedEqual(models.Manager()) |
| 840 |
self.assertSerializedResultEqual( |
| 841 |
FoodQuerySet.as_manager(), |
| 842 |
( |
| 843 |
"migrations.models.FoodQuerySet.as_manager()", |
| 844 |
{"import migrations.models"}, |
| 845 |
), |
| 846 |
) |
| 847 |
self.assertSerializedEqual(FoodManager("a", "b")) |
| 848 |
self.assertSerializedEqual(FoodManager("x", "y", c=3, d=4)) |
| 849 |
|
| 850 |
def test_serialize_frozensets(self): |
| 851 |
self.assertSerializedEqual(frozenset()) |
| 852 |
self.assertSerializedEqual(frozenset("let it go")) |
| 853 |
self.assertSerializedResultEqual( |
| 854 |
frozenset("cba"), ("frozenset(['a', 'b', 'c'])", set()) |
| 855 |
) |
| 856 |
|
| 857 |
def test_serialize_set(self): |
| 858 |
self.assertSerializedEqual(set()) |
| 859 |
self.assertSerializedResultEqual(set(), ("set()", set())) |
| 860 |
self.assertSerializedEqual({"a"}) |
| 861 |
self.assertSerializedResultEqual({"a"}, ("{'a'}", set())) |
| 862 |
self.assertSerializedEqual({"c", "b", "a"}) |
| 863 |
self.assertSerializedResultEqual({"c", "b", "a"}, ("{'a', 'b', 'c'}", set())) |
| 864 |
|
| 865 |
def test_serialize_timedelta(self): |
| 866 |
self.assertSerializedEqual(datetime.timedelta()) |
| 867 |
self.assertSerializedEqual(datetime.timedelta(minutes=42)) |
| 868 |
|
| 869 |
def test_serialize_functools_partial(self): |
| 870 |
value = functools.partial(datetime.timedelta, 1, seconds=2) |
| 871 |
result = self.serialize_round_trip(value) |
| 872 |
self.assertEqual(result.func, value.func) |
| 873 |
self.assertEqual(result.args, value.args) |
| 874 |
self.assertEqual(result.keywords, value.keywords) |
| 875 |
|
| 876 |
def test_serialize_functools_partialmethod(self): |
| 877 |
value = functools.partialmethod(datetime.timedelta, 1, seconds=2) |
| 878 |
result = self.serialize_round_trip(value) |
| 879 |
self.assertIsInstance(result, functools.partialmethod) |
| 880 |
self.assertEqual(result.func, value.func) |
| 881 |
self.assertEqual(result.args, value.args) |
| 882 |
self.assertEqual(result.keywords, value.keywords) |
| 883 |
|
| 884 |
def test_serialize_type_none(self): |
| 885 |
self.assertSerializedEqual(NoneType) |
| 886 |
|
| 887 |
def test_serialize_type_model(self): |
| 888 |
self.assertSerializedEqual(models.Model) |
| 889 |
self.assertSerializedResultEqual( |
| 890 |
MigrationWriter.serialize(models.Model), |
| 891 |
("('models.Model', {'from django.db import models'})", set()), |
| 892 |
) |
| 893 |
|
| 894 |
def test_simple_migration(self): |
| 895 |
""" |
| 896 |
Tests serializing a simple migration. |
| 897 |
""" |
| 898 |
fields = { |
| 899 |
"charfield": models.DateTimeField(default=datetime.datetime.now), |
| 900 |
"datetimefield": models.DateTimeField(default=datetime.datetime.now), |
| 901 |
} |
| 902 |
|
| 903 |
options = { |
| 904 |
"verbose_name": "My model", |
| 905 |
"verbose_name_plural": "My models", |
| 906 |
} |
| 907 |
|
| 908 |
migration = type( |
| 909 |
"Migration", |
| 910 |
(migrations.Migration,), |
| 911 |
{ |
| 912 |
"operations": [ |
| 913 |
migrations.CreateModel( |
| 914 |
"MyModel", tuple(fields.items()), options, (models.Model,) |
| 915 |
), |
| 916 |
migrations.CreateModel( |
| 917 |
"MyModel2", tuple(fields.items()), bases=(models.Model,) |
| 918 |
), |
| 919 |
migrations.CreateModel( |
| 920 |
name="MyModel3", |
| 921 |
fields=tuple(fields.items()), |
| 922 |
options=options, |
| 923 |
bases=(models.Model,), |
| 924 |
), |
| 925 |
migrations.DeleteModel("MyModel"), |
| 926 |
migrations.AddField( |
| 927 |
"OtherModel", "datetimefield", fields["datetimefield"] |
| 928 |
), |
| 929 |
], |
| 930 |
"dependencies": [("testapp", "some_other_one")], |
| 931 |
}, |
| 932 |
) |
| 933 |
writer = MigrationWriter(migration) |
| 934 |
output = writer.as_string() |
| 935 |
# We don't test the output formatting - that's too fragile. |
| 936 |
# Just make sure it runs for now, and that things look alright. |
| 937 |
result = self.safe_exec(output) |
| 938 |
self.assertIn("Migration", result) |
| 939 |
|
| 940 |
def test_migration_path(self): |
| 941 |
test_apps = [ |
| 942 |
"migrations.migrations_test_apps.normal", |
| 943 |
"migrations.migrations_test_apps.with_package_model", |
| 944 |
"migrations.migrations_test_apps.without_init_file", |
| 945 |
] |
| 946 |
|
| 947 |
base_dir = os.path.dirname(os.path.dirname(__file__)) |
| 948 |
|
| 949 |
for app in test_apps: |
| 950 |
with self.modify_settings(INSTALLED_APPS={"append": app}): |
| 951 |
migration = migrations.Migration("0001_initial", app.split(".")[-1]) |
| 952 |
expected_path = os.path.join( |
| 953 |
base_dir, *(app.split(".") + ["migrations", "0001_initial.py"]) |
| 954 |
) |
| 955 |
writer = MigrationWriter(migration) |
| 956 |
self.assertEqual(writer.path, expected_path) |
| 957 |
|
| 958 |
@override_settings( |
| 959 |
MIGRATION_MODULES={"namespace_app": "namespace_app.migrations"}, |
| 960 |
INSTALLED_APPS=[ |
| 961 |
"migrations.migrations_test_apps.distributed_app_location_2.namespace_app" |
| 962 |
], |
| 963 |
) |
| 964 |
def test_migration_path_distributed_namespace(self): |
| 965 |
base_dir = os.path.dirname(os.path.dirname(__file__)) |
| 966 |
test_apps_dir = os.path.join(base_dir, "migrations", "migrations_test_apps") |
| 967 |
expected_msg = ( |
| 968 |
"Could not locate an appropriate location to create " |
| 969 |
"migrations package namespace_app.migrations. Make sure the toplevel " |
| 970 |
"package exists and can be imported." |
| 971 |
) |
| 972 |
with extend_sys_path( |
| 973 |
os.path.join(test_apps_dir, "distributed_app_location_1"), |
| 974 |
os.path.join(test_apps_dir, "distributed_app_location_2"), |
| 975 |
): |
| 976 |
migration = migrations.Migration("0001_initial", "namespace_app") |
| 977 |
writer = MigrationWriter(migration) |
| 978 |
with self.assertRaisesMessage(ValueError, expected_msg): |
| 979 |
writer.path |
| 980 |
|
| 981 |
def test_custom_operation(self): |
| 982 |
migration = type( |
| 983 |
"Migration", |
| 984 |
(migrations.Migration,), |
| 985 |
{ |
| 986 |
"operations": [ |
| 987 |
custom_migration_operations.operations.TestOperation(), |
| 988 |
custom_migration_operations.operations.CreateModel(), |
| 989 |
migrations.CreateModel("MyModel", (), {}, (models.Model,)), |
| 990 |
custom_migration_operations.more_operations.TestOperation(), |
| 991 |
], |
| 992 |
"dependencies": [], |
| 993 |
}, |
| 994 |
) |
| 995 |
writer = MigrationWriter(migration) |
| 996 |
output = writer.as_string() |
| 997 |
result = self.safe_exec(output) |
| 998 |
self.assertIn("custom_migration_operations", result) |
| 999 |
self.assertNotEqual( |
| 1000 |
result["custom_migration_operations"].operations.TestOperation, |
| 1001 |
result["custom_migration_operations"].more_operations.TestOperation, |
| 1002 |
) |
| 1003 |
|
| 1004 |
def test_sorted_dependencies(self): |
| 1005 |
migration = type( |
| 1006 |
"Migration", |
| 1007 |
(migrations.Migration,), |
| 1008 |
{ |
| 1009 |
"operations": [ |
| 1010 |
migrations.AddField("mymodel", "myfield", models.IntegerField()), |
| 1011 |
], |
| 1012 |
"dependencies": [ |
| 1013 |
("testapp10", "0005_fifth"), |
| 1014 |
("testapp02", "0005_third"), |
| 1015 |
("testapp02", "0004_sixth"), |
| 1016 |
("testapp01", "0001_initial"), |
| 1017 |
], |
| 1018 |
}, |
| 1019 |
) |
| 1020 |
output = MigrationWriter(migration, include_header=False).as_string() |
| 1021 |
self.assertIn( |
| 1022 |
" dependencies = [\n" |
| 1023 |
" ('testapp01', '0001_initial'),\n" |
| 1024 |
" ('testapp02', '0004_sixth'),\n" |
| 1025 |
" ('testapp02', '0005_third'),\n" |
| 1026 |
" ('testapp10', '0005_fifth'),\n" |
| 1027 |
" ]", |
| 1028 |
output, |
| 1029 |
) |
| 1030 |
|
| 1031 |
def test_sorted_imports(self): |
| 1032 |
""" |
| 1033 |
#24155 - Tests ordering of imports. |
| 1034 |
""" |
| 1035 |
migration = type( |
| 1036 |
"Migration", |
| 1037 |
(migrations.Migration,), |
| 1038 |
{ |
| 1039 |
"operations": [ |
| 1040 |
migrations.AddField( |
| 1041 |
"mymodel", |
| 1042 |
"myfield", |
| 1043 |
models.DateTimeField( |
| 1044 |
default=datetime.datetime( |
| 1045 |
2012, 1, 1, 1, 1, tzinfo=datetime.timezone.utc |
| 1046 |
), |
| 1047 |
), |
| 1048 |
), |
| 1049 |
migrations.AddField( |
| 1050 |
"mymodel", |
| 1051 |
"myfield2", |
| 1052 |
models.FloatField(default=time.time), |
| 1053 |
), |
| 1054 |
] |
| 1055 |
}, |
| 1056 |
) |
| 1057 |
writer = MigrationWriter(migration) |
| 1058 |
output = writer.as_string() |
| 1059 |
self.assertIn( |
| 1060 |
"import datetime\nimport time\nfrom django.db import migrations, models\n", |
| 1061 |
output, |
| 1062 |
) |
| 1063 |
|
| 1064 |
def test_migration_file_header_comments(self): |
| 1065 |
""" |
| 1066 |
Test comments at top of file. |
| 1067 |
""" |
| 1068 |
migration = type("Migration", (migrations.Migration,), {"operations": []}) |
| 1069 |
dt = datetime.datetime(2015, 7, 31, 4, 40, 0, 0, tzinfo=datetime.timezone.utc) |
| 1070 |
with mock.patch("django.db.migrations.writer.now", lambda: dt): |
| 1071 |
for include_header in (True, False): |
| 1072 |
with self.subTest(include_header=include_header): |
| 1073 |
writer = MigrationWriter(migration, include_header) |
| 1074 |
output = writer.as_string() |
| 1075 |
|
| 1076 |
self.assertEqual( |
| 1077 |
include_header, |
| 1078 |
output.startswith( |
| 1079 |
"# Generated by Django %s on 2015-07-31 04:40\n\n" |
| 1080 |
% get_version() |
| 1081 |
), |
| 1082 |
) |
| 1083 |
if not include_header: |
| 1084 |
# Make sure the output starts with something that's not |
| 1085 |
# a comment or indentation or blank line |
| 1086 |
self.assertRegex( |
| 1087 |
output.splitlines(keepends=True)[0], r"^[^#\s]+" |
| 1088 |
) |
| 1089 |
|
| 1090 |
def test_models_import_omitted(self): |
| 1091 |
""" |
| 1092 |
django.db.models shouldn't be imported if unused. |
| 1093 |
""" |
| 1094 |
migration = type( |
| 1095 |
"Migration", |
| 1096 |
(migrations.Migration,), |
| 1097 |
{ |
| 1098 |
"operations": [ |
| 1099 |
migrations.AlterModelOptions( |
| 1100 |
name="model", |
| 1101 |
options={ |
| 1102 |
"verbose_name": "model", |
| 1103 |
"verbose_name_plural": "models", |
| 1104 |
}, |
| 1105 |
), |
| 1106 |
] |
| 1107 |
}, |
| 1108 |
) |
| 1109 |
writer = MigrationWriter(migration) |
| 1110 |
output = writer.as_string() |
| 1111 |
self.assertIn("from django.db import migrations\n", output) |
| 1112 |
|
| 1113 |
def test_deconstruct_class_arguments(self): |
| 1114 |
# Yes, it doesn't make sense to use a class as a default for a |
| 1115 |
# CharField. It does make sense for custom fields though, for example |
| 1116 |
# an enumfield that takes the enum class as an argument. |
| 1117 |
string = MigrationWriter.serialize( |
| 1118 |
models.CharField(default=DeconstructibleInstances) |
| 1119 |
)[0] |
| 1120 |
self.assertEqual( |
| 1121 |
string, |
| 1122 |
"models.CharField(default=migrations.test_writer.DeconstructibleInstances)", |
| 1123 |
) |
| 1124 |
|
| 1125 |
def test_register_serializer(self): |
| 1126 |
class ComplexSerializer(BaseSerializer): |
| 1127 |
def serialize(self): |
| 1128 |
return "complex(%r)" % self.value, {} |
| 1129 |
|
| 1130 |
MigrationWriter.register_serializer(complex, ComplexSerializer) |
| 1131 |
self.assertSerializedEqual(complex(1, 2)) |
| 1132 |
MigrationWriter.unregister_serializer(complex) |
| 1133 |
with self.assertRaisesMessage(ValueError, "Cannot serialize: (1+2j)"): |
| 1134 |
self.assertSerializedEqual(complex(1, 2)) |
| 1135 |
|
| 1136 |
def test_register_non_serializer(self): |
| 1137 |
with self.assertRaisesMessage( |
| 1138 |
ValueError, "'TestModel1' must inherit from 'BaseSerializer'." |
| 1139 |
): |
| 1140 |
MigrationWriter.register_serializer(complex, TestModel1) |
| 1141 |
|
| 1142 |
def test_composite_pk_import(self): |
| 1143 |
migration = type( |
| 1144 |
"Migration", |
| 1145 |
(migrations.Migration,), |
| 1146 |
{ |
| 1147 |
"operations": [ |
| 1148 |
migrations.AddField( |
| 1149 |
"foo", |
| 1150 |
"bar", |
| 1151 |
models.CompositePrimaryKey("foo_id", "bar_id"), |
| 1152 |
), |
| 1153 |
], |
| 1154 |
}, |
| 1155 |
) |
| 1156 |
writer = MigrationWriter(migration) |
| 1157 |
output = writer.as_string() |
| 1158 |
self.assertEqual(output.count("import"), 1) |
| 1159 |
self.assertIn("from django.db import migrations, models", output) |