Skip to content

Commit 397bdb2

Browse files
committed
sync tests pass locally
1 parent 13b7a44 commit 397bdb2

File tree

9 files changed

+543
-92
lines changed

9 files changed

+543
-92
lines changed

lambdas/id_sync/poetry.lock

Lines changed: 34 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lambdas/id_sync/pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ moto = "~5.1.5"
2525
python-stdnum = "^2.1"
2626
coverage = "^7.8.0"
2727
redis = "^4.6.0"
28+
jwt = "^1.4.0"
29+
cache = "^1.0.3"
2830

2931
[tool.poetry.group.dev.dependencies]
3032
coverage = "^7.8.0"

lambdas/id_sync/src/pds_details.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
record Processor
33
'''
44
from common.clients import logger, secrets_manager_client
5+
from common.cache import Cache
56
from clients import pds_env
6-
from cache import Cache
77
from common.pds_service import PdsService
88
from common.authentication import AppRestrictedAuth, Service
99

1010

11-
def get_pds_patient_details(id: str) -> dict:
11+
def get_pds_patient_details(nhs_number: str) -> dict:
1212
try:
13-
logger.info(f"Get PDS patient details for {id}")
13+
logger.info(f"Get PDS patient details for {nhs_number}")
1414

1515
cache = Cache(directory="/tmp")
1616
authenticator = AppRestrictedAuth(
@@ -21,9 +21,14 @@ def get_pds_patient_details(id: str) -> dict:
2121
)
2222
pds_service = PdsService(authenticator, pds_env)
2323

24-
details = pds_service.get_patient_details(id)
24+
patient = pds_service.get_patient_details(nhs_number)
2525

26-
return details.get("id")
26+
if patient:
27+
pds_nhs_number = patient["identifier"][0]["value"]
28+
return pds_nhs_number
29+
else:
30+
logger.info(f"No patient details found for ID: {nhs_number}")
31+
return None
2732
except Exception:
28-
logger.exception(f"Error getting PDS patient details for {id}")
33+
logger.exception(f"Error getting PDS patient details for {nhs_number}")
2934
return None

lambdas/id_sync/src/record_processor.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'''
44
from common.aws_lambda_sqs_event_record import AwsLambdaSqsEventRecord
55
from common.clients import logger
6+
from pds_details import get_pds_patient_details
67
import json
78
from typing import Optional
89

@@ -13,21 +14,26 @@ def process_record(event_record: AwsLambdaSqsEventRecord):
1314

1415
id = get_id(event_record.body)
1516

16-
exists = check_records_exist(id)
17+
if id:
18+
exists = check_records_exist(id)
1719

18-
if exists:
19-
# get patient details from PDS
20-
patient_details = get_pds_patient_details(id)
20+
if exists:
21+
# get patient details from PDS
22+
patient_details = get_pds_patient_details(id)
23+
if not patient_details:
24+
return {"status": "error", "message": f"No records returned for ID: {id}"}
2125

22-
patient_details_id = patient_details.get("id")
26+
patient_details_id = patient_details.get("id")
2327

24-
# if patient NHS != id, update patient index of vax events to new number
25-
if patient_details_id != id:
26-
return update_patient_index(id, patient_details_id)
28+
# if patient NHS != id, update patient index of vax events to new number
29+
if patient_details_id != id:
30+
return update_patient_index(id, patient_details_id)
31+
else:
32+
return {"status": "success", "message": "No update required"}
2733
else:
28-
return {"status": "success", "message": "No update required"}
34+
return {"status": "error", "message": f"No records found for ID: {id}"}
2935
else:
30-
return {"status": "error", "message": f"No records found for ID: {id}"}
36+
return {"status": "error", "message": "No ID found in event record"}
3137

3238

3339
def check_records_exist(id: str) -> bool:
@@ -36,22 +42,6 @@ def check_records_exist(id: str) -> bool:
3642
return False
3743

3844

39-
def get_pds_patient_details(id: str) -> dict:
40-
# TODO: Implement logic to retrieve patient details from PDS
41-
logger.info(f"TODO Get patient details for {id}")
42-
43-
authenticator = AppRestrictedAuth(
44-
service=Service.PDS,
45-
secret_manager_client=boto3.client("secretsmanager", config=boto_config),
46-
environment=pds_env,
47-
cache=cache,
48-
)
49-
pds_service = PdsService(authenticator, pds_env)
50-
51-
52-
return {"id": id, "name": "Mr Man"}
53-
54-
5545
def update_patient_index(old_id: str, new_id: str):
5646
# TODO: Implement logic to update patient index in Redis or other data store
5747
logger.info(f"TODO Update patient index from {old_id} to {new_id}")

lambdas/id_sync/tests/test_id_sync.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import unittest
22
from unittest.mock import patch, MagicMock
3-
from id_sync import handler
3+
4+
with patch('common.log_decorator.logging_decorator') as mock_decorator:
5+
mock_decorator.return_value = lambda f: f # Pass-through decorator
6+
from id_sync import handler
47

58

69
class TestIdSyncHandler(unittest.TestCase):
@@ -18,9 +21,9 @@ def setUp(self):
1821
self.mock_logger = self.logger_patcher.start()
1922

2023
# Patch the logging decorator to pass through
21-
self.log_decorator_patcher = patch('id_sync.logging_decorator')
22-
self.mock_log_decorator = self.log_decorator_patcher.start()
23-
self.mock_log_decorator.return_value = lambda f: f # Pass-through decorator
24+
# self.log_decorator_patcher = patch('id_sync.logging_decorator')
25+
# self.mock_log_decorator = self.log_decorator_patcher.start()
26+
# self.mock_log_decorator.return_value = lambda f: f # Pass-through decorator
2427

2528
# Set up test data
2629
self.single_sqs_event = {
@@ -388,7 +391,3 @@ def test_handler_logs_correct_event_type(self):
388391

389392
# Check that it specifically logs "SQS event"
390393
self.mock_logger.info.assert_any_call("id_sync processing event with %d records", 1)
391-
392-
393-
if __name__ == '__main__':
394-
unittest.main()

0 commit comments

Comments
 (0)