I like Tom's initial proposition. As mentioned ours is very similar. Here 
is currently what we are using:

class MigrationTestBase(TransactionTestCase):
    """
    Custom TestCase containing an extended set of asserts for testing
    migrations and schema operations.
    Most of this code was derived from Django's private MigrationTestBase:
    
https://github.com/django/django/blob/stable/1.7.x/tests/migrations/test_base.py

    Notes:
        If you would like to override setUp() in the test, you will want to
        call super().setup() or explicitly invoke 
self.migrate_all_the_way() to
        ensure a clean history at the start of each test.
    """

    # MUST specify which apps we will need to test within the
    app_label = None
    test_migration_name = None

    # last_migration_nodes is a list of tuples assigned after each time we
    # migrate.
    # if last_migration_node is None, we will use apps from django.apps 
which
    # is the fully migrated App Registry created via our existing source 
code.
    last_migration_nodes = None

    @classmethod
    def setUpClass(cls):
        super(MigrationTestBase, cls).setUpClass()
        if not cls.app_label or not cls.test_migration_name:
            raise NotImplementedError('Must define class defaults: 
app_label, '
                                      'test_migration_name')

    def setUp(self):
        super(MigrationTestBase, self).setUp()
        self.migrate_all_the_way()

    def tearDown(self):
        self.migrate_all_the_way()
        super(MigrationTestBase, self).tearDown()

    def migrate_to(self, app_label, migration_name):
        call_command('migrate', app_label, migration_name, verbosity=0)
        self.last_migration_nodes = [(app_label, migration_name)]

    def migrate_to_test_migration(self):
        self.migrate_to(self.app_label, self.test_migration_name)

    def migrate_all_the_way(self):
        call_command('migrate', verbosity=0)
        self.last_migration_nodes = None

    def get_current_model(self, app_label, model):
        if self.last_migration_nodes is not None:
            conn = connections[DEFAULT_DB_ALIAS]
            loader = MigrationLoader(conn)
            proj_state = loader.project_state(self.last_migration_nodes)
            self.apps = proj_state.render()
            return self.apps.get_model(app_label, model)
        return dj_apps.get_model(app_label, model)

    def get_table_description(self, table):
        with connection.cursor() as cursor:
            return connection.introspection.get_table_description(
                cursor, table
            )

    def assertTableExists(self, table):
        with connection.cursor() as cursor:
            self.assertIn(
                table,
                connection.introspection.get_table_list(cursor)
            )

    def assertTableNotExists(self, table):
        with connection.cursor() as cursor:
            self.assertNotIn(
                table,
                connection.introspection.get_table_list(cursor)
            )

    def assertColumnExists(self, table, column):
        self.assertIn(
            column,
            [c.name for c in self.get_table_description(table)]
        )

    def assertColumnNotExists(self, table, column):
        self.assertNotIn(
            column,
            [c.name for c in self.get_table_description(table)]
        )

    def assertColumnNull(self, table, column):
        self.assertEqual(
            [
                c.null_ok
                for c in self.get_table_description(table) if c.name == 
column
            ][0],
            True
        )

    def assertColumnNotNull(self, table, column):
        self.assertEqual(
            [
                c.null_ok
                for c in self.get_table_description(table)
                if c.name == column
            ][0],
            False
        )

    def assertIndexExists(self, table, columns, value=True):
        with connection.cursor() as cursor:
            self.assertEqual(
                value,
                any(
                    c["index"]
                    for c in connection.introspection.get_constraints(
                        cursor, table).values()
                    if c['columns'] == list(columns)
                ),
            )

    def assertIndexNotExists(self, table, columns):
        return self.assertIndexExists(table, columns, False)

    def assertFKExists(self, table, columns, to, value=True):
        with connection.cursor() as cursor:
            self.assertEqual(
                value,
                any(
                    c["foreign_key"] == to
                    for c in connection.introspection.get_constraints(
                        cursor, table).values()
                    if c['columns'] == list(columns)
                ),
            )

    def assertFKNotExists(self, table, columns, to, value=True):
        return self.assertFKExists(table, columns, to, False)



-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/f400a8b4-7ff9-482d-a34e-63691674b0ea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to