test_apps.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import os
  2. import shutil
  3. import subprocess
  4. import sys
  5. import tempfile
  6. import unittest
  7. from django.db import ConnectionHandler
  8. SETTINGS = """
  9. SECRET_KEY = 'django_auth_tests_secret_key'
  10. INSTALLED_APPS = [
  11. 'django.contrib.auth.apps.BaseAuthConfig',
  12. 'django.contrib.contenttypes',
  13. ]
  14. MIGRATION_MODULES = {'auth': None}
  15. DATABASES = %(databases)r
  16. """
  17. class AppConfigTests(unittest.TestCase):
  18. def test_no_migrations(self):
  19. project_path = tempfile.mkdtemp()
  20. try:
  21. databases = {
  22. 'default': {
  23. 'ENGINE': 'django.db.backends.sqlite3',
  24. 'NAME': os.path.join(project_path, 'db.sqlite3'),
  25. }
  26. }
  27. with open(os.path.join(project_path, 'no_migrations.py'), 'w') as fp:
  28. fp.write(SETTINGS % {'databases': databases})
  29. with open(os.devnull, 'wb') as devnull:
  30. cmd = [
  31. sys.executable,
  32. '-m', 'django',
  33. 'migrate',
  34. '--settings', 'no_migrations',
  35. '--pythonpath', project_path,
  36. ]
  37. returncode = subprocess.call(cmd, stdout=devnull, stderr=devnull)
  38. # Migrate command ran without errors.
  39. self.assertEqual(returncode, 0)
  40. # Auth tables weren't created.
  41. conns = ConnectionHandler(databases)
  42. try:
  43. self.assertEqual(
  44. set(conns['default'].introspection.table_names()),
  45. {'django_content_type', 'django_migrations'},
  46. )
  47. finally:
  48. conns.close_all()
  49. finally:
  50. shutil.rmtree(project_path)