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
|
|
|
|
2015-02-22 20:50:16 +00:00
|
|
|
from django.apps import apps
|
2016-01-25 00:23:38 +00:00
|
|
|
from django.db import connections
|
2015-06-02 20:19:23 +00:00
|
|
|
from django.db.migrations.recorder import MigrationRecorder
|
2014-11-19 21:08:57 +00:00
|
|
|
from django.test import 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"]
|
|
|
|
|
2015-06-02 20:19:23 +00:00
|
|
|
def tearDown(self):
|
|
|
|
# Reset applied-migrations state.
|
2016-01-25 00:23:38 +00:00
|
|
|
for db in connections:
|
|
|
|
recorder = MigrationRecorder(connections[db])
|
|
|
|
recorder.migration_qs.filter(app='migrations').delete()
|
2015-06-02 20:19:23 +00:00
|
|
|
|
2016-01-25 00:23:38 +00:00
|
|
|
def get_table_description(self, table, using='default'):
|
|
|
|
with connections[using].cursor() as cursor:
|
|
|
|
return connections[using].introspection.get_table_description(cursor, table)
|
2014-01-09 15:05:15 +00:00
|
|
|
|
2016-01-25 00:23:38 +00:00
|
|
|
def assertTableExists(self, table, using='default'):
|
|
|
|
with connections[using].cursor() as cursor:
|
|
|
|
self.assertIn(table, connections[using].introspection.table_names(cursor))
|
2013-07-25 12:52:35 +00:00
|
|
|
|
2016-01-25 00:23:38 +00:00
|
|
|
def assertTableNotExists(self, table, using='default'):
|
|
|
|
with connections[using].cursor() as cursor:
|
|
|
|
self.assertNotIn(table, connections[using].introspection.table_names(cursor))
|
2013-07-25 12:52:35 +00:00
|
|
|
|
2016-01-25 00:23:38 +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
|
|
|
|
2016-01-25 00:23:38 +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):
|
|
|
|
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
|
|
|
|
2016-01-25 00:23:38 +00:00
|
|
|
def assertColumnNull(self, table, column, using='default'):
|
|
|
|
self.assertEqual(self._get_column_allows_null(table, column, using), True)
|
2013-07-25 12:52:35 +00:00
|
|
|
|
2016-01-25 00:23:38 +00:00
|
|
|
def assertColumnNotNull(self, table, column, using='default'):
|
|
|
|
self.assertEqual(self._get_column_allows_null(table, column, using), False)
|
|
|
|
|
|
|
|
def assertIndexExists(self, table, columns, value=True, using='default'):
|
|
|
|
with connections[using].cursor() as cursor:
|
2014-01-09 15:05:15 +00:00
|
|
|
self.assertEqual(
|
|
|
|
value,
|
|
|
|
any(
|
|
|
|
c["index"]
|
2016-01-25 00:23:38 +00:00
|
|
|
for c in connections[using].introspection.get_constraints(cursor, table).values()
|
2014-01-09 15:05:15 +00:00
|
|
|
if c['columns'] == list(columns)
|
|
|
|
),
|
|
|
|
)
|
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
|
|
|
|
2016-01-25 00:23:38 +00:00
|
|
|
def assertFKExists(self, table, columns, to, value=True, using='default'):
|
|
|
|
with connections[using].cursor() as cursor:
|
2014-06-08 01:17:31 +00:00
|
|
|
self.assertEqual(
|
|
|
|
value,
|
|
|
|
any(
|
|
|
|
c["foreign_key"] == to
|
2016-01-25 00:23:38 +00:00
|
|
|
for c in connections[using].introspection.get_constraints(cursor, table).values()
|
2014-06-08 01:17:31 +00:00
|
|
|
if c['columns'] == list(columns)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def assertFKNotExists(self, table, columns, to, value=True):
|
|
|
|
return self.assertFKExists(table, columns, to, False)
|
2015-02-22 20:50:16 +00:00
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def temporary_migration_module(self, app_label='migrations', module=None):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
temp_dir = tempfile.mkdtemp()
|
|
|
|
try:
|
|
|
|
target_dir = tempfile.mkdtemp(dir=temp_dir)
|
|
|
|
with open(os.path.join(target_dir, '__init__.py'), 'w'):
|
|
|
|
pass
|
|
|
|
target_migrations_dir = os.path.join(target_dir, 'migrations')
|
|
|
|
|
|
|
|
if module is None:
|
|
|
|
module = apps.get_app_config(app_label).name + '.migrations'
|
|
|
|
|
|
|
|
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):
|
|
|
|
new_module = os.path.basename(target_dir) + '.migrations'
|
|
|
|
with self.settings(MIGRATION_MODULES={app_label: new_module}):
|
|
|
|
yield target_migrations_dir
|
|
|
|
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(temp_dir)
|