97 lines
3 KiB
Python
97 lines
3 KiB
Python
"""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"
|