Browse Source

ENH: basic testing for iso8601 code which had no explicit tests + spit out ValueError for incorrect type of input and ParseError otherwise

pull/352/head
Yaroslav Halchenko 11 years ago
parent
commit
f1adf75b59
  1. 1
      fail2ban-testcases
  2. 13
      server/iso8601.py
  3. 26
      testcases/misctestcase.py

1
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:

13
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)

26
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)

Loading…
Cancel
Save