diff --git a/sandbox/api/app.py b/sandbox/api/app.py index e1c3a37..ff80694 100644 --- a/sandbox/api/app.py +++ b/sandbox/api/app.py @@ -2,11 +2,11 @@ from typing import Union from flask import Flask, request -from .related_person import determine_success_response + +from .related_person import determine_success_response, special_cases from .utils import ( ERROR_RESPONSE, QUESTIONNAIRE_RESPONSE_SUCCESS, - EMPTY_RESPONSE, check_for_errors, generate_response, load_json_file, @@ -46,9 +46,8 @@ def get_related_persons() -> Union[dict, tuple]: patient_identifier = request.args.get("patient:identifier", "") include = request.args.get("_include", "") - if identifier == "9000000033": - # 200 But Empty response - return generate_response(load_json_file(EMPTY_RESPONSE)) + if response := special_cases(identifier): + return response return determine_success_response(identifier, patient_identifier, include) diff --git a/sandbox/api/related_person.py b/sandbox/api/related_person.py index 4322f13..5894733 100644 --- a/sandbox/api/related_person.py +++ b/sandbox/api/related_person.py @@ -1,18 +1,24 @@ -from .utils import ( - generate_response, - load_json_file, -) +from typing import Any, Optional + +from .utils import generate_response, load_json_file, EMPTY_RESPONSE INCLUDE_FLAG = "RelatedPerson:patient" +def special_cases(identifier: str) -> Optional[dict]: + """Determines if the identifier is a special case""" + if identifier == "9000000033": + # 200 But Empty response + return generate_response(load_json_file(EMPTY_RESPONSE)) + + def determine_success_response( identifier: str, patient_identifier: str, include: str ) -> dict: """Determines the response for a successful request""" if identifier and not patient_identifier: - # One to one relationship + # One to many response = load_json_file( check_for_include( include, @@ -20,11 +26,11 @@ def determine_success_response( "./api/responses/GET_RelatedPerson/list_relationship_include.json", ) ) - response = update_value("9000000017", identifier, response) + response = update_values("9000000017", identifier, response) return generate_response(response) if identifier and patient_identifier: - # One to many relationship + # One to one response = load_json_file( check_for_include( include, @@ -32,8 +38,8 @@ def determine_success_response( "./api/responses/GET_RelatedPerson/verify_relationship_include.json", ) ) - response = update_value("9000000017", identifier, response) - response = update_value("9000000009", patient_identifier, response) + response = update_values("9000000017", identifier, response) + response = update_values("9000000009", patient_identifier, response) return generate_response(response) raise ValueError("Invalid request") @@ -58,25 +64,36 @@ def check_for_include( return without_include_file -def update_value(from_str: str, to_str: str, response: dict) -> dict: +def update_values(from_str: str, to_str: str, data: Any) -> dict: """Updates values in the response (can be run recursively) Args: from_str (str): The string to replace to_str (str): The string to replace with - response (dict): The response to update + data (Any): The response to update, likely to dict/list Returns: dict: The updated response """ - for key, value in response.items(): - if isinstance(value, dict): - # If the value is a dictionary, call the function recursively - update_value(value, from_str, to_str) - elif value == from_str: - # If the value matches, update it - response[key] = to_str - elif isinstance(value, str): - # Value may be inside a string so replace if in string - response[key] = value.replace(from_str, to_str) - return response + if isinstance(data, dict): + # If the data is a dictionary, iterate over its items + for key, value in data.items(): + if isinstance(value, (dict, list)): + # If the value is a dictionary, call the function recursively + update_values(from_str, to_str, value) + elif value == from_str: + # If the value matches, update it + data[key] = to_str + elif isinstance(value, str): + # Value may be inside a string so replace if in string + data[key] = value.replace(from_str, to_str) + elif isinstance(data, list): + # If the data is a list, iterate over its elements + for index, item in enumerate(data): + if isinstance(item, (dict, list)): + # Recurse if the item is a nested dictionary or list + data[index] = update_values(from_str, to_str, item) + elif item == from_str: + # Update if the item matches the target_value + data[index] = to_str + return data diff --git a/sandbox/api/tests/test_app.py b/sandbox/api/tests/test_app.py index 77517fc..121a597 100644 --- a/sandbox/api/tests/test_app.py +++ b/sandbox/api/tests/test_app.py @@ -4,6 +4,7 @@ from .conftest import RELATED_PERSON_API_ENDPOINT, QUESTIONNAIRE_RESPONSE_API_ENDPOINT +RELATED_PERSON_FILE_PATH = "sandbox.api.related_person" UTILS_FILE_PATH = "sandbox.api.utils" APP_FILE_PATH = "sandbox.api.app" @@ -24,16 +25,6 @@ def test_health_check(client: object, endpoint: str) -> None: @pytest.mark.parametrize( "request_args,response_file_name,status_code", [ - ( - "identifier=9000000041", - "./api/responses/not_found.json", - 404, - ), - ( - "identifier=9000000017&patient:identifier=9000000041", - "./api/responses/not_found.json", - 404, - ), ( "identifier=9000000033", "./api/responses/GET_RelatedPerson/empty_response_9000000033.json", @@ -41,52 +32,32 @@ def test_health_check(client: object, endpoint: str) -> None: ), ( "identifier=9000000017", - "./api/responses/GET_RelatedPerson/list_relationship_9000000017.json", + "./api/responses/GET_RelatedPerson/list_relationship.json", 200, ), ( "identifier=9000000017&_include=RelatedPerson:patient", - "./api/responses/GET_RelatedPerson/list_relationship_include_9000000017.json", + "./api/responses/GET_RelatedPerson/list_relationship_include.json", 200, ), ( "identifier=9000000017&_include=any", - "./api/responses/GET_RelatedPerson/list_relationship_9000000017.json", - 200, - ), - ( - "identifier=9000000017&patient:identifier=9000000009", - "./api/responses/GET_RelatedPerson/verify_relationship_9000000009.json", - 200, - ), - ( - "identifier=9000000017&patient:identifier=9000000009&_include=RelatedPerson:patient", - "./api/responses/GET_RelatedPerson/verify_relationship_include_9000000009.json", - 200, - ), - ( - "identifier=9000000017&patient:identifier=9000000009&_include=any", - "./api/responses/GET_RelatedPerson/verify_relationship_9000000009.json", - 200, - ), - ( - "identifier=9000000017&patient:identifier=9000000025", - "./api/responses/GET_RelatedPerson/verify_relationship_9000000025.json", + "./api/responses/GET_RelatedPerson/list_relationship.json", 200, ), ( "identifier=9000000017&patient:identifier=9000000025&_include=RelatedPerson:patient", - "./api/responses/GET_RelatedPerson/verify_relationship_include_9000000025.json", + "./api/responses/GET_RelatedPerson/verify_relationship_include.json", 200, ), ( "identifier=9000000017&patient:identifier=9000000025&_include=any", - "./api/responses/GET_RelatedPerson/verify_relationship_9000000025.json", + "./api/responses/GET_RelatedPerson/verify_relationship.json", 200, ), ], ) -@patch(f"{UTILS_FILE_PATH}.load_json_file") +@patch(f"{RELATED_PERSON_FILE_PATH}.load_json_file") def test_related_person( mock_load_json_file: MagicMock, request_args: str,