Skip to content

Commit 6a77cc4

Browse files
committed
NPA-4689 Add Unit Tests
1 parent 4df6cbe commit 6a77cc4

File tree

4 files changed

+92
-11
lines changed

4 files changed

+92
-11
lines changed

sandbox/api/constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@
6969
# GET QuestionnaireResponse
7070
GET_QUESTIONNAIRE_RESPONSE_DIRECTORY = "./api/examples/GET_QuestionnaireResponse/"
7171
GET_QUESTIONNAIRE_RESPONSE__SUCCESS = f"{GET_QUESTIONNAIRE_RESPONSE_DIRECTORY}success.yaml"
72+
GET_QUESTIONNAIRE_RESPONSE__INVALID = f"{GET_QUESTIONNAIRE_RESPONSE_DIRECTORY}errors/invalid-reference-code.yaml"
73+
GET_QUESTIONNAIRE_RESPONSE__MISSING = f"{GET_QUESTIONNAIRE_RESPONSE_DIRECTORY}errors/missing-reference-code.yaml"
74+
GET_QUESTIONNAIRE_RESPONSE__NOT_FOUND = (
75+
f"{GET_QUESTIONNAIRE_RESPONSE_DIRECTORY}errors/questionnaire_response_not_found.yaml"
76+
)
7277

7378
# GET RelatedPerson
7479
RELATED_DIRECTORY = "./api/examples/GET_RelatedPerson/"

sandbox/api/get_questionnaire_response.py

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

4-
from .constants import INTERNAL_SERVER_ERROR_EXAMPLE, GET_QUESTIONNAIRE_RESPONSE__SUCCESS
4+
from flask import request
5+
6+
from .constants import (
7+
GET_QUESTIONNAIRE_RESPONSE__INVALID,
8+
GET_QUESTIONNAIRE_RESPONSE__MISSING,
9+
GET_QUESTIONNAIRE_RESPONSE__NOT_FOUND,
10+
GET_QUESTIONNAIRE_RESPONSE__SUCCESS,
11+
INTERNAL_SERVER_ERROR_EXAMPLE,
12+
)
513
from .utils import generate_response_from_example
614

715
basicConfig(level=INFO, format="%(asctime)s - %(message)s")
@@ -15,7 +23,17 @@ def get_questionnaire_response_response() -> Union[dict, tuple]:
1523
Union[dict, tuple]: Response for GET /QuestionnaireResponse
1624
"""
1725
try:
18-
return generate_response_from_example(GET_QUESTIONNAIRE_RESPONSE__SUCCESS, 200)
26+
reference_code = request.args.get("referenceCode")
27+
if reference_code == "19318ZGLAB":
28+
return generate_response_from_example(GET_QUESTIONNAIRE_RESPONSE__SUCCESS, 200)
29+
elif reference_code == "INVALID":
30+
return generate_response_from_example(GET_QUESTIONNAIRE_RESPONSE__INVALID, 400)
31+
elif reference_code == "" or reference_code is None:
32+
return generate_response_from_example(GET_QUESTIONNAIRE_RESPONSE__MISSING, 404)
33+
elif reference_code == "ABC123XY":
34+
return generate_response_from_example(GET_QUESTIONNAIRE_RESPONSE__NOT_FOUND, 404)
35+
else:
36+
raise ValueError("Invalid reference code")
1937
except Exception:
2038
logger.exception("GET questionnaire response failed")
2139
return generate_response_from_example(INTERNAL_SERVER_ERROR_EXAMPLE, 500)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from unittest.mock import MagicMock, patch
2+
import pytest
3+
from flask import Response
4+
from json import dumps, loads
5+
6+
GET_QUESTIONNAIRE_RESPONSE_API_ENDPOINT = "/FHIR/R4/QuestionnaireResponse"
7+
8+
9+
@pytest.mark.parametrize(
10+
("request_args", "response_file_name", "status_code"),
11+
[
12+
(
13+
"referenceCode=19318ZGLAB",
14+
"./api/examples/GET_QuestionnaireResponse/success.yaml",
15+
200,
16+
),
17+
(
18+
"referenceCode=INVALID",
19+
"./api/examples/GET_QuestionnaireResponse/errors/invalid-reference-code.yaml",
20+
400,
21+
),
22+
(
23+
"referenceCode=",
24+
"./api/examples/GET_QuestionnaireResponse/errors/missing-reference-code.yaml",
25+
404,
26+
),
27+
(
28+
"referenceCode=ABC123XY",
29+
"./api/examples/GET_QuestionnaireResponse/errors/questionnaire_response_not_found.yaml",
30+
404,
31+
),
32+
(
33+
"referenceCode=INVALID_CODE",
34+
"./api/examples/errors/internal-server-error.yaml",
35+
500,
36+
),
37+
],
38+
)
39+
@patch("sandbox.api.get_questionnaire_response.generate_response_from_example")
40+
def test_get_consent_returns_expected_responses__mocked_utils(
41+
mock_generate_response_from_example: MagicMock,
42+
request_args: str,
43+
response_file_name: str,
44+
status_code: int,
45+
client: object,
46+
) -> None:
47+
"""Test GET Consent endpoint."""
48+
mock_generate_response_from_example.return_value = mocked_response = Response(
49+
dumps({"data": "mocked"}),
50+
status=status_code,
51+
content_type="application/json",
52+
)
53+
# Act
54+
response = client.get(f"{GET_QUESTIONNAIRE_RESPONSE_API_ENDPOINT}?{request_args}")
55+
# Assert
56+
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
57+
assert response.status_code == status_code
58+
assert response.json == loads(mocked_response.get_data(as_text=True))

specification/validated-relationships-service-api.yaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,12 @@ paths:
259259
260260
## Sandbox test scenarios
261261
262-
| Scenario | Request | Response |
263-
| ------------------------- | -------------------------------- | --------------------------------------------- |
264-
| Valid reference code | referenceCode=19318ZGLAB | HTTP Status 200 with QuestionnaireResponse |
265-
| Invalid reference code | referenceCode=INVALID | HTTP Status 400 with error message |
266-
| Missing reference code | No referenceCode parameter | HTTP Status 400 with error message |
267-
| Non-existent reference | referenceCode=ABC123XY | HTTP Status 404 with error message |
262+
| Scenario | Request | Response |
263+
| --------------------------- | -------------------------- | ------------------------------------------------------------- |
264+
| Valid reference code | referenceCode=19318ZGLAB | HTTP Status 200 with QuestionnaireResponse |
265+
| Invalid reference code | referenceCode=INVALID | HTTP Status 400 with INVALID_REFERENCE_CODE message |
266+
| Missing reference code | No referenceCode parameter | HTTP Status 400 with MISSING_REFERENCE_CODE message |
267+
| Non-existent reference code | referenceCode=ABC123XY | HTTP Status 404 with QUESTIONNAIRE_RESPONSE_NOT_FOUND message |
268268
269269
operationId: get-questionnaire-response
270270
parameters:
@@ -295,11 +295,11 @@ paths:
295295
| HTTP status | Error code | Description |
296296
| ----------- | ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
297297
| 400 | `BAD_REQUEST` | Bad request. |
298-
| 400 | `INVALID_REFERENCE_CODE` | The reference code must be alphanumeric and exactly 10 characters long. |
299-
| 400 | `MISSING_REFERENCE_CODE` | The reference code is required but was not provided in the request. |
298+
| 400 | `INVALID_REFERENCE_CODE` | The reference code must be alphanumeric and exactly 10 characters long. |
299+
| 400 | `MISSING_REFERENCE_CODE` | The reference code is required but was not provided in the request. |
300300
| 401 | `ACCESS_DENIED` | Missing or invalid OAuth 2.0 bearer token in request. |
301301
| 403 | `FORBIDDEN` | Access denied to resource. |
302-
| 404 | `QUESTIONNAIRE_RESPONSE_NOT_FOUND` | No questionnaire response was found for the provided reference code. |
302+
| 404 | `QUESTIONNAIRE_RESPONSE_NOT_FOUND` | No questionnaire response was found for the provided reference code. |
303303
| 404 | `INVALIDATED_RESOURCE` | Resource that has been marked as invalid was requested - invalid resources cannot be retrieved |
304304
| 405 | `METHOD_NOT_ALLOWED` | The method is not allowed. |
305305
| 408 | `TIMEOUT` | Request timed out. |

0 commit comments

Comments
 (0)