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""" """Test invalid expression"""
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
timedelta_from_string("foo") timedelta_from_string("foo")
with self.assertRaises(ValueError):
timedelta_from_string("bar=baz")
def test_validation(self): def test_validation(self):
"""Test Django model field validator""" """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: if key.lower() not in ALLOWED_KEYS:
continue continue
kwargs[key.lower()] = float(value) kwargs[key.lower()] = float(value)
if len(kwargs) < 1:
raise ValueError("No valid keys to pass to timedelta")
return datetime.timedelta(**kwargs) return datetime.timedelta(**kwargs)