Skip to content

Commit 6cf183a

Browse files
committed
get_items_to_update try catch
1 parent 354dc03 commit 6cf183a

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

lambdas/id_sync/src/ieds_db_operations.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,21 @@ def ieds_update_patient_id(old_id: str, new_id: str) -> dict:
150150
def get_items_to_update(old_patient_pk: str) -> list:
151151
"""Get items that need to be updated in the IEDS table."""
152152
logger.info(f"Getting items to update for old patient PK: {old_patient_pk}")
153-
response = get_ieds_table().query(
154-
KeyConditionExpression=Key('PatientPK').eq(old_patient_pk),
155-
Limit=BATCH_SIZE
156-
)
153+
try:
154+
response = get_ieds_table().query(
155+
KeyConditionExpression=Key('PatientPK').eq(old_patient_pk),
156+
Limit=BATCH_SIZE
157+
)
157158

158-
if 'Items' not in response or not response['Items']:
159-
logger.warning(f"No items found for old patient PK: {old_patient_pk}")
160-
return []
159+
if 'Items' not in response or not response['Items']:
160+
logger.warning(f"No items found for old patient PK: {old_patient_pk}")
161+
return []
161162

162-
return response['Items']
163+
return response['Items']
164+
except Exception as e:
165+
logger.exception(f"Error querying items for old patient PK: {old_patient_pk}")
166+
raise IdSyncException(
167+
message=f"Error querying items for old patient PK: {old_patient_pk}",
168+
nhs_numbers=[old_patient_pk],
169+
exception=e
170+
)

lambdas/id_sync/tests/test_ieds_db_operations.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,3 +625,54 @@ def test_ieds_update_patient_id_special_characters(self):
625625

626626
# Verify transact_write_items was called with special characters
627627
self.mock_table.transact_write_items.assert_called_once()
628+
629+
630+
class TestGetItemsToUpdate(TestIedsDbOperations):
631+
632+
def setUp(self):
633+
super().setUp()
634+
# Mock get_ieds_table()
635+
self.mock_get_ieds_table = patch('ieds_db_operations.get_ieds_table')
636+
self.mock_get_ieds_table_patcher = self.mock_get_ieds_table.start()
637+
self.mock_table = MagicMock()
638+
self.mock_get_ieds_table_patcher.return_value = self.mock_table
639+
640+
def tearDown(self):
641+
patch.stopall()
642+
643+
def test_get_items_to_update_success(self):
644+
"""Test successful retrieval of items to update"""
645+
# Arrange
646+
patient_id = "test-patient-123"
647+
expected_items = [
648+
{'PK': f'Patient#{patient_id}', 'PatientPK': f'Patient#{patient_id}'},
649+
{'PK': f'Patient#{patient_id}#record1', 'PatientPK': f'Patient#{patient_id}'}
650+
]
651+
self.mock_table.query.return_value = {
652+
'Items': expected_items,
653+
'Count': len(expected_items)
654+
}
655+
656+
# Act
657+
result = ieds_db_operations.get_items_to_update(f"Patient#{patient_id}")
658+
659+
# Assert
660+
self.assertEqual(result, expected_items)
661+
662+
# Verify query was called with correct parameters
663+
self.mock_table.query.assert_called_once()
664+
665+
def test_get_items_to_update_no_records(self):
666+
"""Test when no records are found for the patient ID"""
667+
# Arrange
668+
patient_id = "test-patient-no-records"
669+
self.mock_table.query.return_value = {
670+
'Items': [],
671+
'Count': 0
672+
}
673+
674+
# Act
675+
result = ieds_db_operations.get_items_to_update(f"Patient#{patient_id}")
676+
677+
# Assert
678+
self.assertEqual(result, [])

0 commit comments

Comments
 (0)