Skip to content

Commit

Permalink
fix get_groups return type and updated test - Fixes #111
Browse files Browse the repository at this point in the history
  • Loading branch information
bensteUEM committed Oct 4, 2024
1 parent 0ba2219 commit 315539c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
49 changes: 31 additions & 18 deletions churchtools_api/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 12 additions & 8 deletions tests/test_churchtools_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 315539c

Please sign in to comment.