|
1 | | -import pprint |
2 | | -import uuid |
3 | | -from typing import NamedTuple, Literal, Optional, List |
| 1 | + |
4 | 2 | from decimal import Decimal |
5 | 3 | from utils.base_test import ImmunizationBaseTest |
6 | | -from utils.constants import ( |
7 | | - valid_nhs_number1, |
8 | | - valid_nhs_number2, |
9 | | - valid_patient_identifier2, |
10 | | - valid_patient_identifier1, |
11 | | -) |
| 4 | +from utils.constants import valid_nhs_number1 |
| 5 | + |
12 | 6 | from utils.resource import generate_imms_resource, generate_filtered_imms_resource |
13 | 7 | from utils.mappings import VaccineTypes |
14 | 8 | from lib.env import get_service_base_path |
@@ -132,199 +126,10 @@ def test_search_backwards_compatible(self): |
132 | 126 | self.assertEqual(imms_identifier[0]["value"], identifier_value) |
133 | 127 |
|
134 | 128 | # Check structure of one of the imms resources |
135 | | - # expected_imms_resource["patient"]["reference"] = response_patient["fullUrl"] |
136 | 129 | response_imm = next(item for item in entries if item["resource"]["id"] == imms_id) |
137 | 130 | self.assertEqual( |
138 | 131 | response_imm["fullUrl"], f"{get_service_base_path()}/Immunization/{imms_id}" |
139 | 132 | ) |
140 | 133 | self.assertEqual(response_imm["search"], {"mode": "match"}) |
141 | 134 | expected_imms_resource["patient"]["reference"] = response_imm["resource"]["patient"]["reference"] |
142 | 135 | self.assertEqual(response_imm["resource"], expected_imms_resource) |
143 | | - |
144 | | - def test_search_immunization_parameter_smoke_tests(self): |
145 | | - time_1 = "2024-01-30T13:28:17.271+00:00" |
146 | | - time_2 = "2024-02-01T13:28:17.271+00:00" |
147 | | - stored_records = [ |
148 | | - generate_imms_resource(valid_nhs_number1, VaccineTypes.mmr, imms_identifier_value=str(uuid.uuid4())), |
149 | | - generate_imms_resource(valid_nhs_number1, VaccineTypes.flu, imms_identifier_value=str(uuid.uuid4())), |
150 | | - generate_imms_resource(valid_nhs_number1, VaccineTypes.covid_19, imms_identifier_value=str(uuid.uuid4())), |
151 | | - generate_imms_resource(valid_nhs_number1, VaccineTypes.covid_19, |
152 | | - occurrence_date_time=time_1, |
153 | | - imms_identifier_value=str(uuid.uuid4())), |
154 | | - generate_imms_resource(valid_nhs_number1, VaccineTypes.covid_19, |
155 | | - occurrence_date_time=time_2, |
156 | | - imms_identifier_value=str(uuid.uuid4())), |
157 | | - generate_imms_resource(valid_nhs_number2, VaccineTypes.flu, imms_identifier_value=str(uuid.uuid4())), |
158 | | - generate_imms_resource(valid_nhs_number2, VaccineTypes.covid_19, imms_identifier_value=str(uuid.uuid4())), |
159 | | - ] |
160 | | - |
161 | | - created_resource_ids = list(self.store_records(*stored_records)) |
162 | | - # created_resource_ids = [result["id"] for result in stored_records] |
163 | | - |
164 | | - # When |
165 | | - class SearchTestParams(NamedTuple): |
166 | | - method: Literal["POST", "GET"] |
167 | | - query_string: Optional[str] |
168 | | - body: Optional[str] |
169 | | - should_be_success: bool |
170 | | - expected_indexes: List[int] |
171 | | - expected_status_code: int = 200 |
172 | | - |
173 | | - searches = [ |
174 | | - SearchTestParams( |
175 | | - "GET", |
176 | | - "", |
177 | | - None, |
178 | | - False, |
179 | | - [], |
180 | | - 400 |
181 | | - ), |
182 | | - # No results. |
183 | | - SearchTestParams( |
184 | | - "GET", |
185 | | - f"patient.identifier={valid_patient_identifier2}&-immunization.target={VaccineTypes.mmr}", |
186 | | - None, |
187 | | - True, |
188 | | - [], |
189 | | - 200 |
190 | | - ), |
191 | | - # Basic success. |
192 | | - SearchTestParams( |
193 | | - "GET", |
194 | | - f"patient.identifier={valid_patient_identifier1}&-immunization.target={VaccineTypes.mmr}", |
195 | | - None, |
196 | | - True, |
197 | | - [0], |
198 | | - 200 |
199 | | - ), |
200 | | - # "Or" params. |
201 | | - SearchTestParams( |
202 | | - "GET", |
203 | | - f"patient.identifier={valid_patient_identifier1}&-immunization.target={VaccineTypes.mmr}," |
204 | | - + f"{VaccineTypes.flu}", |
205 | | - None, |
206 | | - True, |
207 | | - [0, 1], |
208 | | - 200 |
209 | | - ), |
210 | | - # GET does not support body. |
211 | | - SearchTestParams( |
212 | | - "GET", |
213 | | - f"patient.identifier={valid_patient_identifier1}&-immunization.target={VaccineTypes.mmr}", |
214 | | - f"patient.identifier={valid_patient_identifier1}", |
215 | | - True, |
216 | | - [0], |
217 | | - 200 |
218 | | - ), |
219 | | - SearchTestParams( |
220 | | - "POST", |
221 | | - None, |
222 | | - f"patient.identifier={valid_patient_identifier1}&-immunization.target={VaccineTypes.mmr}", |
223 | | - True, |
224 | | - [0], |
225 | | - 200 |
226 | | - ), |
227 | | - # Duplicated NHS number not allowed, spread across query and content. |
228 | | - SearchTestParams( |
229 | | - "POST", |
230 | | - f"patient.identifier={valid_patient_identifier1}&-immunization.target={VaccineTypes.mmr}", |
231 | | - f"patient.identifier={valid_patient_identifier1}", |
232 | | - False, |
233 | | - [], |
234 | | - 400 |
235 | | - ), |
236 | | - SearchTestParams( |
237 | | - "GET", |
238 | | - f"patient.identifier={valid_patient_identifier1}" |
239 | | - f"&patient.identifier={valid_patient_identifier1}" |
240 | | - f"&-immunization.target={VaccineTypes.mmr}", |
241 | | - None, |
242 | | - False, |
243 | | - [], |
244 | | - 400 |
245 | | - ), |
246 | | - # "And" params not supported. |
247 | | - SearchTestParams( |
248 | | - "GET", |
249 | | - f"patient.identifier={valid_patient_identifier1}&-immunization.target={VaccineTypes.mmr}" |
250 | | - f"&-immunization.target={VaccineTypes.flu}", |
251 | | - None, |
252 | | - False, |
253 | | - [], |
254 | | - 400 |
255 | | - ), |
256 | | - # Date |
257 | | - SearchTestParams( |
258 | | - "GET", |
259 | | - f"patient.identifier={valid_patient_identifier1}&-immunization.target={VaccineTypes.covid_19}", |
260 | | - None, |
261 | | - True, |
262 | | - [2, 3, 4], |
263 | | - 200 |
264 | | - ), |
265 | | - SearchTestParams( |
266 | | - "GET", |
267 | | - f"patient.identifier={valid_patient_identifier1}&-immunization.target={VaccineTypes.covid_19}" |
268 | | - f"&-date.from=2024-01-30", |
269 | | - None, |
270 | | - True, |
271 | | - [3, 4], |
272 | | - 200 |
273 | | - ), |
274 | | - SearchTestParams( |
275 | | - "GET", |
276 | | - f"patient.identifier={valid_patient_identifier1}&-immunization.target={VaccineTypes.covid_19}" |
277 | | - f"&-date.to=2024-01-30", |
278 | | - None, |
279 | | - True, |
280 | | - [2, 3], |
281 | | - 200 |
282 | | - ), |
283 | | - SearchTestParams( |
284 | | - "GET", |
285 | | - f"patient.identifier={valid_patient_identifier1}&-immunization.target={VaccineTypes.covid_19}" |
286 | | - f"&-date.from=2024-01-01&-date.to=2024-01-30", |
287 | | - None, |
288 | | - True, |
289 | | - [3], |
290 | | - 200 |
291 | | - ), |
292 | | - # "from" after "to" is an error. |
293 | | - SearchTestParams( |
294 | | - "GET", |
295 | | - f"patient.identifier={valid_patient_identifier1}&-immunization.target={VaccineTypes.covid_19}" |
296 | | - f"&-date.from=2024-02-01&-date.to=2024-01-30", |
297 | | - None, |
298 | | - False, |
299 | | - [0], |
300 | | - 400 |
301 | | - ), |
302 | | - ] |
303 | | - |
304 | | - for search in searches: |
305 | | - pprint.pprint(search) |
306 | | - response = self.default_imms_api.search_immunizations_full(search.method, search.query_string, |
307 | | - body=search.body, |
308 | | - expected_status_code=search.expected_status_code) |
309 | | - |
310 | | - # Then |
311 | | - assert response.ok == search.should_be_success, response.text |
312 | | - |
313 | | - results: dict = response.json() |
314 | | - if search.should_be_success: |
315 | | - assert "entry" in results.keys() |
316 | | - assert response.status_code == 200 |
317 | | - assert results["resourceType"] == "Bundle" |
318 | | - |
319 | | - result_ids = [result["resource"]["id"] for result in results["entry"]] |
320 | | - created_and_returned_ids = list(set(result_ids) & set(created_resource_ids)) |
321 | | - print("\n Search Test Debug Info:") |
322 | | - print("Search method:", search.method) |
323 | | - print("Search query string:", search.query_string) |
324 | | - print("Expected indexes:", search.expected_indexes) |
325 | | - print("Expected IDs:", [created_resource_ids[i] for i in search.expected_indexes]) |
326 | | - print("Actual returned IDs:", result_ids) |
327 | | - print("Matched IDs:", created_and_returned_ids) |
328 | | - assert len(created_and_returned_ids) == len(search.expected_indexes) |
329 | | - for expected_index in search.expected_indexes: |
330 | | - assert created_resource_ids[expected_index] in result_ids |
0 commit comments