|
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 |
|
@@ -364,6 +370,27 @@ def test_create_immunization(self): |
364 | 370 | self.assertEqual(self._MOCK_NEW_UUID, created_id) |
365 | 371 | self.assertEqual(1, created_version) |
366 | 372 |
|
| 373 | + def test_create_immunization_keeps_first_site_and_route_snomed_coding(self): |
| 374 | + """it should keep the first SNOMED coding for site and route during API create""" |
| 375 | + self.mock_redis.hget.return_value = "COVID" |
| 376 | + self.mock_redis_getter.return_value = self.mock_redis |
| 377 | + self.authoriser.authorise.return_value = True |
| 378 | + self.imms_repo.check_immunization_identifier_exists.return_value = False |
| 379 | + self.imms_repo.create_immunization.return_value = self._MOCK_NEW_UUID |
| 380 | + |
| 381 | + req_imms = create_covid_immunization_dict_no_id(VALID_NHS_NUMBER) |
| 382 | + first_site_coding = add_snomed_coding(req_imms, "site", "999999999", "Replacement site that should be ignored") |
| 383 | + first_route_coding = add_snomed_coding( |
| 384 | + req_imms, "route", "888888888", "Replacement route that should be ignored" |
| 385 | + ) |
| 386 | + |
| 387 | + created_id = self.pre_validate_fhir_service.create_immunization(req_imms, "Test") |
| 388 | + |
| 389 | + self.assertEqual(self._MOCK_NEW_UUID, created_id) |
| 390 | + self.assertEqual(req_imms["site"]["coding"], [first_site_coding]) |
| 391 | + self.assertEqual(req_imms["route"]["coding"], [first_route_coding]) |
| 392 | + self.imms_repo.create_immunization.assert_called_once_with(Immunization.parse_obj(req_imms), "Test") |
| 393 | + |
367 | 394 | def test_create_immunization_with_id_throws_error(self): |
368 | 395 | """it should throw exception if id present in create Immunization""" |
369 | 396 | imms = create_covid_immunization_dict("an-id", "9990548609") |
@@ -586,6 +613,39 @@ def test_update_immunization(self): |
586 | 613 | self.assertEqual(call_args[3], "Test") |
587 | 614 | self.authoriser.authorise.assert_called_once_with("Test", ApiOperationCode.UPDATE, {"COVID"}) |
588 | 615 |
|
| 616 | + def test_update_immunization_keeps_first_site_and_route_snomed_coding(self): |
| 617 | + """it should keep the first SNOMED coding for site and route during API update""" |
| 618 | + imms_id = "an-id" |
| 619 | + original_immunisation = create_covid_immunization_dict(imms_id, VALID_NHS_NUMBER) |
| 620 | + identifier = Identifier( |
| 621 | + system=original_immunisation["identifier"][0]["system"], |
| 622 | + value=original_immunisation["identifier"][0]["value"], |
| 623 | + ) |
| 624 | + updated_immunisation = create_covid_immunization_dict(imms_id, VALID_NHS_NUMBER, "2021-02-07T13:28:00+00:00") |
| 625 | + first_site_coding = add_snomed_coding( |
| 626 | + updated_immunisation, "site", "999999999", "Replacement site that should be ignored" |
| 627 | + ) |
| 628 | + first_route_coding = add_snomed_coding( |
| 629 | + updated_immunisation, "route", "888888888", "Replacement route that should be ignored" |
| 630 | + ) |
| 631 | + existing_resource_meta = ImmunizationRecordMetadata( |
| 632 | + identifier=identifier, resource_version=1, is_deleted=False, is_reinstated=False |
| 633 | + ) |
| 634 | + |
| 635 | + self.imms_repo.get_immunization_resource_and_metadata_by_id.return_value = ( |
| 636 | + original_immunisation, |
| 637 | + existing_resource_meta, |
| 638 | + ) |
| 639 | + self.imms_repo.update_immunization.return_value = 2 |
| 640 | + self.authoriser.authorise.return_value = True |
| 641 | + |
| 642 | + updated_version = self.fhir_service.update_immunization(imms_id, updated_immunisation, "Test", 1) |
| 643 | + |
| 644 | + self.assertEqual(updated_version, 2) |
| 645 | + self.assertEqual(updated_immunisation["site"]["coding"], [first_site_coding]) |
| 646 | + self.assertEqual(updated_immunisation["route"]["coding"], [first_route_coding]) |
| 647 | + self.imms_repo.update_immunization.assert_called_once() |
| 648 | + |
589 | 649 | def test_update_immunization_raises_validation_exception_when_nhs_number_invalid(self): |
590 | 650 | """it should raise a CustomValidationError when the patient's NHS number in the payload is invalid""" |
591 | 651 | imms_id = "an-id" |
|
0 commit comments