tests.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import sys
  2. from django.core import management
  3. from django.core.management.base import CommandError
  4. from django.test import TestCase
  5. from django.utils import translation
  6. from django.utils.six import StringIO
  7. class CommandTests(TestCase):
  8. def test_command(self):
  9. out = StringIO()
  10. management.call_command('dance', stdout=out)
  11. self.assertEqual(out.getvalue(),
  12. "I don't feel like dancing Rock'n'Roll.\n")
  13. def test_command_style(self):
  14. out = StringIO()
  15. management.call_command('dance', style='Jive', stdout=out)
  16. self.assertEqual(out.getvalue(),
  17. "I don't feel like dancing Jive.\n")
  18. def test_language_preserved(self):
  19. out = StringIO()
  20. with translation.override('fr'):
  21. management.call_command('dance', stdout=out)
  22. self.assertEqual(translation.get_language(), 'fr')
  23. def test_explode(self):
  24. """ Test that an unknown command raises CommandError """
  25. self.assertRaises(CommandError, management.call_command, ('explode',))
  26. def test_system_exit(self):
  27. """ Exception raised in a command should raise CommandError with
  28. call_command, but SystemExit when run from command line
  29. """
  30. with self.assertRaises(CommandError):
  31. management.call_command('dance', example="raise")
  32. old_stderr = sys.stderr
  33. sys.stderr = err = StringIO()
  34. try:
  35. with self.assertRaises(SystemExit):
  36. management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute()
  37. finally:
  38. sys.stderr = old_stderr
  39. self.assertIn("CommandError", err.getvalue())
  40. def test_default_en_us_locale_set(self):
  41. # Forces en_us when set to true
  42. out = StringIO()
  43. with translation.override('pl'):
  44. management.call_command('leave_locale_alone_false', stdout=out)
  45. self.assertEqual(out.getvalue(), "en-us\n")
  46. def test_configured_locale_preserved(self):
  47. # Leaves locale from settings when set to false
  48. out = StringIO()
  49. with translation.override('pl'):
  50. management.call_command('leave_locale_alone_true', stdout=out)
  51. self.assertEqual(out.getvalue(), "pl\n")