|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | + |
| 4 | +import requests |
| 5 | +from fhirclient.models.bundle import Bundle |
| 6 | + |
| 7 | +from fhirclient.client import FHIRClient |
| 8 | + |
| 9 | + |
| 10 | +class TestFHIRClientPagination(unittest.TestCase): |
| 11 | + def setUp(self) -> None: |
| 12 | + state = { |
| 13 | + "app_id": "AppID", |
| 14 | + "app_secret": "AppSecret", |
| 15 | + "scope": "user/*.read", |
| 16 | + "redirect": "http://test.invalid/redirect", |
| 17 | + "patient_id": "PatientID", |
| 18 | + "server": { |
| 19 | + "base_uri": "http://test.invalid/", |
| 20 | + "auth_type": "none", |
| 21 | + "auth": { |
| 22 | + "app_id": "AppId", |
| 23 | + }, |
| 24 | + }, |
| 25 | + "launch_token": "LaunchToken", |
| 26 | + "launch_context": { |
| 27 | + "encounter": "EncounterID", |
| 28 | + "patient": "PatientID", |
| 29 | + }, |
| 30 | + "jwt_token": "JwtToken", |
| 31 | + } |
| 32 | + |
| 33 | + # Confirm round trip |
| 34 | + self.client = FHIRClient(state=state) |
| 35 | + |
| 36 | + self.bundle = { |
| 37 | + "resourceType": "Bundle", |
| 38 | + "type": "searchset", |
| 39 | + "link": [ |
| 40 | + {"relation": "self", "url": "http://example.com/fhir/Bundle/1"}, |
| 41 | + {"relation": "next", "url": "http://example.com/fhir/Bundle/2"}, |
| 42 | + ], |
| 43 | + "entry": [ |
| 44 | + { |
| 45 | + "fullUrl": "http://example.com/fhir/Patient/1", |
| 46 | + "resource": { |
| 47 | + "resourceType": "Patient", |
| 48 | + "id": "1", |
| 49 | + "name": [{"family": "Doe", "given": ["John"]}], |
| 50 | + "gender": "male", |
| 51 | + "birthDate": "1980-01-01", |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + "fullUrl": "http://example.com/fhir/Patient/2", |
| 56 | + "resource": { |
| 57 | + "resourceType": "Patient", |
| 58 | + "id": "2", |
| 59 | + "name": [{"family": "Smith", "given": ["Jane"]}], |
| 60 | + "gender": "female", |
| 61 | + "birthDate": "1990-05-15", |
| 62 | + }, |
| 63 | + }, |
| 64 | + ], |
| 65 | + } |
| 66 | + |
| 67 | + def test_get_next_link(self): |
| 68 | + next_link = self.client._get_next_link(Bundle(self.bundle)) |
| 69 | + self.assertEqual(next_link, "http://example.com/fhir/Bundle/2") |
| 70 | + |
| 71 | + def test_get_next_link_no_next(self): |
| 72 | + bundle_without_next = { |
| 73 | + "resourceType": "Bundle", |
| 74 | + "type": "searchset", |
| 75 | + "link": [{"relation": "self", "url": "http://example.com/fhir/Bundle/1"}], |
| 76 | + } |
| 77 | + bundle = Bundle(bundle_without_next) |
| 78 | + next_link = self.client._get_next_link(bundle) |
| 79 | + self.assertIsNone(next_link) |
| 80 | + |
| 81 | + def test_sanitize_next_link_valid(self): |
| 82 | + next_link = "http://example.com/fhir/Bundle/2?page=2&size=10" |
| 83 | + sanitized_link = self.client._sanitize_next_link(next_link) |
| 84 | + self.assertEqual(sanitized_link, next_link) |
| 85 | + |
| 86 | + def test_sanitize_next_link_invalid_scheme(self): |
| 87 | + next_link = "ftp://example.com/fhir/Bundle/2?page=2&size=10" |
| 88 | + with self.assertRaises(ValueError): |
| 89 | + self.client._sanitize_next_link(next_link) |
| 90 | + |
| 91 | + def test_sanitize_next_link_invalid_domain(self): |
| 92 | + next_link = "http:///fhir/Bundle/2?page=2&size=10" |
| 93 | + with self.assertRaises(ValueError): |
| 94 | + self.client._sanitize_next_link(next_link) |
| 95 | + |
| 96 | + @patch("requests.get") |
| 97 | + def test_execute_pagination_request_success(self, mock_get): |
| 98 | + mock_response = MagicMock() |
| 99 | + # Set up the mock to return a specific JSON payload when its json() method is called |
| 100 | + mock_response.json.return_value = self.bundle |
| 101 | + mock_response.raise_for_status = MagicMock() |
| 102 | + mock_get.return_value = mock_response |
| 103 | + |
| 104 | + next_link = "http://example.com/fhir/Bundle/2" |
| 105 | + sanitized_link = self.client._sanitize_next_link(next_link) |
| 106 | + result = self.client._execute_pagination_request(sanitized_link) |
| 107 | + self.assertIsInstance(result, Bundle) |
| 108 | + self.assertIn("entry", result.as_json()) |
| 109 | + mock_get.assert_called_once_with(sanitized_link) |
| 110 | + |
| 111 | + @patch("requests.get") |
| 112 | + def test_execute_pagination_request_http_error(self, mock_get): |
| 113 | + mock_get.side_effect = requests.exceptions.HTTPError("HTTP Error") |
| 114 | + |
| 115 | + next_link = "http://example.com/fhir/Bundle/2" |
| 116 | + sanitized_link = self.client._sanitize_next_link(next_link) |
| 117 | + |
| 118 | + with self.assertRaises(requests.exceptions.HTTPError): |
| 119 | + self.client._execute_pagination_request(sanitized_link) |
| 120 | + |
| 121 | + @patch("requests.get") |
| 122 | + def test_execute_pagination_request_returns_last_bundle_if_no_next_link(self, mock_get): |
| 123 | + # Mock the response to simulate a Bundle with no next link |
| 124 | + mock_response = MagicMock() |
| 125 | + mock_response.json.return_value = { |
| 126 | + "resourceType": "Bundle", |
| 127 | + "type": "searchset", |
| 128 | + "link": [{"relation": "self", "url": "http://example.com/fhir/Bundle/1"}], |
| 129 | + "entry": [{"resource": {"resourceType": "Patient", "id": "1"}}], |
| 130 | + } |
| 131 | + mock_response.raise_for_status = MagicMock() |
| 132 | + mock_get.return_value = mock_response |
| 133 | + |
| 134 | + sanitized_link = "http://example.com/fhir/Bundle/1" |
| 135 | + result = self.client._execute_pagination_request(sanitized_link) |
| 136 | + |
| 137 | + # Check that the result is the Bundle itself, not None |
| 138 | + self.assertIsInstance(result, Bundle) |
| 139 | + self.assertTrue(hasattr(result, 'entry')) |
| 140 | + mock_get.assert_called_once_with(sanitized_link) |
| 141 | + |
| 142 | + @patch("fhirclient.client.FHIRClient._execute_pagination_request") |
| 143 | + def test_fetch_next_page(self, mock_execute_request): |
| 144 | + mock_execute_request.return_value = Bundle(self.bundle) |
| 145 | + result = self.client._fetch_next_page(Bundle(self.bundle)) |
| 146 | + self.assertIsInstance(result, Bundle) |
| 147 | + self.assertTrue(hasattr(result, "entry")) |
| 148 | + mock_execute_request.assert_called_once() |
| 149 | + |
| 150 | + def test_fetch_next_page_no_next_link(self): |
| 151 | + bundle_without_next = { |
| 152 | + "resourceType": "Bundle", |
| 153 | + "type": "searchset", |
| 154 | + "link": [{"relation": "self", "url": "http://example.com/fhir/Bundle/1"}], |
| 155 | + } |
| 156 | + bundle = Bundle(bundle_without_next) |
| 157 | + result = self.client._fetch_next_page(bundle) |
| 158 | + self.assertIsNone(result) |
| 159 | + |
| 160 | + @patch("fhirclient.client.FHIRClient._fetch_next_page") |
| 161 | + def test_iter_pages(self, mock_fetch_next_page): |
| 162 | + # Set up the mock to return a new bundle, then None to stop iteration |
| 163 | + mock_fetch_next_page.side_effect = [Bundle(self.bundle), None] |
| 164 | + pages = list(self.client.iter_pages(Bundle(self.bundle))) |
| 165 | + |
| 166 | + # Check that two bundles were returned (the first bundle and the one from mock) |
| 167 | + self.assertEqual(len(pages), 2) |
| 168 | + self.assertIsInstance(pages[0], Bundle) |
| 169 | + self.assertIsInstance(pages[1], Bundle) |
| 170 | + |
| 171 | + # Compare JSON representations instead of object instances |
| 172 | + self.assertEqual(pages[0].as_json(), self.bundle) |
| 173 | + self.assertEqual(pages[1].as_json(), self.bundle) |
| 174 | + |
| 175 | + # Ensure that _fetch_next_page was called twice |
| 176 | + self.assertEqual(mock_fetch_next_page.call_count, 2) |
0 commit comments