11import unittest
22from unittest .mock import patch , MagicMock
33from boto3 .dynamodb .conditions import Key
4+ from exceptions .id_sync_exception import IdSyncException
45
56import ieds_db_operations
67
@@ -261,6 +262,7 @@ def test_get_ieds_table_exception_handling(self):
261262 self .mock_get_ieds_table_name .assert_called_once ()
262263 self .mock_get_dynamodb_table .assert_called_once ()
263264
265+
264266class TestIedsCheckExists (TestIedsDbOperations ):
265267
266268 def setUp (self ):
@@ -662,20 +664,21 @@ def test_ieds_update_patient_id_update_exception(self):
662664 # Arrange
663665 old_id = "old-patient-error"
664666 new_id = "new-patient-error"
665- self .mock_table .update_item .side_effect = Exception ("DynamoDB update failed" )
667+ test_exception = Exception ("DynamoDB update failed" )
668+ self .mock_table .update_item .side_effect = test_exception
666669
667670 # Act & Assert
668671 with self .assertRaises (Exception ) as context :
669672 ieds_db_operations .ieds_update_patient_id (old_id , new_id )
670673
671- self .assertEqual (str (context .exception ), "DynamoDB update failed" )
674+ exception = context .exception
675+
676+ self .assertEqual (exception .message , "Error updating patient Id from :old-patient-error to new-patient-error" )
677+ self .assertEqual (exception .nhs_numbers , ["old-patient-error" , "new-patient-error" ])
678+ self .assertEqual (exception .inner_exception , test_exception )
672679
673680 # Verify update was attempted
674- self .mock_table .update_item .assert_called_once_with (
675- Key = {"PK" : f"Patient#{ old_id } " },
676- UpdateExpression = "SET PK = :new_id" ,
677- ExpressionAttributeValues = {":new_id" : f"Patient#{ new_id } " }
678- )
681+ self .mock_table .update_item .assert_called_once ()
679682
680683 # Verify logger exception was called
681684 self .mock_logger .exception .assert_called_once_with ("Error updating patient ID" )
@@ -685,13 +688,18 @@ def test_ieds_update_patient_id_get_table_exception(self):
685688 # Arrange
686689 old_id = "old-patient-123"
687690 new_id = "new-patient-456"
688- self .mock_get_ieds_table_patcher .side_effect = Exception ("Failed to get IEDS table" )
691+ test_exception = Exception ("Failed to get IEDS table" )
692+ self .mock_get_ieds_table_patcher .side_effect = test_exception
689693
690694 # Act & Assert
691695 with self .assertRaises (Exception ) as context :
692696 ieds_db_operations .ieds_update_patient_id (old_id , new_id )
693697
694- self .assertEqual (str (context .exception ), "Failed to get IEDS table" )
698+ exception = context .exception
699+
700+ self .assertEqual (exception .message , "Error updating patient Id from :old-patient-123 to new-patient-456" )
701+ self .assertEqual (exception .nhs_numbers , [old_id , new_id ])
702+ self .assertEqual (exception .inner_exception , test_exception )
695703
696704 # Verify get_ieds_table was called
697705 self .mock_get_ieds_table_patcher .assert_called_once ()
@@ -713,6 +721,11 @@ def test_ieds_update_patient_id_missing_response_metadata(self):
713721 with self .assertRaises (Exception ) as context :
714722 ieds_db_operations .ieds_update_patient_id (old_id , new_id )
715723
724+ exception = context .exception
725+ self .assertIsInstance (exception , IdSyncException )
726+ self .assertEqual (exception .message , f"Error updating patient Id from :{ old_id } to { new_id } " )
727+ self .assertEqual (exception .nhs_numbers , [old_id , new_id ])
728+
716729 # Verify update was attempted
717730 self .mock_table .update_item .assert_called_once ()
718731
0 commit comments