''' unit_tester.py - The module contains the MailScriptTester class. Copyright (c) 2012 Intra2net AG Author: Plamen Dimitrov This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ''' import unittest import datetime from mail_date_parser import MailDateParser class DateParseTester(unittest.TestCase): """Used to test the date retrievel and the MailDateParser class.""" # class attributes # DateInterpreter instance testing the DateInterpreter methods date_interp = None # datetime for comparison with extracted datetimes and assertions true_date = None # wrong naming conventions are due to python unittest library def setUp(self): """Prepares the testing confitions.""" self.date_interp = MailDateParser() self.true_date = datetime.datetime(2007, 12, 11, 18, 24, 35) def test_received_date_extraction1(self): """Tests the date extraction method.""" 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 = "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") def test_received_date_extraction3(self): """Tests the date extraction method.""" 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 = "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 = "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 = '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 ;'\ '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 ; 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 ; '\ '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 ; '\ '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 ;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 ; 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 ; 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 ;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 ; '\ '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 ; 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.""" 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, 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, true_date2, 11*60)), "Failed at comparison test") if __name__ == '__main__': unittest.main()