mirror of
https://github.com/Sea-Haven-Industries/meal-order-manager.git
synced 2026-09-30 18:13:13 +00:00
Some checks failed
Build Lambda Layer / build (push) Has been cancelled
* feat(meals): dual-read week keys and meal-to-Flex join (PLAT-134) * style(meals): apply ruff format (PLAT-134) * docs(meals): clarify week-key dual-read is same-instant (PLAT-134)
102 lines
3.2 KiB
Python
102 lines
3.2 KiB
Python
"""Tests for Sunday-week / ISO dual-read week keys and GET helpers."""
|
|
|
|
from datetime import datetime
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from shared import db
|
|
from shared.week_keys import (
|
|
WEEK_ID_RE,
|
|
candidate_week_ids,
|
|
candidate_week_pks,
|
|
week_id_iso,
|
|
week_id_ustrftime,
|
|
)
|
|
|
|
MISMATCH_DT = datetime(2026, 1, 1)
|
|
MATCH_DT = datetime(2026, 1, 4)
|
|
|
|
|
|
def test_ustrftime_and_iso_differ_on_new_year():
|
|
assert week_id_ustrftime(MISMATCH_DT) == "2026-W00"
|
|
assert week_id_iso(MISMATCH_DT) == "2026-W01"
|
|
assert WEEK_ID_RE.match("2026-W00")
|
|
assert WEEK_ID_RE.match("2026-W01")
|
|
|
|
|
|
def test_candidate_week_ids_u_week_first_then_iso():
|
|
# 2026-01-01: same instant encoded two ways, not a next-week lookup.
|
|
assert candidate_week_ids(MISMATCH_DT) == ("2026-W00", "2026-W01")
|
|
|
|
|
|
def test_candidate_week_ids_unique_when_formats_match():
|
|
ids = candidate_week_ids(MATCH_DT)
|
|
assert week_id_ustrftime(MATCH_DT) == week_id_iso(MATCH_DT) == "2026-W01"
|
|
assert ids == ("2026-W01",)
|
|
assert len(ids) == 1
|
|
|
|
|
|
def test_candidate_week_pks_prefixed():
|
|
assert candidate_week_pks(MISMATCH_DT) == ("WEEK#2026-W00", "WEEK#2026-W01")
|
|
assert candidate_week_pks(MATCH_DT) == ("WEEK#2026-W01",)
|
|
|
|
|
|
def _table_with_get_items(items_by_pk: dict[str, dict]):
|
|
table = MagicMock()
|
|
|
|
def get_item(Key):
|
|
item = items_by_pk.get(Key["PK"])
|
|
if item is None:
|
|
return {}
|
|
if item.get("SK") != Key["SK"]:
|
|
return {}
|
|
return {"Item": item}
|
|
|
|
table.get_item.side_effect = get_item
|
|
return table
|
|
|
|
|
|
def test_get_menu_when_falls_back_to_iso():
|
|
iso_item = {"PK": "WEEK#2026-W01", "SK": "MENU", "source": "iso"}
|
|
table = _table_with_get_items({"WEEK#2026-W01": iso_item})
|
|
|
|
with patch.object(db, "_get_table", return_value=table):
|
|
result = db.get_menu("2026-W00", when=MISMATCH_DT)
|
|
|
|
assert result == iso_item
|
|
pks = [call.kwargs["Key"]["PK"] for call in table.get_item.call_args_list]
|
|
assert pks == ["WEEK#2026-W00", "WEEK#2026-W01"]
|
|
|
|
|
|
def test_get_menu_when_prefers_u_week_if_both_exist():
|
|
u_item = {"PK": "WEEK#2026-W00", "SK": "MENU", "source": "u"}
|
|
iso_item = {"PK": "WEEK#2026-W01", "SK": "MENU", "source": "iso"}
|
|
table = _table_with_get_items({"WEEK#2026-W00": u_item, "WEEK#2026-W01": iso_item})
|
|
|
|
with patch.object(db, "_get_table", return_value=table):
|
|
result = db.get_menu("2026-W00", when=MISMATCH_DT)
|
|
|
|
assert result == u_item
|
|
assert table.get_item.call_count == 1
|
|
assert table.get_item.call_args.kwargs["Key"]["PK"] == "WEEK#2026-W00"
|
|
|
|
|
|
def test_get_menu_without_when_uses_week_argument_as_single_key():
|
|
table = _table_with_get_items({})
|
|
|
|
with patch.object(db, "_get_table", return_value=table):
|
|
result = db.get_menu("2026-W34")
|
|
|
|
assert result is None
|
|
table.get_item.assert_called_once_with(Key={"PK": "WEEK#2026-W34", "SK": "MENU"})
|
|
|
|
|
|
def test_get_orders_when_falls_back_to_iso():
|
|
iso_orders = [{"PK": "WEEK#2026-W01", "SK": "ORDER#pat"}]
|
|
table = MagicMock()
|
|
table.query.side_effect = [{"Items": []}, {"Items": iso_orders}]
|
|
|
|
with patch.object(db, "_get_table", return_value=table):
|
|
result = db.get_orders("2026-W00", when=MISMATCH_DT)
|
|
|
|
assert result == iso_orders
|
|
assert table.query.call_count == 2
|