|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +from pds_details import get_pds_patient_details |
| 4 | + |
| 5 | + |
| 6 | +class TestGetPdsPatientDetails(unittest.TestCase): |
| 7 | + |
| 8 | + def setUp(self): |
| 9 | + """Set up test fixtures and mocks""" |
| 10 | + # Mock the dependencies |
| 11 | + self.mock_boto3_client = MagicMock() |
| 12 | + self.test_patient_id = "9912003888" |
| 13 | + |
| 14 | + # Patch all external dependencies |
| 15 | + self.logger_patcher = patch('pds_details.logger') |
| 16 | + self.mock_logger = self.logger_patcher.start() |
| 17 | + |
| 18 | + self.secrets_manager_patcher = patch('pds_details.secrets_manager_client') |
| 19 | + self.mock_secrets_manager = self.secrets_manager_patcher.start() |
| 20 | + |
| 21 | + self.pds_env_patcher = patch('pds_details.pds_env') |
| 22 | + self.mock_pds_env = self.pds_env_patcher.start() |
| 23 | + |
| 24 | + self.cache_patcher = patch('pds_details.Cache') |
| 25 | + self.mock_cache_class = self.cache_patcher.start() |
| 26 | + self.mock_cache_instance = MagicMock() |
| 27 | + self.mock_cache_class.return_value = self.mock_cache_instance |
| 28 | + |
| 29 | + self.auth_patcher = patch('pds_details.AppRestrictedAuth') |
| 30 | + self.mock_auth_class = self.auth_patcher.start() |
| 31 | + self.mock_auth_instance = MagicMock() |
| 32 | + self.mock_auth_class.return_value = self.mock_auth_instance |
| 33 | + |
| 34 | + self.pds_service_patcher = patch('pds_details.PdsService') |
| 35 | + self.mock_pds_service_class = self.pds_service_patcher.start() |
| 36 | + self.mock_pds_service_instance = MagicMock() |
| 37 | + self.mock_pds_service_class.return_value = self.mock_pds_service_instance |
| 38 | + |
| 39 | + def tearDown(self): |
| 40 | + """Clean up patches""" |
| 41 | + patch.stopall() |
| 42 | + |
| 43 | + def test_get_pds_patient_details_success(self): |
| 44 | + """Test successful retrieval of patient details""" |
| 45 | + # Arrange |
| 46 | + expected_patient_data = { |
| 47 | + "id": "9912003888", |
| 48 | + "name": "John Doe", |
| 49 | + "birthDate": "1990-01-01", |
| 50 | + "gender": "male" |
| 51 | + } |
| 52 | + self.mock_pds_service_instance.get_patient_details.return_value = expected_patient_data |
| 53 | + |
| 54 | + # Act |
| 55 | + result = get_pds_patient_details(self.test_patient_id, self.mock_boto3_client) |
| 56 | + |
| 57 | + # Assert |
| 58 | + self.assertEqual(result, "9912003888") |
| 59 | + |
| 60 | + # Verify Cache was initialized correctly |
| 61 | + self.mock_cache_class.assert_called_once_with(directory="/tmp") |
| 62 | + |
| 63 | + # Verify AppRestrictedAuth was initialized correctly |
| 64 | + from common.authentication import Service |
| 65 | + self.mock_auth_class.assert_called_once_with( |
| 66 | + service=Service.PDS, |
| 67 | + secret_manager_client=self.mock_secrets_manager, |
| 68 | + environment=self.mock_pds_env, |
| 69 | + cache=self.mock_cache_instance |
| 70 | + ) |
| 71 | + |
| 72 | + # Verify PdsService was initialized correctly |
| 73 | + self.mock_pds_service_class.assert_called_once_with( |
| 74 | + self.mock_auth_instance, |
| 75 | + self.mock_pds_env |
| 76 | + ) |
| 77 | + |
| 78 | + # Verify get_patient_details was called |
| 79 | + self.mock_pds_service_instance.get_patient_details.assert_called_once_with(self.test_patient_id) |
| 80 | + |
| 81 | + def test_get_pds_patient_details_missing_id_in_response(self): |
| 82 | + """Test when PDS response doesn't contain 'id' field""" |
| 83 | + # Arrange |
| 84 | + patient_data_without_id = { |
| 85 | + "name": "John Doe", |
| 86 | + "birthDate": "1990-01-01", |
| 87 | + "gender": "male" |
| 88 | + # Missing 'id' field |
| 89 | + } |
| 90 | + self.mock_pds_service_instance.get_patient_details.return_value = patient_data_without_id |
| 91 | + |
| 92 | + # Act |
| 93 | + result = get_pds_patient_details(self.test_patient_id, self.mock_boto3_client) |
| 94 | + |
| 95 | + # Assert |
| 96 | + self.assertIsNone(result) |
| 97 | + self.mock_pds_service_instance.get_patient_details.assert_called_once_with(self.test_patient_id) |
| 98 | + |
| 99 | + def test_get_pds_patient_details_empty_response(self): |
| 100 | + """Test when PDS returns empty response""" |
| 101 | + # Arrange |
| 102 | + self.mock_pds_service_instance.get_patient_details.return_value = {} |
| 103 | + |
| 104 | + # Act |
| 105 | + result = get_pds_patient_details(self.test_patient_id, self.mock_boto3_client) |
| 106 | + |
| 107 | + # Assert |
| 108 | + self.assertIsNone(result) |
| 109 | + self.mock_pds_service_instance.get_patient_details.assert_called_once_with(self.test_patient_id) |
| 110 | + |
| 111 | + def test_get_pds_patient_details_none_response(self): |
| 112 | + """Test when PDS returns None""" |
| 113 | + # Arrange |
| 114 | + self.mock_pds_service_instance.get_patient_details.return_value = None |
| 115 | + |
| 116 | + # Act |
| 117 | + with self.assertRaises(AttributeError): |
| 118 | + get_pds_patient_details(self.test_patient_id, self.mock_boto3_client) |
| 119 | + |
| 120 | + # Assert |
| 121 | + self.mock_pds_service_instance.get_patient_details.assert_called_once_with(self.test_patient_id) |
| 122 | + |
| 123 | + def test_get_pds_patient_details_pds_service_exception(self): |
| 124 | + """Test when PdsService.get_patient_details raises an exception""" |
| 125 | + # Arrange |
| 126 | + self.mock_pds_service_instance.get_patient_details.side_effect = Exception("PDS API error") |
| 127 | + |
| 128 | + # Act & Assert |
| 129 | + with self.assertRaises(Exception) as context: |
| 130 | + get_pds_patient_details(self.test_patient_id, self.mock_boto3_client) |
| 131 | + |
| 132 | + self.assertEqual(str(context.exception), "PDS API error") |
| 133 | + self.mock_pds_service_instance.get_patient_details.assert_called_once_with(self.test_patient_id) |
| 134 | + |
| 135 | + def test_get_pds_patient_details_cache_initialization_error(self): |
| 136 | + """Test when Cache initialization fails""" |
| 137 | + # Arrange |
| 138 | + self.mock_cache_class.side_effect = OSError("Cannot write to /tmp") |
| 139 | + |
| 140 | + # Act & Assert |
| 141 | + with self.assertRaises(OSError) as context: |
| 142 | + get_pds_patient_details(self.test_patient_id, self.mock_boto3_client) |
| 143 | + |
| 144 | + self.assertEqual(str(context.exception), "Cannot write to /tmp") |
| 145 | + self.mock_cache_class.assert_called_once_with(directory="/tmp") |
| 146 | + |
| 147 | + def test_get_pds_patient_details_auth_initialization_error(self): |
| 148 | + """Test when AppRestrictedAuth initialization fails""" |
| 149 | + # Arrange |
| 150 | + self.mock_auth_class.side_effect = ValueError("Invalid authentication parameters") |
| 151 | + |
| 152 | + # Act & Assert |
| 153 | + with self.assertRaises(ValueError) as context: |
| 154 | + get_pds_patient_details(self.test_patient_id, self.mock_boto3_client) |
| 155 | + |
| 156 | + self.assertEqual(str(context.exception), "Invalid authentication parameters") |
| 157 | + |
| 158 | + def test_get_pds_patient_details_exception(self): |
| 159 | + """Test when an exception occurs""" |
| 160 | + # Arrange |
| 161 | + self.mock_logger.info.side_effect = Exception("Logging system failure") |
| 162 | + |
| 163 | + # Act |
| 164 | + result = get_pds_patient_details(self.test_patient_id, self.mock_boto3_client) |
| 165 | + |
| 166 | + # Assert |
| 167 | + self.assertIsNone(result) |
| 168 | + |
| 169 | + # Verify logger.exception was called due to the caught exception |
| 170 | + self.mock_logger.exception.assert_called_once_with( |
| 171 | + f"Error getting PDS patient details for {self.test_patient_id}") |
0 commit comments