3636NHS_NUMBER_USED_IN_SAMPLE_DATA = "9000000009"
3737
3838
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
39+ def add_duplicate_snomed_with_leading_non_snomed_coding (
40+ immunization : dict , field_name : str , code : str , display : str
41+ ) -> list :
42+ first_snomed_coding = deepcopy (immunization [field_name ]["coding" ][0 ])
43+ leading_non_snomed_coding = deepcopy (first_snomed_coding )
44+ leading_non_snomed_coding ["system" ] = "http://snomed.info/test"
45+ duplicate_snomed_coding = {** first_snomed_coding , "code" : code , "display" : display }
46+ immunization [field_name ]["coding" ] = [
47+ leading_non_snomed_coding ,
48+ first_snomed_coding ,
49+ duplicate_snomed_coding ,
50+ ]
51+ return deepcopy (immunization [field_name ]["coding" ])
4352
4453
4554class TestFhirServiceBase (unittest .TestCase ):
@@ -354,6 +363,7 @@ def test_create_immunization(self):
354363
355364 nhs_number = VALID_NHS_NUMBER
356365 req_imms = create_covid_immunization_dict_no_id (nhs_number )
366+ expected_validated_imms = deepcopy (req_imms )
357367
358368 # When
359369 created_id , created_version = self .fhir_service .create_immunization (req_imms , "Test" )
@@ -366,30 +376,31 @@ def test_create_immunization(self):
366376 self .assertEqual (create_identifier .value , "ACME-vacc123456" )
367377 self .imms_repo .create_immunization .assert_called_once_with (Immunization .parse_obj (req_imms ), "Test" )
368378
369- self .validator .validate .assert_called_once_with (req_imms )
379+ self .validator .validate .assert_called_once_with (expected_validated_imms )
370380 self .assertEqual (self ._MOCK_NEW_UUID , created_id )
371381 self .assertEqual (1 , created_version )
372382
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"""
383+ def test_create_immunization_persists_all_site_and_route_codings (self ):
384+ """it should validate against the first SNOMED coding and persist all codings during API create"""
375385 self .mock_redis .hget .return_value = "COVID"
376386 self .mock_redis_getter .return_value = self .mock_redis
377387 self .authoriser .authorise .return_value = True
378388 self .imms_repo .get_immunization_by_identifier .return_value = (None , None )
379389 self .imms_repo .create_immunization .return_value = self ._MOCK_NEW_UUID
380390
381391 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 (
392+ expected_site_codings = add_duplicate_snomed_with_leading_non_snomed_coding (
393+ req_imms , "site" , "999999999" , "Replacement site that should be ignored"
394+ )
395+ expected_route_codings = add_duplicate_snomed_with_leading_non_snomed_coding (
384396 req_imms , "route" , "888888888" , "Replacement route that should be ignored"
385397 )
386398
387399 created_id , created_version = self .pre_validate_fhir_service .create_immunization (req_imms , "Test" )
388400
389401 self .assertEqual (self ._MOCK_NEW_UUID , created_id )
390- self .assertEqual (1 , created_version )
391- self .assertEqual (req_imms ["site" ]["coding" ], [first_site_coding ])
392- self .assertEqual (req_imms ["route" ]["coding" ], [first_route_coding ])
402+ self .assertEqual (req_imms ["site" ]["coding" ], expected_site_codings )
403+ self .assertEqual (req_imms ["route" ]["coding" ], expected_route_codings )
393404 self .imms_repo .create_immunization .assert_called_once_with (Immunization .parse_obj (req_imms ), "Test" )
394405
395406 def test_create_immunization_with_id_throws_error (self ):
@@ -511,6 +522,7 @@ def test_raises_duplicate_error_if_identifier_already_exits(self):
511522
512523 nhs_number = VALID_NHS_NUMBER
513524 req_imms = create_covid_immunization_dict_no_id (nhs_number )
525+ expected_validated_imms = deepcopy (req_imms )
514526
515527 # When
516528 with self .assertRaises (IdentifierDuplicationError ) as error :
@@ -523,8 +535,7 @@ def test_raises_duplicate_error_if_identifier_already_exits(self):
523535 self .assertEqual (duplicate_identifier .system , "https://supplierABC/identifiers/vacc" )
524536 self .assertEqual (duplicate_identifier .value , "ACME-vacc123456" )
525537 self .imms_repo .create_immunization .assert_not_called ()
526- self .imms_repo .update_immunization .assert_not_called ()
527- self .validator .validate .assert_called_once_with (req_imms )
538+ self .validator .validate .assert_called_once_with (expected_validated_imms )
528539 self .assertEqual (
529540 "The provided identifier: https://supplierABC/identifiers/vacc#ACME-vacc123456 is duplicated" ,
530541 str (error .exception ),
@@ -614,19 +625,19 @@ def test_update_immunization(self):
614625 self .assertEqual (call_args [3 ], "Test" )
615626 self .authoriser .authorise .assert_called_once_with ("Test" , ApiOperationCode .UPDATE , {"COVID" })
616627
617- def test_update_immunization_keeps_first_site_and_route_snomed_coding (self ):
618- """it should keep the first SNOMED coding for site and route during API update"""
628+ def test_update_immunization_persists_all_site_and_route_codings (self ):
629+ """it should validate against the first SNOMED coding and persist all codings during API update"""
619630 imms_id = "an-id"
620631 original_immunisation = create_covid_immunization_dict (imms_id , VALID_NHS_NUMBER )
621632 identifier = Identifier (
622633 system = original_immunisation ["identifier" ][0 ]["system" ],
623634 value = original_immunisation ["identifier" ][0 ]["value" ],
624635 )
625636 updated_immunisation = create_covid_immunization_dict (imms_id , VALID_NHS_NUMBER , "2021-02-07T13:28:00+00:00" )
626- first_site_coding = add_snomed_coding (
637+ expected_site_codings = add_duplicate_snomed_with_leading_non_snomed_coding (
627638 updated_immunisation , "site" , "999999999" , "Replacement site that should be ignored"
628639 )
629- first_route_coding = add_snomed_coding (
640+ expected_route_codings = add_duplicate_snomed_with_leading_non_snomed_coding (
630641 updated_immunisation , "route" , "888888888" , "Replacement route that should be ignored"
631642 )
632643 existing_resource_meta = ImmunizationRecordMetadata (
@@ -643,9 +654,11 @@ def test_update_immunization_keeps_first_site_and_route_snomed_coding(self):
643654 updated_version = self .fhir_service .update_immunization (imms_id , updated_immunisation , "Test" , 1 )
644655
645656 self .assertEqual (updated_version , 2 )
646- self .assertEqual (updated_immunisation ["site" ]["coding" ], [ first_site_coding ] )
647- self .assertEqual (updated_immunisation ["route" ]["coding" ], [ first_route_coding ] )
657+ self .assertEqual (updated_immunisation ["site" ]["coding" ], expected_site_codings )
658+ self .assertEqual (updated_immunisation ["route" ]["coding" ], expected_route_codings )
648659 self .imms_repo .update_immunization .assert_called_once ()
660+ call_args = self .imms_repo .update_immunization .call_args [0 ]
661+ self .assertEqual (call_args [1 ], Immunization .parse_obj (updated_immunisation ))
649662
650663 def test_update_immunization_raises_validation_exception_when_nhs_number_invalid (self ):
651664 """it should raise a CustomValidationError when the patient's NHS number in the payload is invalid"""
0 commit comments