Compare commits
3 commits
e7ae7123f6
...
e677082560
Author | SHA1 | Date | |
---|---|---|---|
Edward Betts | e677082560 | ||
Edward Betts | 128d7ac282 | ||
Edward Betts | e38e357f63 |
|
@ -2,10 +2,8 @@
|
|||
|
||||
from datetime import date, datetime, timezone
|
||||
|
||||
DateOrDateTime = datetime | date
|
||||
|
||||
|
||||
def as_date(d: DateOrDateTime) -> date:
|
||||
def as_date(d: datetime | date) -> date:
|
||||
"""Convert datetime to date."""
|
||||
match d:
|
||||
case datetime():
|
||||
|
@ -16,7 +14,7 @@ def as_date(d: DateOrDateTime) -> date:
|
|||
raise TypeError(f"Unsupported type: {type(d)}")
|
||||
|
||||
|
||||
def as_datetime(d: DateOrDateTime) -> datetime:
|
||||
def as_datetime(d: datetime | date) -> datetime:
|
||||
"""Date/time of event."""
|
||||
match d:
|
||||
case datetime():
|
||||
|
@ -51,10 +49,10 @@ def human_readable_delta(future_date: date) -> str | None:
|
|||
# Formatting the output
|
||||
parts = []
|
||||
if months > 0:
|
||||
parts.append(f"{months} months")
|
||||
parts.append(f"{months} month{'s' if months > 1 else ''}")
|
||||
if weeks > 0:
|
||||
parts.append(f"{weeks} weeks")
|
||||
parts.append(f"{weeks} week{'s' if weeks > 1 else ''}")
|
||||
if days > 0:
|
||||
parts.append(f"{days} days")
|
||||
parts.append(f"{days} day{'s' if days > 1 else ''}")
|
||||
|
||||
return " ".join(parts) if parts else None
|
||||
|
|
96
tests/test_utils.py
Normal file
96
tests/test_utils.py
Normal file
|
@ -0,0 +1,96 @@
|
|||
"""Test utility functions."""
|
||||
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
|
||||
import pytest
|
||||
from agenda.utils import as_date, as_datetime, human_readable_delta
|
||||
from freezegun import freeze_time
|
||||
|
||||
|
||||
def test_as_date_with_datetime() -> None:
|
||||
"""Test converting a datetime object to a date."""
|
||||
dt = datetime(2024, 7, 7, 12, 0, 0, tzinfo=timezone.utc)
|
||||
result = as_date(dt)
|
||||
assert result == date(2024, 7, 7)
|
||||
|
||||
|
||||
def test_as_date_with_date() -> None:
|
||||
"""Test passing a date object through as_date."""
|
||||
d = date(2024, 7, 7)
|
||||
result = as_date(d)
|
||||
assert result == d
|
||||
|
||||
|
||||
def test_as_date_with_invalid_type() -> None:
|
||||
"""Test as_date with an invalid type, expecting a TypeError."""
|
||||
with pytest.raises(TypeError):
|
||||
as_date("2024-07-07")
|
||||
|
||||
|
||||
def test_as_datetime_with_datetime() -> None:
|
||||
"""Test passing a datetime object through as_datetime."""
|
||||
dt = datetime(2024, 7, 7, 12, 0, 0, tzinfo=timezone.utc)
|
||||
result = as_datetime(dt)
|
||||
assert result == dt
|
||||
|
||||
|
||||
def test_as_datetime_with_date() -> None:
|
||||
"""Test converting a date object to a datetime."""
|
||||
d = date(2024, 7, 7)
|
||||
result = as_datetime(d)
|
||||
expected = datetime(2024, 7, 7, 0, 0, 0, tzinfo=timezone.utc)
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_as_datetime_with_invalid_type() -> None:
|
||||
"""Test as_datetime with an invalid type, expecting a TypeError."""
|
||||
with pytest.raises(TypeError):
|
||||
as_datetime("2024-07-07")
|
||||
|
||||
|
||||
@freeze_time("2024-07-01")
|
||||
def test_human_readable_delta_future_date() -> None:
|
||||
"""Test human_readable_delta with a future date 45 days from today."""
|
||||
future_date = date.today() + timedelta(days=45)
|
||||
result = human_readable_delta(future_date)
|
||||
assert result == "1 month 2 weeks 1 day"
|
||||
|
||||
|
||||
@freeze_time("2024-07-01")
|
||||
def test_human_readable_delta_today() -> None:
|
||||
"""Test human_readable_delta with today's date, expecting None."""
|
||||
today = date.today()
|
||||
result = human_readable_delta(today)
|
||||
assert result is None
|
||||
|
||||
|
||||
@freeze_time("2024-07-01")
|
||||
def test_human_readable_delta_past_date() -> None:
|
||||
"""Test human_readable_delta with a past date, expecting None."""
|
||||
past_date = date.today() - timedelta(days=1)
|
||||
result = human_readable_delta(past_date)
|
||||
assert result is None
|
||||
|
||||
|
||||
@freeze_time("2024-07-01")
|
||||
def test_human_readable_delta_months_only() -> None:
|
||||
"""Test human_readable_delta with a future date 60 days from today."""
|
||||
future_date = date.today() + timedelta(days=60)
|
||||
result = human_readable_delta(future_date)
|
||||
assert result == "2 months"
|
||||
|
||||
|
||||
@freeze_time("2024-07-01")
|
||||
def test_human_readable_delta_weeks_only() -> None:
|
||||
"""Test human_readable_delta with a future date 14 days from today."""
|
||||
future_date = date.today() + timedelta(days=14)
|
||||
result = human_readable_delta(future_date)
|
||||
assert result == "2 weeks"
|
||||
|
||||
|
||||
@freeze_time("2024-07-01")
|
||||
def test_human_readable_delta_days_only() -> None:
|
||||
"""Test human_readable_delta with a future date 3 days from today."""
|
||||
future_date = date.today() + timedelta(days=3)
|
||||
result = human_readable_delta(future_date)
|
||||
assert result == "3 days"
|
Loading…
Reference in a new issue