Style improvements and cache from settings validation added
[imap-fix-internaldate] / src / unit_tester.py
index 13da94b..56f6efc 100644 (file)
@@ -16,94 +16,97 @@ GNU General Public License for more details.
 '''
 
 import unittest
-import datetime, date_interpreter
+import datetime
+from mail_date_parser import MailDateParser
 
 class MailScriptTester(unittest.TestCase):
+    """Used to test the date retrievel and the MailDateParser class."""
 
     # class attributes
     # DateInterpreter instance testing the DateInterpreter methods
-    date_interp = None
+    date_interp = MailDateParser()
     # datetime for comparison with extracted datetimes and assertions
-    true_date = None
+    true_date = datetime.datetime(2007, 12, 11, 18, 24, 35)
 
-    def setUp(self):
-        self.date_interp = date_interpreter.DateInterpreter()
-        self.true_date = datetime.datetime(2007, 12, 11, 18, 24, 35)
+    def set_up(self):
+        """Prepares the testing confitions."""
+        # tester has problems if object is set to none so
+        # attributes are prepared above
 
     def test_received_date_extraction1(self):
         """Tests the date extraction method."""
-        date = [[0, b"Tue, 11 Dec 2007 18:24:35 +0100"]]
+        date = "Tue, 11 Dec 2007 18:24:35 +0100"
         extracted_date = self.date_interp.extract_received_date(date)
         self.assertEqual(extracted_date, self.true_date, "Failed date format 1")
 
     def test_received_date_extraction2(self):
         """Tests the date extraction method."""
-        date = [[0, b"11 Dec 2007 \r\n18:24:35 +0100"]]
+        date = "11 Dec 2007 \r\n18:24:35 +0100"
         extracted_date = self.date_interp.extract_received_date(date)
         self.assertEqual(extracted_date, self.true_date, "Failed date format 2")
         return
 
     def test_received_date_extraction3(self):
         """Tests the date extraction method."""  
-        date = [[0, b"11 Dec 2007 18:24:35 +0100"]]
+        date = "11 Dec 2007 18:24:35 +0100"
         extracted_date = self.date_interp.extract_received_date(date)
         self.assertEqual(extracted_date, self.true_date, "Failed date format 3")
 
     def test_received_date_extraction4(self):
         """Tests the date extraction method."""
-        date = [[0, b"11 Dec 2007 18:24:35"]]
+        date = "11 Dec 2007 18:24:35"
         extracted_date = self.date_interp.extract_received_date(date)
         #should not be equal because of time zone assumption
         self.assertNotEqual(extracted_date, self.true_date, "Failed date format 4")
 
     def test_received_date_extraction5(self):
         """Tests the received date extraction method."""
-        date = [[0, b"11 Dec 2007 18:24:35 GMT"]]
+        date = "11 Dec 2007 18:24:35 GMT"
         extracted_date = self.date_interp.extract_received_date(date)
         #should not be equal because of time zone assumption
         self.assertNotEqual(extracted_date, self.true_date, "Failed date format 5")
 
     def test_received_date_extraction6(self):
         """Tests the received date extraction method."""
-        date = [[0, b'Received: from intranator.m.i2n ([unix socket])'
-                b'by intranator.m.i2n with LMTPA; Tue, 11 Dec 2007 18:24:35'
-                b'+0100Received: from localhost (intranator.m.i2n [127.0.0.1])'
-                b'by localhost (Postfix) with ESMTP id 895812AC54for <intra2net_thomas@intranator.m.i2n>;'
-                b'Sun, 13 Mar 2011 18:47:18 +0100 (CET)Received: from re04.intra2net.com '
-                b'(re04.intra2net.com [82.165.46.26])(using TLSv1 with cipher ADH-AES256-SHA '
-                b'(256/256 bits))(No client certificate requested)by intranator.m.i2n (Postfix) with '
-                b'ESMTPS id 28DB92AC53for <thomas.jarosch@intra2net.com>; Sun, 13 Mar 2011 18:47:15 +0100 '
-                b'(CET)Received: from postfix.charite.de (postfix.charite.de [141.42.206.35])(using TLSv1 '
-                b'with cipher ADH-AES256-SHA (256/256 bits))(No client certificate requested)by '
-                b're04.intra2net.com (Postfix) with ESMTP id C054A3010Afor <thomas.jarosch@intra2net.com>; '
-                b'Sun, 13 Mar 2011 18:47:14 +0100 (CET)Received: from localhost (localhost [127.0.0.1])by '
-                b'de.postfix.org (Postfix) with ESMTP id 7FCCFF7879for <thomas.jarosch@intra2net.com>; '
-                b'Sun, 13 Mar 2011 18:47:14 +0100 (CET)Received: from de.postfix.org ([127.0.0.1])by '
-                b'localhost (de.postfix.org [127.0.0.1]) (amavisd-new, port 10026)with LMTP id '
-                b'YSXF-vf3+6E1 for <thomas.jarosch@intra2net.com>;Sun, 13 Mar 2011 18:47:14 +0100 (CET)'
-                b'Received: from de.postfix.org (localhost [127.0.0.1])by de.postfix.org (Postfix) with '
-                b'ESMTP id 3C3123DF1Efor <thomas.jarosch@intra2net.com>; Sun, 13 Mar 2011 18:46:33 +0100 '
-                b'(CET)Received: from localhost (localhost [127.0.0.1])by de.postfix.org (Postfix) with '
-                b'ESMTP id AB6CE3DBD2for <amavis-users@amavis.org>; Sun, 13 Mar 2011 18:45:57 +0100 (CET)'
-                b'Received: from de.postfix.org ([127.0.0.1])by localhost (de.postfix.org [127.0.0.1]) '
-                b'(amavisd-new, port 10024)with ESMTP id mBYiZO8wREeS for <amavis-users@amavis.org>;Sun, '
-                b'13 Mar 2011 18:45:56 +0100 (CET)Received: from mail.inetmsg.com (mail.inetmsg.com '
-                b'[173.10.94.185])by de.postfix.org (Postfix) with ESMTPSfor <amavis-users@amavis.org>; '
-                b'Sun, 13 Mar 2011 18:45:55 +0100 (CET)Received: from [192.168.1.107] (fw1.inetmsg.com '
-                b'[10.20.30.253])(using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits))'
-                b'(No client certificate requested)by mail.inetmsg.com (INetMsg Mail Service) with ESMTPSA '
-                b'id 0B95326CD1for <amavis-users@amavis.org>; Sun, 13 Mar 2011 10:45:41 -0700 (PDT)"]]']]
+        date = 'Received: from intranator.m.i2n ([unix socket])'\
+            'by intranator.m.i2n with LMTPA; Tue, 11 Dec 2007 18:24:35'\
+            '+0100Received: from localhost (intranator.m.i2n [127.0.0.1])'\
+            'by localhost (Postfix) with ESMTP id 895812AC54for <intra2net_thomas@intranator.m.i2n>;'\
+            'Sun, 13 Mar 2011 18:47:18 +0100 (CET)Received: from re04.intra2net.com '\
+            '(re04.intra2net.com [82.165.46.26])(using TLSv1 with cipher ADH-AES256-SHA '\
+            '(256/256 bits))(No client certificate requested)by intranator.m.i2n (Postfix) with '\
+            'ESMTPS id 28DB92AC53for <thomas.jarosch@intra2net.com>; Sun, 13 Mar 2011 18:47:15 +0100 '\
+            '(CET)Received: from postfix.charite.de (postfix.charite.de [141.42.206.35])(using TLSv1 '\
+            'with cipher ADH-AES256-SHA (256/256 bits))(No client certificate requested)by '\
+            're04.intra2net.com (Postfix) with ESMTP id C054A3010Afor <thomas.jarosch@intra2net.com>; '\
+            'Sun, 13 Mar 2011 18:47:14 +0100 (CET)Received: from localhost (localhost [127.0.0.1])by '\
+            'de.postfix.org (Postfix) with ESMTP id 7FCCFF7879for <thomas.jarosch@intra2net.com>; '\
+            'Sun, 13 Mar 2011 18:47:14 +0100 (CET)Received: from de.postfix.org ([127.0.0.1])by '\
+            'localhost (de.postfix.org [127.0.0.1]) (amavisd-new, port 10026)with LMTP id '\
+            'YSXF-vf3+6E1 for <thomas.jarosch@intra2net.com>;Sun, 13 Mar 2011 18:47:14 +0100 (CET)'\
+            'Received: from de.postfix.org (localhost [127.0.0.1])by de.postfix.org (Postfix) with '\
+            'ESMTP id 3C3123DF1Efor <thomas.jarosch@intra2net.com>; Sun, 13 Mar 2011 18:46:33 +0100 '\
+            '(CET)Received: from localhost (localhost [127.0.0.1])by de.postfix.org (Postfix) with '\
+            'ESMTP id AB6CE3DBD2for <amavis-users@amavis.org>; Sun, 13 Mar 2011 18:45:57 +0100 (CET)'\
+            'Received: from de.postfix.org ([127.0.0.1])by localhost (de.postfix.org [127.0.0.1]) '\
+            '(amavisd-new, port 10024)with ESMTP id mBYiZO8wREeS for <amavis-users@amavis.org>;Sun, '\
+            '13 Mar 2011 18:45:56 +0100 (CET)Received: from mail.inetmsg.com (mail.inetmsg.com '\
+            '[173.10.94.185])by de.postfix.org (Postfix) with ESMTPSfor <amavis-users@amavis.org>; '\
+            'Sun, 13 Mar 2011 18:45:55 +0100 (CET)Received: from [192.168.1.107] (fw1.inetmsg.com '\
+            '[10.20.30.253])(using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits))'\
+            '(No client certificate requested)by mail.inetmsg.com (INetMsg Mail Service) with ESMTPSA '\
+            'id 0B95326CD1for <amavis-users@amavis.org>; Sun, 13 Mar 2011 10:45:41 -0700 (PDT)"]]'
         extracted_date = self.date_interp.extract_received_date(date)
         #should not be equal because of time zone assumption
         self.assertEqual(extracted_date, self.true_date, "Failed date format 6")
 
     def test_compare_dates(self):
         """Tests the date comparison method."""
-        self.true_date2 = datetime.datetime(2007, 12, 11, 18, 34, 35)
+        true_date2 = datetime.datetime(2007, 12, 11, 18, 34, 35)
         #is difference of 10 mins significant if tolerance is 9 mins
-        self.assertTrue(bool(self.date_interp.compare_dates(self.true_date, self.true_date2, 9*60)), "Failed at comparison test")
+        self.assertTrue(bool(self.date_interp.compare_dates(self.true_date, true_date2, 9*60)), "Failed at comparison test")
         #is difference of 10 mins significant if tolerance is 11 mins
-        self.assertFalse(bool(self.date_interp.compare_dates(self.true_date, self.true_date2, 11*60)), "Failed at comparison test")
+        self.assertFalse(bool(self.date_interp.compare_dates(self.true_date, true_date2, 11*60)), "Failed at comparison test")
 
 if __name__ == '__main__':
     unittest.main()