Skip to content

Commit b1b3636

Browse files
committed
NPA-5191: Add new test
1 parent 0750f0b commit b1b3636

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

sandbox/api/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@
9393
# POST QuestionnaireResponse
9494
POST_QUESTIONNAIRE_RESPONSE_DIRECTORY = "./api/examples/POST_QuestionnaireResponse/"
9595
POST_QUESTIONNAIRE_RESPONSE__SUCCESS = f"{POST_QUESTIONNAIRE_RESPONSE_DIRECTORY}success.yaml"
96-
POST_QUESTIONNAIRE_RESPONSE__DUPLICATE_RELATIONSHIP_ERROR = f"{POST_QUESTIONNAIRE_RESPONSE_DIRECTORY}errors/duplicate_relationship_error.yaml"
96+
POST_QUESTIONNAIRE_RESPONSE__DUPLICATE_RELATIONSHIP_ERROR = \
97+
f"{POST_QUESTIONNAIRE_RESPONSE_DIRECTORY}errors/duplicate_relationship_error.yaml"
9798

9899
# GET QuestionnaireResponse
99100
GET_QUESTIONNAIRE_RESPONSE_DIRECTORY = "./api/examples/GET_QuestionnaireResponse/"

sandbox/api/post_questionnaire_response.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
from logging import INFO, basicConfig, getLogger
22
from typing import Union
33

4-
from .constants import INTERNAL_SERVER_ERROR_EXAMPLE, POST_QUESTIONNAIRE_RESPONSE__SUCCESS
4+
from flask import request
5+
6+
from .constants import (
7+
INTERNAL_SERVER_ERROR_EXAMPLE,
8+
POST_QUESTIONNAIRE_RESPONSE__SUCCESS,
9+
POST_QUESTIONNAIRE_RESPONSE__DUPLICATE_RELATIONSHIP_ERROR,
10+
)
511
from .utils import generate_response_from_example
612

713
basicConfig(level=INFO, format="%(asctime)s - %(message)s")
@@ -15,7 +21,24 @@ def post_questionnaire_response_response() -> Union[dict, tuple]:
1521
Union[dict, tuple]: Response for POST /QuestionnaireResponse
1622
"""
1723
try:
18-
return generate_response_from_example(POST_QUESTIONNAIRE_RESPONSE__SUCCESS, 200)
24+
logger.debug("Received request to POST questionnaire response")
25+
# Validate body - beyond the scope of sandbox - assume body is valid for scenario
26+
json = request.get_json()
27+
source_identifier = json.get("source", {}).get("identifier", {}).get("value")
28+
response = None
29+
30+
# Successful questionnaire response
31+
if source_identifier in ["9000000009", "9000000017"]: # Example NHS numbers for success
32+
response = generate_response_from_example(POST_QUESTIONNAIRE_RESPONSE__SUCCESS, 200)
33+
# Duplicate relationship
34+
elif source_identifier == "9000000049":
35+
response = generate_response_from_example(POST_QUESTIONNAIRE_RESPONSE__DUPLICATE_RELATIONSHIP_ERROR, 409)
36+
else:
37+
# Out of scope errors
38+
raise ValueError("Invalid Request")
39+
40+
return response
41+
1942
except Exception:
2043
logger.exception("POST questionnaire response failed")
2144
return generate_response_from_example(INTERNAL_SERVER_ERROR_EXAMPLE, 500)

sandbox/api/tests/test_post_questionnaire_response.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,54 @@
44
import pytest
55
from flask import Response
66

7+
from sandbox.api.constants import (
8+
POST_QUESTIONNAIRE_RESPONSE__SUCCESS,
9+
POST_QUESTIONNAIRE_RESPONSE__DUPLICATE_RELATIONSHIP_ERROR,
10+
)
11+
712
QUESTIONNAIRE_RESPONSE_API_ENDPOINT = "/FHIR/R4/QuestionnaireResponse"
813

914

1015
@pytest.mark.parametrize(
11-
("url_path", "response_file_name", "status_code"),
16+
("nhs_num", "response_file_name", "status_code"),
1217
[
1318
(
14-
QUESTIONNAIRE_RESPONSE_API_ENDPOINT,
15-
"./api/examples/POST_QuestionnaireResponse/success.yaml",
19+
"9000000009",
20+
POST_QUESTIONNAIRE_RESPONSE__SUCCESS,
1621
200,
1722
),
23+
(
24+
"9000000049",
25+
POST_QUESTIONNAIRE_RESPONSE__DUPLICATE_RELATIONSHIP_ERROR,
26+
409,
27+
),
1828
],
1929
)
2030
@patch("sandbox.api.post_questionnaire_response.generate_response_from_example")
2131
def test_post_questionnaire_response(
2232
mock_generate_response_from_example: MagicMock,
23-
url_path: str,
33+
nhs_num: str,
2434
response_file_name: str,
25-
client: object,
2635
status_code: int,
36+
client: object,
2737
) -> None:
28-
"""Test related_persons endpoint with identifier only."""
38+
"""Test POST QuestionnaireResponse endpoint with different scenarios."""
2939
# Arrange
3040
mock_generate_response_from_example.return_value = mocked_response = Response(
3141
dumps({"data": "mocked"}),
3242
status=status_code,
3343
content_type="application/json",
3444
)
45+
json = {
46+
"resourceType": "QuestionnaireResponse",
47+
"source": {
48+
"identifier": {
49+
"value": nhs_num
50+
}
51+
}
52+
}
3553
# Act
36-
response = client.post(url_path, json={"data": "mocked"})
54+
response = client.post(QUESTIONNAIRE_RESPONSE_API_ENDPOINT, json=json)
3755
# Assert
3856
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
3957
assert response.status_code == status_code

0 commit comments

Comments
 (0)