Skip to content

Commit aae6283

Browse files
committed
Refactor PDS service caching logic and update coverage dependency
1 parent bdf2fe4 commit aae6283

4 files changed

Lines changed: 55 additions & 6 deletions

File tree

.github/workflows/quality-checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ jobs:
188188
working-directory: lambdas/mock_pds
189189
id: mock_pds
190190
env:
191-
PYTHONPATH: ${{ env.LAMBDA_PATH }}/mock_pds/src:${{ env.LAMBDA_PATH }}/mock_pds/tests
191+
PYTHONPATH: ${{ env.LAMBDA_PATH }}/mock_pds/src
192192
continue-on-error: true
193193
run: |
194194
poetry install

lambdas/mock_pds/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ packages = [{include = "src"}]
99
[tool.poetry.dependencies]
1010
python = "~3.11"
1111
redis = "^6.1.0"
12-
coverage = "^7.13.2"
12+
coverage = "^7.13.5"
1313

1414
[build-system]
1515
requires = ["poetry-core >= 1.5.0"]

lambdas/shared/src/common/api_clients/get_pds_details.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,23 @@
1010
from common.clients import get_secrets_manager_client, logger
1111

1212
_pds_service: PdsService | None = None
13+
_pds_service_config: tuple[str, str | None] | None = None
1314

1415

1516
def get_pds_service() -> PdsService:
16-
global _pds_service
17-
if _pds_service is None:
18-
environment = os.getenv("PDS_ENV", "int")
19-
base_url = os.getenv("PDS_BASE_URL", "").strip() or None
17+
global _pds_service, _pds_service_config
18+
environment = os.getenv("PDS_ENV", "int")
19+
base_url = os.getenv("PDS_BASE_URL", "").strip() or None
20+
config = (environment, base_url)
21+
22+
if _pds_service is None or _pds_service_config != config:
2023
authenticator = (
2124
None
2225
if base_url
2326
else AppRestrictedAuth(secret_manager_client=get_secrets_manager_client(), environment=environment)
2427
)
2528
_pds_service = PdsService(authenticator, environment, base_url=base_url)
29+
_pds_service_config = config
2630
return _pds_service
2731

2832

lambdas/shared/tests/test_common/api_clients/test_pds_details.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class TestGetPdsPatientDetails(unittest.TestCase):
99
def setUp(self):
1010
self.test_patient_id = "9912003888"
1111
get_pds_service.__globals__["_pds_service"] = None
12+
get_pds_service.__globals__["_pds_service_config"] = None
1213

1314
self.logger_patcher = patch("common.api_clients.get_pds_details.logger")
1415
self.mock_logger = self.logger_patcher.start()
@@ -25,6 +26,7 @@ def setUp(self):
2526

2627
def tearDown(self):
2728
get_pds_service.__globals__["_pds_service"] = None
29+
get_pds_service.__globals__["_pds_service_config"] = None
2830
patch.stopall()
2931

3032
def test_pds_get_patient_details_success(self):
@@ -112,3 +114,46 @@ def test_whitespace_only_base_url_uses_authenticator(self):
112114
"ref",
113115
base_url=None,
114116
)
117+
118+
@patch.dict("os.environ", {"PDS_ENV": "ref", "PDS_BASE_URL": "https://mock-pds-v1.example/Patient"}, clear=False)
119+
def test_rebuilds_cached_service_when_base_url_changes(self):
120+
pds_get_patient_details(self.test_patient_id)
121+
122+
self.mock_pds_service_class.reset_mock()
123+
self.mock_pds_service_instance.get_patient_details.reset_mock()
124+
new_instance = MagicMock()
125+
self.mock_pds_service_class.return_value = new_instance
126+
127+
with patch.dict("os.environ", {"PDS_BASE_URL": "https://mock-pds-v2.example/Patient"}, clear=False):
128+
pds_get_patient_details("1234567890")
129+
130+
self.mock_auth_class.assert_not_called()
131+
self.mock_pds_service_class.assert_called_once_with(
132+
None,
133+
"ref",
134+
base_url="https://mock-pds-v2.example/Patient",
135+
)
136+
new_instance.get_patient_details.assert_called_once_with("1234567890")
137+
138+
@patch.dict("os.environ", {"PDS_ENV": "int", "PDS_BASE_URL": ""}, clear=False)
139+
def test_rebuilds_cached_service_when_environment_changes(self):
140+
pds_get_patient_details(self.test_patient_id)
141+
142+
self.mock_pds_service_class.reset_mock()
143+
self.mock_auth_class.reset_mock()
144+
self.mock_pds_service_instance.get_patient_details.reset_mock()
145+
new_auth = MagicMock()
146+
new_service = MagicMock()
147+
self.mock_auth_class.return_value = new_auth
148+
self.mock_pds_service_class.return_value = new_service
149+
150+
with patch.dict("os.environ", {"PDS_ENV": "ref", "PDS_BASE_URL": ""}, clear=False):
151+
pds_get_patient_details("1234567890")
152+
153+
self.mock_auth_class.assert_called_once_with(secret_manager_client=unittest.mock.ANY, environment="ref")
154+
self.mock_pds_service_class.assert_called_once_with(
155+
new_auth,
156+
"ref",
157+
base_url=None,
158+
)
159+
new_service.get_patient_details.assert_called_once_with("1234567890")

0 commit comments

Comments
 (0)