Skip to content

Commit 87e5f28

Browse files
committed
Resolved comments and updated the handler itself
1 parent 9ee07c2 commit 87e5f28

File tree

3 files changed

+4
-71
lines changed

3 files changed

+4
-71
lines changed

backend/src/models/errors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import uuid
22
from dataclasses import dataclass
33
from enum import Enum
4-
from typing import Any, Union
4+
from typing import Any
55

66

77
class Severity(str, Enum):
@@ -120,7 +120,7 @@ def to_operation_outcome(self) -> dict:
120120
class UnhandledResponseError(RuntimeError):
121121
"""Use this error when the response from an external service (ex: dynamodb) can't be handled"""
122122

123-
response: Union[dict, str]
123+
response: dict | str
124124
message: str
125125

126126
def __str__(self):

backend/src/update_imms_handler.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import argparse
22
import logging
33
import pprint
4-
import uuid
54

6-
from constants import GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE
7-
from controller.aws_apig_response_utils import create_response
85
from controller.fhir_controller import FhirController, make_controller
96
from local_lambda import load_string
107
from log_structure import function_info
11-
from models.errors import Code, Severity, create_operation_outcome
128

139
logging.basicConfig(level="INFO")
1410
logger = logging.getLogger()
@@ -20,17 +16,7 @@ def update_imms_handler(event, _context):
2016

2117

2218
def update_imms(event, controller: FhirController):
23-
try:
24-
return controller.update_immunization(event)
25-
except Exception: # pylint: disable = broad-exception-caught
26-
logger.exception("Unhandled exception")
27-
exp_error = create_operation_outcome(
28-
resource_id=str(uuid.uuid4()),
29-
severity=Severity.error,
30-
code=Code.server_error,
31-
diagnostics=GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE,
32-
)
33-
return create_response(500, exp_error)
19+
return controller.update_immunization(event)
3420

3521

3622
if __name__ == "__main__":

backend/tests/test_update_imms.py

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
import unittest
2-
from unittest.mock import create_autospec, patch
2+
from unittest.mock import create_autospec
33

4-
from constants import GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE
54
from controller.fhir_controller import FhirController
65
from update_imms_handler import update_imms
76

87

98
class TestUpdateImmunizations(unittest.TestCase):
109
def setUp(self):
1110
self.controller = create_autospec(FhirController)
12-
self.logger_exception_patcher = patch("logging.Logger.exception")
13-
self.mock_logger_exception = self.logger_exception_patcher.start()
14-
self.logger_info_patcher = patch("logging.Logger.info")
15-
self.mock_logger_info = self.logger_info_patcher.start()
16-
self.logger_exception_patcher = patch("logging.Logger.exception")
17-
self.mock_logger_exception = self.logger_exception_patcher.start()
18-
19-
def tearDown(self):
20-
patch.stopall()
2111

2212
def test_update_immunization(self):
2313
"""it should call service update method"""
@@ -32,46 +22,3 @@ def test_update_immunization(self):
3222
# Then
3323
self.controller.update_immunization.assert_called_once_with(lambda_event)
3424
self.assertDictEqual(exp_res, act_res)
35-
36-
@patch("update_imms_handler.create_response")
37-
def test_update_imms_exception(self, mock_create_response):
38-
"""unhandled exceptions should result in 500"""
39-
lambda_event = {"pathParameters": {"id": "an-id"}}
40-
error_msg = "an unhandled error"
41-
self.controller.update_immunization.side_effect = Exception(error_msg)
42-
43-
mock_response = "controller-response-error"
44-
mock_create_response.return_value = mock_response
45-
46-
# When
47-
act_res = update_imms(lambda_event, self.controller)
48-
49-
# Then
50-
# check parameters used to call create_response
51-
args, kwargs = mock_create_response.call_args
52-
self.assertEqual(args[0], 500)
53-
issue = args[1]["issue"][0]
54-
severity = issue["severity"]
55-
code = issue["code"]
56-
diagnostics = issue["diagnostics"]
57-
self.assertEqual(severity, "error")
58-
self.assertEqual(code, "exception")
59-
self.assertEqual(diagnostics, GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE)
60-
self.assertEqual(act_res, mock_response)
61-
62-
def test_update_imms_with_duplicated_identifier_returns_error(self):
63-
"""Should return an IdentifierDuplication error"""
64-
lambda_event = {"pathParameters": {"id": "an-id"}}
65-
error_msg = {
66-
"statusCode": 422,
67-
"headers": {"Content-Type": "application/fhir+json"},
68-
"body": '{"resourceType": "OperationOutcome", "id": "5c132d8a-a928-4e0e-8792-0c6456e625c2", "meta": {"profile": ["https://simplifier.net/guide/UKCoreDevelopment2/ProfileUKCore-OperationOutcome"]}, "issue": [{"severity": "error", "code": "exception", "details": {"coding": [{"system": "https://fhir.nhs.uk/Codesystem/http-error-codes","code": "DUPLICATE"}]}, "diagnostics": "The provided identifier: id-id is duplicated"}]}',
69-
}
70-
self.controller.update_immunization.return_value = error_msg
71-
72-
act_res = update_imms(lambda_event, self.controller)
73-
74-
# Then
75-
self.controller.update_immunization.assert_called_once_with(lambda_event)
76-
self.assertEqual(act_res["statusCode"], 422)
77-
self.assertDictEqual(act_res, error_msg)

0 commit comments

Comments
 (0)