From 24f445931858c795149597e67dcd911faa756753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Mon, 9 Jan 2023 16:14:01 +0100 Subject: [PATCH] Avoid raising exception on format_duration(None) Fixes #25 --- Dockerfile | 2 +- src/web/common/util/time.py | 7 +++++-- tests/test_time.py | 6 ++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6c391a0..edb576c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ RUN npm install && \ FROM python:3.10.9-alpine -ENV VERSION_NUMBER 0.1.2-5-g33e136a +ENV VERSION_NUMBER 0.1.2-6-g751182d ENV PROD 1 RUN apk add \ diff --git a/src/web/common/util/time.py b/src/web/common/util/time.py index 5da1689..38a9aa3 100644 --- a/src/web/common/util/time.py +++ b/src/web/common/util/time.py @@ -9,7 +9,7 @@ def now() -> datetime: def format_duration( - duration: timedelta, format_string: str = "%H hours %m minutes" + duration: timedelta | None, format_string: str = "%H hours %m minutes" ) -> str: """ Format timedelta into the specified format_string. @@ -23,7 +23,10 @@ def format_duration( hour_seconds = 60 * minute_seconds day_seconds = 24 * hour_seconds if not isinstance(duration, timedelta): - duration = timedelta(seconds=duration) + if duration == None: + duration = timedelta(seconds=0) + else: + duration = timedelta(seconds=duration) seconds_total = int(duration.total_seconds()) # timestamps where end is before start if seconds_total < 0: diff --git a/tests/test_time.py b/tests/test_time.py index 98b713c..d85e077 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -56,3 +56,9 @@ class FormatDurationTest(unittest.TestCase): delta = timedelta(hours=-2) result = format_duration(delta, "%H hours") self.assertEqual(result, "0 hours") + + def test_none(self): + try: + format_duration(None) + except TypeError as exc: + assert False, f"format_duration(None) raised an exception {exc}"