Skip to content

Commit

Permalink
NPA-3321 Update Recursion and Fix Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JackPlowman committed Oct 23, 2024
1 parent 9827582 commit 07a1295
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 63 deletions.
9 changes: 4 additions & 5 deletions sandbox/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
61 changes: 39 additions & 22 deletions sandbox/api/related_person.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
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,
"./api/responses/GET_RelatedPerson/list_relationship.json",
"./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,
"./api/responses/GET_RelatedPerson/verify_relationship.json",
"./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")
Expand All @@ -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
43 changes: 7 additions & 36 deletions sandbox/api/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -24,69 +25,39 @@ 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",
200,
),
(
"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,
Expand Down

0 comments on commit 07a1295

Please sign in to comment.