|
| 1 | + |
| 2 | +from decimal import Decimal |
| 3 | +import pprint |
| 4 | +from typing import NamedTuple, Literal, Optional |
| 5 | +import uuid |
| 6 | +from utils.base_test import ImmunizationBaseTest |
| 7 | +from utils.constants import valid_nhs_number1 |
| 8 | + |
| 9 | +from utils.resource import generate_imms_resource, generate_filtered_imms_resource |
| 10 | +from utils.mappings import VaccineTypes |
| 11 | +from lib.env import get_service_base_path |
| 12 | + |
| 13 | + |
| 14 | +class TestSearchImmunizationByIdentifier(ImmunizationBaseTest): |
| 15 | + |
| 16 | + def store_records(self, *resources): |
| 17 | + ids = [] |
| 18 | + for res in resources: |
| 19 | + imms_id = self.default_imms_api.create_immunization_resource(res) |
| 20 | + ids.append(imms_id) |
| 21 | + return ids[0] if len(ids) == 1 else tuple(ids) |
| 22 | + |
| 23 | + def test_search_imms(self): |
| 24 | + for imms_api in self.imms_apis: |
| 25 | + with self.subTest(imms_api): |
| 26 | + covid19_imms_data = generate_imms_resource() |
| 27 | + rsv_imms_data = generate_imms_resource() |
| 28 | + covid_ids = self.store_records(covid19_imms_data) |
| 29 | + rsv_ids = self.store_records(rsv_imms_data) |
| 30 | + |
| 31 | + # Retrieve the resources to get the identifier system and value via read API |
| 32 | + covid_resource = imms_api.get_immunization_by_id(covid_ids).json() |
| 33 | + rsv_resource = imms_api.get_immunization_by_id(rsv_ids).json() |
| 34 | + |
| 35 | + # Extract identifier components safely for covid resource |
| 36 | + identifiers = covid_resource.get("identifier", []) |
| 37 | + identifier_system = identifiers[0].get("system") |
| 38 | + identifier_value = identifiers[0].get("value") |
| 39 | + |
| 40 | + # Extract identifier components safely for rsv resource |
| 41 | + rsv_identifiers = rsv_resource.get("identifier", []) |
| 42 | + rsv_identifier_system = rsv_identifiers[0].get("system") |
| 43 | + rsv_identifier_value = rsv_identifiers[0].get("value") |
| 44 | + |
| 45 | + # When |
| 46 | + search_response = imms_api.search_immunization_by_identifier(identifier_system, identifier_value) |
| 47 | + self.assertEqual(search_response.status_code, 200, search_response.text) |
| 48 | + bundle = search_response.json() |
| 49 | + self.assertEqual(bundle.get("resourceType"), "Bundle", bundle) |
| 50 | + entries = bundle.get("entry", []) |
| 51 | + self.assertTrue(entries, "Expected at least one match in Bundle.entry") |
| 52 | + self.assertEqual(len(entries), 1, f"Expected exactly one match, got {len(entries)}") |
| 53 | + |
| 54 | + # When |
| 55 | + rsv_search_response = imms_api.search_immunization_by_identifier( |
| 56 | + rsv_identifier_system, |
| 57 | + rsv_identifier_value |
| 58 | + ) |
| 59 | + self.assertEqual(rsv_search_response.status_code, 200, search_response.text) |
| 60 | + rsv_bundle = rsv_search_response.json() |
| 61 | + self.assertEqual(bundle.get("resourceType"), "Bundle", rsv_bundle) |
| 62 | + entries = rsv_bundle.get("entry", []) |
| 63 | + self.assertTrue(entries, "Expected at least one match in Bundle.entry") |
| 64 | + self.assertEqual(len(entries), 1, f"Expected exactly one match, got {len(entries)}") |
| 65 | + |
| 66 | + def test_search_backwards_compatible(self): |
| 67 | + """Test that SEARCH 200 response body is backwards compatible with Immunisation History FHIR API. |
| 68 | + This test proves that the search endpoint’s response is still shaped exactly like the |
| 69 | + Immunisation History FHIR API expects (“backwards compatible”), not just that it returns a 200 |
| 70 | + """ |
| 71 | + for imms_api in self.imms_apis: |
| 72 | + with self.subTest(imms_api): |
| 73 | + |
| 74 | + stored_imms_resource = generate_imms_resource() |
| 75 | + imms_identifier_value = stored_imms_resource["identifier"][0]["value"] |
| 76 | + imms_id = self.store_records(stored_imms_resource) |
| 77 | + |
| 78 | + # Prepare the imms resource expected from the response. Note that id and identifier_value need to be |
| 79 | + # updated to match those assigned by the create_an_imms_obj and store_records functions. |
| 80 | + expected_imms_resource = generate_filtered_imms_resource( |
| 81 | + crud_operation_to_filter_for="SEARCH", |
| 82 | + imms_identifier_value=imms_identifier_value, |
| 83 | + nhs_number=valid_nhs_number1, |
| 84 | + vaccine_type=VaccineTypes.covid_19, |
| 85 | + ) |
| 86 | + expected_imms_resource["id"] = imms_id |
| 87 | + expected_imms_resource["meta"] = {"versionId": "1"} |
| 88 | + |
| 89 | + # Retrieve the resource to get the identifier system and value via READ API |
| 90 | + imms_resource = imms_api.get_immunization_by_id(imms_id).json() |
| 91 | + identifiers = imms_resource.get("identifier", []) |
| 92 | + identifier_system = identifiers[0].get("system") |
| 93 | + identifier_value = identifiers[0].get("value") |
| 94 | + self.assertIsNotNone(identifier_system, "Identifier system is None") |
| 95 | + self.assertIsNotNone(identifier_value, "Identifier value is None") |
| 96 | + |
| 97 | + # When |
| 98 | + response = imms_api.search_immunization_by_identifier(identifier_system, identifier_value) |
| 99 | + |
| 100 | + # Then |
| 101 | + self.assertEqual(response.status_code, 200, response.text) |
| 102 | + body = response.json(parse_float=Decimal) |
| 103 | + entries = body["entry"] |
| 104 | + response_imms = [item for item in entries if item["resource"]["resourceType"] == "Immunization"] |
| 105 | + response_patients = [item for item in entries if item["resource"]["resourceType"] == "Patient"] |
| 106 | + response_other_entries = [ |
| 107 | + item for item in entries if item["resource"]["resourceType"] not in ("Patient", "Immunization") |
| 108 | + ] |
| 109 | + |
| 110 | + # Check bundle structure apart from entry |
| 111 | + self.assertEqual(body["resourceType"], "Bundle") |
| 112 | + self.assertEqual(body["type"], "searchset") |
| 113 | + self.assertEqual(body["total"], len(response_imms)) |
| 114 | + |
| 115 | + # Check that entry only contains a patient and immunizations |
| 116 | + self.assertEqual(len(response_other_entries), 0) |
| 117 | + self.assertEqual(len(response_patients), 0) |
| 118 | + |
| 119 | + # Check Immunization structure |
| 120 | + for entry in response_imms: |
| 121 | + self.assertEqual(entry["search"], {"mode": "match"}) |
| 122 | + self.assertTrue(entry["fullUrl"].startswith("https://")) |
| 123 | + self.assertEqual(entry["resource"]["resourceType"], "Immunization") |
| 124 | + imms_identifier = entry["resource"]["identifier"] |
| 125 | + self.assertEqual(len(imms_identifier), 1, "Immunization did not have exactly 1 identifier") |
| 126 | + self.assertEqual(imms_identifier[0]["system"], identifier_system) |
| 127 | + self.assertEqual(imms_identifier[0]["value"], identifier_value) |
| 128 | + |
| 129 | + # Check structure of one of the imms resources |
| 130 | + response_imm = next(item for item in entries if item["resource"]["id"] == imms_id) |
| 131 | + self.assertEqual( |
| 132 | + response_imm["fullUrl"], f"{get_service_base_path()}/Immunization/{imms_id}" |
| 133 | + ) |
| 134 | + self.assertEqual(response_imm["search"], {"mode": "match"}) |
| 135 | + expected_imms_resource["patient"]["reference"] = response_imm["resource"]["patient"]["reference"] |
| 136 | + self.assertEqual(response_imm["resource"], expected_imms_resource) |
| 137 | + |
| 138 | + def test_search_immunization_parameter_smoke_tests(self): |
| 139 | + stored_records = generate_imms_resource( |
| 140 | + valid_nhs_number1, VaccineTypes.covid_19, |
| 141 | + imms_identifier_value=str(uuid.uuid4())) |
| 142 | + |
| 143 | + imms_id = self.store_records(stored_records) |
| 144 | + # Retrieve the resources to get the identifier system and value via read API |
| 145 | + covid_resource = self.default_imms_api.get_immunization_by_id(imms_id).json() |
| 146 | + |
| 147 | + # Extract identifier components safely for covid resource |
| 148 | + identifiers = covid_resource.get("identifier", []) |
| 149 | + identifier_system = identifiers[0].get("system") |
| 150 | + identifier_value = identifiers[0].get("value") |
| 151 | + |
| 152 | + # created_resource_ids = [result["id"] for result in stored_records] |
| 153 | + |
| 154 | + class SearchTestParams(NamedTuple): |
| 155 | + method: Literal["POST", "GET"] |
| 156 | + query_string: Optional[str] |
| 157 | + body: Optional[str] |
| 158 | + should_be_success: bool |
| 159 | + expected_status_code: int = 200 |
| 160 | + |
| 161 | + searches = [ |
| 162 | + SearchTestParams( |
| 163 | + "GET", |
| 164 | + "", |
| 165 | + None, |
| 166 | + False, |
| 167 | + 400 |
| 168 | + ), |
| 169 | + # No results. |
| 170 | + SearchTestParams( |
| 171 | + "GET", |
| 172 | + f"identifier={identifier_system}|{identifier_value}", |
| 173 | + None, |
| 174 | + True, |
| 175 | + 200 |
| 176 | + ), |
| 177 | + SearchTestParams( |
| 178 | + "POST", |
| 179 | + "", |
| 180 | + f"identifier={identifier_system}|{identifier_value}", |
| 181 | + True, |
| 182 | + 200 |
| 183 | + ), |
| 184 | + SearchTestParams( |
| 185 | + "POST", |
| 186 | + f"identifier={identifier_system}|{identifier_value}", |
| 187 | + f"identifier={identifier_system}|{identifier_value}", |
| 188 | + False, |
| 189 | + 400 |
| 190 | + ), |
| 191 | + ] |
| 192 | + for search in searches: |
| 193 | + pprint.pprint(search) |
| 194 | + response = self.default_imms_api.search_immunizations_full( |
| 195 | + search.method, |
| 196 | + search.query_string, |
| 197 | + body=search.body, |
| 198 | + expected_status_code=search.expected_status_code) |
| 199 | + |
| 200 | + # Then |
| 201 | + assert response.ok == search.should_be_success, response.text |
| 202 | + |
| 203 | + results: dict = response.json() |
| 204 | + if search.should_be_success: |
| 205 | + assert "entry" in results.keys() |
| 206 | + assert response.status_code == 200 |
| 207 | + assert results["resourceType"] == "Bundle" |
| 208 | + assert results["type"] == "searchset" |
| 209 | + assert results["total"] == 1 |
| 210 | + assert isinstance(results["entry"], list) |
| 211 | + else: |
| 212 | + assert "entry" not in results.keys() |
| 213 | + assert response.status_code != 200 |
| 214 | + assert results["resourceType"] == "OperationOutcome" |
0 commit comments