PyXR

c:\python24\lib \ test \ test_mailbox.py



0001 import mailbox
0002 import os
0003 import time
0004 import unittest
0005 from test import test_support
0006 
0007 # cleanup earlier tests
0008 try:
0009     os.unlink(test_support.TESTFN)
0010 except os.error:
0011     pass
0012 
0013 FROM_ = "From some.body@dummy.domain  Sat Jul 24 13:43:35 2004\n"
0014 DUMMY_MESSAGE = """\
0015 From: some.body@dummy.domain
0016 To: me@my.domain
0017 Subject: Simple Test
0018 
0019 This is a dummy message.
0020 """
0021 
0022 class MaildirTestCase(unittest.TestCase):
0023 
0024     def setUp(self):
0025         # create a new maildir mailbox to work with:
0026         self._dir = test_support.TESTFN
0027         os.mkdir(self._dir)
0028         os.mkdir(os.path.join(self._dir, "cur"))
0029         os.mkdir(os.path.join(self._dir, "tmp"))
0030         os.mkdir(os.path.join(self._dir, "new"))
0031         self._counter = 1
0032         self._msgfiles = []
0033 
0034     def tearDown(self):
0035         map(os.unlink, self._msgfiles)
0036         os.rmdir(os.path.join(self._dir, "cur"))
0037         os.rmdir(os.path.join(self._dir, "tmp"))
0038         os.rmdir(os.path.join(self._dir, "new"))
0039         os.rmdir(self._dir)
0040 
0041     def createMessage(self, dir, mbox=False):
0042         t = int(time.time() % 1000000)
0043         pid = self._counter
0044         self._counter += 1
0045         filename = os.extsep.join((str(t), str(pid), "myhostname", "mydomain"))
0046         tmpname = os.path.join(self._dir, "tmp", filename)
0047         newname = os.path.join(self._dir, dir, filename)
0048         fp = open(tmpname, "w")
0049         self._msgfiles.append(tmpname)
0050         if mbox:
0051             fp.write(FROM_)
0052         fp.write(DUMMY_MESSAGE)
0053         fp.close()
0054         if hasattr(os, "link"):
0055             os.link(tmpname, newname)
0056         else:
0057             fp = open(newname, "w")
0058             fp.write(DUMMY_MESSAGE)
0059             fp.close()
0060         self._msgfiles.append(newname)
0061         return tmpname
0062 
0063     def test_empty_maildir(self):
0064         """Test an empty maildir mailbox"""
0065         # Test for regression on bug #117490:
0066         # Make sure the boxes attribute actually gets set.
0067         self.mbox = mailbox.Maildir(test_support.TESTFN)
0068         self.assert_(hasattr(self.mbox, "boxes"))
0069         self.assert_(len(self.mbox.boxes) == 0)
0070         self.assert_(self.mbox.next() is None)
0071         self.assert_(self.mbox.next() is None)
0072 
0073     def test_nonempty_maildir_cur(self):
0074         self.createMessage("cur")
0075         self.mbox = mailbox.Maildir(test_support.TESTFN)
0076         self.assert_(len(self.mbox.boxes) == 1)
0077         self.assert_(self.mbox.next() is not None)
0078         self.assert_(self.mbox.next() is None)
0079         self.assert_(self.mbox.next() is None)
0080 
0081     def test_nonempty_maildir_new(self):
0082         self.createMessage("new")
0083         self.mbox = mailbox.Maildir(test_support.TESTFN)
0084         self.assert_(len(self.mbox.boxes) == 1)
0085         self.assert_(self.mbox.next() is not None)
0086         self.assert_(self.mbox.next() is None)
0087         self.assert_(self.mbox.next() is None)
0088 
0089     def test_nonempty_maildir_both(self):
0090         self.createMessage("cur")
0091         self.createMessage("new")
0092         self.mbox = mailbox.Maildir(test_support.TESTFN)
0093         self.assert_(len(self.mbox.boxes) == 2)
0094         self.assert_(self.mbox.next() is not None)
0095         self.assert_(self.mbox.next() is not None)
0096         self.assert_(self.mbox.next() is None)
0097         self.assert_(self.mbox.next() is None)
0098 
0099     def test_unix_mbox(self):
0100         ### should be better!
0101         import email.Parser
0102         fname = self.createMessage("cur", True)
0103         n = 0
0104         for msg in mailbox.PortableUnixMailbox(open(fname),
0105                                                email.Parser.Parser().parse):
0106             n += 1
0107             self.assertEqual(msg["subject"], "Simple Test")
0108             self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE))
0109         self.assertEqual(n, 1)
0110 
0111     # XXX We still need more tests!
0112 
0113 
0114 def test_main():
0115     test_support.run_unittest(MaildirTestCase)
0116 
0117 
0118 if __name__ == "__main__":
0119     test_main()
0120 

Generated by PyXR 0.9.4
SourceForge.net Logo