@@ -539,3 +539,71 @@ def test_get_items_from_patient_id_no_records(self):
539539
540540 # Assert
541541 self .assertEqual (result , [])
542+
543+
544+ class TestIedsDbOperationsConditional (unittest .TestCase ):
545+ def setUp (self ):
546+ # Patch logger to suppress output
547+ self .logger_patcher = patch ('ieds_db_operations.logger' )
548+ self .mock_logger = self .logger_patcher .start ()
549+
550+ # Patch get_ieds_table_name and get_ieds_table
551+ self .get_ieds_table_name_patcher = patch ('ieds_db_operations.get_ieds_table_name' )
552+ self .mock_get_ieds_table_name = self .get_ieds_table_name_patcher .start ()
553+ self .mock_get_ieds_table_name .return_value = 'test-table'
554+
555+ self .get_ieds_table_patcher = patch ('ieds_db_operations.get_ieds_table' )
556+ self .mock_get_ieds_table = self .get_ieds_table_patcher .start ()
557+
558+ # Patch dynamodb client
559+ self .dynamodb_client_patcher = patch ('ieds_db_operations.dynamodb_client' )
560+ self .mock_dynamodb_client = self .dynamodb_client_patcher .start ()
561+
562+ def tearDown (self ):
563+ patch .stopall ()
564+
565+ def test_ieds_update_patient_id_empty_inputs (self ):
566+ res = ieds_db_operations .ieds_update_patient_id ('' , '' )
567+ self .assertEqual (res ['status' ], 'error' )
568+
569+ def test_ieds_update_patient_id_same_ids (self ):
570+ res = ieds_db_operations .ieds_update_patient_id ('a' , 'a' )
571+ self .assertEqual (res ['status' ], 'success' )
572+
573+ def test_ieds_update_with_items_to_update_uses_provided_list (self ):
574+ items = [{'PK' : 'Patient#1' }, {'PK' : 'Patient#1#r2' }]
575+ # patch transact_write_items to return success
576+ self .mock_dynamodb_client .transact_write_items = MagicMock (
577+ return_value = {'ResponseMetadata' : {'HTTPStatusCode' : 200 }})
578+
579+ res = ieds_db_operations .ieds_update_patient_id ('1' , '2' , items_to_update = items )
580+ self .assertEqual (res ['status' ], 'success' )
581+ # ensure transact called at least once
582+ self .mock_dynamodb_client .transact_write_items .assert_called ()
583+
584+ def test_ieds_update_batches_multiple_calls (self ):
585+ # create 60 items to force 3 batches (25,25,10)
586+ items = [{'PK' : f'Patient#old#{ i } ' } for i in range (60 )]
587+ called = []
588+
589+ def fake_transact (TransactItems ):
590+ called .append (len (TransactItems ))
591+ return {'ResponseMetadata' : {'HTTPStatusCode' : 200 }}
592+
593+ self .mock_dynamodb_client .transact_write_items = MagicMock (side_effect = fake_transact )
594+
595+ res = ieds_db_operations .ieds_update_patient_id ('old' , 'new' , items_to_update = items )
596+ self .assertEqual (res ['status' ], 'success' )
597+ # should have been called 3 times
598+ self .assertEqual (len (called ), 3 )
599+ self .assertEqual (called [0 ], 25 )
600+ self .assertEqual (called [1 ], 25 )
601+ self .assertEqual (called [2 ], 10 )
602+
603+ def test_ieds_update_non_200_response (self ):
604+ items = [{'PK' : 'Patient#1' }]
605+ self .mock_dynamodb_client .transact_write_items = MagicMock (
606+ return_value = {'ResponseMetadata' : {'HTTPStatusCode' : 500 }})
607+
608+ res = ieds_db_operations .ieds_update_patient_id ('1' , '2' , items_to_update = items )
609+ self .assertEqual (res ['status' ], 'error' )
0 commit comments