blob: f10a6290333c7f5aa5ff34263f0e3bdc0a2f88f1 (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 | from __future__ import absolute_import, unicode_literals
import sys
from django.contrib.auth.models import User
from django.core import management
try:
    from django.db.backends import utils
except ImportError:
    from django.db.backends import util as utils
from django.test import TestCase
from django.test.utils import override_settings
from django.utils import six
@override_settings(DEBUG=True)
class DebugSQLShellTestCase(TestCase):
    def setUp(self):
        self.original_cursor_wrapper = utils.CursorDebugWrapper
        # Since debugsqlshell monkey-patches django.db.backends.utils, we can
        # test it simply by loading it, without executing it. But we have to
        # undo the monkey-patch on exit.
        command_name = 'debugsqlshell'
        app_name = management.get_commands()[command_name]
        management.load_command_class(app_name, command_name)
    def tearDown(self):
        utils.CursorDebugWrapper = self.original_cursor_wrapper
    def test_command(self):
        original_stdout, sys.stdout = sys.stdout, six.StringIO()
        try:
            User.objects.count()
            self.assertIn("SELECT COUNT(*)", sys.stdout.getvalue())
        finally:
            sys.stdout = original_stdout
 |