Set up tests, add tests for common.util.time, add %d
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
67f5090bf8
commit
34ce1e9b05
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"python.testing.pytestArgs": [
|
||||
"tests"
|
||||
],
|
||||
"python.testing.unittestEnabled": false,
|
||||
"python.testing.pytestEnabled": true
|
||||
}
|
|
@ -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
|
||||
|
|
2
Makefile
2
Makefile
|
@ -39,7 +39,7 @@ createsuperuser:
|
|||
shell:
|
||||
python src/web/manage.py shell
|
||||
|
||||
poetry.lock:
|
||||
poetry.lock: pyproject.toml
|
||||
poetry install
|
||||
|
||||
test: 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]
|
||||
|
|
|
@ -13,31 +13,26 @@ 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)}
|
||||
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)
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue