Skip to content

Commit

Permalink
Improve repesentation of MessageLog is no message data is available
Browse files Browse the repository at this point in the history
  • Loading branch information
eht16 committed Apr 13, 2024
1 parent 88e195d commit 59b874f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/mailer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ class Meta:
def __str__(self):
try:
email = self.email
return f"On {self.when_attempted}, \"{email.subject}\" to {', '.join(email.to)}"
if email:
return f"On {self.when_attempted}, \"{email.subject}\" to {', '.join(email.to)}"
else:
return f'On {self.when_attempted}, "{self.message_id}"'
except Exception:
return "<MessageLog repr unavailable>"

Expand Down
8 changes: 6 additions & 2 deletions tests/test_mailer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import pickle
import time
from unittest.mock import Mock, patch
from unittest.mock import Mock, PropertyMock, patch

import django
import lockfile
Expand Down Expand Up @@ -690,8 +690,12 @@ def test_message_log_str(self):
f'On {log.when_attempted}, "Subject Log 中" to 1gol@example.com',
)

with patch.object(MessageLog, "when_attempted", new_callable=PropertyMock) as log_mock:
log_mock.side_effect = ValueError
self.assertEqual(str(log), "<MessageLog repr unavailable>")

log.message_data = None
self.assertEqual(str(log), "<MessageLog repr unavailable>")
self.assertEqual(str(log), f'On {log.when_attempted}, "{log.message_id}"')


class DbToEmailTest(TestCase):
Expand Down

0 comments on commit 59b874f

Please sign in to comment.