File: test_shell_plus.py

package info (click to toggle)
python-django-extensions 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,820 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (189 lines) | stat: -rw-r--r-- 5,755 bytes parent folder | download
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -*- coding: utf-8 -*-
import os
import re
import pytest
import inspect

from textwrap import dedent
from django.core.management import call_command
from django.db.models import Model
from django.test import override_settings

from django_extensions.management.commands import shell_plus


def test_shell_plus_command(capsys):
    script = dedent(
        """\
        def _fn(x): return x * 2
        print([_fn(i) for i in range(10)])
        """
    )
    call_command("shell_plus", "--command=" + script)
    out, err = capsys.readouterr()

    assert out.rstrip().endswith(repr([i * 2 for i in range(10)]))


@pytest.mark.django_db()
@override_settings(SHELL_PLUS_SQLPARSE_ENABLED=False, SHELL_PLUS_PYGMENTS_ENABLED=False)
def test_shell_plus_print_sql(capsys):
    try:
        from django.db import connection
        from django.db.backends import utils

        CursorDebugWrapper = utils.CursorDebugWrapper
        force_debug_cursor = True if connection.force_debug_cursor else False
        call_command(
            "shell_plus",
            "--plain",
            "--print-sql",
            "--command=User.objects.all().exists()",
        )
    finally:
        utils.CursorDebugWrapper = CursorDebugWrapper
        connection.force_debug_cursor = force_debug_cursor

    out, err = capsys.readouterr()

    assert re.search(r"SELECT\s+.+\s+FROM\s+.auth_user.\s+LIMIT\s+1", out)


@pytest.mark.django_db()
@override_settings(SHELL_PLUS_SQLPARSE_ENABLED=False, SHELL_PLUS_PYGMENTS_ENABLED=False)
def test_shell_plus_print_sql_truncate(capsys):
    try:
        from django.db import connection
        from django.db.backends import utils

        CursorDebugWrapper = utils.CursorDebugWrapper
        force_debug_cursor = True if connection.force_debug_cursor else False
        call_command(
            "shell_plus",
            "--plain",
            "--print-sql",
            "--truncate-sql=0",
            "--command=User.objects.all().exists()",
        )
    finally:
        utils.CursorDebugWrapper = CursorDebugWrapper
        connection.force_debug_cursor = force_debug_cursor

    out, err = capsys.readouterr()

    assert re.search(r"SELECT\s+.+\s+FROM\s+.auth_user.\s+LIMIT\s+1", out)

    try:
        from django.db import connection
        from django.db.backends import utils

        CursorDebugWrapper = utils.CursorDebugWrapper
        force_debug_cursor = True if connection.force_debug_cursor else False
        call_command(
            "shell_plus",
            "--plain",
            "--print-sql",
            "--truncate-sql=4",
            "--command=User.objects.all().exists()",
        )
    finally:
        utils.CursorDebugWrapper = CursorDebugWrapper
        connection.force_debug_cursor = force_debug_cursor

    out, err = capsys.readouterr()

    assert re.search(r"SELE", out)
    assert not re.search(r"SELEC", out)


def test_shell_plus_plain_startup():
    command = shell_plus.Command()
    command.tests_mode = True

    parser = command.create_parser("test", "shell_plus")
    args = ["--plain"]
    options = parser.parse_args(args=args)

    retcode = command.handle(**vars(options))

    assert retcode == 130


def test_shell_plus_plain_startup_with_pythonrc(monkeypatch):
    command = shell_plus.Command()
    command.tests_mode = True

    parser = command.create_parser("test", "shell_plus")
    args = ["--plain", "--use-pythonrc"]
    options = parser.parse_args(args=args)

    tests_dir = os.path.dirname(
        os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
    )
    pythonrc_file = os.path.join(tests_dir, "pythonrc.py")
    assert os.path.isfile(pythonrc_file)
    monkeypatch.setenv("PYTHONSTARTUP", pythonrc_file)

    retcode = command.handle(**vars(options))
    assert retcode == 130

    imported_objects = command.tests_imported_objects
    assert "pythonrc_test_func" in imported_objects
    assert imported_objects["pythonrc_test_func"]() == "pythonrc was loaded"


def test_shell_plus_plain_loading_standard_django_imports(monkeypatch):
    command = shell_plus.Command()
    command.tests_mode = True

    parser = command.create_parser("test", "shell_plus")
    args = ["--plain"]
    options = parser.parse_args(args=args)

    retcode = command.handle(**vars(options))
    assert retcode == 130

    imported_objects = command.tests_imported_objects
    assert "get_user_model" in imported_objects
    assert "settings" in imported_objects
    assert "timezone" in imported_objects


def test_shell_plus_plain_loading_django_extensions_modules(monkeypatch):
    command = shell_plus.Command()
    command.tests_mode = True

    parser = command.create_parser("test", "shell_plus")
    args = ["--plain"]
    options = parser.parse_args(args=args)

    retcode = command.handle(**vars(options))
    assert retcode == 130

    imported_objects = command.tests_imported_objects
    assert "Club" in imported_objects
    assert "UniqueTestAppModel" in imported_objects
    assert "RandomCharTestModel" in imported_objects


def assert_should_models_be_imported(should_be, cli_arguments=None):
    command = shell_plus.Command()
    objs = command.get_imported_objects(cli_arguments or {})
    imported_models = filter(
        lambda imported: inspect.isclass(imported) and issubclass(imported, Model),
        objs.values(),
    )
    assert bool(list(imported_models)) == should_be


def test_shell_plus_loading_models():
    assert_should_models_be_imported(True)


def test_shell_plus_skipping_models_import_cli():
    assert_should_models_be_imported(False, cli_arguments={"dont_load": ["*"]})


@override_settings(SHELL_PLUS_DONT_LOAD=["*"])
def test_shell_plus_skipping_models_import_settings():
    assert_should_models_be_imported(False)