134 lines
4.6 KiB
Python
134 lines
4.6 KiB
Python
"""Tests for Swedish holiday utilities."""
|
|
|
|
import datetime
|
|
|
|
|
|
from app.utils.holidays import (
|
|
get_holiday_name,
|
|
get_swedish_holidays,
|
|
is_swedish_holiday,
|
|
)
|
|
|
|
|
|
class TestGetSwedishHolidays:
|
|
"""Test get_swedish_holidays function."""
|
|
|
|
def test_returns_dict(self) -> None:
|
|
"""get_swedish_holidays returns a dictionary."""
|
|
result = get_swedish_holidays(2026)
|
|
assert isinstance(result, dict)
|
|
assert len(result) > 0
|
|
|
|
def test_keys_are_dates(self) -> None:
|
|
"""All keys are datetime.date objects."""
|
|
result = get_swedish_holidays(2026)
|
|
for key in result.keys():
|
|
assert isinstance(key, datetime.date)
|
|
|
|
def test_values_are_strings(self) -> None:
|
|
"""All values are strings."""
|
|
result = get_swedish_holidays(2026)
|
|
for value in result.values():
|
|
assert isinstance(value, str)
|
|
assert len(value) > 0
|
|
|
|
def test_christmas_2026(self) -> None:
|
|
"""Christmas 2026 (Dec 25) is in the holidays."""
|
|
result = get_swedish_holidays(2026)
|
|
christmas = datetime.date(2026, 12, 25)
|
|
assert christmas in result
|
|
assert "Jul" in result[christmas] # Holiday name contains "Jul"
|
|
|
|
def test_new_year_2026(self) -> None:
|
|
"""New Year's Day 2026 (Jan 1) is in the holidays."""
|
|
result = get_swedish_holidays(2026)
|
|
new_year = datetime.date(2026, 1, 1)
|
|
assert new_year in result
|
|
|
|
def test_midsummer_2026(self) -> None:
|
|
"""Midsummer 2026 (Midsommar) is in the holidays."""
|
|
result = get_swedish_holidays(2026)
|
|
# Midsummer is always a Saturday in mid-June
|
|
midsummer_2026 = datetime.date(2026, 6, 20)
|
|
assert midsummer_2026 in result
|
|
assert "Midsommar" in result[midsummer_2026]
|
|
|
|
def test_easter_2026(self) -> None:
|
|
"""Easter-related days (Långfredagen, Påskdagen) in 2026."""
|
|
result = get_swedish_holidays(2026)
|
|
# Easter 2026 is April 5; Good Friday would be April 3
|
|
good_friday = datetime.date(2026, 4, 3)
|
|
easter_sunday = datetime.date(2026, 4, 5)
|
|
assert good_friday in result
|
|
assert easter_sunday in result
|
|
|
|
def test_different_years(self) -> None:
|
|
"""Different years have different holiday sets."""
|
|
holidays_2025 = get_swedish_holidays(2025)
|
|
holidays_2026 = get_swedish_holidays(2026)
|
|
holidays_2027 = get_swedish_holidays(2027)
|
|
|
|
# All should have holidays
|
|
assert len(holidays_2025) > 0
|
|
assert len(holidays_2026) > 0
|
|
assert len(holidays_2027) > 0
|
|
|
|
def test_caching(self) -> None:
|
|
"""Calling get_swedish_holidays twice returns same object (cached)."""
|
|
result1 = get_swedish_holidays(2026)
|
|
result2 = get_swedish_holidays(2026)
|
|
# Should be the same object due to caching
|
|
assert result1 is result2
|
|
|
|
def test_random_day_not_holiday(self) -> None:
|
|
"""Random weekday is not a holiday."""
|
|
result = get_swedish_holidays(2026)
|
|
random_day = datetime.date(2026, 6, 8) # Random Monday
|
|
assert random_day not in result
|
|
|
|
|
|
class TestIsSwedishHoliday:
|
|
"""Test is_swedish_holiday function."""
|
|
|
|
def test_christmas_is_holiday(self) -> None:
|
|
"""Christmas is recognized as a holiday."""
|
|
assert is_swedish_holiday(datetime.date(2026, 12, 25))
|
|
|
|
def test_new_year_is_holiday(self) -> None:
|
|
"""New Year's Day is recognized as a holiday."""
|
|
assert is_swedish_holiday(datetime.date(2026, 1, 1))
|
|
|
|
def test_random_day_not_holiday(self) -> None:
|
|
"""Random day is not a holiday."""
|
|
assert not is_swedish_holiday(datetime.date(2026, 6, 8))
|
|
|
|
def test_monday_not_holiday(self) -> None:
|
|
"""Random Monday is not a holiday."""
|
|
assert not is_swedish_holiday(datetime.date(2026, 6, 1))
|
|
|
|
|
|
class TestGetHolidayName:
|
|
"""Test get_holiday_name function."""
|
|
|
|
def test_christmas_name(self) -> None:
|
|
"""Get name of Christmas."""
|
|
name = get_holiday_name(datetime.date(2026, 12, 25))
|
|
assert name is not None
|
|
assert "Jul" in name
|
|
|
|
def test_non_holiday_returns_none(self) -> None:
|
|
"""Non-holiday date returns None."""
|
|
name = get_holiday_name(datetime.date(2026, 6, 8))
|
|
assert name is None
|
|
|
|
def test_new_year_name(self) -> None:
|
|
"""Get name of New Year's Day."""
|
|
name = get_holiday_name(datetime.date(2026, 1, 1))
|
|
assert name is not None
|
|
|
|
def test_midsummer_name(self) -> None:
|
|
"""Get name of Midsummer."""
|
|
name = get_holiday_name(datetime.date(2026, 6, 20))
|
|
assert name is not None
|
|
assert "Midsommar" in name
|