test_base.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from django.test import TransactionTestCase
  2. from django.db import connection
  3. class MigrationTestBase(TransactionTestCase):
  4. """
  5. Contains an extended set of asserts for testing migrations and schema operations.
  6. """
  7. available_apps = ["migrations"]
  8. def assertTableExists(self, table):
  9. self.assertIn(table, connection.introspection.get_table_list(connection.cursor()))
  10. def assertTableNotExists(self, table):
  11. self.assertNotIn(table, connection.introspection.get_table_list(connection.cursor()))
  12. def assertColumnExists(self, table, column):
  13. self.assertIn(column, [c.name for c in connection.introspection.get_table_description(connection.cursor(), table)])
  14. def assertColumnNotExists(self, table, column):
  15. self.assertNotIn(column, [c.name for c in connection.introspection.get_table_description(connection.cursor(), table)])
  16. def assertColumnNull(self, table, column):
  17. self.assertEqual([c.null_ok for c in connection.introspection.get_table_description(connection.cursor(), table) if c.name == column][0], True)
  18. def assertColumnNotNull(self, table, column):
  19. self.assertEqual([c.null_ok for c in connection.introspection.get_table_description(connection.cursor(), table) if c.name == column][0], False)
  20. def assertIndexExists(self, table, columns, value=True):
  21. self.assertEqual(
  22. value,
  23. any(
  24. c["index"]
  25. for c in connection.introspection.get_constraints(connection.cursor(), table).values()
  26. if c['columns'] == list(columns)
  27. ),
  28. )
  29. def assertIndexNotExists(self, table, columns):
  30. return self.assertIndexExists(table, columns, False)