Skip to content

Commit 7e2e42a

Browse files
committed
init: PoC
1 parent 8adc7f4 commit 7e2e42a

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

backend/src/fhir_service.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,15 @@ def _validate_patient(self, imms: dict) -> dict:
362362
logger.warning("Skipping PDS check")
363363
return contained_patient
364364

365+
# JW: PoC
366+
# the above pre-validation code is valid I think, it's just a question of
367+
# removing the PDS call below
368+
# TODO: we then have to tweak the tests, possibly write new ones
369+
# -
370+
if nhs_number_mod11_check(nhs_number):
371+
return contained_patient
372+
373+
'''
365374
patient = self.pds_service.get_patient_details(nhs_number)
366375
# To check whether the Superseded NHS number present in PDS
367376
if patient:
@@ -371,5 +380,6 @@ def _validate_patient(self, imms: dict) -> dict:
371380
return diagnostics_error
372381
373382
return patient
383+
'''
374384

375385
raise InvalidPatientId(patient_identifier=nhs_number)

backend/tests/test_fhir_service.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from fhir_service import FhirService, UpdateOutcome, get_service_url
1717
from models.errors import InvalidPatientId, CustomValidationError
1818
from models.fhir_immunization import ImmunizationValidator
19+
from models.utils.generic_utils import get_contained_patient
1920
from pds_service import PdsService
2021
from pydantic import ValidationError
2122
from pydantic.error_wrappers import ErrorWrapper
@@ -25,7 +26,7 @@
2526
create_covid_19_immunization_dict_no_id,
2627
VALID_NHS_NUMBER,
2728
)
28-
from utils.generic_utils import load_json_data
29+
from tests.utils.generic_utils import load_json_data
2930
from constants import NHS_NUMBER_USED_IN_SAMPLE_DATA
3031

3132
class TestFhirServiceBase(unittest.TestCase):
@@ -372,17 +373,15 @@ def test_create_immunization(self):
372373

373374
nhs_number = VALID_NHS_NUMBER
374375
req_imms = create_covid_19_immunization_dict_no_id(nhs_number)
375-
376+
req_patient = get_contained_patient(req_imms)
376377
# When
377378
stored_imms = self.fhir_service.create_immunization(req_imms, ["COVID19:create"], "Test")
378379

379380
# 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")
381382

382383
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()
386385
self.assertIsInstance(stored_imms, Immunization)
387386

388387
def test_create_immunization_with_id(self):
@@ -469,6 +468,20 @@ def test_patient_error(self):
469468
self.assertEqual(e.exception.patient_identifier, invalid_nhs_number)
470469
self.imms_repo.create_immunization.assert_not_called()
471470

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+
472485
@patch.dict(os.environ, {"PDS_CHECK_ENABLED": "false"}, False)
473486
def test_pds_check_skipped(self):
474487
bad_patient_imms = create_covid_19_immunization_dict_no_id("a-bad-patient-id")
@@ -500,14 +513,15 @@ def test_update_immunization(self):
500513

501514
nhs_number = VALID_NHS_NUMBER
502515
req_imms = create_covid_19_immunization_dict(imms_id, nhs_number)
516+
req_patient = get_contained_patient(req_imms)
503517

504518
# When
505519
outcome, _, _ = self.fhir_service.update_immunization(imms_id, req_imms, 1, ["COVID19.CRUD"], "Test")
506520

507521
# Then
508522
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()
511525

512526
def test_id_not_present(self):
513527
"""it should populate id in the message if it is not present"""
@@ -539,7 +553,22 @@ def test_patient_error(self):
539553
# Then
540554
self.assertEqual(e.exception.patient_identifier, invalid_nhs_number)
541555
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+
543572
def test_reinstate_immunization_returns_updated_version(self):
544573
"""it should return updated version from reinstate"""
545574
imms_id = "an-id"

0 commit comments

Comments
 (0)