From 89b04675e2091d685914486debc2361d23e895c3 Mon Sep 17 00:00:00 2001 From: red person Date: Wed, 3 Jun 2026 08:11:17 +0300 Subject: [PATCH] Handle missing calendar CLI relation (#1574) --- scripts/odysseus-calendar | 8 +++++++- tests/test_calendar_cli_name.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/test_calendar_cli_name.py diff --git a/scripts/odysseus-calendar b/scripts/odysseus-calendar index cfe0c6d..5625510 100755 --- a/scripts/odysseus-calendar +++ b/scripts/odysseus-calendar @@ -69,11 +69,17 @@ def _parse_dt(s: str) -> datetime: return datetime.fromisoformat(s.replace("Z", "+00:00")) +def _calendar_name(ev: "CalendarEvent") -> str: + cal = getattr(ev, "calendar", None) + name = getattr(cal, "name", "") if cal else "" + return name if isinstance(name, str) else "" + + def _serialize_event(ev: "CalendarEvent") -> dict: return { "uid": ev.uid, "calendar_id": ev.calendar_id, - "calendar_name": ev.calendar.name if ev.calendar else "", + "calendar_name": _calendar_name(ev), "summary": ev.summary, "description": ev.description or "", "location": ev.location or "", diff --git a/tests/test_calendar_cli_name.py b/tests/test_calendar_cli_name.py new file mode 100644 index 0000000..475cdc5 --- /dev/null +++ b/tests/test_calendar_cli_name.py @@ -0,0 +1,32 @@ +import importlib.machinery +import importlib.util +import sys +import types +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import MagicMock + + +ROOT = Path(__file__).resolve().parents[1] + + +def _load_cli(monkeypatch): + db = types.ModuleType("core.database") + db.SessionLocal = MagicMock() + db.CalendarCal = MagicMock() + db.CalendarEvent = MagicMock() + monkeypatch.setitem(sys.modules, "core.database", db) + path = ROOT / "scripts" / "odysseus-calendar" + loader = importlib.machinery.SourceFileLoader("odysseus_calendar_cli", str(path)) + spec = importlib.util.spec_from_loader(loader.name, loader) + module = importlib.util.module_from_spec(spec) + loader.exec_module(module) + return module + + +def test_calendar_name_handles_missing_relation(monkeypatch): + cli = _load_cli(monkeypatch) + + assert cli._calendar_name(SimpleNamespace(calendar=None)) == "" + assert cli._calendar_name(SimpleNamespace(calendar=SimpleNamespace(name=123))) == "" + assert cli._calendar_name(SimpleNamespace(calendar=SimpleNamespace(name="Work"))) == "Work"