Skip to content

Commit 5b72fe6

Browse files
committed
resolve log issues
1 parent 3c7bffe commit 5b72fe6

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

lambdas/id_sync/src/id_sync.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,24 @@ def handler(event_data: Dict[str, Any], _context) -> Dict[str, Any]:
2424

2525
logger.info("id_sync processing event with %d records", len(records))
2626

27-
results = [process_record(record) for record in records]
28-
nhs_numbers = [result["nhs_number"] for result in results]
29-
error_count = sum(1 for result in results if result.get("status") == "error")
27+
# Use explicit loops instead of list comprehensions so we can more
28+
# easily inspect intermediate results and avoid building temporary
29+
# comprehension constructs.
30+
results = []
31+
nhs_numbers = []
32+
error_count = 0
3033

31-
if error_count:
34+
for record in records:
35+
result = process_record(record)
36+
results.append(result)
37+
38+
if "nhs_number" in result:
39+
nhs_numbers.append(result["nhs_number"])
40+
41+
if result.get("status") == "error":
42+
error_count += 1
43+
44+
if error_count > 0:
3245
raise IdSyncException(message=f"Processed {len(records)} records with {error_count} errors",
3346
nhs_numbers=nhs_numbers)
3447

lambdas/id_sync/tests/test_id_sync.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,29 +235,30 @@ def test_handler_process_record_exception(self):
235235
self.assertEqual(exception.message, "Error processing id_sync event")
236236

237237
def test_handler_process_record_missing_nhs_number(self):
238-
"""Test handler when process_record returns incomplete data"""
238+
"""Test handler when process_record returns error and missing NHS number"""
239+
239240
# Setup mocks
240241
mock_event = MagicMock()
241242
mock_event.records = [MagicMock()]
242243
self.mock_aws_lambda_event.return_value = mock_event
243244

244-
# Missing "nhs_number" in response
245+
# Return result without 'nhs_number' but with an 'error' status
245246
self.mock_process_record.return_value = {
246-
"status": "success"
247-
# Missing "nhs_number"
247+
"status": "error",
248+
"message": "Missing NHS number"
249+
# No 'nhs_number'
248250
}
249251

250-
# Call handler
252+
# Call handler and expect exception
251253
with self.assertRaises(IdSyncException) as exception_context:
252254
handler(self.single_sqs_event, None)
253255

254256
exception = exception_context.exception
255257

256-
# convert exception payload to json
257258
self.assertIsInstance(exception, IdSyncException)
258-
self.assertEqual(exception.nhs_numbers, None)
259-
self.assertEqual(exception.message, "Error processing id_sync event")
260-
self.mock_logger.exception.assert_called_once_with("Error processing id_sync event")
259+
self.assertEqual(exception.nhs_numbers, []) # Since no nhs_number was collected
260+
self.assertEqual(exception.message, "Processed 1 records with 1 errors")
261+
self.mock_logger.exception.assert_called_once_with(f"id_sync error: {exception.message}")
261262

262263
def test_handler_context_parameter_ignored(self):
263264
"""Test that context parameter is properly ignored"""

0 commit comments

Comments
 (0)