Skip to content

Commit

Permalink
add+collect tests for tzinfos input types, fix missed case of invalid…
Browse files Browse the repository at this point in the history
… tzinfo object
  • Loading branch information
jbrockmendel committed Feb 26, 2019
1 parent fdc91e1 commit 8e9765b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
3 changes: 3 additions & 0 deletions dateutil/parser/_parser.py
Expand Up @@ -1166,6 +1166,9 @@ def _build_tzinfo(self, tzinfos, tzname, tzoffset):
tzinfo = tz.tzstr(tzdata)
elif isinstance(tzdata, integer_types):
tzinfo = tz.tzoffset(tzname, tzdata)
else:
raise TypeError("Offset must be tzinfo subclass, tz string, "
"or int offset.")
return tzinfo

def _build_tzaware(self, naive, res, tzinfos):
Expand Down
62 changes: 54 additions & 8 deletions dateutil/test/test_parser.py
Expand Up @@ -238,6 +238,60 @@ def test_parse_bytearray(self):
assert res == expected


class TestTzinfoInputTypes(object):
def test_tzinfo_dict_could_return_none(self):
dstr = "2017-02-03 12:40 BRST"
result = parse(dstr, tzinfos={"BRST": None})
expected = datetime(2017, 2, 3, 12, 40)
assert result == expected

def test_tzinfos_callable_could_return_none(self):
dstr = "2017-02-03 12:40 BRST"
result = parse(dstr, tzinfos=lambda *args: None)
expected = datetime(2017, 2, 3, 12, 40)
assert result == expected

def test_invalid_tzinfo_input(self):
dstr = "2014 January 19 09:00 UTC"
# Pass an absurd tzinfos object
tzinfos = {"UTC": ValueError}
with pytest.raises(TypeError):
parse(dstr, tzinfos=tzinfos)

def test_valid_tzinfo_tzinfo_input(self):
dstr = "2014 January 19 09:00 UTC"
tzinfos = {"UTC": tz.tzutc()}
expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzutc())
res = parse(dstr, tzinfos=tzinfos)
assert res == expected

def test_valid_tzinfo_unicode_input(self):
dstr = "2014 January 19 09:00 UTC"
tzinfos = {u"UTC": u"UTC+0"}
expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzutc())
res = parse(dstr, tzinfos=tzinfos)
assert res == expected

def test_valid_tzinfo_callable_input(self):
dstr = "2014 January 19 09:00 UTC"

def tzinfos(*args, **kwargs):
return u"UTC+0"

expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzutc())
res = parse(dstr, tzinfos=tzinfos)
assert res == expected

@pytest.mark.xfail(reason="Doesn't check for UTC in tzinfos dict, "
"not sure it *should*.")
def test_valid_tzinfo_int_input(self):
dstr = "2014 January 19 09:00 UTC"
tzinfos = {u"UTC": -28800}
expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzutc())
res = parse(dstr, tzinfos=tzinfos)
assert res == expected


class ParserTest(unittest.TestCase):

@classmethod
Expand Down Expand Up @@ -508,14 +562,6 @@ def testUnspecifiedDayFallbackFebLeapYear(self):
self.assertEqual(parse("Feb 2008", default=datetime(2010, 1, 31)),
datetime(2008, 2, 29))

def testTzinfoDictionaryCouldReturnNone(self):
self.assertEqual(parse('2017-02-03 12:40 BRST', tzinfos={"BRST": None}),
datetime(2017, 2, 3, 12, 40))

def testTzinfosCallableCouldReturnNone(self):
self.assertEqual(parse('2017-02-03 12:40 BRST', tzinfos=lambda *args: None),
datetime(2017, 2, 3, 12, 40))

def testErrorType01(self):
with pytest.raises(ValueError):
parse('shouldfail')
Expand Down

0 comments on commit 8e9765b

Please sign in to comment.