summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_email
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2012-05-29 01:09:04 (GMT)
committerR David Murray <rdmurray@bitdance.com>2012-05-29 01:09:04 (GMT)
commit6bed342b5895c816ac8d731d780ca6f01f8ea875 (patch)
tree58aceb66be4a432756d64b0568bf6474ff400fbb /Lib/test/test_email
parenta7c9ddb59cf11b9e5832636327ed04fb3bf052b2 (diff)
downloadcpython-6bed342b5895c816ac8d731d780ca6f01f8ea875.zip
cpython-6bed342b5895c816ac8d731d780ca6f01f8ea875.tar.gz
cpython-6bed342b5895c816ac8d731d780ca6f01f8ea875.tar.bz2
Refactor test_email/test_pickleable and add tests for date headers
Diffstat (limited to 'Lib/test/test_email')
-rw-r--r--Lib/test/test_email/test_pickleable.py87
1 files changed, 59 insertions, 28 deletions
diff --git a/Lib/test/test_email/test_pickleable.py b/Lib/test/test_email/test_pickleable.py
index 9ebf933..3a5bd11 100644
--- a/Lib/test/test_email/test_pickleable.py
+++ b/Lib/test/test_email/test_pickleable.py
@@ -2,55 +2,86 @@ import unittest
import textwrap
import copy
import pickle
+import email
+import email.message
from email import policy
-from email import message_from_string
from email.headerregistry import HeaderRegistry
from test.test_email import TestEmailBase
class TestPickleCopyHeader(TestEmailBase):
- unstructured = HeaderRegistry()('subject', 'this is a test')
+ header_factory = HeaderRegistry()
- def test_deepcopy_unstructured(self):
- h = copy.deepcopy(self.unstructured)
- self.assertEqual(str(h), str(self.unstructured))
+ unstructured = header_factory('subject', 'this is a test')
- def test_pickle_unstructured(self):
- p = pickle.dumps(self.unstructured)
+ def _test_deepcopy(self, name, value):
+ header = self.header_factory(name, value)
+ h = copy.deepcopy(header)
+ self.assertEqual(str(h), str(header))
+
+ def _test_pickle(self, name, value):
+ header = self.header_factory(name, value)
+ p = pickle.dumps(header)
h = pickle.loads(p)
- self.assertEqual(str(h), str(self.unstructured))
+ self.assertEqual(str(h), str(header))
- address = HeaderRegistry()('from', 'frodo@mordor.net')
+ headers = (
+ ('subject', 'this is a test'),
+ ('from', 'frodo@mordor.net'),
+ ('to', 'a: k@b.com, y@z.com;, j@f.com'),
+ ('date', 'Tue, 29 May 2012 09:24:26 +1000'),
+ )
- def test_deepcopy_address(self):
- h = copy.deepcopy(self.address)
- self.assertEqual(str(h), str(self.address))
+ for header in headers:
+ locals()['test_deepcopy_'+header[0]] = (
+ lambda self, header=header:
+ self._test_deepcopy(*header))
- def test_pickle_address(self):
- p = pickle.dumps(self.address)
- h = pickle.loads(p)
- self.assertEqual(str(h), str(self.address))
+ for header in headers:
+ locals()['test_pickle_'+header[0]] = (
+ lambda self, header=header:
+ self._test_pickle(*header))
class TestPickleCopyMessage(TestEmailBase):
- testmsg = message_from_string(textwrap.dedent("""\
- From: frodo@mordor.net
- To: bilbo@underhill.org
- Subject: help
+ msgs = {}
+
+ # Note: there will be no custom header objects in the parsed message.
+ msgs['parsed'] = email.message_from_string(textwrap.dedent("""\
+ Date: Tue, 29 May 2012 09:24:26 +1000
+ From: frodo@mordor.net
+ To: bilbo@underhill.org
+ Subject: help
- I think I forgot the ring.
- """), policy=policy.default)
+ I think I forgot the ring.
+ """), policy=policy.default)
- def test_deepcopy(self):
- msg2 = copy.deepcopy(self.testmsg)
- self.assertEqual(msg2.as_string(), self.testmsg.as_string())
+ msgs['created'] = email.message.Message(policy=policy.default)
+ msgs['created']['Date'] = 'Tue, 29 May 2012 09:24:26 +1000'
+ msgs['created']['From'] = 'frodo@mordor.net'
+ msgs['created']['To'] = 'bilbo@underhill.org'
+ msgs['created']['Subject'] = 'help'
+ msgs['created'].set_payload('I think I forgot the ring.')
- def test_pickle(self):
- p = pickle.dumps(self.testmsg)
+ def _test_deepcopy(self, msg):
+ msg2 = copy.deepcopy(msg)
+ self.assertEqual(msg2.as_string(), msg.as_string())
+
+ def _test_pickle(self, msg):
+ p = pickle.dumps(msg)
msg2 = pickle.loads(p)
- self.assertEqual(msg2.as_string(), self.testmsg.as_string())
+ self.assertEqual(msg2.as_string(), msg.as_string())
+
+ for name, msg in msgs.items():
+ locals()['test_deepcopy_'+name] = (
+ lambda self, msg=msg:
+ self._test_deepcopy(msg))
+ for name, msg in msgs.items():
+ locals()['test_pickle_'+name] = (
+ lambda self, msg=msg:
+ self._test_pickle(msg))
if __name__ == '__main__':