2014-11-19 21:08:57 +00:00
|
|
|
import os
|
2015-02-22 20:50:16 +00:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
from contextlib import contextmanager
|
|
|
|
from importlib import import_module
|
2014-11-19 21:08:57 +00:00
|
|
|
|
2024-11-29 12:04:48 +00:00
|
|
|
from user_commands.utils import AssertFormatterFailureCaughtContext
|
|
|
|
|
2015-02-22 20:50:16 +00:00
|
|
|
from django.apps import apps
|
2024-11-29 12:04:48 +00:00
|
|
|
from django.core.management import call_command
|
2019-07-25 11:43:23 +00:00
|
|
|
from django.db import connection, connections, migrations, models
|
|
|
|
from django.db.migrations.migration import Migration
|
2024-08-22 18:26:42 +00:00
|
|
|
from django.db.migrations.optimizer import MigrationOptimizer
|
2015-06-02 20:19:23 +00:00
|
|
|
from django.db.migrations.recorder import MigrationRecorder
|
2024-08-22 18:26:42 +00:00
|
|
|
from django.db.migrations.serializer import serializer_factory
|
2019-07-25 11:43:23 +00:00
|
|
|
from django.db.migrations.state import ProjectState
|
2024-08-22 18:26:42 +00:00
|
|
|
from django.test import SimpleTestCase, TransactionTestCase
|
2015-02-22 20:50:16 +00:00
|
|
|
from django.test.utils import extend_sys_path
|
|
|
|
from django.utils.module_loading import module_dir
|
2013-07-25 12:52:35 +00:00
|
|
|
|
|
|
|
|
2013-08-23 11:07:43 +00:00
|
|
|
class MigrationTestBase(TransactionTestCase):
|
2013-07-25 12:52:35 +00:00
|
|
|
"""
|
|
|
|
Contains an extended set of asserts for testing migrations and schema operations.
|
|
|
|
"""
|
|
|
|
|
2013-08-23 11:07:43 +00:00
|
|
|
available_apps = ["migrations"]
|
2022-02-03 19:24:19 +00:00
|
|
|
databases = {"default", "other"}
|
2013-08-23 11:07:43 +00:00
|
|
|
|
2015-06-02 20:19:23 +00:00
|
|
|
def tearDown(self):
|
|
|
|
# Reset applied-migrations state.
|
2019-12-20 19:49:56 +00:00
|
|
|
for db in self.databases:
|
2016-01-25 00:23:38 +00:00
|
|
|
recorder = MigrationRecorder(connections[db])
|
2022-02-03 19:24:19 +00:00
|
|
|
recorder.migration_qs.filter(app="migrations").delete()
|
2015-06-02 20:19:23 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def get_table_description(self, table, using="default"):
|
2016-01-25 00:23:38 +00:00
|
|
|
with connections[using].cursor() as cursor:
|
|
|
|
return connections[using].introspection.get_table_description(cursor, table)
|
2014-01-09 15:05:15 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def assertTableExists(self, table, using="default"):
|
2016-01-25 00:23:38 +00:00
|
|
|
with connections[using].cursor() as cursor:
|
|
|
|
self.assertIn(table, connections[using].introspection.table_names(cursor))
|
2013-07-25 12:52:35 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def assertTableNotExists(self, table, using="default"):
|
2016-01-25 00:23:38 +00:00
|
|
|
with connections[using].cursor() as cursor:
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertNotIn(
|
|
|
|
table, connections[using].introspection.table_names(cursor)
|
|
|
|
)
|
2013-07-25 12:52:35 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def assertColumnExists(self, table, column, using="default"):
|
|
|
|
self.assertIn(
|
|
|
|
column, [c.name for c in self.get_table_description(table, using=using)]
|
|
|
|
)
|
2013-07-25 12:52:35 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def assertColumnNotExists(self, table, column, using="default"):
|
|
|
|
self.assertNotIn(
|
|
|
|
column, [c.name for c in self.get_table_description(table, using=using)]
|
|
|
|
)
|
2013-07-25 12:52:35 +00:00
|
|
|
|
2016-01-25 00:23:38 +00:00
|
|
|
def _get_column_allows_null(self, table, column, using):
|
2022-02-03 19:24:19 +00:00
|
|
|
return [
|
|
|
|
c.null_ok
|
|
|
|
for c in self.get_table_description(table, using=using)
|
|
|
|
if c.name == column
|
|
|
|
][0]
|
2013-07-25 12:52:35 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def assertColumnNull(self, table, column, using="default"):
|
2019-10-21 08:55:05 +00:00
|
|
|
self.assertTrue(self._get_column_allows_null(table, column, using))
|
2013-07-25 12:52:35 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def assertColumnNotNull(self, table, column, using="default"):
|
2019-10-21 08:55:05 +00:00
|
|
|
self.assertFalse(self._get_column_allows_null(table, column, using))
|
2016-01-25 00:23:38 +00:00
|
|
|
|
2022-04-28 15:23:15 +00:00
|
|
|
def _get_column_collation(self, table, column, using):
|
|
|
|
return next(
|
|
|
|
f.collation
|
|
|
|
for f in self.get_table_description(table, using=using)
|
|
|
|
if f.name == column
|
|
|
|
)
|
|
|
|
|
|
|
|
def assertColumnCollation(self, table, column, collation, using="default"):
|
|
|
|
self.assertEqual(self._get_column_collation(table, column, using), collation)
|
|
|
|
|
2022-10-16 05:59:39 +00:00
|
|
|
def _get_table_comment(self, table, using):
|
|
|
|
with connections[using].cursor() as cursor:
|
|
|
|
return next(
|
|
|
|
t.comment
|
|
|
|
for t in connections[using].introspection.get_table_list(cursor)
|
|
|
|
if t.name == table
|
|
|
|
)
|
|
|
|
|
|
|
|
def assertTableComment(self, table, comment, using="default"):
|
|
|
|
self.assertEqual(self._get_table_comment(table, using), comment)
|
|
|
|
|
|
|
|
def assertTableCommentNotExists(self, table, using="default"):
|
|
|
|
self.assertIn(self._get_table_comment(table, using), [None, ""])
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def assertIndexExists(
|
|
|
|
self, table, columns, value=True, using="default", index_type=None
|
|
|
|
):
|
2016-01-25 00:23:38 +00:00
|
|
|
with connections[using].cursor() as cursor:
|
2014-01-09 15:05:15 +00:00
|
|
|
self.assertEqual(
|
|
|
|
value,
|
|
|
|
any(
|
|
|
|
c["index"]
|
2022-02-03 19:24:19 +00:00
|
|
|
for c in connections[using]
|
|
|
|
.introspection.get_constraints(cursor, table)
|
|
|
|
.values()
|
2020-04-12 21:59:34 +00:00
|
|
|
if (
|
2022-02-03 19:24:19 +00:00
|
|
|
c["columns"] == list(columns)
|
2024-08-01 07:21:07 +00:00
|
|
|
and (index_type is None or c["type"] == index_type)
|
2022-02-03 19:24:19 +00:00
|
|
|
and not c["unique"]
|
2020-04-12 21:59:34 +00:00
|
|
|
)
|
2014-01-09 15:05:15 +00:00
|
|
|
),
|
|
|
|
)
|
2013-07-25 12:52:35 +00:00
|
|
|
|
|
|
|
def assertIndexNotExists(self, table, columns):
|
|
|
|
return self.assertIndexExists(table, columns, False)
|
2014-06-08 01:17:31 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def assertIndexNameExists(self, table, index, using="default"):
|
2019-10-10 18:04:17 +00:00
|
|
|
with connections[using].cursor() as cursor:
|
|
|
|
self.assertIn(
|
|
|
|
index,
|
|
|
|
connection.introspection.get_constraints(cursor, table),
|
|
|
|
)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def assertIndexNameNotExists(self, table, index, using="default"):
|
2019-10-10 18:04:17 +00:00
|
|
|
with connections[using].cursor() as cursor:
|
|
|
|
self.assertNotIn(
|
|
|
|
index,
|
|
|
|
connection.introspection.get_constraints(cursor, table),
|
|
|
|
)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def assertConstraintExists(self, table, name, value=True, using="default"):
|
2016-11-05 13:12:12 +00:00
|
|
|
with connections[using].cursor() as cursor:
|
2022-02-03 19:24:19 +00:00
|
|
|
constraints = (
|
|
|
|
connections[using].introspection.get_constraints(cursor, table).items()
|
|
|
|
)
|
2016-11-05 13:12:12 +00:00
|
|
|
self.assertEqual(
|
|
|
|
value,
|
2022-02-03 19:24:19 +00:00
|
|
|
any(c["check"] for n, c in constraints if n == name),
|
2016-11-05 13:12:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def assertConstraintNotExists(self, table, name):
|
|
|
|
return self.assertConstraintExists(table, name, False)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def assertUniqueConstraintExists(self, table, columns, value=True, using="default"):
|
2020-04-12 21:59:34 +00:00
|
|
|
with connections[using].cursor() as cursor:
|
2022-02-03 19:24:19 +00:00
|
|
|
constraints = (
|
|
|
|
connections[using].introspection.get_constraints(cursor, table).values()
|
|
|
|
)
|
2020-04-12 21:59:34 +00:00
|
|
|
self.assertEqual(
|
|
|
|
value,
|
2022-02-03 19:24:19 +00:00
|
|
|
any(c["unique"] for c in constraints if c["columns"] == list(columns)),
|
2020-04-12 21:59:34 +00:00
|
|
|
)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def assertFKExists(self, table, columns, to, value=True, using="default"):
|
2022-04-13 09:52:40 +00:00
|
|
|
if not connections[using].features.can_introspect_foreign_keys:
|
|
|
|
return
|
2016-01-25 00:23:38 +00:00
|
|
|
with connections[using].cursor() as cursor:
|
2014-06-08 01:17:31 +00:00
|
|
|
self.assertEqual(
|
|
|
|
value,
|
|
|
|
any(
|
|
|
|
c["foreign_key"] == to
|
2022-02-03 19:24:19 +00:00
|
|
|
for c in connections[using]
|
|
|
|
.introspection.get_constraints(cursor, table)
|
|
|
|
.values()
|
|
|
|
if c["columns"] == list(columns)
|
2014-06-08 01:17:31 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2017-01-25 14:14:05 +00:00
|
|
|
def assertFKNotExists(self, table, columns, to):
|
2014-06-08 01:17:31 +00:00
|
|
|
return self.assertFKExists(table, columns, to, False)
|
2015-02-22 20:50:16 +00:00
|
|
|
|
2024-11-29 12:04:48 +00:00
|
|
|
def assertFormatterFailureCaught(
|
|
|
|
self, *args, module="migrations.test_migrations", **kwargs
|
|
|
|
):
|
|
|
|
with (
|
|
|
|
self.temporary_migration_module(module=module),
|
|
|
|
AssertFormatterFailureCaughtContext(self) as ctx,
|
|
|
|
):
|
|
|
|
call_command(*args, stdout=ctx.stdout, stderr=ctx.stderr, **kwargs)
|
|
|
|
|
2015-02-22 20:50:16 +00:00
|
|
|
@contextmanager
|
2022-02-03 19:24:19 +00:00
|
|
|
def temporary_migration_module(self, app_label="migrations", module=None):
|
2015-02-22 20:50:16 +00:00
|
|
|
"""
|
|
|
|
Allows testing management commands in a temporary migrations module.
|
|
|
|
|
|
|
|
Wrap all invocations to makemigrations and squashmigrations with this
|
|
|
|
context manager in order to avoid creating migration files in your
|
|
|
|
source tree inadvertently.
|
|
|
|
|
|
|
|
Takes the application label that will be passed to makemigrations or
|
|
|
|
squashmigrations and the Python path to a migrations module.
|
|
|
|
|
|
|
|
The migrations module is used as a template for creating the temporary
|
|
|
|
migrations module. If it isn't provided, the application's migrations
|
|
|
|
module is used, if it exists.
|
|
|
|
|
|
|
|
Returns the filesystem path to the temporary migrations module.
|
|
|
|
"""
|
2017-01-26 18:54:16 +00:00
|
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
2015-02-22 20:50:16 +00:00
|
|
|
target_dir = tempfile.mkdtemp(dir=temp_dir)
|
2022-02-03 19:24:19 +00:00
|
|
|
with open(os.path.join(target_dir, "__init__.py"), "w"):
|
2015-02-22 20:50:16 +00:00
|
|
|
pass
|
2022-02-03 19:24:19 +00:00
|
|
|
target_migrations_dir = os.path.join(target_dir, "migrations")
|
2015-02-22 20:50:16 +00:00
|
|
|
|
|
|
|
if module is None:
|
2022-02-03 19:24:19 +00:00
|
|
|
module = apps.get_app_config(app_label).name + ".migrations"
|
2015-02-22 20:50:16 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
source_migrations_dir = module_dir(import_module(module))
|
|
|
|
except (ImportError, ValueError):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
shutil.copytree(source_migrations_dir, target_migrations_dir)
|
|
|
|
|
|
|
|
with extend_sys_path(temp_dir):
|
2022-02-03 19:24:19 +00:00
|
|
|
new_module = os.path.basename(target_dir) + ".migrations"
|
2015-02-22 20:50:16 +00:00
|
|
|
with self.settings(MIGRATION_MODULES={app_label: new_module}):
|
|
|
|
yield target_migrations_dir
|
2019-07-25 11:43:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OperationTestBase(MigrationTestBase):
|
|
|
|
"""Common functions to help test operations."""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super().setUpClass()
|
|
|
|
cls._initial_table_names = frozenset(connection.introspection.table_names())
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.cleanup_test_tables()
|
|
|
|
super().tearDown()
|
|
|
|
|
|
|
|
def cleanup_test_tables(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
table_names = (
|
|
|
|
frozenset(connection.introspection.table_names())
|
|
|
|
- self._initial_table_names
|
|
|
|
)
|
2019-07-25 11:43:23 +00:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
with connection.constraint_checks_disabled():
|
|
|
|
for table_name in table_names:
|
2022-02-03 19:24:19 +00:00
|
|
|
editor.execute(
|
|
|
|
editor.sql_delete_table
|
|
|
|
% {
|
|
|
|
"table": editor.quote_name(table_name),
|
|
|
|
}
|
|
|
|
)
|
2019-07-25 11:43:23 +00:00
|
|
|
|
|
|
|
def apply_operations(self, app_label, project_state, operations, atomic=True):
|
2022-02-03 19:24:19 +00:00
|
|
|
migration = Migration("name", app_label)
|
2019-07-25 11:43:23 +00:00
|
|
|
migration.operations = operations
|
|
|
|
with connection.schema_editor(atomic=atomic) as editor:
|
|
|
|
return migration.apply(project_state, editor)
|
|
|
|
|
|
|
|
def unapply_operations(self, app_label, project_state, operations, atomic=True):
|
2022-02-03 19:24:19 +00:00
|
|
|
migration = Migration("name", app_label)
|
2019-07-25 11:43:23 +00:00
|
|
|
migration.operations = operations
|
|
|
|
with connection.schema_editor(atomic=atomic) as editor:
|
|
|
|
return migration.unapply(project_state, editor)
|
|
|
|
|
|
|
|
def make_test_state(self, app_label, operation, **kwargs):
|
|
|
|
"""
|
|
|
|
Makes a test state using set_up_test_model and returns the
|
|
|
|
original state and the state after the migration is applied.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model(app_label, **kwargs)
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards(app_label, new_state)
|
|
|
|
return project_state, new_state
|
|
|
|
|
|
|
|
def set_up_test_model(
|
2022-02-03 19:24:19 +00:00
|
|
|
self,
|
|
|
|
app_label,
|
|
|
|
second_model=False,
|
|
|
|
third_model=False,
|
|
|
|
index=False,
|
|
|
|
multicol_index=False,
|
|
|
|
related_model=False,
|
|
|
|
mti_model=False,
|
|
|
|
proxy_model=False,
|
|
|
|
manager_model=False,
|
|
|
|
unique_together=False,
|
|
|
|
options=False,
|
|
|
|
db_table=None,
|
|
|
|
constraints=None,
|
2019-10-10 18:04:17 +00:00
|
|
|
indexes=None,
|
2019-07-25 11:43:23 +00:00
|
|
|
):
|
|
|
|
"""Creates a test model state and database table."""
|
|
|
|
# Make the "current" state.
|
|
|
|
model_options = {
|
2022-02-03 19:24:19 +00:00
|
|
|
"swappable": "TEST_SWAP_MODEL",
|
|
|
|
"unique_together": [["pink", "weight"]] if unique_together else [],
|
2019-07-25 11:43:23 +00:00
|
|
|
}
|
|
|
|
if options:
|
2022-02-03 19:24:19 +00:00
|
|
|
model_options["permissions"] = [("can_groom", "Can groom")]
|
2019-07-25 11:43:23 +00:00
|
|
|
if db_table:
|
2022-02-03 19:24:19 +00:00
|
|
|
model_options["db_table"] = db_table
|
|
|
|
operations = [
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
("pink", models.IntegerField(default=3)),
|
|
|
|
("weight", models.FloatField()),
|
2020-11-22 22:27:57 +00:00
|
|
|
("green", models.IntegerField(null=True)),
|
|
|
|
(
|
|
|
|
"yellow",
|
|
|
|
models.CharField(
|
|
|
|
blank=True, null=True, db_default="Yellow", max_length=20
|
|
|
|
),
|
|
|
|
),
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
|
|
|
options=model_options,
|
|
|
|
)
|
|
|
|
]
|
2019-07-25 11:43:23 +00:00
|
|
|
if index:
|
2022-02-03 19:24:19 +00:00
|
|
|
operations.append(
|
|
|
|
migrations.AddIndex(
|
|
|
|
"Pony",
|
|
|
|
models.Index(fields=["pink"], name="pony_pink_idx"),
|
|
|
|
)
|
|
|
|
)
|
2019-07-25 11:43:23 +00:00
|
|
|
if multicol_index:
|
2022-02-03 19:24:19 +00:00
|
|
|
operations.append(
|
|
|
|
migrations.AddIndex(
|
|
|
|
"Pony",
|
|
|
|
models.Index(fields=["pink", "weight"], name="pony_test_idx"),
|
|
|
|
)
|
|
|
|
)
|
2019-10-10 18:04:17 +00:00
|
|
|
if indexes:
|
|
|
|
for index in indexes:
|
2022-02-03 19:24:19 +00:00
|
|
|
operations.append(migrations.AddIndex("Pony", index))
|
2019-07-25 11:43:23 +00:00
|
|
|
if constraints:
|
|
|
|
for constraint in constraints:
|
2022-02-03 19:24:19 +00:00
|
|
|
operations.append(migrations.AddConstraint("Pony", constraint))
|
2019-07-25 11:43:23 +00:00
|
|
|
if second_model:
|
2022-02-03 19:24:19 +00:00
|
|
|
operations.append(
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Stable",
|
|
|
|
[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
)
|
2019-07-25 11:43:23 +00:00
|
|
|
if third_model:
|
2022-02-03 19:24:19 +00:00
|
|
|
operations.append(
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Van",
|
|
|
|
[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
)
|
2019-07-25 11:43:23 +00:00
|
|
|
if related_model:
|
2022-02-03 19:24:19 +00:00
|
|
|
operations.append(
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Rider",
|
|
|
|
[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
("pony", models.ForeignKey("Pony", models.CASCADE)),
|
|
|
|
(
|
|
|
|
"friend",
|
|
|
|
models.ForeignKey("self", models.CASCADE, null=True),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
)
|
2019-07-25 11:43:23 +00:00
|
|
|
if mti_model:
|
2022-02-03 19:24:19 +00:00
|
|
|
operations.append(
|
|
|
|
migrations.CreateModel(
|
|
|
|
"ShetlandPony",
|
|
|
|
fields=[
|
|
|
|
(
|
|
|
|
"pony_ptr",
|
|
|
|
models.OneToOneField(
|
|
|
|
"Pony",
|
|
|
|
models.CASCADE,
|
|
|
|
auto_created=True,
|
|
|
|
parent_link=True,
|
|
|
|
primary_key=True,
|
|
|
|
to_field="id",
|
|
|
|
serialize=False,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
("cuteness", models.IntegerField(default=1)),
|
|
|
|
],
|
|
|
|
bases=["%s.Pony" % app_label],
|
|
|
|
)
|
|
|
|
)
|
2019-07-25 11:43:23 +00:00
|
|
|
if proxy_model:
|
2022-02-03 19:24:19 +00:00
|
|
|
operations.append(
|
|
|
|
migrations.CreateModel(
|
|
|
|
"ProxyPony",
|
|
|
|
fields=[],
|
|
|
|
options={"proxy": True},
|
|
|
|
bases=["%s.Pony" % app_label],
|
|
|
|
)
|
|
|
|
)
|
2019-07-25 11:43:23 +00:00
|
|
|
if manager_model:
|
2019-07-25 11:44:18 +00:00
|
|
|
from .models import FoodManager, FoodQuerySet
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
|
|
operations.append(
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Food",
|
|
|
|
fields=[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
],
|
|
|
|
managers=[
|
|
|
|
("food_qs", FoodQuerySet.as_manager()),
|
|
|
|
("food_mgr", FoodManager("a", "b")),
|
|
|
|
("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
)
|
2019-07-25 11:43:23 +00:00
|
|
|
return self.apply_operations(app_label, ProjectState(), operations)
|
2024-08-22 18:26:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OptimizerTestBase(SimpleTestCase):
|
|
|
|
"""Common functions to help test the optimizer."""
|
|
|
|
|
|
|
|
def optimize(self, operations, app_label):
|
|
|
|
"""
|
|
|
|
Handy shortcut for getting results + number of loops
|
|
|
|
"""
|
|
|
|
optimizer = MigrationOptimizer()
|
|
|
|
return optimizer.optimize(operations, app_label), optimizer._iterations
|
|
|
|
|
|
|
|
def serialize(self, value):
|
|
|
|
return serializer_factory(value).serialize()[0]
|
|
|
|
|
|
|
|
def assertOptimizesTo(
|
|
|
|
self, operations, expected, exact=None, less_than=None, app_label=None
|
|
|
|
):
|
|
|
|
result, iterations = self.optimize(operations, app_label or "migrations")
|
|
|
|
result = [self.serialize(f) for f in result]
|
|
|
|
expected = [self.serialize(f) for f in expected]
|
|
|
|
self.assertEqual(expected, result)
|
|
|
|
if exact is not None and iterations != exact:
|
|
|
|
raise self.failureException(
|
|
|
|
"Optimization did not take exactly %s iterations (it took %s)"
|
|
|
|
% (exact, iterations)
|
|
|
|
)
|
|
|
|
if less_than is not None and iterations >= less_than:
|
|
|
|
raise self.failureException(
|
|
|
|
"Optimization did not take less than %s iterations (it took %s)"
|
|
|
|
% (less_than, iterations)
|
|
|
|
)
|
|
|
|
|
|
|
|
def assertDoesNotOptimize(self, operations, **kwargs):
|
|
|
|
self.assertOptimizesTo(operations, operations, **kwargs)
|