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

Python code coverage for Lib/test/test_email/torture_test.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/a
14n/afrom test.test_email import TestEmailBase
15n/afrom test.support import run_unittest
16n/a
17n/aimport email
18n/afrom email import __file__ as testfile
19n/afrom email.iterators import _structure
20n/a
21n/adef openfile(filename):
22n/a from os.path import join, dirname, abspath
23n/a path = abspath(join(dirname(testfile), os.pardir, 'moredata', filename))
24n/a return open(path, 'r')
25n/a
26n/a# Prevent this test from running in the Python distro
27n/atry:
28n/a openfile('crispin-torture.txt')
29n/aexcept OSError:
30n/a raise unittest.SkipTest
31n/a
32n/a
33n/a
34n/aclass TortureBase(TestEmailBase):
35n/a def _msgobj(self, filename):
36n/a fp = openfile(filename)
37n/a try:
38n/a msg = email.message_from_file(fp)
39n/a finally:
40n/a fp.close()
41n/a return msg
42n/a
43n/a
44n/a
45n/aclass TestCrispinTorture(TortureBase):
46n/a # Mark Crispin's torture test from the SquirrelMail project
47n/a def test_mondo_message(self):
48n/a eq = self.assertEqual
49n/a neq = self.ndiffAssertEqual
50n/a msg = self._msgobj('crispin-torture.txt')
51n/a payload = msg.get_payload()
52n/a eq(type(payload), list)
53n/a eq(len(payload), 12)
54n/a eq(msg.preamble, None)
55n/a eq(msg.epilogue, '\n')
56n/a # Probably the best way to verify the message is parsed correctly is to
57n/a # dump its structure and compare it against the known structure.
58n/a fp = StringIO()
59n/a _structure(msg, fp=fp)
60n/a neq(fp.getvalue(), """\
61n/amultipart/mixed
62n/a text/plain
63n/a message/rfc822
64n/a multipart/alternative
65n/a text/plain
66n/a multipart/mixed
67n/a text/richtext
68n/a application/andrew-inset
69n/a message/rfc822
70n/a audio/basic
71n/a audio/basic
72n/a image/pbm
73n/a message/rfc822
74n/a multipart/mixed
75n/a multipart/mixed
76n/a text/plain
77n/a audio/x-sun
78n/a multipart/mixed
79n/a image/gif
80n/a image/gif
81n/a application/x-be2
82n/a application/atomicmail
83n/a audio/x-sun
84n/a message/rfc822
85n/a multipart/mixed
86n/a text/plain
87n/a image/pgm
88n/a text/plain
89n/a message/rfc822
90n/a multipart/mixed
91n/a text/plain
92n/a image/pbm
93n/a message/rfc822
94n/a application/postscript
95n/a image/gif
96n/a message/rfc822
97n/a multipart/mixed
98n/a audio/basic
99n/a audio/basic
100n/a message/rfc822
101n/a multipart/mixed
102n/a application/postscript
103n/a text/plain
104n/a message/rfc822
105n/a multipart/mixed
106n/a text/plain
107n/a multipart/parallel
108n/a image/gif
109n/a audio/basic
110n/a application/atomicmail
111n/a message/rfc822
112n/a audio/x-sun
113n/a""")
114n/a
115n/adef _testclasses():
116n/a mod = sys.modules[__name__]
117n/a return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')]
118n/a
119n/a
120n/adef suite():
121n/a suite = unittest.TestSuite()
122n/a for testclass in _testclasses():
123n/a suite.addTest(unittest.makeSuite(testclass))
124n/a return suite
125n/a
126n/a
127n/adef test_main():
128n/a for testclass in _testclasses():
129n/a run_unittest(testclass)
130n/a
131n/a
132n/aif __name__ == '__main__':
133n/a unittest.main(defaultTest='suite')