Skip to content

Commit a6a4ac1

Browse files
committed
add id_sync changes missed out in merge conflict
1 parent 6d1b003 commit a6a4ac1

1 file changed

Lines changed: 2 additions & 177 deletions

File tree

lambdas/id_sync/tests/test_pds_details.py

Lines changed: 2 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -1,184 +1,9 @@
11
import unittest
2-
from unittest.mock import MagicMock, patch
32

4-
from exceptions.id_sync_exception import IdSyncException
5-
from pds_details import get_nhs_number_from_pds_resource, pds_get_patient_details
3+
from pds_details import get_nhs_number_from_pds_resource
64

75

8-
class TestGetPdsPatientDetails(unittest.TestCase):
9-
def setUp(self):
10-
"""Set up test fixtures and mocks"""
11-
self.test_patient_id = "9912003888"
12-
13-
# Patch all external dependencies
14-
self.logger_patcher = patch("pds_details.logger")
15-
self.mock_logger = self.logger_patcher.start()
16-
17-
self.secrets_manager_patcher = patch("common.clients.global_secrets_manager_client")
18-
self.mock_secrets_manager = self.secrets_manager_patcher.start()
19-
20-
self.pds_env_patcher = patch("pds_details.get_pds_env")
21-
self.mock_pds_env = self.pds_env_patcher.start()
22-
self.mock_pds_env.return_value = "test-env"
23-
24-
self.auth_patcher = patch("pds_details.AppRestrictedAuth")
25-
self.mock_auth_class = self.auth_patcher.start()
26-
self.mock_auth_instance = MagicMock()
27-
self.mock_auth_class.return_value = self.mock_auth_instance
28-
29-
self.pds_service_patcher = patch("pds_details.PdsService")
30-
self.mock_pds_service_class = self.pds_service_patcher.start()
31-
self.mock_pds_service_instance = MagicMock()
32-
self.mock_pds_service_class.return_value = self.mock_pds_service_instance
33-
34-
def tearDown(self):
35-
"""Clean up patches"""
36-
patch.stopall()
37-
38-
def test_pds_get_patient_details_success(self):
39-
"""Test successful retrieval of patient details"""
40-
# Arrange
41-
expected_patient_data = {
42-
"identifier": [{"value": "9912003888"}],
43-
"name": "John Doe",
44-
"birthDate": "1990-01-01",
45-
"gender": "male",
46-
}
47-
self.mock_pds_service_instance.get_patient_details.return_value = expected_patient_data
48-
49-
# Act
50-
result = pds_get_patient_details(self.test_patient_id)
51-
52-
# Assert
53-
self.assertEqual(result["identifier"][0]["value"], "9912003888")
54-
55-
# Verify get_patient_details was called
56-
self.mock_pds_service_instance.get_patient_details.assert_called_once()
57-
58-
def test_pds_get_patient_details_no_patient_found(self):
59-
"""Test when PDS returns None (no patient found)"""
60-
# Arrange
61-
self.mock_pds_service_instance.get_patient_details.return_value = None
62-
63-
# Act
64-
result = pds_get_patient_details(self.test_patient_id)
65-
66-
# Assert
67-
self.assertIsNone(result)
68-
69-
self.mock_pds_service_instance.get_patient_details.assert_called_once_with(self.test_patient_id)
70-
71-
def test_pds_get_patient_details_empty_response(self):
72-
"""Test when PDS returns empty dict (falsy)"""
73-
# Arrange
74-
self.mock_pds_service_instance.get_patient_details.return_value = None
75-
76-
# Act
77-
result = pds_get_patient_details(self.test_patient_id)
78-
79-
# Assert
80-
self.assertIsNone(result)
81-
82-
def test_pds_get_patient_details_pds_service_exception(self):
83-
"""Test when PdsService.get_patient_details raises an exception"""
84-
# Arrange
85-
mock_exception = Exception("My custom error")
86-
self.mock_pds_service_instance.get_patient_details.side_effect = mock_exception
87-
88-
# Act
89-
with self.assertRaises(IdSyncException) as context:
90-
pds_get_patient_details(self.test_patient_id)
91-
92-
exception = context.exception
93-
94-
# Assert
95-
self.assertEqual(
96-
exception.message,
97-
"Error retrieving patient details from PDS",
98-
)
99-
100-
# Verify exception was logged
101-
self.mock_logger.exception.assert_called_once_with("Error retrieving patient details from PDS")
102-
103-
self.mock_pds_service_instance.get_patient_details.assert_called_once_with(self.test_patient_id)
104-
105-
def test_pds_get_patient_details_auth_initialization_error(self):
106-
"""Test when AppRestrictedAuth initialization fails"""
107-
# Arrange
108-
self.mock_auth_class.side_effect = ValueError("Invalid authentication parameters")
109-
110-
# Act
111-
with self.assertRaises(IdSyncException) as context:
112-
pds_get_patient_details(self.test_patient_id)
113-
114-
# Assert
115-
exception = context.exception
116-
self.assertEqual(
117-
exception.message,
118-
"Error retrieving patient details from PDS",
119-
)
120-
121-
# Verify exception was logged
122-
self.mock_logger.exception.assert_called_once_with("Error retrieving patient details from PDS")
123-
124-
def test_pds_get_patient_details_exception(self):
125-
"""Test when logger.info throws an exception"""
126-
# Arrange
127-
test_exception = Exception("some-random-error")
128-
self.mock_pds_service_class.side_effect = test_exception
129-
test_nhs_number = "another-nhs-number"
130-
131-
# Act
132-
with self.assertRaises(Exception) as context:
133-
pds_get_patient_details(test_nhs_number)
134-
135-
exception = context.exception
136-
# Assert
137-
self.assertEqual(
138-
exception.message,
139-
"Error retrieving patient details from PDS",
140-
)
141-
# Verify logger.exception was called due to the caught exception
142-
self.mock_logger.exception.assert_called_once_with("Error retrieving patient details from PDS")
143-
144-
def test_pds_get_patient_details_different_patient_ids(self):
145-
"""Test with different patient ID formats"""
146-
test_cases = [
147-
("9912003888", {"identifier": [{"value": "9912003888"}]}),
148-
("1234567890", {"identifier": [{"value": "1234567890"}]}),
149-
("0000000000", {"identifier": [{"value": "0000000000"}]}),
150-
]
151-
152-
for patient_id, expected_response in test_cases:
153-
with self.subTest(patient_id=patient_id):
154-
# Reset mocks
155-
self.mock_pds_service_instance.reset_mock()
156-
self.mock_logger.reset_mock()
157-
158-
# Arrange
159-
self.mock_pds_service_instance.get_patient_details.return_value = expected_response
160-
161-
# Act
162-
result = pds_get_patient_details(patient_id)
163-
164-
# Assert
165-
self.assertEqual(result, expected_response)
166-
self.mock_pds_service_instance.get_patient_details.assert_called_once_with(patient_id)
167-
168-
def test_pds_get_patient_details(self):
169-
"""Test with complex identifier structure"""
170-
# Arrange
171-
test_nhs_number = "9912003888"
172-
pds_id = "abcefghijkl"
173-
mock_pds_response = {"identifier": [{"value": pds_id}]}
174-
self.mock_pds_service_instance.get_patient_details.return_value = mock_pds_response
175-
# Act
176-
result = pds_get_patient_details(test_nhs_number)
177-
178-
# Assert - function should extract the value from first identifier
179-
self.assertEqual(result, mock_pds_response)
180-
self.mock_pds_service_instance.get_patient_details.assert_called_once_with(test_nhs_number)
181-
6+
class TestGetNhsNumber(unittest.TestCase):
1827
def test_get_nhs_number_from_pds_resource(self):
1838
"""Test that the NHS Number is retrieved from a full PDS patient resource."""
1849
mock_pds_resource = {

0 commit comments

Comments
 (0)