~ubuntuone-pqm-team/django/stable

« back to all changes in this revision

Viewing changes to tests/modeltests/user_commands/tests.py

  • Committer: Natalia
  • Date: 2014-12-05 15:21:13 UTC
  • Revision ID: natalia.bidart@ubuntu.com-20141205152113-cchtmygjia45gb87
Tags: 1.6.8
- Imported Django 1.6.8 from released tarball.

Show diffs side-by-side

added added

removed removed

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