@@ -644,69 +644,59 @@ def test_decimal_on_create(self):
644644
645645 self .table .put_item .assert_called_once ()
646646
647- # def run_update_immunization_test(self, imms_id, imms, resource, updated_dose_quantity=None):
648- # dynamo_response = {
649- # "ResponseMetadata": {"HTTPStatusCode": 200},
650- # "Attributes": {"Resource": json.dumps(resource)},
651- # }
652- # self.table.update_item = MagicMock(return_value=dynamo_response)
653- # self.table.query = MagicMock(return_value={})
654- # now_epoch = 123456
655- # with patch("time.time") as mock_time:
656- # mock_time.return_value = now_epoch
657- # act_resource, act_version = self.repository.update_immunization(imms_id, imms, self.patient, 1, "Test")
658- # self.assertDictEqual(act_resource, resource)
659- # self.assertEqual(act_version, 2)
660- #
661- # update_exp = (
662- # "SET UpdatedAt = :timestamp, PatientPK = :patient_pk, "
663- # "PatientSK = :patient_sk, #imms_resource = :imms_resource_val, "
664- # "Operation = :operation, Version = :version, SupplierSystem = :supplier_system "
665- # )
666- # patient_id = self.patient["identifier"]["value"]
667- # patient_id = imms["contained"][1]["identifier"][0]["value"]
668- # vaccine_type = get_vaccine_type(imms)
669- # patient_sk = f"{vaccine_type}#{imms_id}"
670- #
671- # self.table.update_item.assert_called_once_with(
672- # Key={"PK": _make_immunization_pk(imms_id)},
673- # UpdateExpression=update_exp,
674- # ExpressionAttributeNames={"#imms_resource": "Resource"},
675- # ExpressionAttributeValues={
676- # ":timestamp": now_epoch,
677- # ":patient_pk": _make_patient_pk(patient_id),
678- # ":patient_sk": patient_sk,
679- # ":imms_resource_val": json.dumps(imms, use_decimal=True),
680- # ":operation": "UPDATE",
681- # ":version": 2,
682- # ":supplier_system": "Test",
683- # },
684- # ReturnValues=ANY,
685- # ConditionExpression=ANY,
686- # )
687- #
688- # if updated_dose_quantity is not None:
689- # imms_resource_val = json.loads(
690- # self.table.update_item.call_args.kwargs["ExpressionAttributeValues"][":imms_resource_val"]
691- # )
692- # self.assertEqual(imms_resource_val["doseQuantity"], updated_dose_quantity)
693- #
694- # def test_decimal_on_update(self):
695- # """it should update record when replacing doseQuantity and keep decimal precision"""
696- # imms_id = "an-imms-id"
697- # imms = create_covid_19_immunization_dict(imms_id)
698- # imms["doseQuantity"] = 1.5556
699- # updated_dose_quantity = 0.7566
700- # imms["doseQuantity"] = updated_dose_quantity
701- # imms["patient"] = self.patient
702- # resource = imms
703- # self.run_update_immunization_test(imms_id, imms, resource, updated_dose_quantity)
704- #
705- # def test_decimal_on_update_patient(self):
706- # """it should update record by replacing both Immunization and Patient and dose quantity"""
707- # imms_id = "an-imms-id"
708- # imms = create_covid_19_immunization_dict(imms_id)
709- # imms["doseQuantity"] = 1.590
710- # imms["patient"] = self.patient
711- # resource = {"doseQuantity": 1.590, "foo": "bar"}
712- # self.run_update_immunization_test(imms_id, imms, resource)
647+ def run_update_immunization_test (self , imms_id , imms , updated_dose_quantity = None ):
648+ dynamo_response = {"ResponseMetadata" : {"HTTPStatusCode" : 200 }}
649+ self .table .update_item = MagicMock (return_value = dynamo_response )
650+ existing_record_metadata = ImmunizationRecordMetadata (resource_version = 1 , is_deleted = False , is_reinstated = False )
651+
652+ # When
653+ updated_version = self .repository .update_immunization (imms_id , imms , existing_record_metadata , "Test" )
654+ self .assertEqual (updated_version , 2 )
655+
656+ update_exp = (
657+ "SET UpdatedAt = :timestamp, PatientPK = :patient_pk, "
658+ "PatientSK = :patient_sk, #imms_resource = :imms_resource_val, "
659+ "Operation = :operation, Version = :version, SupplierSystem = :supplier_system "
660+ )
661+ patient_id = imms ["contained" ][1 ]["identifier" ][0 ]["value" ]
662+ vaccine_type = get_vaccine_type (imms )
663+ patient_sk = f"{ vaccine_type } #{ imms_id } "
664+
665+ self .table .update_item .assert_called_once_with (
666+ Key = {"PK" : _make_immunization_pk (imms_id )},
667+ UpdateExpression = update_exp ,
668+ ExpressionAttributeNames = {"#imms_resource" : "Resource" },
669+ ExpressionAttributeValues = {
670+ ":timestamp" : ANY ,
671+ ":patient_pk" : _make_patient_pk (patient_id ),
672+ ":patient_sk" : patient_sk ,
673+ ":imms_resource_val" : json .dumps (imms , use_decimal = True ),
674+ ":operation" : "UPDATE" ,
675+ ":version" : 2 ,
676+ ":supplier_system" : "Test" ,
677+ },
678+ ConditionExpression = ANY ,
679+ )
680+
681+ if updated_dose_quantity is not None :
682+ imms_resource_val = json .loads (
683+ self .table .update_item .call_args .kwargs ["ExpressionAttributeValues" ][":imms_resource_val" ]
684+ )
685+ self .assertEqual (imms_resource_val ["doseQuantity" ]["value" ], updated_dose_quantity )
686+
687+ def test_decimal_on_update (self ):
688+ """it should update record when replacing doseQuantity and keep decimal precision"""
689+ imms_id = "an-imms-id"
690+ imms = create_covid_19_immunization_dict (imms_id , VALID_NHS_NUMBER )
691+ updated_dose_quantity = 0.7566
692+ imms ["doseQuantity" ]["value" ] = updated_dose_quantity
693+ self .run_update_immunization_test (imms_id , imms , updated_dose_quantity )
694+
695+ def test_decimal_on_update_patient (self ):
696+ """it should update record by replacing both Immunization and Patient and dose quantity"""
697+ imms_id = "an-imms-id"
698+ imms = create_covid_19_immunization_dict (imms_id , VALID_NHS_NUMBER )
699+ updated_dose_quantity = 0.7566
700+ imms ["doseQuantity" ]["value" ] = updated_dose_quantity
701+ imms ["patient" ] = self .patient
702+ self .run_update_immunization_test (imms_id , imms , updated_dose_quantity )
0 commit comments