Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPA-3321 Remove Sandbox NHS Number Filtering #110

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 9 additions & 47 deletions sandbox/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@

from flask import Flask, request

from .related_person import determine_success_response, special_cases
from .utils import (
ERROR_RESPONSE,
LIST_RELATIONSHIP,
LIST_RELATIONSHIP_INCLUDE,
QUESTIONNAIRE_RESPONSE_SUCCESS,
VALIDATE_RELATIONSHIP_009,
VALIDATE_RELATIONSHIP_025,
VALIDATE_RELATIONSHIP_INCLUDE_009,
VALIDATE_RELATIONSHIP_INCLUDE_025,
check_for_empty,
check_for_errors,
check_for_list,
check_for_validate,
generate_response,
load_json_file,
)
Expand Down Expand Up @@ -50,44 +42,14 @@ def get_related_persons() -> Union[dict, tuple]:
if errors := check_for_errors(request):
return errors

identifier = request.args.get("identifier")
patient_identifier = request.args.get("patient:identifier")
include = request.args.get("_include")

if empty := check_for_empty(identifier, patient_identifier):
return empty

# Successful request, select response
if zero_nine := check_for_validate(
"9000000009",
identifier,
patient_identifier,
include,
VALIDATE_RELATIONSHIP_009,
VALIDATE_RELATIONSHIP_INCLUDE_009,
):
return zero_nine

if two_five := check_for_validate(
"9000000025",
identifier,
patient_identifier,
include,
VALIDATE_RELATIONSHIP_025,
VALIDATE_RELATIONSHIP_INCLUDE_025,
):
return two_five

if one_seven := check_for_list(
"9000000017",
identifier,
include,
LIST_RELATIONSHIP,
LIST_RELATIONSHIP_INCLUDE,
):
return one_seven

raise ValueError("Invalid request")
identifier = request.args.get("identifier", "")
patient_identifier = request.args.get("patient:identifier", "")
include = request.args.get("_include", "")

if response := special_cases(identifier):
return response

return determine_success_response(identifier, patient_identifier, include)

except Exception as e:
logger.error(e)
Expand Down
99 changes: 99 additions & 0 deletions sandbox/api/related_person.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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 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_values("9000000017", identifier, response)
return generate_response(response)

if identifier and patient_identifier:
# 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_values("9000000017", identifier, response)
response = update_values("9000000009", patient_identifier, response)
return generate_response(response)

raise ValueError("Invalid request")


def check_for_include(
include: str, without_include_file: str, with_include_file: str
) -> str:
"""Checks if the include parameter is set to patient and determines the response

Args:
include (str): The include parameter supplied to the request
without_include_file (str): The file to return when record matches but does not request included data
with_include_file (str): The file to return when record matches and does request included data

Returns:
str: The response file to return
"""
if include == INCLUDE_FLAG:
return with_include_file
else:
return without_include_file


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
data (Any): The response to update, likely to dict/list

Returns:
dict: The updated 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

This file was deleted.

This file was deleted.

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
Loading