ยปCore Development>Code coverage>Lib/email/test/test_email_torture.py

Python code coverage for Lib/email/test/test_email_torture.py

#countcontent
1n/a# Copyright (C) 2002-2004 Python Software Foundation
2n/a#
3n/a# A torture test of the email package. This should not be run as part of the
4n/a# standard Python test suite since it requires several meg of email messages
5n/a# collected in the wild. These source messages are not checked into the
6n/a# Python distro, but are available as part of the standalone email package at
7n/a# http://sf.net/projects/mimelib
8n/a
9n/aimport sys
10n/aimport os
11n/aimport unittest
12n/afrom io import StringIO
13n/afrom types import ListType
14n/a
15n/afrom email.test.test_email import TestEmailBase
16n/afrom test.support import TestSkipped, run_unittest
17n/a
18n/aimport email
19n/afrom email import __file__ as testfile
20n/afrom email.iterators import _structure
21n/a
22n/adef openfile(filename):
23n/a from os.path import join, dirname, abspath
24n/a path = abspath(join(dirname(testfile), os.pardir, 'moredata', filename))
25n/a return open(path, 'r')
26n/a
27n/a# Prevent this test from running in the Python distro
28n/atry:
29n/a openfile('crispin-torture.txt')
30n/aexcept IOError:
31n/a raise TestSkipped
32n/a
33n/a
34n/a
35n/aclass TortureBase(TestEmailBase):
36n/a def _msgobj(self, filename):
37n/a fp = openfile(filename)
38n/a try:
39n/a msg = email.message_from_file(fp)
40n/a finally:
41n/a fp.close()
42n/a return msg
43n/a
44n/a
45n/a
46n/aclass TestCrispinTorture(TortureBase):
47n/a # Mark Crispin's torture test from the SquirrelMail project
48n/a def test_mondo_message(self):
49n/a eq = self.assertEqual
50n/a neq = self.ndiffAssertEqual
51n/a msg = self._msgobj('crispin-torture.txt')
52n/a payload = msg.get_payload()
53n/a eq(type(payload), ListType)
54n/a eq(len(payload), 12)
55n/a eq(msg.preamble, None)
56n/a eq(msg.epilogue, '\n')
57n/a # Probably the best way to verify the message is parsed correctly is to
58n/a # dump its structure and compare it against the known structure.
59n/a fp = StringIO()
60n/a _structure(msg, fp=fp)
61n/a neq(fp.getvalue(), """\
62n/amultipart/mixed
63n/a text/plain
64n/a message/rfc822
65n/a multipart/alternative
66n/a text/plain
67n/a multipart/mixed
68n/a text/richtext
69n/a application/andrew-inset
70n/a message/rfc822
71n/a audio/basic
72n/a audio/basic
73n/a image/pbm
74n/a message/rfc822
75n/a multipart/mixed
76n/a multipart/mixed
77n/a text/plain
78n/a audio/x-sun
79n/a multipart/mixed
80n/a image/gif
81n/a image/gif
82n/a application/x-be2
83n/a application/atomicmail
84n/a audio/x-sun
85n/a message/rfc822
86n/a multipart/mixed
87n/a text/plain
88n/a image/pgm
89n/a text/plain
90n/a message/rfc822
91n/a multipart/mixed
92n/a text/plain
93n/a image/pbm
94n/a message/rfc822
95n/a application/postscript
96n/a image/gif
97n/a message/rfc822
98n/a multipart/mixed
99n/a audio/basic
100n/a audio/basic
101n/a message/rfc822
102n/a multipart/mixed
103n/a application/postscript
104n/a text/plain
105n/a message/rfc822
106n/a multipart/mixed
107n/a text/plain
108n/a multipart/parallel
109n/a image/gif
110n/a audio/basic
111n/a application/atomicmail
112n/a message/rfc822
113n/a audio/x-sun
114n/a""")
115n/a
116n/a
117n/adef _testclasses():
118n/a mod = sys.modules[__name__]
119n/a return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')]
120n/a
121n/a
122n/adef suite():
123n/a suite = unittest.TestSuite()
124n/a for testclass in _testclasses():
125n/a suite.addTest(unittest.makeSuite(testclass))
126n/a return suite
127n/a
128n/a
129n/adef test_main():
130n/a for testclass in _testclasses():
131n/a run_unittest(testclass)
132n/a
133n/a
134n/a
135n/aif __name__ == '__main__':
136n/a unittest.main(defaultTest='suite')