test_commands.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from __future__ import unicode_literals
  2. from django.core.management import call_command
  3. from django.db import connection
  4. from django.test import TransactionTestCase, skipUnlessDBFeature
  5. @skipUnlessDBFeature("gis_enabled")
  6. class MigrateTests(TransactionTestCase):
  7. """
  8. Tests running the migrate command in Geodjango.
  9. """
  10. available_apps = ["gis_tests.gis_migrations"]
  11. def get_table_description(self, table):
  12. with connection.cursor() as cursor:
  13. return connection.introspection.get_table_description(cursor, table)
  14. def assertTableExists(self, table):
  15. with connection.cursor() as cursor:
  16. self.assertIn(table, connection.introspection.table_names(cursor))
  17. def assertTableNotExists(self, table):
  18. with connection.cursor() as cursor:
  19. self.assertNotIn(table, connection.introspection.table_names(cursor))
  20. def test_migrate_gis(self):
  21. """
  22. Tests basic usage of the migrate command when a model uses Geodjango
  23. fields. Regression test for ticket #22001:
  24. https://code.djangoproject.com/ticket/22001
  25. It's also used to showcase an error in migrations where spatialite is
  26. enabled and geo tables are renamed resulting in unique constraint
  27. failure on geometry_columns. Regression for ticket #23030:
  28. https://code.djangoproject.com/ticket/23030
  29. """
  30. # Make sure the right tables exist
  31. self.assertTableExists("gis_migrations_neighborhood")
  32. self.assertTableExists("gis_migrations_household")
  33. self.assertTableExists("gis_migrations_family")
  34. if connection.features.supports_raster:
  35. self.assertTableExists("gis_migrations_heatmap")
  36. # Unmigrate everything
  37. call_command("migrate", "gis_migrations", "zero", verbosity=0)
  38. # Make sure it's all gone
  39. self.assertTableNotExists("gis_migrations_neighborhood")
  40. self.assertTableNotExists("gis_migrations_household")
  41. self.assertTableNotExists("gis_migrations_family")
  42. if connection.features.supports_raster:
  43. self.assertTableNotExists("gis_migrations_heatmap")
  44. # Even geometry columns metadata
  45. try:
  46. GeoColumn = connection.ops.geometry_columns()
  47. except NotImplementedError:
  48. # Not all GIS backends have geometry columns model
  49. pass
  50. else:
  51. self.assertEqual(
  52. GeoColumn.objects.filter(
  53. **{'%s__in' % GeoColumn.table_name_col(): ["gis_neighborhood", "gis_household"]}
  54. ).count(),
  55. 0)
  56. # Revert the "unmigration"
  57. call_command("migrate", "gis_migrations", verbosity=0)