Skip to content

Commit f854020

Browse files
committed
test: wip sandbox unit test fixes
1 parent 6710e6a commit f854020

File tree

2 files changed

+110
-30
lines changed

2 files changed

+110
-30
lines changed

sandbox/api/tests/test_app.py

Lines changed: 110 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from json import dumps
23
from unittest.mock import MagicMock, patch
34

@@ -30,16 +31,6 @@ def test_health_check(client: object, endpoint: str) -> None:
3031
@pytest.mark.parametrize(
3132
"request_args,response_file_name,status_code",
3233
[
33-
(
34-
"identifier=9000000041",
35-
"./api/responses/not_found.json",
36-
404,
37-
),
38-
(
39-
"identifier=9000000017&patient:identifier=9000000041",
40-
"./api/responses/not_found.json",
41-
404,
42-
),
4334
(
4435
"identifier=9000000033",
4536
"./api/responses/GET_RelatedPerson/empty_response_9000000033.json",
@@ -111,6 +102,46 @@ def test_related_person(
111102
assert response.json == expected_body
112103

113104

105+
@pytest.mark.parametrize(
106+
"request_args,response_file_name,status_code",
107+
[
108+
(
109+
"identifier=9000000041",
110+
"./api/examples/errors/not-found.yaml",
111+
404,
112+
),
113+
(
114+
"identifier=9000000017&patient:identifier=9000000041",
115+
"./api/examples/errors/not-found.yaml",
116+
404,
117+
)
118+
],
119+
)
120+
@patch(f"{UTILS_FILE_PATH}.generate_response_from_example")
121+
def test_related_person__not_found(
122+
mock_generate_response_from_example: MagicMock,
123+
request_args: str,
124+
response_file_name: str,
125+
status_code: int,
126+
client: object
127+
) -> None:
128+
"""Test related_persons endpoint."""
129+
# Arrange
130+
mock_generate_response_from_example.return_value = mocked_response = (
131+
Response(
132+
dumps({"data": "mocked"}),
133+
status=status_code,
134+
content_type="application/json"
135+
)
136+
)
137+
# Act
138+
response = client.get(f"{RELATED_PERSON_API_ENDPOINT}?{request_args}")
139+
# Assert
140+
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
141+
assert response.status_code == status_code
142+
assert response.json == json.loads(mocked_response.get_data(as_text=True))
143+
144+
114145
@pytest.mark.parametrize(
115146
"url_path,response_file_name,status_code",
116147
[
@@ -144,54 +175,106 @@ def test_questionnaire_response(
144175
("request_args,response_file_name,status_code"),
145176
[
146177
(
147-
"performer:identifier=9000000010&status=active&_include=Consent:performer",
148-
"./api/examples/GET_Consent/adults-consenting-active-include-both.yaml",
178+
"performer:identifier=9000000017&status=active",
179+
"./api/examples/GET_Consent/filtered-relationships-status-active.yaml",
149180
200,
150181
),
151182
(
152-
"performer:identifier=9000000017&status=active&_include=Consent:performer",
153-
"./api/examples/GET_Consent/mixed.yaml",
183+
"performer:identifier=9000000017&status=inactive",
184+
"./api/examples/GET_Consent/filtered-relationships-status-inactive.yaml",
154185
200,
155186
),
156187
(
157-
"performer:identifier=9000000019&status=active&_include=Consent:performer",
158-
"./api/examples/GET_Consent/mother-child.yaml",
188+
"performer:identifier=9000000017&status=proposed,active",
189+
"./api/examples/GET_Consent/filtered-relationships-status-proposed-active.yaml",
159190
200,
160191
),
161-
],
192+
(
193+
"performer:identifier=9000000022",
194+
"./api/examples/GET_Consent/multiple-relationships.yaml",
195+
200,
196+
),
197+
(
198+
"performer:identifier=9000000022&_include=Consent:patient",
199+
"./api/examples/GET_Consent/multiple-relationships-include-patient.yaml",
200+
200,
201+
),
202+
(
203+
"performer:identifier=9000000022&_include=Consent:performer",
204+
"./api/examples/GET_Consent/multiple-relationships-include-performer.yaml",
205+
200,
206+
),
207+
(
208+
"performer:identifier=9000000022&_include=Consent:performer,Consent:patient",
209+
"./api/examples/GET_Consent/multiple-relationships-include-performer-patient.yaml",
210+
200,
211+
),
212+
(
213+
"performer:identifier=9000000025",
214+
"./api/examples/GET_Consent/no-relationships.yaml",
215+
200,
216+
),
217+
(
218+
"performer:identifier=9000000010",
219+
"./api/examples/GET_Consent/single-consenting-adult-relationship.yaml",
220+
200,
221+
),
222+
(
223+
"performer:identifier=9000000010&_include=Consent:performer,Consent:patient",
224+
"./api/examples/GET_Consent/single-consenting-adult-relationship-include-performer-patient.yaml",
225+
200,
226+
),
227+
(
228+
"performer:identifier=9000000019",
229+
"./api/examples/GET_Consent/single-mother-child-relationship.yaml",
230+
200,
231+
),
232+
(
233+
"performer:identifier=9000000019&_include=Consent:performer,Consent:patient",
234+
"./api/examples/GET_Consent/single-mother-child-relationship-include-performer-patient.yaml",
235+
200
236+
)
237+
]
162238
)
163239
@patch(f"{APP_FILE_PATH}.generate_response_from_example")
164240
def test_consent(
165241
mock_generate_response_from_example: MagicMock,
166242
request_args: str,
167243
response_file_name: str,
168-
client: object,
169244
status_code: int,
245+
client: object
170246
) -> None:
171247
"""Test Consent endpoint."""
172-
mock_generate_response_from_example.return_value = mock_response = Response(
173-
dumps({"data": "abc"}), status_code
248+
mock_generate_response_from_example.return_value = mocked_response = (
249+
Response(
250+
dumps({"data": "mocked"}),
251+
status=status_code,
252+
content_type="application/json"
253+
)
174254
)
175255
# Act
176256
response = client.get(f"{CONSENT_API_ENDPOINT}?{request_args}")
177257
# Assert
178-
mock_generate_response_from_example.assert_called_once_with(response_file_name, 200)
258+
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
179259
assert response.status_code == status_code
180-
assert response.json == mock_response.json
260+
assert response.json == json.loads(mocked_response.get_data(as_text=True))
181261

182262

183-
@patch(f"{APP_FILE_PATH}.load_json_file")
263+
@patch(f"{APP_FILE_PATH}.generate_response_from_example")
184264
def test_consent__400_bad_request(
185-
mock_load_json_file: MagicMock, client: object
265+
mock_generate_response_from_example: MagicMock,
266+
client: object,
186267
) -> None:
187268
"""Test Consent endpoint."""
188-
mock_load_json_file.return_value = {"data": "mocked"}
269+
mock_generate_response_from_example.return_value = {"data": "mocked"}
189270
# Act
190271
client.get(
191-
f"{CONSENT_API_ENDPOINT}?performer:identifier=9000000012&status=active&_include=Consent:performer"
272+
f"{CONSENT_API_ENDPOINT}?performer:identifier=9000000999"
192273
)
193274
# Assert
194-
mock_load_json_file.assert_called_once_with("./api/responses/not_found.json")
275+
mock_generate_response_from_example.assert_called_once_with(
276+
"./api/examples/errors/not-found.yaml"
277+
)
195278

196279

197280
@patch(f"{APP_FILE_PATH}.remove_system")

sandbox/api/utils.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ def check_for_errors(request: Request, identifier_key: str) -> Optional[tuple]:
4141
identifier = request.args.get(identifier_key)
4242
identifier_without_system = remove_system(request.args.get(identifier_key))
4343

44-
print(f"Identifier = {identifier}")
45-
print(f"Identifier_without_system = {identifier_without_system}")
46-
4744
if not identifier:
4845
return generate_response_from_example(
4946
"./api/examples/errors/missing-identifier.yaml", 400

0 commit comments

Comments
 (0)