Compare commits

..

No commits in common. "e6770825606a3aaa160e353ae5a8f572abf660c8" and "e7ae7123f623fba6e1baa4482b5b22b101ac1a9a" have entirely different histories.

2 changed files with 7 additions and 101 deletions

View file

@ -2,8 +2,10 @@
from datetime import date, datetime, timezone from datetime import date, datetime, timezone
DateOrDateTime = datetime | date
def as_date(d: datetime | date) -> date:
def as_date(d: DateOrDateTime) -> date:
"""Convert datetime to date.""" """Convert datetime to date."""
match d: match d:
case datetime(): case datetime():
@ -14,7 +16,7 @@ def as_date(d: datetime | date) -> date:
raise TypeError(f"Unsupported type: {type(d)}") raise TypeError(f"Unsupported type: {type(d)}")
def as_datetime(d: datetime | date) -> datetime: def as_datetime(d: DateOrDateTime) -> datetime:
"""Date/time of event.""" """Date/time of event."""
match d: match d:
case datetime(): case datetime():
@ -49,10 +51,10 @@ def human_readable_delta(future_date: date) -> str | None:
# Formatting the output # Formatting the output
parts = [] parts = []
if months > 0: if months > 0:
parts.append(f"{months} month{'s' if months > 1 else ''}") parts.append(f"{months} months")
if weeks > 0: if weeks > 0:
parts.append(f"{weeks} week{'s' if weeks > 1 else ''}") parts.append(f"{weeks} weeks")
if days > 0: if days > 0:
parts.append(f"{days} day{'s' if days > 1 else ''}") parts.append(f"{days} days")
return " ".join(parts) if parts else None return " ".join(parts) if parts else None

View file

@ -1,96 +0,0 @@
"""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"