1. import os
    
  2. import shutil
    
  3. import tempfile
    
  4. from contextlib import contextmanager
    
  5. from importlib import import_module
    
  6. 
    
  7. from django.apps import apps
    
  8. from django.db import connection, connections, migrations, models
    
  9. from django.db.migrations.migration import Migration
    
  10. from django.db.migrations.recorder import MigrationRecorder
    
  11. from django.db.migrations.state import ProjectState
    
  12. from django.test import TransactionTestCase
    
  13. from django.test.utils import extend_sys_path
    
  14. from django.utils.module_loading import module_dir
    
  15. 
    
  16. 
    
  17. class MigrationTestBase(TransactionTestCase):
    
  18.     """
    
  19.     Contains an extended set of asserts for testing migrations and schema operations.
    
  20.     """
    
  21. 
    
  22.     available_apps = ["migrations"]
    
  23.     databases = {"default", "other"}
    
  24. 
    
  25.     def tearDown(self):
    
  26.         # Reset applied-migrations state.
    
  27.         for db in self.databases:
    
  28.             recorder = MigrationRecorder(connections[db])
    
  29.             recorder.migration_qs.filter(app="migrations").delete()
    
  30. 
    
  31.     def get_table_description(self, table, using="default"):
    
  32.         with connections[using].cursor() as cursor:
    
  33.             return connections[using].introspection.get_table_description(cursor, table)
    
  34. 
    
  35.     def assertTableExists(self, table, using="default"):
    
  36.         with connections[using].cursor() as cursor:
    
  37.             self.assertIn(table, connections[using].introspection.table_names(cursor))
    
  38. 
    
  39.     def assertTableNotExists(self, table, using="default"):
    
  40.         with connections[using].cursor() as cursor:
    
  41.             self.assertNotIn(
    
  42.                 table, connections[using].introspection.table_names(cursor)
    
  43.             )
    
  44. 
    
  45.     def assertColumnExists(self, table, column, using="default"):
    
  46.         self.assertIn(
    
  47.             column, [c.name for c in self.get_table_description(table, using=using)]
    
  48.         )
    
  49. 
    
  50.     def assertColumnNotExists(self, table, column, using="default"):
    
  51.         self.assertNotIn(
    
  52.             column, [c.name for c in self.get_table_description(table, using=using)]
    
  53.         )
    
  54. 
    
  55.     def _get_column_allows_null(self, table, column, using):
    
  56.         return [
    
  57.             c.null_ok
    
  58.             for c in self.get_table_description(table, using=using)
    
  59.             if c.name == column
    
  60.         ][0]
    
  61. 
    
  62.     def assertColumnNull(self, table, column, using="default"):
    
  63.         self.assertTrue(self._get_column_allows_null(table, column, using))
    
  64. 
    
  65.     def assertColumnNotNull(self, table, column, using="default"):
    
  66.         self.assertFalse(self._get_column_allows_null(table, column, using))
    
  67. 
    
  68.     def _get_column_collation(self, table, column, using):
    
  69.         return next(
    
  70.             f.collation
    
  71.             for f in self.get_table_description(table, using=using)
    
  72.             if f.name == column
    
  73.         )
    
  74. 
    
  75.     def assertColumnCollation(self, table, column, collation, using="default"):
    
  76.         self.assertEqual(self._get_column_collation(table, column, using), collation)
    
  77. 
    
  78.     def assertIndexExists(
    
  79.         self, table, columns, value=True, using="default", index_type=None
    
  80.     ):
    
  81.         with connections[using].cursor() as cursor:
    
  82.             self.assertEqual(
    
  83.                 value,
    
  84.                 any(
    
  85.                     c["index"]
    
  86.                     for c in connections[using]
    
  87.                     .introspection.get_constraints(cursor, table)
    
  88.                     .values()
    
  89.                     if (
    
  90.                         c["columns"] == list(columns)
    
  91.                         and (index_type is None or c["type"] == index_type)
    
  92.                         and not c["unique"]
    
  93.                     )
    
  94.                 ),
    
  95.             )
    
  96. 
    
  97.     def assertIndexNotExists(self, table, columns):
    
  98.         return self.assertIndexExists(table, columns, False)
    
  99. 
    
  100.     def assertIndexNameExists(self, table, index, using="default"):
    
  101.         with connections[using].cursor() as cursor:
    
  102.             self.assertIn(
    
  103.                 index,
    
  104.                 connection.introspection.get_constraints(cursor, table),
    
  105.             )
    
  106. 
    
  107.     def assertIndexNameNotExists(self, table, index, using="default"):
    
  108.         with connections[using].cursor() as cursor:
    
  109.             self.assertNotIn(
    
  110.                 index,
    
  111.                 connection.introspection.get_constraints(cursor, table),
    
  112.             )
    
  113. 
    
  114.     def assertConstraintExists(self, table, name, value=True, using="default"):
    
  115.         with connections[using].cursor() as cursor:
    
  116.             constraints = (
    
  117.                 connections[using].introspection.get_constraints(cursor, table).items()
    
  118.             )
    
  119.             self.assertEqual(
    
  120.                 value,
    
  121.                 any(c["check"] for n, c in constraints if n == name),
    
  122.             )
    
  123. 
    
  124.     def assertConstraintNotExists(self, table, name):
    
  125.         return self.assertConstraintExists(table, name, False)
    
  126. 
    
  127.     def assertUniqueConstraintExists(self, table, columns, value=True, using="default"):
    
  128.         with connections[using].cursor() as cursor:
    
  129.             constraints = (
    
  130.                 connections[using].introspection.get_constraints(cursor, table).values()
    
  131.             )
    
  132.             self.assertEqual(
    
  133.                 value,
    
  134.                 any(c["unique"] for c in constraints if c["columns"] == list(columns)),
    
  135.             )
    
  136. 
    
  137.     def assertFKExists(self, table, columns, to, value=True, using="default"):
    
  138.         if not connections[using].features.can_introspect_foreign_keys:
    
  139.             return
    
  140.         with connections[using].cursor() as cursor:
    
  141.             self.assertEqual(
    
  142.                 value,
    
  143.                 any(
    
  144.                     c["foreign_key"] == to
    
  145.                     for c in connections[using]
    
  146.                     .introspection.get_constraints(cursor, table)
    
  147.                     .values()
    
  148.                     if c["columns"] == list(columns)
    
  149.                 ),
    
  150.             )
    
  151. 
    
  152.     def assertFKNotExists(self, table, columns, to):
    
  153.         return self.assertFKExists(table, columns, to, False)
    
  154. 
    
  155.     @contextmanager
    
  156.     def temporary_migration_module(self, app_label="migrations", module=None):
    
  157.         """
    
  158.         Allows testing management commands in a temporary migrations module.
    
  159. 
    
  160.         Wrap all invocations to makemigrations and squashmigrations with this
    
  161.         context manager in order to avoid creating migration files in your
    
  162.         source tree inadvertently.
    
  163. 
    
  164.         Takes the application label that will be passed to makemigrations or
    
  165.         squashmigrations and the Python path to a migrations module.
    
  166. 
    
  167.         The migrations module is used as a template for creating the temporary
    
  168.         migrations module. If it isn't provided, the application's migrations
    
  169.         module is used, if it exists.
    
  170. 
    
  171.         Returns the filesystem path to the temporary migrations module.
    
  172.         """
    
  173.         with tempfile.TemporaryDirectory() as temp_dir:
    
  174.             target_dir = tempfile.mkdtemp(dir=temp_dir)
    
  175.             with open(os.path.join(target_dir, "__init__.py"), "w"):
    
  176.                 pass
    
  177.             target_migrations_dir = os.path.join(target_dir, "migrations")
    
  178. 
    
  179.             if module is None:
    
  180.                 module = apps.get_app_config(app_label).name + ".migrations"
    
  181. 
    
  182.             try:
    
  183.                 source_migrations_dir = module_dir(import_module(module))
    
  184.             except (ImportError, ValueError):
    
  185.                 pass
    
  186.             else:
    
  187.                 shutil.copytree(source_migrations_dir, target_migrations_dir)
    
  188. 
    
  189.             with extend_sys_path(temp_dir):
    
  190.                 new_module = os.path.basename(target_dir) + ".migrations"
    
  191.                 with self.settings(MIGRATION_MODULES={app_label: new_module}):
    
  192.                     yield target_migrations_dir
    
  193. 
    
  194. 
    
  195. class OperationTestBase(MigrationTestBase):
    
  196.     """Common functions to help test operations."""
    
  197. 
    
  198.     @classmethod
    
  199.     def setUpClass(cls):
    
  200.         super().setUpClass()
    
  201.         cls._initial_table_names = frozenset(connection.introspection.table_names())
    
  202. 
    
  203.     def tearDown(self):
    
  204.         self.cleanup_test_tables()
    
  205.         super().tearDown()
    
  206. 
    
  207.     def cleanup_test_tables(self):
    
  208.         table_names = (
    
  209.             frozenset(connection.introspection.table_names())
    
  210.             - self._initial_table_names
    
  211.         )
    
  212.         with connection.schema_editor() as editor:
    
  213.             with connection.constraint_checks_disabled():
    
  214.                 for table_name in table_names:
    
  215.                     editor.execute(
    
  216.                         editor.sql_delete_table
    
  217.                         % {
    
  218.                             "table": editor.quote_name(table_name),
    
  219.                         }
    
  220.                     )
    
  221. 
    
  222.     def apply_operations(self, app_label, project_state, operations, atomic=True):
    
  223.         migration = Migration("name", app_label)
    
  224.         migration.operations = operations
    
  225.         with connection.schema_editor(atomic=atomic) as editor:
    
  226.             return migration.apply(project_state, editor)
    
  227. 
    
  228.     def unapply_operations(self, app_label, project_state, operations, atomic=True):
    
  229.         migration = Migration("name", app_label)
    
  230.         migration.operations = operations
    
  231.         with connection.schema_editor(atomic=atomic) as editor:
    
  232.             return migration.unapply(project_state, editor)
    
  233. 
    
  234.     def make_test_state(self, app_label, operation, **kwargs):
    
  235.         """
    
  236.         Makes a test state using set_up_test_model and returns the
    
  237.         original state and the state after the migration is applied.
    
  238.         """
    
  239.         project_state = self.set_up_test_model(app_label, **kwargs)
    
  240.         new_state = project_state.clone()
    
  241.         operation.state_forwards(app_label, new_state)
    
  242.         return project_state, new_state
    
  243. 
    
  244.     def set_up_test_model(
    
  245.         self,
    
  246.         app_label,
    
  247.         second_model=False,
    
  248.         third_model=False,
    
  249.         index=False,
    
  250.         multicol_index=False,
    
  251.         related_model=False,
    
  252.         mti_model=False,
    
  253.         proxy_model=False,
    
  254.         manager_model=False,
    
  255.         unique_together=False,
    
  256.         options=False,
    
  257.         db_table=None,
    
  258.         index_together=False,
    
  259.         constraints=None,
    
  260.         indexes=None,
    
  261.     ):
    
  262.         """Creates a test model state and database table."""
    
  263.         # Make the "current" state.
    
  264.         model_options = {
    
  265.             "swappable": "TEST_SWAP_MODEL",
    
  266.             "index_together": [["weight", "pink"]] if index_together else [],
    
  267.             "unique_together": [["pink", "weight"]] if unique_together else [],
    
  268.         }
    
  269.         if options:
    
  270.             model_options["permissions"] = [("can_groom", "Can groom")]
    
  271.         if db_table:
    
  272.             model_options["db_table"] = db_table
    
  273.         operations = [
    
  274.             migrations.CreateModel(
    
  275.                 "Pony",
    
  276.                 [
    
  277.                     ("id", models.AutoField(primary_key=True)),
    
  278.                     ("pink", models.IntegerField(default=3)),
    
  279.                     ("weight", models.FloatField()),
    
  280.                 ],
    
  281.                 options=model_options,
    
  282.             )
    
  283.         ]
    
  284.         if index:
    
  285.             operations.append(
    
  286.                 migrations.AddIndex(
    
  287.                     "Pony",
    
  288.                     models.Index(fields=["pink"], name="pony_pink_idx"),
    
  289.                 )
    
  290.             )
    
  291.         if multicol_index:
    
  292.             operations.append(
    
  293.                 migrations.AddIndex(
    
  294.                     "Pony",
    
  295.                     models.Index(fields=["pink", "weight"], name="pony_test_idx"),
    
  296.                 )
    
  297.             )
    
  298.         if indexes:
    
  299.             for index in indexes:
    
  300.                 operations.append(migrations.AddIndex("Pony", index))
    
  301.         if constraints:
    
  302.             for constraint in constraints:
    
  303.                 operations.append(migrations.AddConstraint("Pony", constraint))
    
  304.         if second_model:
    
  305.             operations.append(
    
  306.                 migrations.CreateModel(
    
  307.                     "Stable",
    
  308.                     [
    
  309.                         ("id", models.AutoField(primary_key=True)),
    
  310.                     ],
    
  311.                 )
    
  312.             )
    
  313.         if third_model:
    
  314.             operations.append(
    
  315.                 migrations.CreateModel(
    
  316.                     "Van",
    
  317.                     [
    
  318.                         ("id", models.AutoField(primary_key=True)),
    
  319.                     ],
    
  320.                 )
    
  321.             )
    
  322.         if related_model:
    
  323.             operations.append(
    
  324.                 migrations.CreateModel(
    
  325.                     "Rider",
    
  326.                     [
    
  327.                         ("id", models.AutoField(primary_key=True)),
    
  328.                         ("pony", models.ForeignKey("Pony", models.CASCADE)),
    
  329.                         (
    
  330.                             "friend",
    
  331.                             models.ForeignKey("self", models.CASCADE, null=True),
    
  332.                         ),
    
  333.                     ],
    
  334.                 )
    
  335.             )
    
  336.         if mti_model:
    
  337.             operations.append(
    
  338.                 migrations.CreateModel(
    
  339.                     "ShetlandPony",
    
  340.                     fields=[
    
  341.                         (
    
  342.                             "pony_ptr",
    
  343.                             models.OneToOneField(
    
  344.                                 "Pony",
    
  345.                                 models.CASCADE,
    
  346.                                 auto_created=True,
    
  347.                                 parent_link=True,
    
  348.                                 primary_key=True,
    
  349.                                 to_field="id",
    
  350.                                 serialize=False,
    
  351.                             ),
    
  352.                         ),
    
  353.                         ("cuteness", models.IntegerField(default=1)),
    
  354.                     ],
    
  355.                     bases=["%s.Pony" % app_label],
    
  356.                 )
    
  357.             )
    
  358.         if proxy_model:
    
  359.             operations.append(
    
  360.                 migrations.CreateModel(
    
  361.                     "ProxyPony",
    
  362.                     fields=[],
    
  363.                     options={"proxy": True},
    
  364.                     bases=["%s.Pony" % app_label],
    
  365.                 )
    
  366.             )
    
  367.         if manager_model:
    
  368.             from .models import FoodManager, FoodQuerySet
    
  369. 
    
  370.             operations.append(
    
  371.                 migrations.CreateModel(
    
  372.                     "Food",
    
  373.                     fields=[
    
  374.                         ("id", models.AutoField(primary_key=True)),
    
  375.                     ],
    
  376.                     managers=[
    
  377.                         ("food_qs", FoodQuerySet.as_manager()),
    
  378.                         ("food_mgr", FoodManager("a", "b")),
    
  379.                         ("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
    
  380.                     ],
    
  381.                 )
    
  382.             )
    
  383.         return self.apply_operations(app_label, ProjectState(), operations)