tests.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import os
  2. import sys
  3. import warnings
  4. from django.db import connection
  5. from django.core import management
  6. from django.core.management import CommandError
  7. from django.core.management.utils import find_command, popen_wrapper
  8. from django.test import SimpleTestCase
  9. from django.utils import translation
  10. from django.utils.deprecation import RemovedInDjango20Warning
  11. from django.utils.six import StringIO
  12. class CommandTests(SimpleTestCase):
  13. def test_command(self):
  14. out = StringIO()
  15. management.call_command('dance', stdout=out)
  16. self.assertIn("I don't feel like dancing Rock'n'Roll.\n", out.getvalue())
  17. def test_command_style(self):
  18. out = StringIO()
  19. management.call_command('dance', style='Jive', stdout=out)
  20. self.assertIn("I don't feel like dancing Jive.\n", out.getvalue())
  21. # Passing options as arguments also works (thanks argparse)
  22. management.call_command('dance', '--style', 'Jive', stdout=out)
  23. self.assertIn("I don't feel like dancing Jive.\n", out.getvalue())
  24. def test_language_preserved(self):
  25. out = StringIO()
  26. with translation.override('fr'):
  27. management.call_command('dance', stdout=out)
  28. self.assertEqual(translation.get_language(), 'fr')
  29. def test_explode(self):
  30. """ Test that an unknown command raises CommandError """
  31. self.assertRaises(CommandError, management.call_command, ('explode',))
  32. def test_system_exit(self):
  33. """ Exception raised in a command should raise CommandError with
  34. call_command, but SystemExit when run from command line
  35. """
  36. with self.assertRaises(CommandError):
  37. management.call_command('dance', example="raise")
  38. old_stderr = sys.stderr
  39. sys.stderr = err = StringIO()
  40. try:
  41. with self.assertRaises(SystemExit):
  42. management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute()
  43. finally:
  44. sys.stderr = old_stderr
  45. self.assertIn("CommandError", err.getvalue())
  46. def test_default_en_us_locale_set(self):
  47. # Forces en_us when set to true
  48. out = StringIO()
  49. with translation.override('pl'):
  50. management.call_command('leave_locale_alone_false', stdout=out)
  51. self.assertEqual(out.getvalue(), "en-us\n")
  52. def test_configured_locale_preserved(self):
  53. # Leaves locale from settings when set to false
  54. out = StringIO()
  55. with translation.override('pl'):
  56. management.call_command('leave_locale_alone_true', stdout=out)
  57. self.assertEqual(out.getvalue(), "pl\n")
  58. def test_find_command_without_PATH(self):
  59. """
  60. find_command should still work when the PATH environment variable
  61. doesn't exist (#22256).
  62. """
  63. current_path = os.environ.pop('PATH', None)
  64. try:
  65. self.assertIsNone(find_command('_missing_'))
  66. finally:
  67. if current_path is not None:
  68. os.environ['PATH'] = current_path
  69. def test_call_command_option_parsing(self):
  70. """
  71. When passing the long option name to call_command, the available option
  72. key is the option dest name (#22985).
  73. """
  74. out = StringIO()
  75. management.call_command('dance', stdout=out, opt_3=True)
  76. self.assertIn("option3", out.getvalue())
  77. self.assertNotIn("opt_3", out.getvalue())
  78. self.assertNotIn("opt-3", out.getvalue())
  79. def test_optparse_compatibility(self):
  80. """
  81. optparse should be supported during Django 1.8/1.9 releases.
  82. """
  83. out = StringIO()
  84. with warnings.catch_warnings():
  85. warnings.filterwarnings("ignore", category=RemovedInDjango20Warning)
  86. management.call_command('optparse_cmd', stdout=out)
  87. self.assertEqual(out.getvalue(), "All right, let's dance Rock'n'Roll.\n")
  88. # Simulate command line execution
  89. old_stdout, old_stderr = sys.stdout, sys.stderr
  90. sys.stdout, sys.stderr = StringIO(), StringIO()
  91. try:
  92. management.execute_from_command_line(['django-admin', 'optparse_cmd'])
  93. finally:
  94. output = sys.stdout.getvalue()
  95. sys.stdout, sys.stderr = old_stdout, old_stderr
  96. self.assertEqual(output, "All right, let's dance Rock'n'Roll.\n")
  97. def test_calling_a_command_with_only_empty_parameter_should_ends_gracefully(self):
  98. out = StringIO()
  99. management.call_command('hal', "--empty", stdout=out)
  100. self.assertIn("Dave, I can't do that.\n", out.getvalue())
  101. def test_calling_command_with_app_labels_and_parameters_should_be_ok(self):
  102. out = StringIO()
  103. management.call_command('hal', 'myapp', "--verbosity", "3", stdout=out)
  104. self.assertIn("Dave, my mind is going. I can feel it. I can feel it.\n", out.getvalue())
  105. def test_calling_command_with_parameters_and_app_labels_at_the_end_should_be_ok(self):
  106. out = StringIO()
  107. management.call_command('hal', "--verbosity", "3", "myapp", stdout=out)
  108. self.assertIn("Dave, my mind is going. I can feel it. I can feel it.\n", out.getvalue())
  109. def test_calling_a_command_with_no_app_labels_and_parameters_should_raise_a_command_error(self):
  110. out = StringIO()
  111. with self.assertRaises(CommandError):
  112. management.call_command('hal', stdout=out)
  113. def test_output_transaction(self):
  114. out = StringIO()
  115. management.call_command('transaction', stdout=out, no_color=True)
  116. output = out.getvalue().strip()
  117. self.assertTrue(output.startswith(connection.ops.start_transaction_sql()))
  118. self.assertTrue(output.endswith(connection.ops.end_transaction_sql()))
  119. class UtilsTests(SimpleTestCase):
  120. def test_no_existent_external_program(self):
  121. self.assertRaises(CommandError, popen_wrapper, ['a_42_command_that_doesnt_exist_42'])