|
36 | 36 | NHS_NUMBER_USED_IN_SAMPLE_DATA = "9000000009" |
37 | 37 |
|
38 | 38 |
|
| 39 | +def add_snomed_coding(immunization: dict, field_name: str, code: str, display: str) -> dict: |
| 40 | + first_coding = deepcopy(immunization[field_name]["coding"][0]) |
| 41 | + immunization[field_name]["coding"].append({"system": first_coding["system"], "code": code, "display": display}) |
| 42 | + return first_coding |
| 43 | + |
| 44 | + |
39 | 45 | class TestFhirServiceBase(unittest.TestCase): |
40 | 46 | """Base class for all tests to set up common fixtures""" |
41 | 47 |
|
@@ -362,6 +368,27 @@ def test_create_immunization(self): |
362 | 368 | self.validator.validate.assert_called_once_with(req_imms) |
363 | 369 | self.assertEqual(self._MOCK_NEW_UUID, created_id) |
364 | 370 |
|
| 371 | + def test_create_immunization_keeps_first_site_and_route_snomed_coding(self): |
| 372 | + """it should keep the first SNOMED coding for site and route during API create""" |
| 373 | + self.mock_redis.hget.return_value = "COVID" |
| 374 | + self.mock_redis_getter.return_value = self.mock_redis |
| 375 | + self.authoriser.authorise.return_value = True |
| 376 | + self.imms_repo.check_immunization_identifier_exists.return_value = False |
| 377 | + self.imms_repo.create_immunization.return_value = self._MOCK_NEW_UUID |
| 378 | + |
| 379 | + req_imms = create_covid_immunization_dict_no_id(VALID_NHS_NUMBER) |
| 380 | + first_site_coding = add_snomed_coding(req_imms, "site", "999999999", "Replacement site that should be ignored") |
| 381 | + first_route_coding = add_snomed_coding( |
| 382 | + req_imms, "route", "888888888", "Replacement route that should be ignored" |
| 383 | + ) |
| 384 | + |
| 385 | + created_id = self.pre_validate_fhir_service.create_immunization(req_imms, "Test") |
| 386 | + |
| 387 | + self.assertEqual(self._MOCK_NEW_UUID, created_id) |
| 388 | + self.assertEqual(req_imms["site"]["coding"], [first_site_coding]) |
| 389 | + self.assertEqual(req_imms["route"]["coding"], [first_route_coding]) |
| 390 | + self.imms_repo.create_immunization.assert_called_once_with(Immunization.parse_obj(req_imms), "Test") |
| 391 | + |
365 | 392 | def test_create_immunization_with_id_throws_error(self): |
366 | 393 | """it should throw exception if id present in create Immunization""" |
367 | 394 | imms = create_covid_immunization_dict("an-id", "9990548609") |
@@ -539,6 +566,39 @@ def test_update_immunization(self): |
539 | 566 | self.assertEqual(call_args[3], "Test") |
540 | 567 | self.authoriser.authorise.assert_called_once_with("Test", ApiOperationCode.UPDATE, {"COVID"}) |
541 | 568 |
|
| 569 | + def test_update_immunization_keeps_first_site_and_route_snomed_coding(self): |
| 570 | + """it should keep the first SNOMED coding for site and route during API update""" |
| 571 | + imms_id = "an-id" |
| 572 | + original_immunisation = create_covid_immunization_dict(imms_id, VALID_NHS_NUMBER) |
| 573 | + identifier = Identifier( |
| 574 | + system=original_immunisation["identifier"][0]["system"], |
| 575 | + value=original_immunisation["identifier"][0]["value"], |
| 576 | + ) |
| 577 | + updated_immunisation = create_covid_immunization_dict(imms_id, VALID_NHS_NUMBER, "2021-02-07T13:28:00+00:00") |
| 578 | + first_site_coding = add_snomed_coding( |
| 579 | + updated_immunisation, "site", "999999999", "Replacement site that should be ignored" |
| 580 | + ) |
| 581 | + first_route_coding = add_snomed_coding( |
| 582 | + updated_immunisation, "route", "888888888", "Replacement route that should be ignored" |
| 583 | + ) |
| 584 | + existing_resource_meta = ImmunizationRecordMetadata( |
| 585 | + identifier=identifier, resource_version=1, is_deleted=False, is_reinstated=False |
| 586 | + ) |
| 587 | + |
| 588 | + self.imms_repo.get_immunization_resource_and_metadata_by_id.return_value = ( |
| 589 | + original_immunisation, |
| 590 | + existing_resource_meta, |
| 591 | + ) |
| 592 | + self.imms_repo.update_immunization.return_value = 2 |
| 593 | + self.authoriser.authorise.return_value = True |
| 594 | + |
| 595 | + updated_version = self.fhir_service.update_immunization(imms_id, updated_immunisation, "Test", 1) |
| 596 | + |
| 597 | + self.assertEqual(updated_version, 2) |
| 598 | + self.assertEqual(updated_immunisation["site"]["coding"], [first_site_coding]) |
| 599 | + self.assertEqual(updated_immunisation["route"]["coding"], [first_route_coding]) |
| 600 | + self.imms_repo.update_immunization.assert_called_once() |
| 601 | + |
542 | 602 | def test_update_immunization_raises_validation_exception_when_nhs_number_invalid(self): |
543 | 603 | """it should raise a CustomValidationError when the patient's NHS number in the payload is invalid""" |
544 | 604 | imms_id = "an-id" |
|
0 commit comments