lib: test edgecase for timedelta_from_string

This commit is contained in:
Jens Langhammer 2020-12-13 18:35:51 +01:00
parent 1c0a6efeb1
commit f056b026d6
4 changed files with 4 additions and 0 deletions

View File

View File

@ -20,6 +20,8 @@ class TestTimeUtils(TestCase):
"""Test invalid expression"""
with self.assertRaises(ValueError):
timedelta_from_string("foo")
with self.assertRaises(ValueError):
timedelta_from_string("bar=baz")
def test_validation(self):
"""Test Django model field validator"""

View File

@ -35,4 +35,6 @@ def timedelta_from_string(expr: str) -> datetime.timedelta:
if key.lower() not in ALLOWED_KEYS:
continue
kwargs[key.lower()] = float(value)
if len(kwargs) < 1:
raise ValueError("No valid keys to pass to timedelta")
return datetime.timedelta(**kwargs)