Implementace sluzby workdays
- endpointy /workdays/add, is-workday, next, previous, between - /holidays, /countries (filtr eu), /markets, /health, /version - data svatku z knihovny holidays 0.109, vsech 27 zemi EU vcetne regionu - vychozi zeme CZ a cestina pres DEFAULT_COUNTRY a DEFAULT_LANGUAGE - vlastni vikend, extra_holidays a extra_workdays v requestu - testy logiky, API a testy proti realnym kalendarum - CLAUDE.md s povinnym overenim vysledku proti realite - dokumentace ve slozce documentation - .gitignore: vyjimka pro app/lib
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
"""HTTP layer: parameters, responses, errors and the reverse proxy prefix."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.main import app
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def client():
|
||||
with TestClient(app) as test_client:
|
||||
yield test_client
|
||||
|
||||
|
||||
def test_health(client):
|
||||
response = client.get("/health")
|
||||
assert response.status_code == 200
|
||||
body = response.json()
|
||||
assert body["status"] == "ok"
|
||||
assert body["default_country"] == "CZ"
|
||||
|
||||
|
||||
def test_docs(client):
|
||||
assert client.get("/docs").status_code == 200
|
||||
|
||||
|
||||
def test_openapi_servers_use_proxy_prefix():
|
||||
with TestClient(app, root_path="/apps/workdays") as proxied:
|
||||
schema = proxied.get("/openapi.json").json()
|
||||
assert schema["servers"][0]["url"] == "/apps/workdays"
|
||||
assert "/workdays/add" in schema["paths"]
|
||||
|
||||
|
||||
def test_add_defaults_to_cz(client):
|
||||
body = client.get("/workdays/add", params={"date": "26.11.2026", "days": 3}).json()
|
||||
assert body["result"] == "2026-12-01"
|
||||
assert body["calendar"]["code"] == "CZ"
|
||||
assert body["calendar"]["language"] == "cs"
|
||||
assert body["calendar"]["weekend"] == ["sat", "sun"]
|
||||
|
||||
|
||||
def test_add_lists_skipped_holidays(client):
|
||||
body = client.get("/workdays/add", params={"date": "2026-12-23", "days": 1}).json()
|
||||
assert body["result"] == "2026-12-28"
|
||||
assert [item["date"] for item in body["skipped_holidays"]] == ["2026-12-24", "2026-12-25"]
|
||||
|
||||
|
||||
def test_add_with_extra_days(client):
|
||||
body = client.get(
|
||||
"/workdays/add",
|
||||
params={"date": "2026-11-26", "days": 1, "extra_holidays": "2026-11-27", "extra_workdays": "28.11.2026"},
|
||||
).json()
|
||||
assert body["result"] == "2026-11-28"
|
||||
|
||||
|
||||
def test_is_workday(client):
|
||||
body = client.get("/workdays/is-workday", params={"date": "2026-12-24"}).json()
|
||||
assert body["is_workday"] is False
|
||||
assert body["holidays"] == ["Štědrý den"]
|
||||
|
||||
|
||||
def test_language_falls_back_to_country_default(client):
|
||||
body = client.get("/workdays/is-workday", params={"date": "2026-10-03", "country": "DE"}).json()
|
||||
assert body["calendar"]["language"] == "de"
|
||||
assert body["holidays"] == ["Tag der Deutschen Einheit"]
|
||||
|
||||
|
||||
def test_language_variant_match(client):
|
||||
body = client.get("/workdays/is-workday", params={"date": "2026-12-24", "language": "en"}).json()
|
||||
assert body["calendar"]["language"] == "en_US"
|
||||
|
||||
|
||||
def test_region_and_market(client):
|
||||
body = client.get("/workdays/is-workday", params={"date": "2026-06-04", "country": "de", "region": "by"}).json()
|
||||
assert body["is_workday"] is False
|
||||
assert body["calendar"]["region"] == "BY"
|
||||
|
||||
body = client.get("/workdays/next", params={"date": "2026-04-02", "market": "XECB"}).json()
|
||||
assert body["result"] == "2026-04-07"
|
||||
assert body["calendar"]["type"] == "market"
|
||||
|
||||
|
||||
def test_between(client):
|
||||
body = client.get("/workdays/between", params={"start": "2026-12-01", "end": "2026-12-31"}).json()
|
||||
assert body["workdays"] == 21
|
||||
assert body["holidays_off"] == 2
|
||||
|
||||
|
||||
def test_holidays_list(client):
|
||||
body = client.get("/holidays", params={"year": 2026}).json()
|
||||
assert len(body["holidays"]) == 13
|
||||
christmas = next(item for item in body["holidays"] if item["date"] == "2026-12-26")
|
||||
assert christmas["is_weekend"] is True
|
||||
|
||||
|
||||
def test_countries_eu_filter(client):
|
||||
items = client.get("/countries", params={"eu": True}).json()["items"]
|
||||
assert len(items) == 27
|
||||
germany = next(item for item in items if item["code"] == "DE")
|
||||
assert {"code": "BY", "name": "Bayern"} in germany["regions"]
|
||||
|
||||
|
||||
def test_markets(client):
|
||||
items = client.get("/markets").json()["items"]
|
||||
assert any(item["code"] == "XECB" for item in items)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("params", "error_code"),
|
||||
[
|
||||
({"date": "2026-13-01", "days": 1}, "invalid_date"),
|
||||
({"date": "zitra", "days": 1}, "invalid_date"),
|
||||
({"date": "2026-11-26", "days": 1, "country": "XX"}, "unknown_country"),
|
||||
({"date": "2026-11-26", "days": 1, "country": "DE", "region": "ZZ"}, "unknown_region"),
|
||||
({"date": "2026-11-26", "days": 1, "categories": "foo"}, "unknown_category"),
|
||||
({"date": "2026-11-26", "days": 1, "country": "CZ", "market": "XECB"}, "invalid_request"),
|
||||
({"date": "2026-11-26", "days": 1, "weekend": "mon,tue,wed,thu,fri,sat,sun"}, "invalid_request"),
|
||||
({"date": "1900-01-01", "days": 1}, "out_of_range"),
|
||||
({"date": "2100-12-30", "days": 5}, "out_of_range"),
|
||||
],
|
||||
)
|
||||
def test_errors(client, params, error_code):
|
||||
response = client.get("/workdays/add", params=params)
|
||||
assert response.status_code == 400
|
||||
assert response.json()["error_code"] == error_code
|
||||
@@ -0,0 +1,164 @@
|
||||
"""Results compared with real calendars.
|
||||
|
||||
Every expected value below comes from a source outside this service, mostly
|
||||
the law that defines the holidays. When a test here fails after a library
|
||||
update, check the source first: the law may have changed, not the code.
|
||||
|
||||
Sources:
|
||||
- CZ: zakon c. 245/2000 Sb., o statnich svatcich, ve zneni od roku 2016
|
||||
(Velky patek). Fond pracovni doby 2026 je 250 dnu.
|
||||
- DE: Feiertagsgesetze der Lander, Bayern: Fronleichnam celostatne v BY,
|
||||
Maria Himmelfahrt jen v obcich s prevahou katoliku.
|
||||
- AT: Feiertagsruhegesetz 1957.
|
||||
- FR: Code du travail L3133-1, Alsasko a Mosela navic Vendredi saint a 26.12.
|
||||
- PL: ustawa o dniach wolnych od pracy, Wigilia 24.12 od roku 2025.
|
||||
- SK: zakon c. 241/1993 Z. z.
|
||||
- XECB: kalendar TARGET2 Evropske centralni banky.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
from app.services.calendars import holidays_in_year, resolve_calendar
|
||||
from app.services.workdays import WorkCalendar, add_workdays, count_workdays
|
||||
|
||||
|
||||
def calendar_for(kind="country", code="CZ", region=None, categories=None, language="cs") -> WorkCalendar:
|
||||
info = resolve_calendar(kind, code, region, categories, language)
|
||||
return WorkCalendar(
|
||||
weekend=info.default_weekend,
|
||||
min_date=info.min_date,
|
||||
max_date=info.max_date,
|
||||
holidays_in_year=lambda year: holidays_in_year(info.spec, year),
|
||||
)
|
||||
|
||||
|
||||
def holiday_dates(kind="country", code="CZ", region=None, categories=None, year=2026) -> set[date]:
|
||||
info = resolve_calendar(kind, code, region, categories, None)
|
||||
return set(holidays_in_year(info.spec, year))
|
||||
|
||||
|
||||
# Czechia
|
||||
|
||||
|
||||
CZ_2026 = {
|
||||
date(2026, 1, 1),
|
||||
date(2026, 4, 3),
|
||||
date(2026, 4, 6),
|
||||
date(2026, 5, 1),
|
||||
date(2026, 5, 8),
|
||||
date(2026, 7, 5),
|
||||
date(2026, 7, 6),
|
||||
date(2026, 9, 28),
|
||||
date(2026, 10, 28),
|
||||
date(2026, 11, 17),
|
||||
date(2026, 12, 24),
|
||||
date(2026, 12, 25),
|
||||
date(2026, 12, 26),
|
||||
}
|
||||
|
||||
|
||||
def test_cz_2026_holidays_exactly_match_law():
|
||||
assert holiday_dates(code="CZ", year=2026) == CZ_2026
|
||||
|
||||
|
||||
def test_cz_2027_easter():
|
||||
dates = holiday_dates(code="CZ", year=2027)
|
||||
assert date(2027, 3, 26) in dates
|
||||
assert date(2027, 3, 29) in dates
|
||||
|
||||
|
||||
def test_cz_working_time_fund_2026():
|
||||
calendar = calendar_for()
|
||||
assert count_workdays(calendar, date(2026, 1, 1), date(2026, 12, 31)).workdays == 250
|
||||
|
||||
|
||||
def test_cz_example_from_assignment():
|
||||
assert add_workdays(calendar_for(), date(2026, 11, 26), 3).result == date(2026, 12, 1)
|
||||
|
||||
|
||||
def test_cz_over_christmas():
|
||||
assert add_workdays(calendar_for(), date(2026, 12, 23), 1).result == date(2026, 12, 28)
|
||||
|
||||
|
||||
def test_cz_over_easter():
|
||||
assert add_workdays(calendar_for(), date(2026, 4, 2), 1).result == date(2026, 4, 7)
|
||||
|
||||
|
||||
def test_cz_backwards_over_may_holidays():
|
||||
assert add_workdays(calendar_for(), date(2026, 5, 11), -1).result == date(2026, 5, 7)
|
||||
|
||||
|
||||
def test_cz_names_in_czech():
|
||||
info = resolve_calendar("country", "CZ", None, None, "cs")
|
||||
names = holidays_in_year(info.spec, 2026)[date(2026, 12, 24)]
|
||||
assert names == ("Štědrý den",)
|
||||
|
||||
|
||||
# Germany
|
||||
|
||||
|
||||
def test_de_corpus_christi_only_in_region():
|
||||
assert date(2026, 6, 4) not in holiday_dates(code="DE")
|
||||
assert date(2026, 6, 4) in holiday_dates(code="DE", region="BY")
|
||||
|
||||
|
||||
def test_de_region_by_name():
|
||||
assert date(2026, 6, 4) in holiday_dates(code="DE", region="Bayern")
|
||||
|
||||
|
||||
def test_de_assumption_is_catholic_category_in_bavaria():
|
||||
assert date(2026, 8, 15) not in holiday_dates(code="DE", region="BY")
|
||||
assert date(2026, 8, 15) in holiday_dates(code="DE", region="BY", categories=("public", "catholic"))
|
||||
|
||||
|
||||
def test_de_unity_day():
|
||||
assert date(2026, 10, 3) in holiday_dates(code="DE")
|
||||
|
||||
|
||||
# Other EU countries
|
||||
|
||||
|
||||
def test_at_national_day_and_immaculate_conception():
|
||||
dates = holiday_dates(code="AT")
|
||||
assert date(2026, 10, 26) in dates
|
||||
assert date(2026, 12, 8) in dates
|
||||
|
||||
|
||||
def test_fr_alsace_moselle():
|
||||
assert date(2026, 12, 26) not in holiday_dates(code="FR")
|
||||
assert date(2026, 12, 26) in holiday_dates(code="FR", region="57")
|
||||
assert date(2026, 4, 3) in holiday_dates(code="FR", region="57")
|
||||
|
||||
|
||||
def test_pl_christmas_eve_since_2025():
|
||||
assert date(2024, 12, 24) not in holiday_dates(code="PL", year=2024)
|
||||
assert date(2026, 12, 24) in holiday_dates(code="PL", year=2026)
|
||||
|
||||
|
||||
def test_sk_constitution_and_uprising():
|
||||
dates = holiday_dates(code="SK")
|
||||
assert date(2026, 8, 29) in dates
|
||||
assert date(2026, 7, 5) in dates
|
||||
|
||||
|
||||
def test_every_eu_country_has_holidays_in_2026():
|
||||
from app.services.calendars import EU_MEMBERS
|
||||
|
||||
for code in EU_MEMBERS:
|
||||
assert len(holiday_dates(code=code)) >= 8, code
|
||||
|
||||
|
||||
# Financial calendar
|
||||
|
||||
|
||||
def test_target2_2026():
|
||||
assert holiday_dates(kind="market", code="XECB") == {
|
||||
date(2026, 1, 1),
|
||||
date(2026, 4, 3),
|
||||
date(2026, 4, 6),
|
||||
date(2026, 5, 1),
|
||||
date(2026, 12, 25),
|
||||
date(2026, 12, 26),
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
"""Workday arithmetic over a hand made calendar, independent of the holiday library."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
import pytest
|
||||
|
||||
from app.errors import OutOfRangeError
|
||||
from app.services.workdays import (
|
||||
WorkCalendar,
|
||||
add_workdays,
|
||||
count_workdays,
|
||||
next_workday,
|
||||
previous_workday,
|
||||
)
|
||||
|
||||
# November and December 2026, a single fictional holiday on Friday 27.11.
|
||||
HOLIDAYS = {date(2026, 11, 27): ("Testovaci svatek",)}
|
||||
|
||||
|
||||
def make_calendar(**overrides) -> WorkCalendar:
|
||||
params = {
|
||||
"weekend": frozenset({5, 6}),
|
||||
"min_date": date(2026, 1, 1),
|
||||
"max_date": date(2026, 12, 31),
|
||||
"holidays_in_year": lambda year: HOLIDAYS,
|
||||
}
|
||||
params.update(overrides)
|
||||
return WorkCalendar(**params)
|
||||
|
||||
|
||||
def test_add_skips_weekend_and_holiday():
|
||||
outcome = add_workdays(make_calendar(), date(2026, 11, 26), 1)
|
||||
assert outcome.result == date(2026, 11, 30)
|
||||
assert [info.day for info in outcome.skipped] == [date(2026, 11, 27)]
|
||||
|
||||
|
||||
def test_add_zero_returns_start_even_on_weekend():
|
||||
assert add_workdays(make_calendar(), date(2026, 11, 28), 0).result == date(2026, 11, 28)
|
||||
|
||||
|
||||
def test_add_from_weekend_counts_from_next_workday():
|
||||
assert add_workdays(make_calendar(), date(2026, 11, 28), 1).result == date(2026, 11, 30)
|
||||
|
||||
|
||||
def test_add_negative_moves_back():
|
||||
assert add_workdays(make_calendar(), date(2026, 11, 30), -1).result == date(2026, 11, 26)
|
||||
|
||||
|
||||
def test_add_and_subtract_are_symmetric_between_workdays():
|
||||
calendar = make_calendar()
|
||||
forward = add_workdays(calendar, date(2026, 11, 2), 17).result
|
||||
assert add_workdays(calendar, forward, -17).result == date(2026, 11, 2)
|
||||
|
||||
|
||||
def test_extra_holiday_and_extra_workday():
|
||||
calendar = make_calendar(
|
||||
extra_holidays=frozenset({date(2026, 11, 30)}),
|
||||
extra_workdays=frozenset({date(2026, 11, 28)}),
|
||||
)
|
||||
assert add_workdays(calendar, date(2026, 11, 26), 1).result == date(2026, 11, 28)
|
||||
assert add_workdays(calendar, date(2026, 11, 26), 2).result == date(2026, 12, 1)
|
||||
|
||||
|
||||
def test_extra_workday_wins_over_holiday():
|
||||
calendar = make_calendar(extra_workdays=frozenset({date(2026, 11, 27)}))
|
||||
info = calendar.describe(date(2026, 11, 27))
|
||||
assert info.is_workday is True
|
||||
assert info.override == "extra_workday"
|
||||
assert info.holidays == ("Testovaci svatek",)
|
||||
|
||||
|
||||
def test_custom_weekend():
|
||||
calendar = make_calendar(weekend=frozenset({4, 5}))
|
||||
assert calendar.is_workday(date(2026, 11, 29)) is True
|
||||
assert calendar.is_workday(date(2026, 11, 28)) is False
|
||||
|
||||
|
||||
def test_next_and_previous():
|
||||
calendar = make_calendar()
|
||||
assert next_workday(calendar, date(2026, 11, 26)) == date(2026, 11, 30)
|
||||
assert next_workday(calendar, date(2026, 11, 26), include_start=True) == date(2026, 11, 26)
|
||||
assert next_workday(calendar, date(2026, 11, 28), include_start=True) == date(2026, 11, 30)
|
||||
assert previous_workday(calendar, date(2026, 11, 30)) == date(2026, 11, 26)
|
||||
|
||||
|
||||
def test_count_inclusive_and_signed():
|
||||
calendar = make_calendar()
|
||||
outcome = count_workdays(calendar, date(2026, 11, 23), date(2026, 11, 29))
|
||||
assert outcome.workdays == 4
|
||||
assert outcome.weekend_days == 2
|
||||
assert [info.day for info in outcome.holidays_off] == [date(2026, 11, 27)]
|
||||
|
||||
reverse = count_workdays(calendar, date(2026, 11, 29), date(2026, 11, 23))
|
||||
assert reverse.workdays == -4
|
||||
assert reverse.weekend_days == -2
|
||||
|
||||
|
||||
def test_count_single_day():
|
||||
assert count_workdays(make_calendar(), date(2026, 11, 26), date(2026, 11, 26)).workdays == 1
|
||||
|
||||
|
||||
def test_out_of_range_start():
|
||||
with pytest.raises(OutOfRangeError):
|
||||
add_workdays(make_calendar(), date(2025, 12, 31), 1)
|
||||
|
||||
|
||||
def test_out_of_range_result():
|
||||
with pytest.raises(OutOfRangeError):
|
||||
add_workdays(make_calendar(), date(2026, 12, 30), 5)
|
||||
Reference in New Issue
Block a user