tests.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from __future__ import unicode_literals
  2. from django.apps import apps
  3. from django.core.management.color import no_style
  4. from django.core.management.sql import (sql_create, sql_delete, sql_indexes,
  5. sql_destroy_indexes, sql_all)
  6. from django.db import connections, DEFAULT_DB_ALIAS, router
  7. from django.test import TestCase
  8. from django.utils import six
  9. # See also initial_sql_regress for 'custom_sql_for_model' tests
  10. class SQLCommandsTestCase(TestCase):
  11. """Tests for several functions in django/core/management/sql.py"""
  12. def count_ddl(self, output, cmd):
  13. return len([o for o in output if o.startswith(cmd)])
  14. def test_sql_create(self):
  15. app_config = apps.get_app_config('commands_sql')
  16. output = sql_create(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
  17. create_tables = [o for o in output if o.startswith('CREATE TABLE')]
  18. self.assertEqual(len(create_tables), 3)
  19. # Lower so that Oracle's upper case tbl names wont break
  20. sql = create_tables[-1].lower()
  21. six.assertRegex(self, sql, r'^create table .commands_sql_book.*')
  22. def test_sql_delete(self):
  23. app_config = apps.get_app_config('commands_sql')
  24. output = sql_delete(app_config, no_style(), connections[DEFAULT_DB_ALIAS], close_connection=False)
  25. drop_tables = [o for o in output if o.startswith('DROP TABLE')]
  26. self.assertEqual(len(drop_tables), 3)
  27. # Lower so that Oracle's upper case tbl names wont break
  28. sql = drop_tables[-1].lower()
  29. six.assertRegex(self, sql, r'^drop table .commands_sql_comment.*')
  30. def test_sql_indexes(self):
  31. app_config = apps.get_app_config('commands_sql')
  32. output = sql_indexes(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
  33. # PostgreSQL creates one additional index for CharField
  34. self.assertIn(self.count_ddl(output, 'CREATE INDEX'), [3, 4])
  35. def test_sql_destroy_indexes(self):
  36. app_config = apps.get_app_config('commands_sql')
  37. output = sql_destroy_indexes(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
  38. # PostgreSQL creates one additional index for CharField
  39. self.assertIn(self.count_ddl(output, 'DROP INDEX'), [3, 4])
  40. def test_sql_all(self):
  41. app_config = apps.get_app_config('commands_sql')
  42. output = sql_all(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
  43. self.assertEqual(self.count_ddl(output, 'CREATE TABLE'), 3)
  44. # PostgreSQL creates one additional index for CharField
  45. self.assertIn(self.count_ddl(output, 'CREATE INDEX'), [3, 4])
  46. class TestRouter(object):
  47. def allow_migrate(self, db, model):
  48. return False
  49. class SQLCommandsRouterTestCase(TestCase):
  50. def setUp(self):
  51. self._old_routers = router.routers
  52. router.routers = [TestRouter()]
  53. def tearDown(self):
  54. router.routers = self._old_routers
  55. def test_router_honored(self):
  56. app_config = apps.get_app_config('commands_sql')
  57. for sql_command in (sql_all, sql_create, sql_delete, sql_indexes, sql_destroy_indexes):
  58. if sql_command is sql_delete:
  59. output = sql_command(app_config, no_style(), connections[DEFAULT_DB_ALIAS], close_connection=False)
  60. else:
  61. output = sql_command(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
  62. self.assertEqual(len(output), 0,
  63. "%s command is not honoring routers" % sql_command.__name__)