|
16 | 16 | from fhir_service import FhirService, UpdateOutcome, get_service_url |
17 | 17 | from models.errors import InvalidPatientId, CustomValidationError |
18 | 18 | from models.fhir_immunization import ImmunizationValidator |
| 19 | +from models.utils.generic_utils import get_contained_patient |
19 | 20 | from pds_service import PdsService |
20 | 21 | from pydantic import ValidationError |
21 | 22 | from pydantic.error_wrappers import ErrorWrapper |
|
25 | 26 | create_covid_19_immunization_dict_no_id, |
26 | 27 | VALID_NHS_NUMBER, |
27 | 28 | ) |
28 | | -from utils.generic_utils import load_json_data |
| 29 | +from tests.utils.generic_utils import load_json_data |
29 | 30 | from constants import NHS_NUMBER_USED_IN_SAMPLE_DATA |
30 | 31 |
|
31 | 32 | class TestFhirServiceBase(unittest.TestCase): |
@@ -372,17 +373,15 @@ def test_create_immunization(self): |
372 | 373 |
|
373 | 374 | nhs_number = VALID_NHS_NUMBER |
374 | 375 | req_imms = create_covid_19_immunization_dict_no_id(nhs_number) |
375 | | - |
| 376 | + req_patient = get_contained_patient(req_imms) |
376 | 377 | # When |
377 | 378 | stored_imms = self.fhir_service.create_immunization(req_imms, ["COVID19:create"], "Test") |
378 | 379 |
|
379 | 380 | # Then |
380 | | - self.imms_repo.create_immunization.assert_called_once_with(req_imms, pds_patient, ["COVID19:create"], "Test") |
| 381 | + self.imms_repo.create_immunization.assert_called_once_with(req_imms, req_patient, ["COVID19:create"], "Test") |
381 | 382 |
|
382 | 383 | self.validator.validate.assert_called_once_with(req_imms) |
383 | | - self.fhir_service.pds_service.get_patient_details.assert_called_once_with( |
384 | | - nhs_number |
385 | | - ) |
| 384 | + self.fhir_service.pds_service.get_patient_details.assert_not_called() |
386 | 385 | self.assertIsInstance(stored_imms, Immunization) |
387 | 386 |
|
388 | 387 | def test_create_immunization_with_id(self): |
@@ -469,6 +468,20 @@ def test_patient_error(self): |
469 | 468 | self.assertEqual(e.exception.patient_identifier, invalid_nhs_number) |
470 | 469 | self.imms_repo.create_immunization.assert_not_called() |
471 | 470 |
|
| 471 | + def test_patient_error_invalid_nhs_number(self): |
| 472 | + """it should throw error when NHS number checksum is incorrect""" |
| 473 | + self.fhir_service.pds_service.get_patient_details.return_value = None |
| 474 | + invalid_nhs_number = "9434765911" # check digit 1 doesn't match result (9) |
| 475 | + bad_patient_imms = create_covid_19_immunization_dict_no_id(invalid_nhs_number) |
| 476 | + |
| 477 | + with self.assertRaises(InvalidPatientId) as e: |
| 478 | + # When |
| 479 | + self.fhir_service.create_immunization(bad_patient_imms, "COVID19:create", "Test") |
| 480 | + |
| 481 | + # Then |
| 482 | + self.assertEqual(e.exception.patient_identifier, invalid_nhs_number) |
| 483 | + self.imms_repo.create_immunization.assert_not_called() |
| 484 | + |
472 | 485 | @patch.dict(os.environ, {"PDS_CHECK_ENABLED": "false"}, False) |
473 | 486 | def test_pds_check_skipped(self): |
474 | 487 | bad_patient_imms = create_covid_19_immunization_dict_no_id("a-bad-patient-id") |
@@ -500,14 +513,15 @@ def test_update_immunization(self): |
500 | 513 |
|
501 | 514 | nhs_number = VALID_NHS_NUMBER |
502 | 515 | req_imms = create_covid_19_immunization_dict(imms_id, nhs_number) |
| 516 | + req_patient = get_contained_patient(req_imms) |
503 | 517 |
|
504 | 518 | # When |
505 | 519 | outcome, _, _ = self.fhir_service.update_immunization(imms_id, req_imms, 1, ["COVID19.CRUD"], "Test") |
506 | 520 |
|
507 | 521 | # Then |
508 | 522 | self.assertEqual(outcome, UpdateOutcome.UPDATE) |
509 | | - self.imms_repo.update_immunization.assert_called_once_with(imms_id, req_imms, pds_patient, 1,["COVID19.CRUD"], "Test") |
510 | | - self.fhir_service.pds_service.get_patient_details.assert_called_once_with(nhs_number) |
| 523 | + self.imms_repo.update_immunization.assert_called_once_with(imms_id, req_imms, req_patient, 1,["COVID19.CRUD"], "Test") |
| 524 | + self.fhir_service.pds_service.get_patient_details.assert_not_called() |
511 | 525 |
|
512 | 526 | def test_id_not_present(self): |
513 | 527 | """it should populate id in the message if it is not present""" |
@@ -539,7 +553,22 @@ def test_patient_error(self): |
539 | 553 | # Then |
540 | 554 | self.assertEqual(e.exception.patient_identifier, invalid_nhs_number) |
541 | 555 | self.imms_repo.update_immunization.assert_not_called() |
542 | | - |
| 556 | + |
| 557 | + def test_patient_error_invalid_nhs_number(self): |
| 558 | + """it should throw error when NHS number checksum is incorrect""" |
| 559 | + self.fhir_service.pds_service.get_patient_details.return_value = None |
| 560 | + imms_id = "an-id" |
| 561 | + invalid_nhs_number = "9434765911" # check digit 1 doesn't match result (9) |
| 562 | + bad_patient_imms = create_covid_19_immunization_dict(imms_id, invalid_nhs_number) |
| 563 | + |
| 564 | + with self.assertRaises(InvalidPatientId) as e: |
| 565 | + # When |
| 566 | + self.fhir_service.update_immunization(imms_id, bad_patient_imms, 1, ["COVID19:update"], "Test") |
| 567 | + |
| 568 | + # Then |
| 569 | + self.assertEqual(e.exception.patient_identifier, invalid_nhs_number) |
| 570 | + self.imms_repo.update_immunization.assert_not_called() |
| 571 | + |
543 | 572 | def test_reinstate_immunization_returns_updated_version(self): |
544 | 573 | """it should return updated version from reinstate""" |
545 | 574 | imms_id = "an-id" |
|
0 commit comments