Skip to content

Commit 393de15

Browse files
committed
test: updated tests
1 parent 87b95a2 commit 393de15

File tree

5 files changed

+189
-138
lines changed

5 files changed

+189
-138
lines changed

sandbox/api/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@
7575
CONSENT__SINGLE_MOTHER_CHILD_RELATIONSHIP_INCLUDE_BOTH = (
7676
consent_dir + "single-mother-child-relationship-include-performer-patient.yaml"
7777
)
78-
CONSENT__STATUS_PARAM_INVALID = consent_dir + "/errors/invalid-status-parameter.yaml"
78+
CONSENT__STATUS_PARAM_INVALID = consent_dir + "errors/invalid-status-parameter.yaml"

sandbox/api/tests/test_app.py

Lines changed: 8 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -102,44 +102,6 @@ def test_related_person(
102102
assert response.json == expected_body
103103

104104

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 = Response(
131-
dumps({"data": "mocked"}), status=status_code, content_type="application/json"
132-
)
133-
# Act
134-
response = client.get(f"{RELATED_PERSON_API_ENDPOINT}?{request_args}")
135-
# Assert
136-
mock_generate_response_from_example.assert_called_once_with(
137-
response_file_name, status_code
138-
)
139-
assert response.status_code == status_code
140-
assert response.json == json.loads(mocked_response.get_data(as_text=True))
141-
142-
143105
@pytest.mark.parametrize(
144106
"url_path,response_file_name,status_code",
145107
[
@@ -173,69 +135,24 @@ def test_questionnaire_response(
173135
("request_args,response_file_name,status_code"),
174136
[
175137
(
176-
"performer:identifier=9000000017&status=active",
177-
"./api/examples/GET_Consent/filtered-relationships-status-active.yaml",
178-
200,
179-
),
180-
(
181-
"performer:identifier=9000000017&status=inactive",
182-
"./api/examples/GET_Consent/filtered-relationships-status-inactive.yaml",
183-
200,
184-
),
185-
(
186-
"performer:identifier=9000000017&status=proposed,active",
187-
"./api/examples/GET_Consent/filtered-relationships-status-proposed-active.yaml",
188-
200,
189-
),
190-
(
191-
"performer:identifier=9000000022",
192-
"./api/examples/GET_Consent/multiple-relationships.yaml",
193-
200,
194-
),
195-
(
196-
"performer:identifier=9000000022&_include=Consent:patient",
197-
"./api/examples/GET_Consent/multiple-relationships-include-patient.yaml",
138+
"performer:identifier=9000000025", # No performer record found error
139+
"./api/examples/GET_Consent/no-relationships.yaml",
198140
200,
199141
),
200142
(
201-
"performer:identifier=9000000022&_include=Consent:performer",
202-
"./api/examples/GET_Consent/multiple-relationships-include-performer.yaml",
203-
200,
204-
),
205-
(
206-
"performer:identifier=9000000022&_include=Consent:performer,Consent:patient",
207-
"./api/examples/GET_Consent/multiple-relationships-include-performer-patient.yaml",
208-
200,
209-
),
210-
(
211-
"performer:identifier=9000000010",
212-
"./api/examples/GET_Consent/single-consenting-adult-relationship.yaml",
213-
200,
214-
),
215-
(
216-
"performer:identifier=9000000010&_include=Consent:performer,Consent:patient",
217-
"./api/examples/GET_Consent/single-consenting-adult-relationship-include-performer-patient.yaml",
218-
200,
219-
),
220-
(
221-
"performer:identifier=9000000019",
222-
"./api/examples/GET_Consent/single-mother-child-relationship.yaml",
223-
200,
224-
),
225-
(
226-
"performer:identifier=9000000019&_include=Consent:performer,Consent:patient",
227-
"./api/examples/GET_Consent/single-mother-child-relationship-include-performer-patient.yaml",
228-
200,
143+
"performer:identifier=9000000999", # No performer record found error
144+
"./api/examples/errors/not-found.yaml",
145+
404,
229146
),
230147
],
231148
)
232-
@patch(f"{UTILS_FILE_PATH}.generate_response_from_example")
233-
def test_consent(
149+
@patch(f"{APP_FILE_PATH}.generate_response_from_example")
150+
def test_consent_from_app(
234151
mock_generate_response_from_example: MagicMock,
235152
request_args: str,
236153
response_file_name: str,
237154
status_code: int,
238-
client: object,
155+
client: object
239156
) -> None:
240157
"""Test Consent endpoint."""
241158
mock_generate_response_from_example.return_value = mocked_response = Response(
@@ -251,41 +168,6 @@ def test_consent(
251168
assert response.json == json.loads(mocked_response.get_data(as_text=True))
252169

253170

254-
@patch(f"{APP_FILE_PATH}.generate_response_from_example")
255-
def test_consent_no_relationships(
256-
mock_generate_response_from_example: MagicMock, client: object
257-
) -> None:
258-
"""Test Consent endpoint."""
259-
mock_generate_response_from_example.return_value = mocked_response = Response(
260-
dumps({"data": "mocked"}), status=200, content_type="application/json"
261-
)
262-
# Act
263-
response = client.get(f"{CONSENT_API_ENDPOINT}?performer:identifier=9000000025")
264-
# Assert
265-
mock_generate_response_from_example.assert_called_once_with(
266-
"./api/examples/GET_Consent/no-relationships.yaml", 200
267-
)
268-
assert response.status_code == 200
269-
assert response.json == json.loads(mocked_response.get_data(as_text=True))
270-
271-
272-
@patch(f"{APP_FILE_PATH}.generate_response_from_example")
273-
def test_consent__404_bad_request(
274-
mock_generate_response_from_example: MagicMock,
275-
client: object,
276-
) -> None:
277-
"""Test Consent endpoint."""
278-
mock_generate_response_from_example.return_value = Response(
279-
dumps({"data": "mocked"}), status=404, content_type="application/json"
280-
)
281-
# Act
282-
client.get(f"{CONSENT_API_ENDPOINT}?performer:identifier=9000000999")
283-
# Assert
284-
mock_generate_response_from_example.assert_called_once_with(
285-
"./api/examples/errors/not-found.yaml", 404
286-
)
287-
288-
289171
@patch(f"{APP_FILE_PATH}.remove_system")
290172
@patch(f"{APP_FILE_PATH}.generate_response_from_example")
291173
def test_consent__500_internal_server_error(

sandbox/api/tests/test_utils.py

Lines changed: 163 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,146 @@
1+
import json
12
from json import dumps
23
from unittest.mock import MagicMock, patch
34

45
import pytest
56
from flask import Response
67

78
from ..utils import load_json_file
8-
from .conftest import RELATED_PERSON_API_ENDPOINT
9+
from .conftest import RELATED_PERSON_API_ENDPOINT, CONSENT_API_ENDPOINT
910

1011
FILE_PATH = "sandbox.api.utils"
1112

1213

14+
@pytest.mark.parametrize(
15+
"request_args,response_file_name,status_code",
16+
[
17+
(
18+
"identifier=9000000041",
19+
"./api/examples/errors/not-found.yaml",
20+
404,
21+
),
22+
(
23+
"identifier=9000000017&patient:identifier=9000000041",
24+
"./api/examples/errors/not-found.yaml",
25+
404,
26+
),
27+
],
28+
)
29+
@patch(f"{FILE_PATH}.generate_response_from_example")
30+
def test_related_person__not_found(
31+
mock_generate_response_from_example: MagicMock,
32+
request_args: str,
33+
response_file_name: str,
34+
status_code: int,
35+
client: object,
36+
) -> None:
37+
"""Test related_persons endpoint."""
38+
# Arrange
39+
mock_generate_response_from_example.return_value = mocked_response = Response(
40+
dumps({"data": "mocked"}), status=status_code, content_type="application/json"
41+
)
42+
# Act
43+
response = client.get(f"{RELATED_PERSON_API_ENDPOINT}?{request_args}")
44+
# Assert
45+
mock_generate_response_from_example.assert_called_once_with(
46+
response_file_name, status_code
47+
)
48+
assert response.status_code == status_code
49+
assert response.json == json.loads(mocked_response.get_data(as_text=True))
50+
51+
52+
@pytest.mark.parametrize(
53+
("request_args,response_file_name,status_code"),
54+
[
55+
(
56+
"performer:identifier=9000000017&status=active",
57+
"./api/examples/GET_Consent/filtered-relationships-status-active.yaml",
58+
200,
59+
),
60+
(
61+
"performer:identifier=9000000017&status=inactive",
62+
"./api/examples/GET_Consent/filtered-relationships-status-inactive.yaml",
63+
200,
64+
),
65+
(
66+
"performer:identifier=9000000017&status=proposed,active",
67+
"./api/examples/GET_Consent/filtered-relationships-status-proposed-active.yaml",
68+
200,
69+
),
70+
(
71+
"performer:identifier=9000000022",
72+
"./api/examples/GET_Consent/multiple-relationships.yaml",
73+
200,
74+
),
75+
(
76+
"performer:identifier=9000000022&_include=Consent:patient",
77+
"./api/examples/GET_Consent/multiple-relationships-include-patient.yaml",
78+
200,
79+
),
80+
(
81+
"performer:identifier=9000000022&_include=Consent:performer",
82+
"./api/examples/GET_Consent/multiple-relationships-include-performer.yaml",
83+
200,
84+
),
85+
(
86+
"performer:identifier=9000000022&_include=Consent:performer,Consent:patient",
87+
"./api/examples/GET_Consent/multiple-relationships-include-performer-patient.yaml",
88+
200,
89+
),
90+
(
91+
"performer:identifier=9000000010",
92+
"./api/examples/GET_Consent/single-consenting-adult-relationship.yaml",
93+
200,
94+
),
95+
(
96+
"performer:identifier=9000000010&_include=Consent:performer,Consent:patient",
97+
"./api/examples/GET_Consent/single-consenting-adult-relationship-include-performer-patient.yaml",
98+
200,
99+
),
100+
(
101+
"performer:identifier=9000000019",
102+
"./api/examples/GET_Consent/single-mother-child-relationship.yaml",
103+
200,
104+
),
105+
(
106+
"performer:identifier=9000000019&_include=Consent:performer,Consent:patient",
107+
"./api/examples/GET_Consent/single-mother-child-relationship-include-performer-patient.yaml",
108+
200,
109+
),
110+
(
111+
"performer:identifier=9000000017&status=test", #Invalid status parameter error
112+
"./api/examples/GET_Consent/errors/invalid-status-parameter.yaml",
113+
400,
114+
),
115+
(
116+
"performer:identifier=9000000019&_include=test", #Invalid include parameter error
117+
"./api/examples/errors/invalid-include-parameter.yaml",
118+
400,
119+
),
120+
],
121+
)
122+
@patch(f"{FILE_PATH}.generate_response_from_example")
123+
def test_consent(
124+
mock_generate_response_from_example: MagicMock,
125+
request_args: str,
126+
response_file_name: str,
127+
status_code: int,
128+
client: object,
129+
) -> None:
130+
"""Test Consent endpoint."""
131+
mock_generate_response_from_example.return_value = mocked_response = Response(
132+
dumps({"data": "mocked"}), status=status_code, content_type="application/json"
133+
)
134+
# Act
135+
response = client.get(f"{CONSENT_API_ENDPOINT}?{request_args}")
136+
# Assert
137+
mock_generate_response_from_example.assert_called_once_with(
138+
response_file_name, status_code
139+
)
140+
assert response.status_code == status_code
141+
assert response.json == json.loads(mocked_response.get_data(as_text=True))
142+
143+
13144
@patch(f"{FILE_PATH}.open")
14145
def test_get_response(mock_open: MagicMock) -> None:
15146
# Arrange
@@ -25,28 +156,50 @@ def test_get_response(mock_open: MagicMock) -> None:
25156

26157

27158
@pytest.mark.parametrize(
28-
"request_args,response_file_name,status_code",
159+
"endpoint,request_args,response_file_name,status_code",
29160
[
30161
(
31-
"", # identifier is missing
162+
RELATED_PERSON_API_ENDPOINT, # Related person - identifier is missing
163+
"",
32164
"./api/examples/errors/missing-identifier.yaml",
33165
400,
34166
),
35167
(
36-
"identifier=123456789", # identifier length is less than 10
168+
RELATED_PERSON_API_ENDPOINT, # Related person - identifier length is less than 10
169+
"identifier=123456789",
37170
"./api/examples/errors/invalid-identifier.yaml",
38171
400,
39172
),
40-
# (
41-
# "identifier=https://fhir.nhs.uk/ID/nhs-number|1234567890", # identifier system invalid
42-
# "./api/responses/GET_RelatedPerson/bad_request_identifier_invalid_system.json",
43-
# 400,
44-
# ),
173+
(
174+
RELATED_PERSON_API_ENDPOINT, # Related person - identifier system invalid
175+
"identifier=https://fhir.nhs.uk/Id/nhs-number|A730675929",
176+
"./api/examples/errors/invalid-identifier-system.yaml",
177+
400,
178+
),
179+
(
180+
CONSENT_API_ENDPOINT, # Consent - Invalid performer identifier
181+
"performer:identifier=90000009990",
182+
"./api/examples/errors/invalid-identifier.yaml",
183+
400,
184+
),
185+
(
186+
CONSENT_API_ENDPOINT, # Consent - missing performer identifier
187+
"",
188+
"./api/examples/errors/missing-identifier.yaml",
189+
400,
190+
),
191+
(
192+
CONSENT_API_ENDPOINT, # Related person - identifier system invalid
193+
"performer:identifier=https://fhir.nhs.uk/Id/nhs-number|A730675929",
194+
"./api/examples/errors/invalid-identifier-system.yaml",
195+
400,
196+
),
45197
],
46198
)
47199
@patch(f"{FILE_PATH}.generate_response_from_example")
48200
def test_check_for_errors(
49201
mock_generate_response_from_example: MagicMock,
202+
endpoint : str,
50203
request_args: str,
51204
response_file_name: str,
52205
status_code: int,
@@ -56,7 +209,7 @@ def test_check_for_errors(
56209
dumps({"data": "mocked"}), status=status_code, content_type="application/json"
57210
)
58211
# Act
59-
response = client.get(f"{RELATED_PERSON_API_ENDPOINT}?{request_args}")
212+
response = client.get(f"{endpoint}?{request_args}")
60213
# Assert
61214
mock_generate_response_from_example.assert_called_once_with(
62215
response_file_name, status_code

sandbox/api/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def check_for_errors(request: Request, identifier_key: str) -> Optional[tuple]:
5353
elif (
5454
isinstance(identifier, str)
5555
and "|" in identifier
56-
and "https://fhir.nhs.uk/Id/nhs-number" != identifier.split("|", maxsplit=2)[0]
56+
and "https://fhir.nhs.uk/Id/nhs-number" == identifier.split("|", maxsplit=2)[0]
5757
):
5858
# invalid identifier system
5959
return generate_response_from_example(

0 commit comments

Comments
 (0)