Skip to content

Commit 9e945e6

Browse files
committed
Resolve comments part 1 - simple fixes
1 parent 4767e9b commit 9e945e6

File tree

8 files changed

+17
-156
lines changed

8 files changed

+17
-156
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
class IdSyncException(Exception):
22
"""Custom exception for ID Sync errors."""
33

4-
def __init__(self, message: str, nhs_numbers: list = None, exception=None):
4+
def __init__(self, message: str):
55
self.message = message
6-
self.nhs_numbers = nhs_numbers
7-
self.inner_exception = exception
86
super().__init__(message)

lambdas/id_sync/src/ieds_db_operations.py

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,15 @@ def get_ieds_table():
2121
return ieds_table
2222

2323

24-
def ieds_update_patient_id(old_id: str, new_id: str, items_to_update: list | None = None) -> dict:
24+
def ieds_update_patient_id(old_id: str, new_id: str, items_to_update: list) -> dict:
2525
"""Update the patient ID (new NHS number) in the IEDS table."""
26-
if not old_id or not new_id or not old_id.strip() or not new_id.strip():
27-
return make_status("Old ID and New ID cannot be empty", status="error")
26+
if not items_to_update:
27+
logger.info("No items found to update for patient NHS Number")
28+
return make_status("No items found to update for patient ID")
2829

29-
if old_id == new_id:
30-
return make_status("No change in patient ID")
30+
logger.info(f"Items to update: {len(items_to_update)}")
3131

3232
try:
33-
if items_to_update is None:
34-
logger.info("Getting items to update in IEDS table...")
35-
items_to_update = get_items_from_patient_id(old_id)
36-
else:
37-
logger.info("Using provided items_to_update list, size=%d", len(items_to_update))
38-
39-
if not items_to_update:
40-
logger.warning("No items found to update for patient NHS Number")
41-
return make_status("No items found to update for patient ID")
42-
43-
logger.info(f"Items to update: {len(items_to_update)}")
44-
4533
# Build transact items and execute them in batches via helpers to keep
4634
# the top-level function easy to read and test.
4735
transact_items = build_transact_items(old_id, new_id, items_to_update)
@@ -61,12 +49,9 @@ def ieds_update_patient_id(old_id: str, new_id: str, items_to_update: list | Non
6149

6250
except Exception as e:
6351
logger.exception("Error updating patient ID")
64-
logger.info("Error details: %s", e)
6552
raise IdSyncException(
6653
message="Error updating patient ID",
67-
nhs_numbers=[old_id, new_id],
68-
exception=e,
69-
)
54+
) from e
7055

7156

7257
def get_items_from_patient_id(id: str) -> list:
@@ -80,12 +65,10 @@ def get_items_from_patient_id(id: str) -> list:
8065
return paginate_items_for_patient_pk(patient_pk)
8166
except IdSyncException:
8267
raise
83-
except Exception as e:
68+
except Exception:
8469
logger.exception("Error querying items for patient PK")
8570
raise IdSyncException(
8671
message="Error querying items for patient PK",
87-
nhs_numbers=[patient_pk],
88-
exception=e,
8972
)
9073

9174

@@ -111,8 +94,6 @@ def paginate_items_for_patient_pk(patient_pk: str) -> list:
11194
logger.exception("Unexpected DynamoDB response: missing 'Items'")
11295
raise IdSyncException(
11396
message="No Items in DynamoDB response",
114-
nhs_numbers=[patient_pk],
115-
exception=response,
11697
)
11798

11899
items = response.get("Items", [])

lambdas/id_sync/src/pds_details.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def pds_get_patient_details(nhs_number: str) -> dict:
3131
except Exception as e:
3232
msg = "Error retrieving patient details from PDS"
3333
logger.exception(msg)
34-
raise IdSyncException(message=msg, exception=e)
34+
raise IdSyncException(message=msg) from e
3535

3636

3737
def get_nhs_number_from_pds_resource(pds_resource: dict) -> str:

lambdas/id_sync/src/record_processor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def process_nhs_number(nhs_number: str) -> Dict[str, Any]:
7777
logger.info("No records matched PDS demographics: %d", discarded_count)
7878
return make_status("No records matched PDS demographics; update skipped")
7979

80-
response = ieds_update_patient_id(nhs_number, new_nhs_number, items_to_update=matching_records)
80+
response = ieds_update_patient_id(nhs_number, new_nhs_number, matching_records)
8181
# add counts for observability
8282
response["matched"] = len(matching_records)
8383
response["discarded"] = discarded_count

lambdas/id_sync/tests/test_id_sync.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ def test_handler_aws_lambda_event_exception(self):
196196
self.mock_logger.exception.assert_called_once_with("Error processing id_sync event")
197197
self.mock_process_record.assert_not_called()
198198

199-
self.assertEqual(result.nhs_numbers, None)
200199
self.assertEqual(result.message, "Error processing id_sync event")
201200

202201
def test_handler_process_record_exception(self):
@@ -216,7 +215,6 @@ def test_handler_process_record_exception(self):
216215
self.mock_process_record.assert_called_once_with(mock_event.records[0])
217216
self.mock_logger.exception.assert_called_once_with("Error processing id_sync event")
218217

219-
self.assertEqual(exception.nhs_numbers, None)
220218
self.assertEqual(exception.message, "Error processing id_sync event")
221219

222220
def test_handler_process_record_missing_nhs_number(self):
@@ -241,7 +239,6 @@ def test_handler_process_record_missing_nhs_number(self):
241239
exception = exception_context.exception
242240

243241
self.assertIsInstance(exception, IdSyncException)
244-
self.assertIsNone(exception.nhs_numbers)
245242
self.assertEqual(exception.message, "Processed 1 records with 1 errors")
246243
self.mock_logger.exception.assert_called_once_with(f"id_sync error: {exception.message}")
247244

lambdas/id_sync/tests/test_ieds_db_operations.py

Lines changed: 6 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -331,20 +331,16 @@ def test_ieds_update_patient_id_success(self):
331331

332332
# Mock items to update
333333
mock_items = [
334-
{"PK": "Patient#old-patient-123", "PatientPK": "Patient#old-patient-123"},
335-
{
336-
"PK": "Patient#old-patient-123#record1",
337-
"PatientPK": "Patient#old-patient-123",
338-
},
334+
{"PK": "Immunization#old-patient-123", "PatientPK": "Patient#old-patient-123"},
335+
{"PK": "Patient#old-patient-123#record1", "PatientPK": "Patient#old-patient-123"},
339336
]
340-
self.mock_get_items_from_patient_id.return_value = mock_items
341337

342338
# Mock successful transact_write_items response
343339
mock_transact_response = {"ResponseMetadata": {"HTTPStatusCode": 200}}
344340
self.mock_dynamodb_client_patcher.transact_write_items.return_value = mock_transact_response
345341

346342
# Act
347-
result = ieds_db_operations.ieds_update_patient_id(old_id, new_id)
343+
result = ieds_db_operations.ieds_update_patient_id(old_id, new_id, mock_items)
348344

349345
# Assert - Update expected message to match actual implementation
350346
expected_result = {
@@ -353,9 +349,6 @@ def test_ieds_update_patient_id_success(self):
353349
}
354350
self.assertEqual(result, expected_result)
355351

356-
# Verify get_items_from_patient_id was called
357-
self.mock_get_items_from_patient_id.assert_called_once_with(old_id)
358-
359352
# Verify transact_write_items was called
360353
self.mock_dynamodb_client_patcher.transact_write_items.assert_called_once()
361354

@@ -367,14 +360,13 @@ def test_ieds_update_patient_id_non_200_response(self):
367360

368361
# Mock items to update
369362
mock_items = [{"PK": "Patient#old-patient-123", "PatientPK": "Patient#old-patient-123"}]
370-
self.mock_get_items_from_patient_id.return_value = mock_items
371363

372364
# Mock failed transact_write_items response (not update_item)
373365
mock_transact_response = {"ResponseMetadata": {"HTTPStatusCode": 400}}
374366
self.mock_dynamodb_client_patcher.transact_write_items.return_value = mock_transact_response
375367

376368
# Act
377-
result = ieds_db_operations.ieds_update_patient_id(old_id, new_id)
369+
result = ieds_db_operations.ieds_update_patient_id(old_id, new_id, mock_items)
378370

379371
# Assert
380372
expected_result = {
@@ -392,11 +384,8 @@ def test_ieds_update_patient_id_no_items_found(self):
392384
old_id = "old-patient-123"
393385
new_id = "new-patient-456"
394386

395-
# Mock empty items list
396-
self.mock_get_items_from_patient_id.return_value = []
397-
398387
# Act
399-
result = ieds_db_operations.ieds_update_patient_id(old_id, new_id)
388+
result = ieds_db_operations.ieds_update_patient_id(old_id, new_id, [])
400389

401390
# Assert
402391
expected_result = {
@@ -405,71 +394,9 @@ def test_ieds_update_patient_id_no_items_found(self):
405394
}
406395
self.assertEqual(result, expected_result)
407396

408-
# Verify get_items_from_patient_id was called
409-
self.mock_get_items_from_patient_id.assert_called_once_with(old_id)
410-
411397
# Verify no transact operation was attempted
412398
self.mock_table.transact_write_items.assert_not_called()
413399

414-
def test_ieds_update_patient_id_empty_old_id(self):
415-
"""Test update with empty old_id"""
416-
# Arrange
417-
old_id = ""
418-
new_id = "new-patient-456"
419-
420-
# Act
421-
result = ieds_db_operations.ieds_update_patient_id(old_id, new_id)
422-
423-
# Assert
424-
expected_result = {
425-
"status": "error",
426-
"message": "Old ID and New ID cannot be empty",
427-
}
428-
self.assertEqual(result, expected_result)
429-
430-
# Verify no update was attempted
431-
self.mock_table.transact_write_items.assert_not_called()
432-
self.mock_get_ieds_table_patcher.assert_not_called()
433-
434-
def test_ieds_update_patient_id_empty_new_id(self):
435-
"""Test update with empty new_id"""
436-
# Arrange
437-
old_id = "old-patient-123"
438-
new_id = ""
439-
440-
# Act
441-
result = ieds_db_operations.ieds_update_patient_id(old_id, new_id)
442-
443-
# Assert
444-
expected_result = {
445-
"status": "error",
446-
"message": "Old ID and New ID cannot be empty",
447-
}
448-
self.assertEqual(result, expected_result)
449-
450-
# Verify no update was attempted
451-
self.mock_table.transact_write_items.assert_not_called()
452-
self.mock_get_ieds_table_patcher.assert_not_called()
453-
454-
def test_ieds_update_patient_id_same_old_and_new_id(self):
455-
"""Test update when old_id and new_id are the same"""
456-
# Arrange
457-
patient_id = "same-patient-id"
458-
459-
# Act
460-
result = ieds_db_operations.ieds_update_patient_id(patient_id, patient_id)
461-
462-
# Assert
463-
expected_result = {
464-
"status": "success",
465-
"message": "No change in patient ID",
466-
}
467-
self.assertEqual(result, expected_result)
468-
469-
# Verify no update was attempted
470-
self.mock_table.transact_write_items.assert_not_called()
471-
self.mock_get_ieds_table_patcher.assert_not_called()
472-
473400
def test_ieds_update_patient_id_update_exception(self):
474401
"""Test exception handling during transact_write_items"""
475402
# Arrange
@@ -483,52 +410,24 @@ def test_ieds_update_patient_id_update_exception(self):
483410
"PatientPK": "Patient#old-patient-error",
484411
}
485412
]
486-
self.mock_get_items_from_patient_id.return_value = mock_items
487413

488414
test_exception = Exception("DynamoDB transact failed")
489415
self.mock_dynamodb_client_patcher.transact_write_items.side_effect = test_exception
490416

491417
# Act & Assert
492418
with self.assertRaises(Exception) as context:
493-
ieds_db_operations.ieds_update_patient_id(old_id, new_id)
419+
ieds_db_operations.ieds_update_patient_id(old_id, new_id, mock_items)
494420

495421
exception = context.exception
496422
self.assertIsInstance(exception, IdSyncException)
497423
self.assertEqual(exception.message, "Error updating patient ID")
498-
self.assertEqual(exception.inner_exception, test_exception)
499424

500425
# Verify transact was attempted
501426
self.mock_dynamodb_client_patcher.transact_write_items.assert_called_once()
502427

503428
# Verify logger exception was called
504429
self.mock_logger.exception.assert_called_once_with("Error updating patient ID")
505430

506-
def test_ieds_update_patient_id_special_characters(self):
507-
"""Test update with special characters in IDs"""
508-
# Arrange
509-
old_id = "old-patient@123#$%"
510-
new_id = "new-patient&456*()+"
511-
512-
# Mock items to update
513-
mock_items = [{"PK": f"Patient#{old_id}", "PatientPK": f"Patient#{old_id}"}]
514-
self.mock_get_items_from_patient_id.return_value = mock_items
515-
516-
mock_transact_response = {"ResponseMetadata": {"HTTPStatusCode": 200}}
517-
self.mock_dynamodb_client_patcher.transact_write_items.return_value = mock_transact_response
518-
519-
# Act
520-
result = ieds_db_operations.ieds_update_patient_id(old_id, new_id)
521-
522-
# Assert
523-
self.assertEqual(result["status"], "success")
524-
self.assertEqual(
525-
result["message"],
526-
f"IEDS update. {len(mock_items)} item(s) updated in 1 batch(es).",
527-
)
528-
529-
# Verify transact_write_items was called with special characters
530-
self.mock_dynamodb_client_patcher.transact_write_items.assert_called_once()
531-
532431

533432
class TestGetItemsToUpdate(TestIedsDbOperations):
534433
def setUp(self):
@@ -601,14 +500,6 @@ def setUp(self):
601500
def tearDown(self):
602501
patch.stopall()
603502

604-
def test_ieds_update_patient_id_empty_inputs(self):
605-
res = ieds_db_operations.ieds_update_patient_id("", "")
606-
self.assertEqual(res["status"], "error")
607-
608-
def test_ieds_update_patient_id_same_ids(self):
609-
res = ieds_db_operations.ieds_update_patient_id("a", "a")
610-
self.assertEqual(res["status"], "success")
611-
612503
def test_ieds_update_with_items_to_update_uses_provided_list(self):
613504
items = [{"PK": "Patient#1"}, {"PK": "Patient#1#r2"}]
614505
# patch transact_write_items to return success

lambdas/id_sync/tests/test_pds_details.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,10 @@ def test_pds_get_patient_details_pds_service_exception(self):
100100
exception = context.exception
101101

102102
# Assert
103-
self.assertEqual(exception.inner_exception, mock_exception)
104103
self.assertEqual(
105104
exception.message,
106105
"Error retrieving patient details from PDS",
107106
)
108-
self.assertEqual(exception.nhs_numbers, None)
109107

110108
# Verify exception was logged
111109
self.mock_logger.exception.assert_called_once_with("Error retrieving patient details from PDS")
@@ -127,7 +125,6 @@ def test_pds_get_patient_details_cache_initialization_error(self):
127125
exception.message,
128126
"Error retrieving patient details from PDS",
129127
)
130-
self.assertEqual(exception.nhs_numbers, None)
131128

132129
# Verify exception was logged
133130
self.mock_logger.exception.assert_called_once_with("Error retrieving patient details from PDS")
@@ -149,7 +146,6 @@ def test_pds_get_patient_details_auth_initialization_error(self):
149146
exception.message,
150147
"Error retrieving patient details from PDS",
151148
)
152-
self.assertEqual(exception.nhs_numbers, None)
153149

154150
# Verify exception was logged
155151
self.mock_logger.exception.assert_called_once_with("Error retrieving patient details from PDS")
@@ -167,12 +163,10 @@ def test_pds_get_patient_details_exception(self):
167163

168164
exception = context.exception
169165
# Assert
170-
self.assertEqual(exception.inner_exception, test_exception)
171166
self.assertEqual(
172167
exception.message,
173168
"Error retrieving patient details from PDS",
174169
)
175-
self.assertEqual(exception.nhs_numbers, None)
176170
# Verify logger.exception was called due to the caught exception
177171
self.mock_logger.exception.assert_called_once_with("Error retrieving patient details from PDS")
178172

lambdas/id_sync/tests/test_record_processor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def test_update_called_on_match(self):
229229

230230
result = process_record(test_sqs_record)
231231
self.assertEqual(result["status"], "success")
232-
self.mock_ieds_update_patient_id.assert_called_once_with(nhs_number, pds_id, items_to_update=[item])
232+
self.mock_ieds_update_patient_id.assert_called_once_with(nhs_number, pds_id, [item])
233233

234234
def test_process_record_no_records_exist(self):
235235
"""Test when no records exist for the patient ID"""

0 commit comments

Comments
 (0)