diff --git a/churchtools_api/persons.py b/churchtools_api/persons.py index dea0409..50c9ff2 100644 --- a/churchtools_api/persons.py +++ b/churchtools_api/persons.py @@ -92,6 +92,8 @@ def get_persons_masterdata( """Function to get the Masterdata of the persons module. This information is required to map some IDs to specific items. + Special case treatment for resultClass sexes + first item is copied for "None" references as fallback Arguments: resultClass: the name of the masterdata to retrieve. Defaults to All @@ -111,8 +113,11 @@ def get_persons_masterdata( if resultClass: response_data = response_data[resultClass] + if resultClass == "sexes": + response_data.insert(0, {**response_data[0], "id": None}) if returnAsDict: response_data = {item["id"]: item["name"] for item in response_data} + response_data[None] = response_data[0] logger.debug("Person Masterdata load successful len=%s", len(response_data)) diff --git a/tests/test_churchtools_api_persons.py b/tests/test_churchtools_api_persons.py index 55f14ce..54b026f 100644 --- a/tests/test_churchtools_api_persons.py +++ b/tests/test_churchtools_api_persons.py @@ -111,10 +111,25 @@ def test_get_persons_sex_id(self) -> None: EXPECTED_RESULT = "sex.unknown" SAMPLE_USER_ID = 513 - person = self.api.get_persons(ids=[SAMPLE_USER_ID]) + person = self.api.get_persons(ids=[SAMPLE_USER_ID])[0] gender_map = self.api.get_persons_masterdata( resultClass="sexes", returnAsDict=True ) - result = gender_map[person[0]["sexId"]] + result = gender_map[person["sexId"]] + + assert result == EXPECTED_RESULT + + def test_get_persons_sex_id_none(self) -> None: + """Tests that persons sexId can be converted if NULL. + + IMPORTANT - This test method and the parameters used depend on target system! + the hard coded sample exists on ELKW1610.KRZ.TOOLS + """ + EXPECTED_RESULT = "sex.unknown" + + gender_map = self.api.get_persons_masterdata( + resultClass="sexes", returnAsDict=True + ) + result = gender_map[None] assert result == EXPECTED_RESULT