11import unittest
22from unittest .mock import patch , MagicMock
3- from pds_details import pds_get_patient_details
3+ from pds_details import pds_get_patient_details , pds_get_patient_id
44from models .id_sync_exception import IdSyncException
55
66
@@ -57,7 +57,7 @@ def test_pds_get_patient_details_success(self):
5757 result = pds_get_patient_details (self .test_patient_id )
5858
5959 # Assert
60- self .assertEqual (result , "9912003888" )
60+ self .assertEqual (result [ "identifier" ][ 0 ][ "value" ] , "9912003888" )
6161
6262 # Verify Cache was initialized correctly
6363 self .mock_cache_class .assert_called_once ()
@@ -76,114 +76,19 @@ def test_pds_get_patient_details_no_patient_found(self):
7676 # Assert
7777 self .assertIsNone (result )
7878
79- # Verify both logger calls
80- self .mock_logger .info .assert_any_call (f"No patient details found for ID: { self .test_patient_id } " )
81-
8279 self .mock_pds_service_instance .get_patient_details .assert_called_once_with (self .test_patient_id )
8380
8481 def test_pds_get_patient_details_empty_response (self ):
8582 """Test when PDS returns empty dict (falsy)"""
8683 # Arrange
87- self .mock_pds_service_instance .get_patient_details .return_value = {}
84+ self .mock_pds_service_instance .get_patient_details .return_value = None
8885
8986 # Act
9087 result = pds_get_patient_details (self .test_patient_id )
9188
9289 # Assert
9390 self .assertIsNone (result )
9491
95- # Verify both logger calls
96- self .mock_logger .info .assert_any_call (f"No patient details found for ID: { self .test_patient_id } " )
97-
98- def test_pds_get_patient_details_missing_identifier_field (self ):
99- """Test when PDS response doesn't contain 'identifier' field"""
100- # Arrange
101- test_nhs_number = "my-nhs-number"
102- patient_data_without_identifier = {
103- "name" : "John Doe" ,
104- "birthDate" : "1990-01-01" ,
105- "gender" : "male"
106- # Missing 'identifier' field
107- }
108- self .mock_pds_service_instance .get_patient_details .return_value = patient_data_without_identifier
109-
110- # Act
111- with self .assertRaises (IdSyncException ) as context :
112- pds_get_patient_details (test_nhs_number )
113-
114- exception = context .exception
115-
116- # Assert
117- self .assertEqual (exception .message , f"Error getting PDS patient details for { test_nhs_number } " )
118- self .assertEqual (exception .nhs_numbers , None )
119-
120- # Verify exception was logged
121- self .mock_logger .exception .assert_called_once_with (
122- f"Error getting PDS patient details for { test_nhs_number } " )
123-
124- def test_pds_get_patient_details_empty_identifier_array (self ):
125- """Test when identifier array is empty"""
126- # Arrange
127- patient_data_empty_identifier = {
128- "identifier" : [], # Empty array
129- "name" : "John Doe"
130- }
131- self .mock_pds_service_instance .get_patient_details .return_value = patient_data_empty_identifier
132-
133- # Act
134- with self .assertRaises (IdSyncException ) as context :
135- pds_get_patient_details (self .test_patient_id )
136-
137- # Assert
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 )
141-
142- # Verify exception was logged due to IndexError
143- self .mock_logger .exception .assert_called_once_with (
144- f"Error getting PDS patient details for { self .test_patient_id } " )
145-
146- def test_pds_get_patient_details_identifier_missing_value (self ):
147- """Test when identifier object doesn't have 'value' field"""
148- # Arrange
149- patient_data_missing_value = {
150- "identifier" : [
151- {"system" : "https://fhir.nhs.uk/Id/nhs-number" } # Missing 'value'
152- ]
153- }
154- self .mock_pds_service_instance .get_patient_details .return_value = patient_data_missing_value
155-
156- # Act
157- with self .assertRaises (IdSyncException ) as context :
158- pds_get_patient_details (self .test_patient_id )
159-
160- exception = context .exception
161-
162- # Assert
163- self .assertEqual (exception .message , f"Error getting PDS patient details for { self .test_patient_id } " )
164- self .assertEqual (exception .nhs_numbers , None )
165-
166- # Verify exception was logged due to KeyError
167- self .mock_logger .exception .assert_called_once_with (
168- f"Error getting PDS patient details for { self .test_patient_id } " )
169-
170- def test_pds_get_patient_details_multiple_identifiers (self ):
171- """Test when patient has multiple identifiers - should return first one"""
172- # Arrange
173- patient_data_multiple_identifiers = {
174- "identifier" : [
175- {"value" : "9912003888" }, # First identifier
176- {"value" : "9912003999" } # Second identifier
177- ]
178- }
179- self .mock_pds_service_instance .get_patient_details .return_value = patient_data_multiple_identifiers
180-
181- # Act
182- result = pds_get_patient_details (self .test_patient_id )
183-
184- # Assert
185- self .assertEqual (result , "9912003888" ) # Should return first identifier
186-
18792 def test_pds_get_patient_details_pds_service_exception (self ):
18893 """Test when PdsService.get_patient_details raises an exception"""
18994 # Arrange
@@ -286,7 +191,7 @@ def test_pds_get_patient_details_different_patient_ids(self):
286191 result = pds_get_patient_details (patient_id )
287192
288193 # Assert
289- self .assertEqual (result , patient_id )
194+ self .assertEqual (result , test_cases [ patient_id ] )
290195 self .mock_pds_service_instance .get_patient_details .assert_called_once_with (patient_id )
291196
292197 def test_pds_get_patient_details_service_dependencies (self ):
@@ -299,7 +204,7 @@ def test_pds_get_patient_details_service_dependencies(self):
299204 result = pds_get_patient_details (self .test_patient_id )
300205
301206 # Assert service initialization order and parameters
302- self .assertEqual (result , "9912003888" )
207+ self .assertEqual (result [ "identifier" ][ 0 ][ "value" ] , "9912003888" )
303208
304209 # Verify initialization order by checking call counts
305210 self .assertEqual (self .mock_cache_class .call_count , 1 )
@@ -315,15 +220,60 @@ def test_pds_get_patient_details(self):
315220 # Arrange
316221 test_nhs_number = "9912003888"
317222 pds_id = "abcefghijkl"
318- self . mock_pds_service_instance . get_patient_details . return_value = {"identifier" : [{"value" : pds_id }]}
319-
223+ mock_pds_response = {"identifier" : [{"value" : pds_id }]}
224+ self . mock_pds_service_instance . get_patient_details . return_value = mock_pds_response
320225 # Act
321226 result = pds_get_patient_details (test_nhs_number )
322227
323228 # Assert - function should extract the value from first identifier
324- self .assertEqual (result , pds_id )
229+ self .assertEqual (result , mock_pds_response )
325230 self .mock_pds_service_instance .get_patient_details .assert_called_once_with (test_nhs_number )
326231
327232
328- if __name__ == '__main__' :
329- unittest .main ()
233+ # test pds_get_patient_id function
234+ class TestGetPdsPatientId (unittest .TestCase ):
235+ def setUp (self ):
236+ """Set up test fixtures and mocks"""
237+ self .test_nhs_number = "9912003888"
238+
239+ # Patch all external dependencies
240+ self .logger_patcher = patch ('pds_details.logger' )
241+ self .mock_logger = self .logger_patcher .start ()
242+
243+ self .pds_get_patient_details_patcher = patch ('pds_details.pds_get_patient_details' )
244+ self .mock_pds_get_patient_details = self .pds_get_patient_details_patcher .start ()
245+
246+ def tearDown (self ):
247+ """Clean up patches"""
248+ patch .stopall ()
249+
250+ def test_pds_get_patient_id_success (self ):
251+ """Test successful retrieval of PDS patient ID"""
252+ # Arrange
253+ expected_id = "1234567890"
254+ self .mock_pds_get_patient_details .return_value = {"identifier" : [{"value" : expected_id }]}
255+
256+ # Act
257+ result = pds_get_patient_id (self .test_nhs_number )
258+
259+ # Assert
260+ self .assertEqual (result , expected_id )
261+
262+ def test_pds_get_patient_id_empty_identifier_array (self ):
263+ """Test when identifier array is empty"""
264+ # Arrange
265+ patient_data_empty_identifier = {
266+ "identifier" : [], # Empty array
267+ "name" : "John Doe"
268+ }
269+ self .mock_pds_get_patient_details .return_value = patient_data_empty_identifier
270+
271+ # Act
272+ with self .assertRaises (IdSyncException ) as context :
273+ pds_get_patient_id (self .test_nhs_number )
274+
275+ # Assert
276+ exception = context .exception
277+ self .assertEqual (exception .message , f"Error getting PDS patient ID for { self .test_nhs_number } " )
278+ self .assertEqual (exception .nhs_numbers , None )
279+
0 commit comments