Cleanup: add reprs for Attachment, EmailAddress

This commit is contained in:
medmunds
2020-09-11 12:36:38 -07:00
committed by Mike Edmunds
parent feee8b5c5a
commit 03dd15d485
2 changed files with 28 additions and 0 deletions

View File

@@ -229,6 +229,10 @@ class EmailAddress:
self.username = addr_spec self.username = addr_spec
self.domain = '' self.domain = ''
def __repr__(self):
return "EmailAddress({display_name!r}, {addr_spec!r})".format(
display_name=self.display_name, addr_spec=self.addr_spec)
@property @property
def address(self): def address(self):
if self._address is None: if self._address is None:
@@ -309,6 +313,18 @@ class Attachment:
if self.content_type is None: if self.content_type is None:
self.content_type = self.mimetype self.content_type = self.mimetype
def __repr__(self):
details = [
self.mimetype,
"len={length}".format(length=len(self.content)),
]
if self.name:
details.append("name={name!r}".format(name=self.name))
if self.inline:
details.insert(0, "inline")
details.append("content_id={content_id!r}".format(content_id=self.content_id))
return "Attachment<{details}>".format(details=", ".join(details))
@property @property
def b64content(self): def b64content(self):
"""Content encoded as a base64 ascii string""" """Content encoded as a base64 ascii string"""

View File

@@ -173,6 +173,10 @@ class ParseAddressListTests(SimpleTestCase):
with self.assertRaisesMessage(ValueError, "cannot contain newlines"): with self.assertRaisesMessage(ValueError, "cannot contain newlines"):
_ = EmailAddress(name, addr) _ = EmailAddress(name, addr)
def test_email_address_repr(self):
self.assertEqual("EmailAddress('Name', 'addr@example.com')",
repr(EmailAddress('Name', 'addr@example.com')))
class NormalizedAttachmentTests(SimpleTestCase): class NormalizedAttachmentTests(SimpleTestCase):
"""Test utils.Attachment""" """Test utils.Attachment"""
@@ -188,6 +192,7 @@ class NormalizedAttachmentTests(SimpleTestCase):
self.assertFalse(att.inline) self.assertFalse(att.inline)
self.assertIsNone(att.content_id) self.assertIsNone(att.content_id)
self.assertEqual(att.cid, "") self.assertEqual(att.cid, "")
self.assertEqual(repr(att), "Attachment<image/x-emoticon, len=3, name='emoticon.txt'>")
def test_content_disposition_inline(self): def test_content_disposition_inline(self):
image = MIMEImage(b";-)", "x-emoticon") image = MIMEImage(b";-)", "x-emoticon")
@@ -198,11 +203,14 @@ class NormalizedAttachmentTests(SimpleTestCase):
self.assertTrue(att.inline) # even without the Content-ID self.assertTrue(att.inline) # even without the Content-ID
self.assertIsNone(att.content_id) self.assertIsNone(att.content_id)
self.assertEqual(att.cid, "") self.assertEqual(att.cid, "")
self.assertEqual(repr(att), "Attachment<inline, image/x-emoticon, len=3, content_id=None>")
image["Content-ID"] = "<abc123@example.net>" image["Content-ID"] = "<abc123@example.net>"
att = Attachment(image, "ascii") att = Attachment(image, "ascii")
self.assertEqual(att.content_id, "<abc123@example.net>") self.assertEqual(att.content_id, "<abc123@example.net>")
self.assertEqual(att.cid, "abc123@example.net") self.assertEqual(att.cid, "abc123@example.net")
self.assertEqual(repr(att),
"Attachment<inline, image/x-emoticon, len=3, content_id='<abc123@example.net>'>")
def test_content_id_implies_inline(self): def test_content_id_implies_inline(self):
"""A MIME object with a Content-ID should be assumed to be inline""" """A MIME object with a Content-ID should be assumed to be inline"""
@@ -211,17 +219,21 @@ class NormalizedAttachmentTests(SimpleTestCase):
att = Attachment(image, "ascii") att = Attachment(image, "ascii")
self.assertTrue(att.inline) self.assertTrue(att.inline)
self.assertEqual(att.content_id, "<abc123@example.net>") self.assertEqual(att.content_id, "<abc123@example.net>")
self.assertEqual(repr(att),
"Attachment<inline, image/x-emoticon, len=3, content_id='<abc123@example.net>'>")
# ... but not if explicit Content-Disposition says otherwise # ... but not if explicit Content-Disposition says otherwise
image["Content-Disposition"] = "attachment" image["Content-Disposition"] = "attachment"
att = Attachment(image, "ascii") att = Attachment(image, "ascii")
self.assertFalse(att.inline) self.assertFalse(att.inline)
self.assertIsNone(att.content_id) # ignored for non-inline Attachment self.assertIsNone(att.content_id) # ignored for non-inline Attachment
self.assertEqual(repr(att), "Attachment<image/x-emoticon, len=3>")
def test_content_type(self): def test_content_type(self):
att = Attachment(MIMEText("text", "plain", "iso8859-1"), "ascii") att = Attachment(MIMEText("text", "plain", "iso8859-1"), "ascii")
self.assertEqual(att.mimetype, "text/plain") self.assertEqual(att.mimetype, "text/plain")
self.assertEqual(att.content_type, 'text/plain; charset="iso8859-1"') self.assertEqual(att.content_type, 'text/plain; charset="iso8859-1"')
self.assertEqual(repr(att), "Attachment<text/plain, len=4>")
class LazyCoercionTests(SimpleTestCase): class LazyCoercionTests(SimpleTestCase):