From 34ce1e9b051980b42d203d34e8f7251772ea7630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Thu, 5 Jan 2023 15:18:57 +0100 Subject: [PATCH] Set up tests, add tests for common.util.time, add %d --- .vscode/settings.json | 7 ++++++ CHANGELOG.md | 3 ++- Makefile | 2 +- poetry.lock | 6 +++--- src/web/common/util/time.py | 43 ++++++++++++++++--------------------- tests/test_time.py | 32 +++++++++++++++++++++++---- 6 files changed, 60 insertions(+), 33 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9b38853 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b116fc..1aaf77d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Unreleased -* Add tests for common.util.time +* Add %d for days to common.util.time.format_duration +* Set up tests, add tests for common.util.time * Display total hours played on homepage * Add format_duration to common.util.time * Allow deleting sessions diff --git a/Makefile b/Makefile index 14cbc14..13b7ee5 100644 --- a/Makefile +++ b/Makefile @@ -39,7 +39,7 @@ createsuperuser: shell: python src/web/manage.py shell -poetry.lock: +poetry.lock: pyproject.toml poetry install test: poetry.lock diff --git a/poetry.lock b/poetry.lock index d099a78..bdc0bdc 100644 --- a/poetry.lock +++ b/poetry.lock @@ -98,14 +98,14 @@ files = [ [[package]] name = "django" -version = "4.1.4" +version = "4.1.5" description = "A high-level Python web framework that encourages rapid development and clean, pragmatic design." category = "main" optional = false python-versions = ">=3.8" files = [ - {file = "Django-4.1.4-py3-none-any.whl", hash = "sha256:0b223bfa55511f950ff741983d408d78d772351284c75e9f77d2b830b6b4d148"}, - {file = "Django-4.1.4.tar.gz", hash = "sha256:d38a4e108d2386cb9637da66a82dc8d0733caede4c83c4afdbda78af4214211b"}, + {file = "Django-4.1.5-py3-none-any.whl", hash = "sha256:4b214a05fe4c99476e99e2445c8b978c8369c18d4dea8e22ec412862715ad763"}, + {file = "Django-4.1.5.tar.gz", hash = "sha256:ff56ebd7ead0fd5dbe06fe157b0024a7aaea2e0593bb3785fb594cf94dad58ef"}, ] [package.dependencies] diff --git a/src/web/common/util/time.py b/src/web/common/util/time.py index e0db6ad..82b7ff4 100644 --- a/src/web/common/util/time.py +++ b/src/web/common/util/time.py @@ -13,32 +13,27 @@ def format_duration( ) -> str: """ Format timedelta into the specified format_string. - If duration is less than 60 seconds, skips formatting, returns "less than a minute". - If duration is 0, skips formatting, returns 0. Valid format variables: - %H hours - %m minutes - %s seconds + - %r total seconds """ - seconds_in_hours = 3600 - seconds_in_minute = 60 - hours: int - minutes: int - seconds: int - seconds_total: int - remainder: int - seconds_total = duration.total_seconds() - if seconds_total == 0: - return 0 - else: - hours = int(seconds_total // seconds_in_hours) - remainder = int(seconds_total % seconds_in_hours) - minutes = int(remainder // seconds_in_minute) - if hours == 0 and minutes == 0: - return "less than a minute" - seconds = int(minutes % seconds_in_minute) - literals = {"%H": str(hours), "%m": str(minutes), "%s": str(seconds)} - formatted_string = format_string - for pattern, replacement in literals.items(): - formatted_string = re.sub(pattern, replacement, formatted_string) - return formatted_string + minute_seconds = 60 + hour_seconds = 60 * minute_seconds + day_seconds = 24 * hour_seconds + seconds_total = int(duration.total_seconds()) + days, remainder = divmod(seconds_total, day_seconds) + hours, remainder = divmod(remainder, hour_seconds) + minutes, seconds = divmod(remainder, minute_seconds) + literals = { + "%d": str(days), + "%H": str(hours), + "%m": str(minutes), + "%s": str(seconds), + "%r": str(seconds_total), + } + formatted_string = format_string + for pattern, replacement in literals.items(): + formatted_string = re.sub(pattern, replacement, formatted_string) + return formatted_string diff --git a/tests/test_time.py b/tests/test_time.py index eae04c2..53a74f4 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -8,6 +8,11 @@ class FormatDurationTest(unittest.TestCase): return super().setUp() + def test_only_days(self): + delta = timedelta(days=3) + result = format_duration(delta, "%d days") + self.assertEqual(result, "3 days") + def test_only_hours(self): delta = timedelta(hours=1) result = format_duration(delta, "%H hours") @@ -23,7 +28,26 @@ class FormatDurationTest(unittest.TestCase): result = format_duration(delta, "%s seconds") self.assertEqual(result, "1 seconds") - def test_only_less_than_minute_seconds(self): - delta = timedelta(seconds=59) - result = format_duration(delta) - self.assertEqual(result, "less than a minute") + def test_only_rawseconds(self): + delta = timedelta(seconds=5690) + result = format_duration(delta, "%r total seconds") + self.assertEqual(result, "5690 total seconds") + + def test_empty(self): + delta = timedelta() + result = format_duration(delta, "") + self.assertEqual(result, "") + + def test_zero(self): + delta = timedelta() + result = format_duration(delta, "%r seconds") + self.assertEqual(result, "0 seconds") + + def test_all_at_once(self): + delta = timedelta(days=50, hours=10, minutes=34, seconds=24) + result = format_duration( + delta, "%d days, %H hours, %m minutes, %s seconds, %r total seconds" + ) + self.assertEqual( + result, "50 days, 10 hours, 34 minutes, 24 seconds, 4358064 total seconds" + )