summaryrefslogtreecommitdiff
path: root/tests/shell
diff options
context:
space:
mode:
authorNatalia <124304+nessita@users.noreply.github.com>2025-02-07 15:25:23 -0300
committerNatalia <124304+nessita@users.noreply.github.com>2025-02-10 22:59:44 -0300
commit16c7dc543c3a0e8a4c7554e36dd6166d5c12c872 (patch)
tree50fb1f76d90cd271e01a386edaa24aa9c8344de2 /tests/shell
parent7b0c587a68c2e486ccf8c967de55e0a66f8e5a2a (diff)
[5.2.x] Refs #35515 -- Fixed shell command verbose output when auto-importing 0 or 1 object.
Co-authored-by: Salvo Polizzi <salvopolizzi03@gmail.com> Backport of 0597e8ad1e55b565292ead732916aa0e39bdf37b from main.
Diffstat (limited to 'tests/shell')
-rw-r--r--tests/shell/tests.py40
1 files changed, 39 insertions, 1 deletions
diff --git a/tests/shell/tests.py b/tests/shell/tests.py
index 3ca12edfed..c3cff25aad 100644
--- a/tests/shell/tests.py
+++ b/tests/shell/tests.py
@@ -7,7 +7,7 @@ from django.contrib.auth.models import Group, Permission, User
from django.contrib.contenttypes.models import ContentType
from django.core.management import CommandError, call_command
from django.core.management.commands import shell
-from django.db import models
+from django.db import connection, models
from django.test import SimpleTestCase
from django.test.utils import (
captured_stdin,
@@ -275,6 +275,44 @@ class ShellCommandAutoImportsTestCase(SimpleTestCase):
" from shell.models import Phone, Marker",
)
+ def test_message_with_stdout_one_object(self):
+ class TestCommand(shell.Command):
+ def get_namespace(self):
+ return {"connection": connection}
+
+ with captured_stdout() as stdout:
+ TestCommand().get_and_report_namespace(verbosity=2)
+
+ cases = {
+ 0: "",
+ 1: "1 object imported automatically (use -v 2 for details).",
+ 2: (
+ "1 object imported automatically, including:\n\n"
+ " from django.utils.connection import connection"
+ ),
+ }
+ for verbosity, expected in cases.items():
+ with self.subTest(verbosity=verbosity):
+ with captured_stdout() as stdout:
+ TestCommand().get_and_report_namespace(verbosity=verbosity)
+ self.assertEqual(stdout.getvalue().strip(), expected)
+
+ def test_message_with_stdout_zero_objects(self):
+ class TestCommand(shell.Command):
+ def get_namespace(self):
+ return {}
+
+ cases = {
+ 0: "",
+ 1: "0 objects imported automatically.",
+ 2: "0 objects imported automatically.",
+ }
+ for verbosity, expected in cases.items():
+ with self.subTest(verbosity=verbosity):
+ with captured_stdout() as stdout:
+ TestCommand().get_and_report_namespace(verbosity=verbosity)
+ self.assertEqual(stdout.getvalue().strip(), expected)
+
@override_settings(INSTALLED_APPS=["shell", "django.contrib.contenttypes"])
def test_message_with_stdout_listing_objects_with_isort(self):
sorted_imports = (