From f1adf75b59d51651d0ad6a0e53c00b5bb33ebe99 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Thu, 12 Sep 2013 23:12:18 -0400 Subject: [PATCH] ENH: basic testing for iso8601 code which had no explicit tests + spit out ValueError for incorrect type of input and ParseError otherwise --- fail2ban-testcases | 1 + server/iso8601.py | 13 +++++++++---- testcases/misctestcase.py | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/fail2ban-testcases b/fail2ban-testcases index 5aed6e43..7088d068 100755 --- a/fail2ban-testcases +++ b/fail2ban-testcases @@ -167,6 +167,7 @@ tests.addTest(unittest.makeSuite(sockettestcase.Socket)) tests.addTest(unittest.makeSuite(misctestcase.HelpersTest)) tests.addTest(unittest.makeSuite(misctestcase.SetupTest)) tests.addTest(unittest.makeSuite(misctestcase.TestsUtilsTest)) +tests.addTest(unittest.makeSuite(misctestcase.CustomDateFormatsTest)) # Filter if not opts.no_network: diff --git a/server/iso8601.py b/server/iso8601.py index a42989ea..3d788274 100644 --- a/server/iso8601.py +++ b/server/iso8601.py @@ -111,7 +111,7 @@ def parse_date(datestring, default_timezone=UTC): default. """ if not isinstance(datestring, basestring): - raise ParseError("Expecting a string %r" % datestring) + raise ValueError("Expecting a string %r" % datestring) m = ISO8601_REGEX.match(datestring) if not m: raise ParseError("Unable to parse date string %r" % datestring) @@ -121,6 +121,11 @@ def parse_date(datestring, default_timezone=UTC): groups["fraction"] = 0 else: groups["fraction"] = int(float("0.%s" % groups["fraction"]) * 1e6) - return datetime(int(groups["year"]), int(groups["month"]), int(groups["day"]), - int(groups["hour"]), int(groups["minute"]), int(groups["second"]), - int(groups["fraction"]), tz) + + try: + return datetime(int(groups["year"]), int(groups["month"]), int(groups["day"]), + int(groups["hour"]), int(groups["minute"]), int(groups["second"]), + int(groups["fraction"]), tz) + except Exception, e: + raise ParseError("Failed to create a valid datetime record due to: %s" + % e) diff --git a/testcases/misctestcase.py b/testcases/misctestcase.py index 08c8fc30..747f83e4 100644 --- a/testcases/misctestcase.py +++ b/testcases/misctestcase.py @@ -167,3 +167,29 @@ class TestsUtilsTest(unittest.TestCase): # in this case compressed and not should be the same (?) self.assertTrue(pindex > 10) # we should have some traceback self.assertEqual(s[:pindex], s[pindex+1:pindex*2 + 1]) + +from server import iso8601 +import datetime + +class CustomDateFormatsTest(unittest.TestCase): + + def testIso8601(self): + date = iso8601.parse_date("2007-01-25T12:00:00Z") + self.assertEqual( + date, + datetime.datetime(2007, 1, 25, 12, 0, tzinfo=iso8601.Utc())) + self.assertRaises(ValueError, iso8601.parse_date, None) + self.assertRaises(ValueError, iso8601.parse_date, date) + + self.assertRaises(iso8601.ParseError, iso8601.parse_date, "") + self.assertRaises(iso8601.ParseError, iso8601.parse_date, "Z") + + self.assertRaises(iso8601.ParseError, + iso8601.parse_date, "2007-01-01T120:00:00Z") + self.assertRaises(iso8601.ParseError, + iso8601.parse_date, "2007-13-01T12:00:00Z") + + def testTimeZone(self): + # Just verify consistent operation and improve coverage ;) + self.assertEqual(iso8601.parse_timezone(None), iso8601.UTC) + self.assertEqual(iso8601.parse_timezone('Z'), iso8601.UTC)