test_base.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 get_table_description(self, table):
  9. with connection.cursor() as cursor:
  10. return connection.introspection.get_table_description(cursor, table)
  11. def assertTableExists(self, table):
  12. with connection.cursor() as cursor:
  13. self.assertIn(table, connection.introspection.get_table_list(cursor))
  14. def assertTableNotExists(self, table):
  15. with connection.cursor() as cursor:
  16. self.assertNotIn(table, connection.introspection.get_table_list(cursor))
  17. def assertColumnExists(self, table, column):
  18. self.assertIn(column, [c.name for c in self.get_table_description(table)])
  19. def assertColumnNotExists(self, table, column):
  20. self.assertNotIn(column, [c.name for c in self.get_table_description(table)])
  21. def assertColumnNull(self, table, column):
  22. self.assertEqual([c.null_ok for c in self.get_table_description(table) if c.name == column][0], True)
  23. def assertColumnNotNull(self, table, column):
  24. self.assertEqual([c.null_ok for c in self.get_table_description(table) if c.name == column][0], False)
  25. def assertIndexExists(self, table, columns, value=True):
  26. with connection.cursor() as cursor:
  27. self.assertEqual(
  28. value,
  29. any(
  30. c["index"]
  31. for c in connection.introspection.get_constraints(cursor, table).values()
  32. if c['columns'] == list(columns)
  33. ),
  34. )
  35. def assertIndexNotExists(self, table, columns):
  36. return self.assertIndexExists(table, columns, False)