11import 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
54from controller .fhir_controller import FhirController
65from update_imms_handler import update_imms
76
87
98class 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