11import json
22import unittest
3- from unittest .mock import create_autospec
3+ from unittest .mock import create_autospec , patch
44
55from fhir_controller import FhirController
66from models .errors import Severity , Code , create_operation_outcome
1010
1111class TestUpdateImmunizations (unittest .TestCase ):
1212 def setUp (self ):
13+ # self.controller = create_autospec(FhirController)
14+ self .logger_exception_patcher = patch ("logging.Logger.exception" )
15+ self .mock_logger_exception = self .logger_exception_patcher .start ()
16+ # patch FhirController
1317 self .controller = create_autospec (FhirController )
18+ self .controller .update_immunization = FhirController .update_immunization
19+
20+
21+
22+ def tearDown (self ):
23+ self .logger_exception_patcher .stop ()
24+
1425
1526 def test_update_immunization (self ):
1627 """it should call service update method"""
@@ -26,7 +37,8 @@ def test_update_immunization(self):
2637 self .controller .update_immunization .assert_called_once_with (lambda_event )
2738 self .assertDictEqual (exp_res , act_res )
2839
29- def test_handle_exception (self ):
40+ @patch ("update_imms_handler.FhirController.create_response" )
41+ def test_update_imms_exception (self , mock_create_response ):
3042 """unhandled exceptions should result in 500"""
3143 lambda_event = {"pathParameters" : {"id" : "an-id" }}
3244 error_msg = "an unhandled error"
@@ -38,16 +50,26 @@ def test_handle_exception(self):
3850 code = Code .server_error ,
3951 diagnostics = GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE ,
4052 )
53+ mock_response = "controller-response-error"
54+ mock_create_response .return_value = mock_response
4155
4256 # When
4357 act_res = update_imms (lambda_event , self .controller )
4458
4559 # Then
46- act_body = json .loads (act_res ["body" ])
47- act_body ["id" ] = None
60+ # check parameters used to call create_response
61+ args , kwargs = mock_create_response .call_args
62+ self .assertEqual (args [0 ], 500 )
63+ issue = args [1 ]["issue" ][0 ]
64+ severity = issue ["severity" ]
65+ code = issue ["code" ]
66+ diagnostics = issue ["diagnostics" ]
67+ self .assertEqual (severity , "error" )
68+ self .assertEqual (code , "exception" )
69+ self .assertEqual (diagnostics , GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE )
70+ self .assertEqual (act_res , mock_response )
4871
49- self .assertDictEqual (act_body , exp_error )
50- self .assertEqual (act_res ["statusCode" ], 500 )
72+
5173
5274 def test_update_imms_with_duplicated_identifier_returns_error (self ):
5375 """Should return an IdentifierDuplication error"""
0 commit comments