Skip to content

Commit 6aeeddd

Browse files
authored
Fix crash when booking additional appointments (#19)
Signed-off-by: rafsaf <rafal.safin12@gmail.com>
1 parent 5296f3f commit 6aeeddd

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

medichaser.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -869,9 +869,12 @@ def format_appointments(appointments: list[dict[str, Any]]) -> str:
869869
messages = []
870870
for appointment in appointments:
871871
date = appointment.get("appointmentDate", "N/A")
872-
clinic = appointment.get("clinic", {}).get("name", "N/A")
873-
doctor = appointment.get("doctor", {}).get("name", "N/A")
874-
specialty = appointment.get("specialty", {}).get("name", "N/A")
872+
clinic_info = appointment.get("clinic", {}) or {}
873+
clinic = clinic_info.get("name", "N/A")
874+
doctor_info = appointment.get("doctor", {}) or {}
875+
doctor = doctor_info.get("name", "N/A")
876+
specialty_info = appointment.get("specialty", {}) or {}
877+
specialty = specialty_info.get("name", "N/A")
875878
doctor_languages = appointment.get("doctorLanguages", [])
876879
languages = (
877880
", ".join([lang.get("name", "N/A") for lang in doctor_languages])
@@ -946,9 +949,12 @@ def display_appointments(appointments: list[dict[str, Any]]) -> None:
946949
log.info("--------------------------------------------------")
947950
for appointment in appointments:
948951
date = appointment.get("appointmentDate", "N/A")
949-
clinic = appointment.get("clinic", {}).get("name", "N/A")
950-
doctor = appointment.get("doctor", {}).get("name", "N/A")
951-
specialty = appointment.get("specialty", {}).get("name", "N/A")
952+
clinic_info = appointment.get("clinic", {}) or {}
953+
clinic = clinic_info.get("name", "N/A")
954+
doctor_info = appointment.get("doctor", {}) or {}
955+
doctor = doctor_info.get("name", "N/A")
956+
specialty_info = appointment.get("specialty", {}) or {}
957+
specialty = specialty_info.get("name", "N/A")
952958
doctor_languages = appointment.get("doctorLanguages", [])
953959
languages = (
954960
", ".join([lang.get("name", "N/A") for lang in doctor_languages])

tests.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,24 @@ def test_format_appointments_multiple(self) -> None:
602602
assert "Languages: N/A" in result # First appointment has no languages
603603
assert "Languages: Polish" in result # Second appointment has Polish
604604

605+
def test_format_appointments_none_nested_objects(self) -> None:
606+
"""Test formatting when clinic/doctor/specialty are None."""
607+
appointments: list[dict[str, Any]] = [
608+
{
609+
"appointmentDate": "2025-01-03T09:00:00",
610+
"clinic": None,
611+
"doctor": None,
612+
"specialty": None,
613+
"doctorLanguages": [],
614+
}
615+
]
616+
617+
result = Notifier.format_appointments(appointments)
618+
619+
assert "Clinic: N/A" in result
620+
assert "Doctor: N/A" in result
621+
assert "Specialty: N/A" in result
622+
605623
def test_send_notification_pushbullet(
606624
self, monkeypatch: pytest.MonkeyPatch
607625
) -> None:
@@ -779,6 +797,29 @@ def test_display_appointments_with_data(
779797
mock_log.info.assert_any_call("New appointments found:")
780798
mock_log.info.assert_any_call("Date: 2025-01-01T10:00:00")
781799

800+
def test_display_appointments_none_nested_objects(
801+
self, monkeypatch: pytest.MonkeyPatch
802+
) -> None:
803+
"""Test display_appointments when clinic/doctor/specialty are None."""
804+
mock_log = Mock()
805+
monkeypatch.setattr("medichaser.log", mock_log)
806+
807+
appointments: list[dict[str, Any]] = [
808+
{
809+
"appointmentDate": "2025-01-03T09:00:00",
810+
"clinic": None,
811+
"doctor": None,
812+
"specialty": None,
813+
"doctorLanguages": [],
814+
}
815+
]
816+
817+
display_appointments(appointments)
818+
819+
mock_log.info.assert_any_call(" Clinic: N/A")
820+
mock_log.info.assert_any_call(" Doctor: N/A")
821+
mock_log.info.assert_any_call(" Specialty: N/A")
822+
782823

783824
class TestNotificationFunctions:
784825
"""Test cases for notification functions."""

0 commit comments

Comments
 (0)