Commit 3c5daf34 authored by James Southern's avatar James Southern

add tests for new SimpleTimePeriod

parent 06532509
......@@ -2,8 +2,9 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
from datetime import datetime, timedelta, timezone
from pytest import raises
from tildes.lib.datetime import descriptive_timedelta, utc_now
from tildes.lib.datetime import SimpleTimePeriod, descriptive_timedelta, utc_now
def test_utc_now_has_timezone():
......@@ -60,3 +61,51 @@ def test_above_second_descriptive_timedelta():
"""Ensure it starts describing time in seconds above 1 second."""
test_time = utc_now() - timedelta(seconds=1, microseconds=100)
assert descriptive_timedelta(test_time) == "1 second ago"
def test_error_on_all_whitespace():
"""Ensure that the input isn't all whitespace."""
with raises(ValueError):
test_period = SimpleTimePeriod.from_short_form(" \t\t ")
def test_error_on_double_whitespace():
"""Ensure that the input isn't double space between numbers."""
with raises(ValueError):
test_period = SimpleTimePeriod.from_short_form(" 10y 1d")
def test_error_on_missing_value():
"""Ensure that the regex catches a missing value."""
with raises(ValueError):
test_period = SimpleTimePeriod.from_short_form("1y w 1d")
def test_short_form_to_short_form():
"""Ensure that output matches input."""
test_period = SimpleTimePeriod.from_short_form("1y 2w 3d 10h 10m 15s")
assert test_period.as_short_form() == "1y 2w 3d 10h 10m 15s"
def test_short_form_big_to_short_form():
"""Ensure that output uses bigger units."""
test_period = SimpleTimePeriod.from_short_form("1y 2w 10d 10h 70m 65s")
assert test_period.as_short_form() == "1y 3w 3d 11h 11m 5s"
def test_short_form_to_str():
"""Ensure that a short form gives correct human text."""
test_period = SimpleTimePeriod.from_short_form("1y 2w 3d 10h 10m 15s")
assert str(test_period) == "1 year, 17 days"
def test_timedelta_eq_short_form():
"""Ensure that timedelta constructor matches from_units"""
test_period1 = SimpleTimePeriod(timedelta(days=369, seconds=60))
test_period2 = SimpleTimePeriod.from_units(years=1, days=4, seconds=60)
assert test_period1 == test_period2
def test_timedelta_not_eq_short_form():
"""Ensure that equality operator fails if not equal."""
test_period1 = SimpleTimePeriod(timedelta(days=369, seconds=65))
test_period2 = SimpleTimePeriod.from_units(years=1, days=4, seconds=60)
assert not test_period1 == test_period2
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment