Skip to content

Commit e460511

Browse files
NPA-3889: Update sandbox to use yaml files
1 parent 5327983 commit e460511

File tree

5 files changed

+107
-99
lines changed

5 files changed

+107
-99
lines changed

sandbox/api/app.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
from flask import Flask, request
55

66
from .constants import (
7-
INTERNAL_ERROR_RESPONSE,
87
INTERNAL_SERVER_ERROR_EXAMPLE,
9-
LIST_RELATIONSHIP,
10-
LIST_RELATIONSHIP_INCLUDE,
8+
RELATED__LIST_RELATIONSHIP,
9+
RELATED__LIST_RELATIONSHIP_WITH_INCLUDE,
1110
INVALIDATED_RESOURCE,
12-
QUESTIONNAIRE_RESPONSE_SUCCESS,
13-
VALIDATE_RELATIONSHIP_009,
14-
VALIDATE_RELATIONSHIP_025,
15-
VALIDATE_RELATIONSHIP_INCLUDE_009,
16-
VALIDATE_RELATIONSHIP_INCLUDE_025,
11+
QUESTIONNAIRE_RESPONSE__SUCCESS,
12+
RELATED__VERIFY_RELATIONSHIP_09,
13+
RELATED__VERIFY_RELATIONSHIP_25,
14+
RELATED__VERIFY_RELATIONSHIP_09_WITH_INCLUDE,
15+
RELATED__VERIFY_RELATIONSHIP_25_WITH_INCLUDE,
1716
CONSENT__SINGLE_CONSENTING_ADULT_RELATIONSHIP,
1817
CONSENT__SINGLE_CONSENTING_ADULT_RELATIONSHIP_INCLUDE_BOTH,
1918
CONSENT__SINGLE_MOTHER_CHILD_RELATIONSHIP,
@@ -33,9 +32,7 @@
3332
check_for_related_person_errors,
3433
check_for_list,
3534
check_for_validate,
36-
generate_response,
3735
generate_response_from_example,
38-
load_json_file,
3936
remove_system,
4037
check_for_consent_include_params,
4138
check_for_consent_filtering,
@@ -84,8 +81,8 @@ def get_related_persons() -> Union[dict, tuple]:
8481
identifier,
8582
patient_identifier,
8683
include,
87-
VALIDATE_RELATIONSHIP_009,
88-
VALIDATE_RELATIONSHIP_INCLUDE_009,
84+
RELATED__VERIFY_RELATIONSHIP_09,
85+
RELATED__VERIFY_RELATIONSHIP_09_WITH_INCLUDE,
8986
):
9087
return zero_nine
9188

@@ -94,25 +91,25 @@ def get_related_persons() -> Union[dict, tuple]:
9491
identifier,
9592
patient_identifier,
9693
include,
97-
VALIDATE_RELATIONSHIP_025,
98-
VALIDATE_RELATIONSHIP_INCLUDE_025,
94+
RELATED__VERIFY_RELATIONSHIP_25,
95+
RELATED__VERIFY_RELATIONSHIP_25_WITH_INCLUDE,
9996
):
10097
return two_five
10198

10299
if one_seven := check_for_list(
103100
"9000000017",
104101
identifier,
105102
include,
106-
LIST_RELATIONSHIP,
107-
LIST_RELATIONSHIP_INCLUDE,
103+
RELATED__LIST_RELATIONSHIP,
104+
RELATED__LIST_RELATIONSHIP_WITH_INCLUDE,
108105
):
109106
return one_seven
110107

111108
raise ValueError("Invalid request")
112109

113110
except Exception as e:
114111
logger.error(e)
115-
return generate_response(load_json_file(INTERNAL_ERROR_RESPONSE), 500)
112+
return generate_response_from_example(INTERNAL_SERVER_ERROR_EXAMPLE, 500)
116113

117114

118115
@app.route(f"/{COMMON_PATH}/QuestionnaireResponse", methods=["POST"])
@@ -124,10 +121,10 @@ def post_questionnaire_response() -> Union[dict, tuple]:
124121
"""
125122

126123
try:
127-
return generate_response(load_json_file(QUESTIONNAIRE_RESPONSE_SUCCESS), 200)
124+
return generate_response_from_example(QUESTIONNAIRE_RESPONSE__SUCCESS, 200)
128125
except Exception as e:
129126
logger.error(e)
130-
return generate_response(load_json_file(INTERNAL_ERROR_RESPONSE), 500)
127+
return generate_response_from_example(INTERNAL_SERVER_ERROR_EXAMPLE, 500)
131128

132129

133130
@app.route(f"/{COMMON_PATH}/Consent", methods=["GET"])

sandbox/api/constants.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
1-
EMPTY_RESPONSE = "./api/responses/GET_RelatedPerson/empty_response_9000000033.json"
2-
LIST_RELATIONSHIP = (
3-
"./api/responses/GET_RelatedPerson/list_relationship_9000000017.json"
4-
)
5-
LIST_RELATIONSHIP_INCLUDE = (
6-
"./api/responses/GET_RelatedPerson/list_relationship_include_9000000017.json"
7-
)
8-
VALIDATE_RELATIONSHIP_009 = (
9-
"./api/responses/GET_RelatedPerson/verify_relationship_9000000009.json"
10-
)
11-
VALIDATE_RELATIONSHIP_INCLUDE_009 = (
12-
"./api/responses/GET_RelatedPerson/verify_relationship_include_9000000009.json"
13-
)
14-
VALIDATE_RELATIONSHIP_025 = (
15-
"./api/responses/GET_RelatedPerson/verify_relationship_9000000025.json"
16-
)
17-
VALIDATE_RELATIONSHIP_INCLUDE_025 = (
18-
"./api/responses/GET_RelatedPerson/verify_relationship_include_9000000025.json"
19-
)
20-
INTERNAL_ERROR_RESPONSE = "./api/responses/internal_server_error.json"
211
INCLUDE_FLAG = "RelatedPerson:patient"
222

23-
QUESTIONNAIRE_RESPONSE_SUCCESS = (
24-
"./api/responses/POST_QuestionnaireResponse/questionnaire_response_success.json"
25-
)
26-
273
PATIENT_IDENTIFIERS = ["9000000017", "9000000033"]
284
RELATED_IDENTIFIERS = ["9000000009", "9000000025"]
295

@@ -78,3 +54,33 @@
7854
CONSENT__STATUS_PARAM_INVALID = (
7955
f"{CONSENT__DIRECTORY}errors/invalid-status-parameter.yaml"
8056
)
57+
58+
QR_DIRECTORY = "./api/examples/POST_QuestionnaireResponse/"
59+
QUESTIONNAIRE_RESPONSE__SUCCESS = f"{QR_DIRECTORY}success.yaml"
60+
61+
RELATED_DIRECTORY = "./api/examples/GET_RelatedPerson/"
62+
RELATED__ERROR_IDENTIFIER_MISSING = (
63+
f"{RELATED_DIRECTORY}errors/invalid-identifier-missing.yaml"
64+
)
65+
RELATED__ERROR_IDENTIFIER_SYSTEM = (
66+
f"{RELATED_DIRECTORY}errors/invalid-identifier-system.yaml"
67+
)
68+
RELATED__ERROR_IDENTIFIER = f"{RELATED_DIRECTORY}errors/invalid-identifier.yaml"
69+
RELATED__EMPTY_RESPONSE = f"{RELATED_DIRECTORY}empty_response.yaml"
70+
RELATED__LIST_RELATIONSHIP = f"{RELATED_DIRECTORY}list_relationship_9000000017.yaml"
71+
RELATED__LIST_RELATIONSHIP_WITH_INCLUDE = (
72+
f"{RELATED_DIRECTORY}list_relationship_9000000017_include.yaml"
73+
)
74+
RELATED__VERIFY_RELATIONSHIP_09 = (
75+
f"{RELATED_DIRECTORY}verify_relationship_9000000009.yaml"
76+
)
77+
RELATED__VERIFY_RELATIONSHIP_09_WITH_INCLUDE = (
78+
f"{RELATED_DIRECTORY}verify_relationship_9000000009_include.yaml"
79+
)
80+
RELATED__VERIFY_RELATIONSHIP_25 = (
81+
f"{RELATED_DIRECTORY}verify_relationship_9000000025.yaml"
82+
)
83+
RELATED__VERIFY_RELATIONSHIP_25_WITH_INCLUDE = (
84+
f"{RELATED_DIRECTORY}verify_relationship_9000000025_include.yaml"
85+
)
86+
RELATED__EMPTY_RESPONSE = f"{RELATED_DIRECTORY}empty_response_9000000033.yaml"

sandbox/api/tests/test_app.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,102 +32,111 @@ def test_health_check(client: object, endpoint: str) -> None:
3232
[
3333
(
3434
"identifier=9000000033",
35-
"./api/responses/GET_RelatedPerson/empty_response_9000000033.json",
35+
"./api/examples/GET_RelatedPerson/empty_response_9000000033.yaml",
3636
200,
3737
),
3838
(
3939
"identifier=9000000017",
40-
"./api/responses/GET_RelatedPerson/list_relationship_9000000017.json",
40+
"./api/examples/GET_RelatedPerson/list_relationship_9000000017.yaml",
4141
200,
4242
),
4343
(
4444
"identifier=9000000017&_include=RelatedPerson:patient",
45-
"./api/responses/GET_RelatedPerson/list_relationship_include_9000000017.json",
45+
"./api/examples/GET_RelatedPerson/list_relationship_9000000017_include.yaml",
4646
200,
4747
),
4848
(
4949
"identifier=9000000017&_include=any",
50-
"./api/responses/GET_RelatedPerson/list_relationship_9000000017.json",
50+
"./api/examples/GET_RelatedPerson/list_relationship_9000000017.yaml",
5151
200,
5252
),
5353
(
5454
"identifier=9000000017&patient:identifier=9000000009",
55-
"./api/responses/GET_RelatedPerson/verify_relationship_9000000009.json",
55+
"./api/examples/GET_RelatedPerson/verify_relationship_9000000009.yaml",
5656
200,
5757
),
5858
(
5959
"identifier=9000000017&patient:identifier=9000000009&_include=RelatedPerson:patient",
60-
"./api/responses/GET_RelatedPerson/verify_relationship_include_9000000009.json",
60+
"./api/examples/GET_RelatedPerson/verify_relationship_9000000009_include.yaml",
6161
200,
6262
),
6363
(
6464
"identifier=9000000017&patient:identifier=9000000009&_include=any",
65-
"./api/responses/GET_RelatedPerson/verify_relationship_9000000009.json",
65+
"./api/examples/GET_RelatedPerson/verify_relationship_9000000009.yaml",
6666
200,
6767
),
6868
(
6969
"identifier=9000000017&patient:identifier=9000000025",
70-
"./api/responses/GET_RelatedPerson/verify_relationship_9000000025.json",
70+
"./api/examples/GET_RelatedPerson/verify_relationship_9000000025.yaml",
7171
200,
7272
),
7373
(
7474
"identifier=9000000017&patient:identifier=9000000025&_include=RelatedPerson:patient",
75-
"./api/responses/GET_RelatedPerson/verify_relationship_include_9000000025.json",
75+
"./api/examples/GET_RelatedPerson/verify_relationship_9000000025_include.yaml",
7676
200,
7777
),
7878
(
7979
"identifier=9000000017&patient:identifier=9000000025&_include=any",
80-
"./api/responses/GET_RelatedPerson/verify_relationship_9000000025.json",
80+
"./api/examples/GET_RelatedPerson/verify_relationship_9000000025.yaml",
8181
200,
8282
),
8383
],
8484
)
85-
@patch(f"{UTILS_FILE_PATH}.load_json_file")
85+
@patch("sandbox.api.utils.generate_response_from_example")
8686
def test_related_person(
87-
mock_load_json_file: MagicMock,
87+
mock_generate_response_from_example: MagicMock,
8888
request_args: str,
8989
response_file_name: str,
90-
client: object,
9190
status_code: int,
91+
client: object,
9292
) -> None:
9393
"""Test related_persons endpoint."""
94+
9495
# Arrange
95-
mock_load_json_file.return_value = expected_body = {"data": "mocked"}
96+
mock_generate_response_from_example.return_value = mocked_response = Response(
97+
dumps({"data": "mocked"}), status=status_code, content_type="application/json"
98+
)
9699
# Act
97100
response = client.get(f"{RELATED_PERSON_API_ENDPOINT}?{request_args}")
98101
# Assert
99-
mock_load_json_file.assert_called_once_with(response_file_name)
102+
mock_generate_response_from_example.assert_called_once_with(
103+
response_file_name, status_code
104+
)
100105
assert response.status_code == status_code
101-
assert response.json == expected_body
106+
assert response.json == loads(mocked_response.get_data(as_text=True))
102107

103108

104109
@pytest.mark.parametrize(
105110
"url_path,response_file_name,status_code",
106111
[
107112
(
108113
QUESTIONNAIRE_RESPONSE_API_ENDPOINT,
109-
"./api/responses/POST_QuestionnaireResponse/questionnaire_response_success.json",
114+
"./api/examples/POST_QuestionnaireResponse/success.yaml",
110115
200,
111116
),
112117
],
113118
)
114-
@patch(f"{APP_FILE_PATH}.load_json_file")
119+
@patch(f"{APP_FILE_PATH}.generate_response_from_example")
115120
def test_questionnaire_response(
116-
mock_load_json_file: MagicMock,
121+
mock_generate_response_from_example: MagicMock,
117122
url_path: str,
118123
response_file_name: str,
119124
client: object,
120125
status_code: int,
121126
) -> None:
122127
"""Test related_persons endpoint with identifier only."""
123128
# Arrange
124-
mock_load_json_file.return_value = expected_body = {"data": "mocked"}
129+
mock_generate_response_from_example.return_value = mocked_response = Response(
130+
dumps({"data": "mocked"}), status=status_code, content_type="application/json"
131+
)
125132
# Act
126133
response = client.post(url_path, json={"data": "mocked"})
127134
# Assert
128-
mock_load_json_file.assert_called_once_with(response_file_name)
135+
mock_generate_response_from_example.assert_called_once_with(
136+
response_file_name, status_code
137+
)
129138
assert response.status_code == status_code
130-
assert response.json == expected_body
139+
assert response.json == loads(mocked_response.get_data(as_text=True))
131140

132141

133142
@pytest.mark.parametrize(

sandbox/api/tests/test_utils.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,37 +160,45 @@ def test_get_response(mock_open: MagicMock) -> None:
160160

161161

162162
@pytest.mark.parametrize(
163-
"request_args,response_file_name",
163+
"request_args,response_file_name,status_code",
164164
[
165165
(
166166
"", # identifier is missing
167-
"./api/responses/GET_RelatedPerson/bad_request_identifier_missing.json",
167+
"./api/examples/GET_RelatedPerson/errors/invalid-identifier-missing.yaml",
168+
400,
168169
),
169170
(
170171
"identifier=123456789", # identifier length is less than 10
171-
"./api/responses/GET_RelatedPerson/bad_request_identifier_invalid.json",
172+
"./api/examples/GET_RelatedPerson/errors/invalid-identifier.yaml",
173+
400,
172174
),
173175
(
174176
"identifier=https://fhir.nhs.uk/Id/nhs-number|A730675929", # identifier system invalid
175-
"./api/responses/GET_RelatedPerson/bad_request_identifier_invalid_system.json",
177+
"./api/examples/GET_RelatedPerson/errors/invalid-identifier-system.yaml",
178+
400,
176179
),
177180
],
178181
)
179-
@patch(f"{FILE_PATH}.load_json_file")
182+
@patch("sandbox.api.utils.generate_response_from_example")
180183
def test_check_for_related_person_errors(
181-
mock_load_json_file: MagicMock,
184+
mock_generate_response_from_example: MagicMock,
182185
request_args: str,
183186
response_file_name: str,
187+
status_code: int,
184188
client: object,
185189
) -> None:
186-
mock_load_json_file.return_value = Response(
187-
dumps({"data": "mocked"}), content_type="application/json"
190+
# Arrange
191+
mock_generate_response_from_example.return_value = mocked_response = Response(
192+
dumps({"data": "mocked"}), status=status_code, content_type="application/json"
188193
)
189194
# Act
190195
response = client.get(f"{RELATED_PERSON_API_ENDPOINT}?{request_args}")
191196
# Assert
192-
mock_load_json_file.assert_called_once_with(response_file_name)
193-
assert response.status_code == 500
197+
mock_generate_response_from_example.assert_called_once_with(
198+
response_file_name, status_code
199+
)
200+
assert response.status_code == status_code
201+
assert response.json == loads(mocked_response.get_data(as_text=True))
194202

195203

196204
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)