[Python-checkins] cpython: Refactor test_email/test_pickleable and add tests for date headers

r.david.murray python-checkins at python.org
Tue May 29 03:09:25 CEST 2012


http://hg.python.org/cpython/rev/5c40627e9f11
changeset:   77216:5c40627e9f11
user:        R David Murray <rdmurray at bitdance.com>
date:        Mon May 28 21:09:04 2012 -0400
summary:
  Refactor test_email/test_pickleable and add tests for date headers

files:
  Lib/test/test_email/test_pickleable.py |  87 +++++++++----
  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
--- a/Lib/test/test_email/test_pickleable.py
+++ b/Lib/test/test_email/test_pickleable.py
@@ -2,55 +2,86 @@
 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 at mordor.net')
+    headers = (
+        ('subject', 'this is a test'),
+        ('from',    'frodo at mordor.net'),
+        ('to',      'a: k at b.com, y at z.com;, j at 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 at mordor.net
-            To: bilbo at underhill.org
-            Subject: help
+    msgs = {}
 
-            I think I forgot the ring.
-            """), policy=policy.default)
+    # 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 at mordor.net
+        To: bilbo at underhill.org
+        Subject: help
 
-    def test_deepcopy(self):
-        msg2 = copy.deepcopy(self.testmsg)
-        self.assertEqual(msg2.as_string(), self.testmsg.as_string())
+        I think I forgot the ring.
+        """), policy=policy.default)
 
-    def test_pickle(self):
-        p = pickle.dumps(self.testmsg)
+    msgs['created'] = email.message.Message(policy=policy.default)
+    msgs['created']['Date'] = 'Tue, 29 May 2012 09:24:26 +1000'
+    msgs['created']['From'] = 'frodo at mordor.net'
+    msgs['created']['To'] = 'bilbo at underhill.org'
+    msgs['created']['Subject'] = 'help'
+    msgs['created'].set_payload('I think I forgot the ring.')
+
+    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__':

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list