1
0
mirror of https://github.com/django/django.git synced 2025-01-22 08:10:28 +00:00

Refs #24121 -- Added __repr__() to Message.

This commit is contained in:
François Freitag 2023-08-23 17:27:03 +02:00 committed by Mariusz Felisiak
parent 95b8449158
commit 7bba231b67
2 changed files with 33 additions and 0 deletions

View File

@ -32,6 +32,10 @@ class Message:
def __str__(self):
return str(self.message)
def __repr__(self):
extra_tags = f", extra_tags={self.extra_tags!r}" if self.extra_tags else ""
return f"Message(level={self.level}, message={self.message!r}{extra_tags})"
@property
def tags(self):
return " ".join(tag for tag in [self.extra_tags, self.level_tag] if tag)

View File

@ -17,6 +17,35 @@ class MessageTests(SimpleTestCase):
self.assertNotEqual(msg_1, msg_3)
self.assertNotEqual(msg_2, msg_3)
@override_settings(
MESSAGE_TAGS={
constants.WARNING: "caution",
constants.ERROR: "",
12: "custom",
}
)
def test_repr(self):
tests = [
(constants.INFO, "thing", "", "Message(level=20, message='thing')"),
(
constants.WARNING,
"careful",
"tag1 tag2",
"Message(level=30, message='careful', extra_tags='tag1 tag2')",
),
(
constants.ERROR,
"oops",
"tag",
"Message(level=40, message='oops', extra_tags='tag')",
),
(12, "custom", "", "Message(level=12, message='custom')"),
]
for level, message, extra_tags, expected in tests:
with self.subTest(level=level, message=message):
msg = Message(level, message, extra_tags=extra_tags)
self.assertEqual(repr(msg), expected)
class TestLevelTags(SimpleTestCase):
message_tags = {