From f8f4fa8665ef61caf5266c980046305f3b779c6d Mon Sep 17 00:00:00 2001 From: Michael Manfre Date: Sun, 20 Nov 2022 13:14:49 -0500 Subject: Fix bug in timedeltashort and add tests --- activities/templatetags/activity_tags.py | 4 ++-- activities/tests/templatetags/__init__.py | 0 activities/tests/templatetags/test_activity_tags.py | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 activities/tests/templatetags/__init__.py create mode 100644 activities/tests/templatetags/test_activity_tags.py diff --git a/activities/templatetags/activity_tags.py b/activities/templatetags/activity_tags.py index cbbae57..571e2d6 100644 --- a/activities/templatetags/activity_tags.py +++ b/activities/templatetags/activity_tags.py @@ -26,8 +26,8 @@ def timedeltashort(value: datetime.datetime): hours = seconds // (60 * 60) text = f"{hours:0n}h" elif days < 365: - text = f"{days:0n}h" + text = f"{days:0n}d" else: - years = days // 365.25 + years = max(days // 365.25, 1) text = f"{years:0n}y" return text diff --git a/activities/tests/templatetags/__init__.py b/activities/tests/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/activities/tests/templatetags/test_activity_tags.py b/activities/tests/templatetags/test_activity_tags.py new file mode 100644 index 0000000..987c008 --- /dev/null +++ b/activities/tests/templatetags/test_activity_tags.py @@ -0,0 +1,21 @@ +from datetime import timedelta + +from django.utils import timezone + +from activities.templatetags.activity_tags import timedeltashort + + +def test_timedeltashort_regress(): + assert timedeltashort(None) == "" + assert timedeltashort("") == "" + + value = timezone.now() + + assert timedeltashort(value) == "0s" + assert timedeltashort(value - timedelta(seconds=2)) == "2s" + assert timedeltashort(value - timedelta(minutes=2)) == "2m" + assert timedeltashort(value - timedelta(hours=2)) == "2h" + assert timedeltashort(value - timedelta(days=2)) == "2d" + assert timedeltashort(value - timedelta(days=364)) == "364d" + assert timedeltashort(value - timedelta(days=365)) == "1y" + assert timedeltashort(value - timedelta(days=366)) == "1y" -- cgit v1.2.3