diff --git a/churchtools_api/groups.py b/churchtools_api/groups.py index 032f333..4881ced 100644 --- a/churchtools_api/groups.py +++ b/churchtools_api/groups.py @@ -5,8 +5,9 @@ logger = logging.getLogger(__name__) + class ChurchToolsApiGroups(ChurchToolsApiAbstract): - """ Part definition of ChurchToolsApi which focuses on groups + """Part definition of ChurchToolsApi which focuses on groups Args: ChurchToolsApiAbstract: template with minimum references @@ -41,11 +42,15 @@ def get_groups(self, **kwargs) -> list[dict]: response_data = self.combine_paginated_response_data( response_content, url=url, headers=headers ) - + response_data = ( + [response_data] if isinstance(response_data, dict) else response_data + ) return response_data else: logger.warning( - "%s Something went wrong fetching groups: %s",response.status_code, response.content + "%s Something went wrong fetching groups: %s", + response.status_code, + response.content, ) def get_groups_hierarchies(self): @@ -157,36 +162,44 @@ def create_group( return response_data else: logger.warning( - "%s Something went wrong with creating group: %s", response.status_code, response.content + "%s Something went wrong with creating group: %s", + response.status_code, + response.content, ) - def update_group(self, group_id: int, data: dict): + def update_group(self, group_id: int, data: dict) -> dict: """ - Update a field of the given group + Update a field of the given group. to loookup available names use get_group(group_id=xxx) - :param group_id: int: required group_id - :param data: dict: required group fields data - :return: dict with updated group - :rtype: dict + + Arguments: + group_id: number of the group to update + data: all group fields + + Returns: + dict with updated group """ - url = self.domain + '/api/groups/{}'.format(group_id) + url = self.domain + f"/api/groups/{group_id}" headers = { - 'accept': 'application/json', - 'Content-Type': 'application/json', + "accept": "application/json", + "Content-Type": "application/json", } - response = self.session.patch( - url=url, headers=headers, data=json.dumps(data)) + response = self.session.patch(url=url, headers=headers, data=json.dumps(data)) if response.status_code == 200: response_content = json.loads(response.content) - response_data = response_content['data'].copy() + response_data = response_content["data"].copy() logger.debug( - "First response of Update Group successful {}".format(response_content)) + "First response of Update Group successful {}".format(response_content) + ) return response_data else: logger.warning( - "%s Something went wrong updating group: %s", response.status_code, response.content) + "%s Something went wrong updating group: %s", + response.status_code, + response.content, + ) def delete_group(self, group_id: int) -> bool: """Delete the given group. diff --git a/tests/test_churchtools_api.py b/tests/test_churchtools_api.py index 700742a..d4cc474 100644 --- a/tests/test_churchtools_api.py +++ b/tests/test_churchtools_api.py @@ -187,7 +187,10 @@ def test_get_groups(self) -> None: self.assertTrue(isinstance(groups[0], dict)) self.assertGreater(len(groups), 10) - group = self.api.get_groups(group_id=103) + groups = self.api.get_groups(group_id=103) + self.assertTrue(isinstance(groups, list)) + group = groups[0] + self.assertTrue(isinstance(group, dict)) self.assertEqual(group["id"], 103) self.assertEqual(group["name"], "TestGruppe") @@ -332,15 +335,16 @@ def test_update_group(self): Also cleans the field after executing the test :return: """ - test_group_id = 103 + SAMPLE_GROUP_ID = 103 data = {"note": "TestNote - if this exists an automated test case failed"} - group = self.api.update_group(group_id=test_group_id, data=data) - self.assertEqual(group['information']['note'], data['note']) + group_update_result = self.api.update_group(group_id=SAMPLE_GROUP_ID, data=data) + self.assertEqual(group_update_result["information"]["note"], data["note"]) - group = self.api.update_group( - group_id=test_group_id, data={"note": ""}) - group = self.api.get_groups(group_id=test_group_id) - self.assertEqual(group['information']['note'], '') + group_update_result = self.api.update_group( + group_id=SAMPLE_GROUP_ID, data={"note": ""} + ) + groups = self.api.get_groups(group_id=SAMPLE_GROUP_ID) + self.assertEqual(groups[0]["information"]["note"], "") def test_get_group_members(self) -> None: """Checks if group members can be retrieved from the group and filtering