@@ -86,7 +86,7 @@ class TestGetPractitionerDetailsFromPds(unittest.TestCase):
8686 @patch ("create_notification.logger" )
8787 def test_get_practitioner_success (self , mock_logger , mock_pds_get ):
8888 """Test successful retrieval of GP ODS code."""
89- mock_pds_get .return_value = {"generalPractitioner" : { " value" : "Y12345" }}
89+ mock_pds_get .return_value = {"generalPractitioner" : [{ "identifier" : { " value" : "Y12345" }}] }
9090
9191 result = get_practitioner_details_from_pds ("9481152782" )
9292
@@ -103,7 +103,7 @@ def test_get_practitioner_no_gp_details(self, mock_logger, mock_pds_get):
103103 result = get_practitioner_details_from_pds ("9481152782" )
104104
105105 self .assertIsNone (result )
106- mock_logger .warning .assert_called_once_with ("No patient details found for NHS number " )
106+ mock_logger .warning .assert_called_once_with ("No GP details found for patient " )
107107
108108 @patch ("create_notification.pds_get_patient_details" )
109109 @patch ("create_notification.logger" )
@@ -120,7 +120,7 @@ def test_get_practitioner_gp_is_none(self, mock_logger, mock_pds_get):
120120 @patch ("create_notification.logger" )
121121 def test_get_practitioner_no_value_field (self , mock_logger , mock_pds_get ):
122122 """Test when value field is missing from generalPractitioner."""
123- mock_pds_get .return_value = {"generalPractitioner" : { "system " : "https://fhir.nhs.uk" } }
123+ mock_pds_get .return_value = {"generalPractitioner" : [{ "identifier " : {}}] }
124124
125125 result = get_practitioner_details_from_pds ("9481152782" )
126126
@@ -131,7 +131,7 @@ def test_get_practitioner_no_value_field(self, mock_logger, mock_pds_get):
131131 @patch ("create_notification.logger" )
132132 def test_get_practitioner_empty_value (self , mock_logger , mock_pds_get ):
133133 """Test when value is empty string."""
134- mock_pds_get .return_value = {"generalPractitioner" : { " value" : "" }}
134+ mock_pds_get .return_value = {"generalPractitioner" : [{ "identifier" : { " value" : "" }}] }
135135
136136 result = get_practitioner_details_from_pds ("9481152782" )
137137
@@ -342,5 +342,85 @@ def test_create_mns_notification_with_update_action(self, mock_get_service_url,
342342 mock_get_gp .assert_called ()
343343
344344
345+ @patch ("create_notification.pds_get_patient_details" )
346+ @patch ("create_notification.logger" )
347+ def test_get_practitioner_success_no_end_date (self , mock_logger , mock_pds_get ):
348+ """Test successful retrieval when no end date (current registration)."""
349+ mock_pds_get .return_value = {
350+ "generalPractitioner" : [{"identifier" : {"value" : "Y12345" , "period" : {"start" : "2024-01-01" }}}]
351+ }
352+
353+ result = get_practitioner_details_from_pds ("9481152782" )
354+
355+ self .assertEqual (result , "Y12345" )
356+ mock_logger .warning .assert_not_called ()
357+
358+
359+ @patch ("create_notification.pds_get_patient_details" )
360+ @patch ("create_notification.logger" )
361+ def test_get_practitioner_success_future_end_date (self , mock_logger , mock_pds_get ):
362+ """Test successful retrieval when end date is in the future."""
363+ mock_pds_get .return_value = {
364+ "generalPractitioner" : [
365+ {"identifier" : {"value" : "Y12345" , "period" : {"start" : "2024-01-01" , "end" : "2030-12-31" }}}
366+ ]
367+ }
368+
369+ result = get_practitioner_details_from_pds ("9481152782" )
370+
371+ self .assertEqual (result , "Y12345" )
372+ mock_logger .warning .assert_not_called ()
373+
374+
375+ @patch ("create_notification.pds_get_patient_details" )
376+ @patch ("create_notification.logger" )
377+ def test_get_practitioner_expired_registration (self , mock_logger , mock_pds_get ):
378+ """Test when GP registration has ended (expired)."""
379+ mock_pds_get .return_value = {
380+ "generalPractitioner" : [
381+ {"identifier" : {"value" : "Y12345" , "period" : {"start" : "2020-01-01" , "end" : "2023-12-31" }}}
382+ ]
383+ }
384+
385+ result = get_practitioner_details_from_pds ("9481152782" )
386+
387+ self .assertIsNone (result )
388+ mock_logger .warning .assert_called_with (
389+ "GP registration has ended" ,
390+ extra = {"nhs_number" : "9481152782" , "gp_ods_code" : "Y12345" , "end_date" : "2023-12-31" },
391+ )
392+
393+
394+ @patch ("create_notification.pds_get_patient_details" )
395+ @patch ("create_notification.logger" )
396+ def test_get_practitioner_invalid_end_date_format (self , mock_logger , mock_pds_get ):
397+ """Test when end date has invalid format - should still return GP."""
398+ mock_pds_get .return_value = {
399+ "generalPractitioner" : [
400+ {"identifier" : {"value" : "Y12345" , "period" : {"start" : "2024-01-01" , "end" : "invalid-date" }}}
401+ ]
402+ }
403+
404+ result = get_practitioner_details_from_pds ("9481152782" )
405+
406+ # Should still return GP even with invalid date
407+ self .assertEqual (result , "Y12345" )
408+ mock_logger .warning .assert_called_with (
409+ "Invalid end date format in GP registration" , extra = {"nhs_number" : "9481152782" , "end_date" : "invalid-date" }
410+ )
411+
412+
413+ @patch ("create_notification.pds_get_patient_details" )
414+ @patch ("create_notification.logger" )
415+ def test_get_practitioner_no_period_field (self , mock_logger , mock_pds_get ):
416+ """Test when period field is missing entirely."""
417+ mock_pds_get .return_value = {"generalPractitioner" : [{"identifier" : {"value" : "Y12345" }}]}
418+
419+ result = get_practitioner_details_from_pds ("9481152782" )
420+
421+ self .assertEqual (result , "Y12345" )
422+ mock_logger .warning .assert_not_called ()
423+
424+
345425if __name__ == "__main__" :
346426 unittest .main ()
0 commit comments