| 1 | n/a | import unittest |
|---|
| 2 | n/a | import textwrap |
|---|
| 3 | n/a | import copy |
|---|
| 4 | n/a | import pickle |
|---|
| 5 | n/a | import email |
|---|
| 6 | n/a | import email.message |
|---|
| 7 | n/a | from email import policy |
|---|
| 8 | n/a | from email.headerregistry import HeaderRegistry |
|---|
| 9 | n/a | from test.test_email import TestEmailBase, parameterize |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | @parameterize |
|---|
| 13 | n/a | class TestPickleCopyHeader(TestEmailBase): |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | header_factory = HeaderRegistry() |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | unstructured = header_factory('subject', 'this is a test') |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | header_params = { |
|---|
| 20 | n/a | 'subject': ('subject', 'this is a test'), |
|---|
| 21 | n/a | 'from': ('from', 'frodo@mordor.net'), |
|---|
| 22 | n/a | 'to': ('to', 'a: k@b.com, y@z.com;, j@f.com'), |
|---|
| 23 | n/a | 'date': ('date', 'Tue, 29 May 2012 09:24:26 +1000'), |
|---|
| 24 | n/a | } |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | def header_as_deepcopy(self, name, value): |
|---|
| 27 | n/a | header = self.header_factory(name, value) |
|---|
| 28 | n/a | h = copy.deepcopy(header) |
|---|
| 29 | n/a | self.assertEqual(str(h), str(header)) |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | def header_as_pickle(self, name, value): |
|---|
| 32 | n/a | header = self.header_factory(name, value) |
|---|
| 33 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
|---|
| 34 | n/a | p = pickle.dumps(header, proto) |
|---|
| 35 | n/a | h = pickle.loads(p) |
|---|
| 36 | n/a | self.assertEqual(str(h), str(header)) |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | @parameterize |
|---|
| 40 | n/a | class TestPickleCopyMessage(TestEmailBase): |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | # Message objects are a sequence, so we have to make them a one-tuple in |
|---|
| 43 | n/a | # msg_params so they get passed to the parameterized test method as a |
|---|
| 44 | n/a | # single argument instead of as a list of headers. |
|---|
| 45 | n/a | msg_params = {} |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | # Note: there will be no custom header objects in the parsed message. |
|---|
| 48 | n/a | msg_params['parsed'] = (email.message_from_string(textwrap.dedent("""\ |
|---|
| 49 | n/a | Date: Tue, 29 May 2012 09:24:26 +1000 |
|---|
| 50 | n/a | From: frodo@mordor.net |
|---|
| 51 | n/a | To: bilbo@underhill.org |
|---|
| 52 | n/a | Subject: help |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | I think I forgot the ring. |
|---|
| 55 | n/a | """), policy=policy.default),) |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | msg_params['created'] = (email.message.Message(policy=policy.default),) |
|---|
| 58 | n/a | msg_params['created'][0]['Date'] = 'Tue, 29 May 2012 09:24:26 +1000' |
|---|
| 59 | n/a | msg_params['created'][0]['From'] = 'frodo@mordor.net' |
|---|
| 60 | n/a | msg_params['created'][0]['To'] = 'bilbo@underhill.org' |
|---|
| 61 | n/a | msg_params['created'][0]['Subject'] = 'help' |
|---|
| 62 | n/a | msg_params['created'][0].set_payload('I think I forgot the ring.') |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | def msg_as_deepcopy(self, msg): |
|---|
| 65 | n/a | msg2 = copy.deepcopy(msg) |
|---|
| 66 | n/a | self.assertEqual(msg2.as_string(), msg.as_string()) |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def msg_as_pickle(self, msg): |
|---|
| 69 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
|---|
| 70 | n/a | p = pickle.dumps(msg, proto) |
|---|
| 71 | n/a | msg2 = pickle.loads(p) |
|---|
| 72 | n/a | self.assertEqual(msg2.as_string(), msg.as_string()) |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | if __name__ == '__main__': |
|---|
| 76 | n/a | unittest.main() |
|---|