3
from django.core import management
4
from django.core.management import CommandError
5
from django.core.management.utils import popen_wrapper
6
from django.test import SimpleTestCase
7
from django.utils import translation
8
from django.utils.six import StringIO
11
class CommandTests(SimpleTestCase):
12
def test_command(self):
14
management.call_command('dance', stdout=out)
15
self.assertEqual(out.getvalue(),
16
"I don't feel like dancing Rock'n'Roll.\n")
18
def test_command_style(self):
20
management.call_command('dance', style='Jive', stdout=out)
21
self.assertEqual(out.getvalue(),
22
"I don't feel like dancing Jive.\n")
24
def test_language_preserved(self):
26
with translation.override('fr'):
27
management.call_command('dance', stdout=out)
28
self.assertEqual(translation.get_language(), 'fr')
30
def test_explode(self):
31
""" Test that an unknown command raises CommandError """
32
self.assertRaises(CommandError, management.call_command, ('explode',))
34
def test_system_exit(self):
35
""" Exception raised in a command should raise CommandError with
36
call_command, but SystemExit when run from command line
38
with self.assertRaises(CommandError):
39
management.call_command('dance', example="raise")
40
old_stderr = sys.stderr
41
sys.stderr = err = StringIO()
43
with self.assertRaises(SystemExit):
44
management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute()
46
sys.stderr = old_stderr
47
self.assertIn("CommandError", err.getvalue())
49
def test_default_en_us_locale_set(self):
50
# Forces en_us when set to true
52
with translation.override('pl'):
53
management.call_command('leave_locale_alone_false', stdout=out)
54
self.assertEqual(out.getvalue(), "en-us\n")
56
def test_configured_locale_preserved(self):
57
# Leaves locale from settings when set to false
59
with translation.override('pl'):
60
management.call_command('leave_locale_alone_true', stdout=out)
61
self.assertEqual(out.getvalue(), "pl\n")
64
class UtilsTests(SimpleTestCase):
66
def test_no_existent_external_program(self):
67
self.assertRaises(CommandError, popen_wrapper, ['a_42_command_that_doesnt_exist_42'])