11import unittest
22from unittest .mock import patch , MagicMock
33from pds_details import pds_get_patient_details
4+ from models .id_sync_exception import IdSyncException
45
56
67class TestGetPdsPatientDetails (unittest .TestCase ):
@@ -58,9 +59,6 @@ def test_pds_get_patient_details_success(self):
5859 # Assert
5960 self .assertEqual (result , "9912003888" )
6061
61- # Verify logger was called
62- self .mock_logger .info .assert_called_once_with (f"Get PDS patient details for { self .test_patient_id } " )
63-
6462 # Verify Cache was initialized correctly
6563 self .mock_cache_class .assert_called_once ()
6664
@@ -79,9 +77,7 @@ def test_pds_get_patient_details_no_patient_found(self):
7977 self .assertIsNone (result )
8078
8179 # Verify both logger calls
82- self .mock_logger .info .assert_any_call (f"Get PDS patient details for { self .test_patient_id } " )
8380 self .mock_logger .info .assert_any_call (f"No patient details found for ID: { self .test_patient_id } " )
84- self .assertEqual (self .mock_logger .info .call_count , 2 )
8581
8682 self .mock_pds_service_instance .get_patient_details .assert_called_once_with (self .test_patient_id )
8783
@@ -97,13 +93,12 @@ def test_pds_get_patient_details_empty_response(self):
9793 self .assertIsNone (result )
9894
9995 # Verify both logger calls
100- self .mock_logger .info .assert_any_call (f"Get PDS patient details for { self .test_patient_id } " )
10196 self .mock_logger .info .assert_any_call (f"No patient details found for ID: { self .test_patient_id } " )
102- self .assertEqual (self .mock_logger .info .call_count , 2 )
10397
10498 def test_pds_get_patient_details_missing_identifier_field (self ):
10599 """Test when PDS response doesn't contain 'identifier' field"""
106100 # Arrange
101+ test_nhs_number = "my-nhs-number"
107102 patient_data_without_identifier = {
108103 "name" : "John Doe" ,
109104 "birthDate" : "1990-01-01" ,
@@ -113,14 +108,18 @@ def test_pds_get_patient_details_missing_identifier_field(self):
113108 self .mock_pds_service_instance .get_patient_details .return_value = patient_data_without_identifier
114109
115110 # Act
116- result = pds_get_patient_details (self .test_patient_id )
111+ with self .assertRaises (IdSyncException ) as context :
112+ pds_get_patient_details (test_nhs_number )
113+
114+ exception = context .exception
117115
118116 # Assert
119- self .assertIsNone (result )
117+ self .assertEqual (exception .message , f"Error getting PDS patient details for { test_nhs_number } " )
118+ self .assertEqual (exception .nhs_numbers , None )
120119
121120 # Verify exception was logged
122121 self .mock_logger .exception .assert_called_once_with (
123- f"Error getting PDS patient details for { self . test_patient_id } " )
122+ f"Error getting PDS patient details for { test_nhs_number } " )
124123
125124 def test_pds_get_patient_details_empty_identifier_array (self ):
126125 """Test when identifier array is empty"""
@@ -132,10 +131,13 @@ def test_pds_get_patient_details_empty_identifier_array(self):
132131 self .mock_pds_service_instance .get_patient_details .return_value = patient_data_empty_identifier
133132
134133 # Act
135- result = pds_get_patient_details (self .test_patient_id )
134+ with self .assertRaises (IdSyncException ) as context :
135+ pds_get_patient_details (self .test_patient_id )
136136
137137 # Assert
138- self .assertIsNone (result )
138+ exception = context .exception
139+ self .assertEqual (exception .message , f"Error getting PDS patient details for { self .test_patient_id } " )
140+ self .assertEqual (exception .nhs_numbers , None )
139141
140142 # Verify exception was logged due to IndexError
141143 self .mock_logger .exception .assert_called_once_with (
@@ -152,10 +154,14 @@ def test_pds_get_patient_details_identifier_missing_value(self):
152154 self .mock_pds_service_instance .get_patient_details .return_value = patient_data_missing_value
153155
154156 # Act
155- result = pds_get_patient_details (self .test_patient_id )
157+ with self .assertRaises (IdSyncException ) as context :
158+ pds_get_patient_details (self .test_patient_id )
159+
160+ exception = context .exception
156161
157162 # Assert
158- self .assertIsNone (result )
163+ self .assertEqual (exception .message , f"Error getting PDS patient details for { self .test_patient_id } " )
164+ self .assertEqual (exception .nhs_numbers , None )
159165
160166 # Verify exception was logged due to KeyError
161167 self .mock_logger .exception .assert_called_once_with (
@@ -181,13 +187,19 @@ def test_pds_get_patient_details_multiple_identifiers(self):
181187 def test_pds_get_patient_details_pds_service_exception (self ):
182188 """Test when PdsService.get_patient_details raises an exception"""
183189 # Arrange
184- self .mock_pds_service_instance .get_patient_details .side_effect = Exception ("PDS API error" )
190+ mock_exception = Exception ("My custom error" )
191+ self .mock_pds_service_instance .get_patient_details .side_effect = mock_exception
185192
186193 # Act
187- result = pds_get_patient_details (self .test_patient_id )
194+ with self .assertRaises (IdSyncException ) as context :
195+ pds_get_patient_details (self .test_patient_id )
196+
197+ exception = context .exception
188198
189199 # Assert
190- self .assertIsNone (result )
200+ self .assertEqual (exception .inner_exception , mock_exception )
201+ self .assertEqual (exception .message , f"Error getting PDS patient details for { self .test_patient_id } " )
202+ self .assertEqual (exception .nhs_numbers , None )
191203
192204 # Verify exception was logged
193205 self .mock_logger .exception .assert_called_once_with (
@@ -201,10 +213,13 @@ def test_pds_get_patient_details_cache_initialization_error(self):
201213 self .mock_cache_class .side_effect = OSError ("Cannot write to /tmp" )
202214
203215 # Act
204- result = pds_get_patient_details (self .test_patient_id )
216+ with self .assertRaises (IdSyncException ) as context :
217+ pds_get_patient_details (self .test_patient_id )
205218
206219 # Assert
207- self .assertIsNone (result )
220+ exception = context .exception
221+ self .assertEqual (exception .message , f"Error getting PDS patient details for { self .test_patient_id } " )
222+ self .assertEqual (exception .nhs_numbers , None )
208223
209224 # Verify exception was logged
210225 self .mock_logger .exception .assert_called_once_with (
@@ -218,37 +233,37 @@ def test_pds_get_patient_details_auth_initialization_error(self):
218233 self .mock_auth_class .side_effect = ValueError ("Invalid authentication parameters" )
219234
220235 # Act
221- result = pds_get_patient_details (self .test_patient_id )
236+ with self .assertRaises (IdSyncException ) as context :
237+ pds_get_patient_details (self .test_patient_id )
222238
223239 # Assert
224- self .assertIsNone (result )
240+ exception = context .exception
241+ self .assertEqual (exception .message , f"Error getting PDS patient details for { self .test_patient_id } " )
242+ self .assertEqual (exception .nhs_numbers , None )
225243
226244 # Verify exception was logged
227245 self .mock_logger .exception .assert_called_once_with (
228246 f"Error getting PDS patient details for { self .test_patient_id } " )
229247
230- def test_pds_get_patient_details_logger_info_exception (self ):
248+ def test_pds_get_patient_details_exception (self ):
231249 """Test when logger.info throws an exception"""
232250 # Arrange
233- self .mock_logger .info .side_effect = Exception ("Logging system failure" )
251+ test_exception = Exception ("some-random-error" )
252+ self .mock_pds_service_class .side_effect = test_exception
253+ test_nhs_number = "another-nhs-number"
234254
235255 # Act
236- result = pds_get_patient_details (self .test_patient_id )
256+ with self .assertRaises (Exception ) as context :
257+ pds_get_patient_details (test_nhs_number )
237258
259+ exception = context .exception
238260 # Assert
239- self .assertIsNone (result )
240-
241- # Verify logger.info was called and raised exception
242- self .mock_logger .info .assert_called_once_with (f"Get PDS patient details for { self .test_patient_id } " )
243-
261+ self .assertEqual (exception .inner_exception , test_exception )
262+ self .assertEqual (exception .message , f"Error getting PDS patient details for { test_nhs_number } " )
263+ self .assertEqual (exception .nhs_numbers , None )
244264 # Verify logger.exception was called due to the caught exception
245265 self .mock_logger .exception .assert_called_once_with (
246- f"Error getting PDS patient details for { self .test_patient_id } " )
247-
248- # Verify that no other services were initialized since exception occurred early
249- self .mock_cache_class .assert_not_called ()
250- self .mock_auth_class .assert_not_called ()
251- self .mock_pds_service_class .assert_not_called ()
266+ f"Error getting PDS patient details for { test_nhs_number } " )
252267
253268 def test_pds_get_patient_details_different_patient_ids (self ):
254269 """Test with different patient ID formats"""
@@ -272,7 +287,6 @@ def test_pds_get_patient_details_different_patient_ids(self):
272287
273288 # Assert
274289 self .assertEqual (result , patient_id )
275- self .mock_logger .info .assert_called_once_with (f"Get PDS patient details for { patient_id } " )
276290 self .mock_pds_service_instance .get_patient_details .assert_called_once_with (patient_id )
277291
278292 def test_pds_get_patient_details_service_dependencies (self ):
0 commit comments