aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2019-10-12 00:05:28 +0200
committerGravatar Johannes Christ <[email protected]>2019-10-12 00:05:30 +0200
commit7625d2abf5ac358ccb79a140e0227d2a51aa06cf (patch)
tree5772d5cf53faa9c38f68a78775dd8b20c7d39737
parentMerge branch 'master' into bot-utils-time-tests (diff)
Raise `ValueError` on negative `max_units`.
-rw-r--r--bot/utils/time.py3
-rw-r--r--tests/utils/test_time.py9
2 files changed, 9 insertions, 3 deletions
diff --git a/bot/utils/time.py b/bot/utils/time.py
index 183eff986..2aea2c099 100644
--- a/bot/utils/time.py
+++ b/bot/utils/time.py
@@ -35,6 +35,9 @@ def humanize_delta(delta: relativedelta, precision: str = "seconds", max_units:
precision specifies the smallest unit of time to include (e.g. "seconds", "minutes").
max_units specifies the maximum number of units of time to include (e.g. 1 may include days but not hours).
"""
+ if max_units <= 0:
+ raise ValueError("max_units must be positive")
+
units = (
("years", delta.years),
("months", delta.months),
diff --git a/tests/utils/test_time.py b/tests/utils/test_time.py
index 61dd55c4a..4baa6395c 100644
--- a/tests/utils/test_time.py
+++ b/tests/utils/test_time.py
@@ -24,9 +24,6 @@ from tests.helpers import AsyncMock
# Very high maximum units, but it only ever iterates over
# each value the relativedelta might have.
(relativedelta(days=2, hours=2), 'hours', 20, '2 days and 2 hours'),
-
- # Negative maximum units.
- (relativedelta(days=2, hours=2), 'hours', -1, 'less than a hour'),
)
)
def test_humanize_delta(
@@ -38,6 +35,12 @@ def test_humanize_delta(
assert time.humanize_delta(delta, precision, max_units) == expected
[email protected]('max_units', (-1, 0))
+def test_humanize_delta_raises_for_invalid_max_units(max_units: int):
+ with pytest.raises(ValueError, match='max_units must be positive'):
+ time.humanize_delta(relativedelta(days=2, hours=2), 'hours', max_units)
+
+
@pytest.mark.parametrize(
('stamp', 'expected'),
(