ยปCore Development>Code coverage>Lib/test/test_email/test_pickleable.py

Python code coverage for Lib/test/test_email/test_pickleable.py

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