|
6 | 6 | import boto3 |
7 | 7 | import botocore.exceptions |
8 | 8 | import simplejson as json |
| 9 | +from boto3.dynamodb.conditions import Key |
9 | 10 | from moto import mock_aws |
10 | 11 |
|
11 | 12 | from common.models.errors import ( |
@@ -138,6 +139,53 @@ def test_create_immunization_conditionalcheckfailedexception_error(self): |
138 | 139 | with self.assertRaises(ResourceFoundError): |
139 | 140 | self.repository.create_immunization(self.immunization, "supplier", "vax-type", self.table, False) |
140 | 141 |
|
| 142 | + def create_immunization_uses_separate_identifier_pk_for_legacy_identifiers_test_logic( |
| 143 | + self, legacy_identifier_system: str, new_identifier_system: str |
| 144 | + ): |
| 145 | + """it should use a new identifier_pk for a legacy identifier""" |
| 146 | + self.mock_redis_getter.return_value = self.mock_redis |
| 147 | + |
| 148 | + legacy_immunization = create_covid_immunization_dict(imms_id) |
| 149 | + legacy_immunization["identifier"][0]["system"] = legacy_identifier_system |
| 150 | + |
| 151 | + self.repository.create_immunization(legacy_immunization, "supplier", "vax-type", self.table, False) |
| 152 | + item = self.table.put_item.call_args.kwargs["Item"] |
| 153 | + |
| 154 | + self.table.query.assert_called_with( |
| 155 | + IndexName="IdentifierGSI", |
| 156 | + KeyConditionExpression=Key("IdentifierPK").eq(new_identifier_system + "#" + "ACME-vacc123456"), |
| 157 | + Limit=1, |
| 158 | + ) |
| 159 | + |
| 160 | + self.table.put_item.assert_called_with( |
| 161 | + Item={ |
| 162 | + "PK": ANY, |
| 163 | + "PatientPK": ANY, |
| 164 | + "PatientSK": ANY, |
| 165 | + "Resource": json.dumps( |
| 166 | + legacy_immunization, use_decimal=True |
| 167 | + ), # resource should contain legacy identifier |
| 168 | + "IdentifierPK": new_identifier_system + "#" + "ACME-vacc123456", |
| 169 | + "Operation": "CREATE", |
| 170 | + "Version": 1, |
| 171 | + "SupplierSystem": "supplier", |
| 172 | + }, |
| 173 | + ConditionExpression=ANY, |
| 174 | + ) |
| 175 | + self.assertEqual(item["PK"], f"Immunization#{legacy_immunization['id']}") |
| 176 | + |
| 177 | + def test_create_immunization_uses_separate_identifier_pk_for_legacy_identifiers_tpp(self): |
| 178 | + """it should use a V5 identifier_pk for a legacy TPP identifier""" |
| 179 | + self.create_immunization_uses_separate_identifier_pk_for_legacy_identifiers_test_logic( |
| 180 | + "YGA", "https://tpp-uk.com/Id/ve/vacc" |
| 181 | + ) |
| 182 | + |
| 183 | + def test_create_immunization_uses_separate_identifier_pk_for_legacy_identifiers_emis(self): |
| 184 | + """it should use a V5 identifier_pk for a legacy EMIS identifier""" |
| 185 | + self.create_immunization_uses_separate_identifier_pk_for_legacy_identifiers_test_logic( |
| 186 | + "YGJ", "https://emishealth.com/identifiers/vacc" |
| 187 | + ) |
| 188 | + |
141 | 189 |
|
142 | 190 | class TestUpdateImmunization(TestImmunizationBatchRepository): |
143 | 191 | def test_update_immunization(self): |
@@ -300,6 +348,63 @@ def test_update_immunization_conditionalcheckfailedexception_error(self): |
300 | 348 | ) |
301 | 349 | self.repository.update_immunization(self.immunization, "supplier", "vax-type", self.table, False) |
302 | 350 |
|
| 351 | + def update_immunization_uses_separate_identifier_pk_for_legacy_identifiers_test_logic( |
| 352 | + self, legacy_identifier_system: str, new_identifier_system: str |
| 353 | + ): |
| 354 | + """it should use a new identifier_pk for a legacy identifier""" |
| 355 | + legacy_immunization = create_covid_immunization_dict(imms_id) |
| 356 | + legacy_immunization["identifier"][0]["system"] = legacy_identifier_system |
| 357 | + |
| 358 | + self.table.query = MagicMock( |
| 359 | + return_value={ |
| 360 | + "Count": 1, |
| 361 | + "Items": [ |
| 362 | + { |
| 363 | + "PK": _make_immunization_pk(imms_id), |
| 364 | + "Resource": json.dumps(legacy_immunization), |
| 365 | + "Version": 1, |
| 366 | + } |
| 367 | + ], |
| 368 | + } |
| 369 | + ) |
| 370 | + |
| 371 | + self.repository.update_immunization(legacy_immunization, "supplier", "vax-type", self.table, True) |
| 372 | + |
| 373 | + self.table.query.assert_called_with( |
| 374 | + IndexName="IdentifierGSI", |
| 375 | + KeyConditionExpression=Key("IdentifierPK").eq(new_identifier_system + "#" + "ACME-vacc123456"), |
| 376 | + Limit=1, |
| 377 | + ) |
| 378 | + |
| 379 | + expected_values = { |
| 380 | + ":timestamp": ANY, |
| 381 | + ":patient_pk": ANY, |
| 382 | + ":patient_sk": ANY, |
| 383 | + ":imms_resource_val": json.dumps(legacy_immunization), # resource should contain legacy identifier |
| 384 | + ":operation": "UPDATE", |
| 385 | + ":version": 2, |
| 386 | + ":supplier_system": "supplier", |
| 387 | + } |
| 388 | + |
| 389 | + self.table.update_item.assert_called_with( |
| 390 | + Key={"PK": _make_immunization_pk(imms_id)}, |
| 391 | + UpdateExpression=ANY, |
| 392 | + ExpressionAttributeNames={"#imms_resource": "Resource"}, |
| 393 | + ExpressionAttributeValues=expected_values, |
| 394 | + ReturnValues=ANY, |
| 395 | + ConditionExpression=ANY, |
| 396 | + ) |
| 397 | + |
| 398 | + def test_update_immunization_uses_separate_identifier_pk_for_legacy_identifiers_tpp(self): |
| 399 | + self.update_immunization_uses_separate_identifier_pk_for_legacy_identifiers_test_logic( |
| 400 | + "YGA", "https://tpp-uk.com/Id/ve/vacc" |
| 401 | + ) |
| 402 | + |
| 403 | + def test_update_immunization_uses_separate_identifier_pk_for_legacy_identifiers_emis(self): |
| 404 | + self.update_immunization_uses_separate_identifier_pk_for_legacy_identifiers_test_logic( |
| 405 | + "YGJ", "https://emishealth.com/identifiers/vacc" |
| 406 | + ) |
| 407 | + |
303 | 408 |
|
304 | 409 | class TestDeleteImmunization(TestImmunizationBatchRepository): |
305 | 410 | def test_delete_immunization(self): |
@@ -413,6 +518,58 @@ def test_delete_immunization_conditionalcheckfailedexception_error(self): |
413 | 518 | ) |
414 | 519 | self.repository.delete_immunization(self.immunization, "supplier", "vax-type", self.table, False) |
415 | 520 |
|
| 521 | + def delete_immunization_uses_separate_identifier_pk_for_legacy_identifiers_test_logic( |
| 522 | + self, legacy_identifier_system: str, new_identifier_system: str |
| 523 | + ): |
| 524 | + """it should use a new identifier_pk for a legacy identifier""" |
| 525 | + legacy_immunization = create_covid_immunization_dict(imms_id) |
| 526 | + legacy_immunization["identifier"][0]["system"] = legacy_identifier_system |
| 527 | + |
| 528 | + self.table.query = MagicMock( |
| 529 | + return_value={ |
| 530 | + "Count": 1, |
| 531 | + "Items": [ |
| 532 | + { |
| 533 | + "PK": _make_immunization_pk(imms_id), |
| 534 | + "Resource": json.dumps(legacy_immunization), |
| 535 | + "Version": 1, |
| 536 | + } |
| 537 | + ], |
| 538 | + } |
| 539 | + ) |
| 540 | + |
| 541 | + response = self.repository.delete_immunization(legacy_immunization, "supplier", "vax-type", self.table, True) |
| 542 | + |
| 543 | + self.table.query.assert_called_with( |
| 544 | + IndexName="IdentifierGSI", |
| 545 | + KeyConditionExpression=Key("IdentifierPK").eq(new_identifier_system + "#" + "ACME-vacc123456"), |
| 546 | + Limit=1, |
| 547 | + ) |
| 548 | + |
| 549 | + self.table.update_item.assert_called_with( |
| 550 | + Key={"PK": _make_immunization_pk(imms_id)}, |
| 551 | + UpdateExpression="SET DeletedAt = :timestamp, Operation = :operation, SupplierSystem = :supplier_system", |
| 552 | + ExpressionAttributeValues={ |
| 553 | + ":timestamp": ANY, |
| 554 | + ":operation": "DELETE", |
| 555 | + ":supplier_system": "supplier", |
| 556 | + }, |
| 557 | + ReturnValues=ANY, |
| 558 | + ConditionExpression=ANY, |
| 559 | + ) |
| 560 | + |
| 561 | + self.assertEqual(response, f"Immunization#{self.immunization['id']}") |
| 562 | + |
| 563 | + def test_delete_immunization_uses_separate_identifier_pk_for_legacy_identifiers_tpp(self): |
| 564 | + self.delete_immunization_uses_separate_identifier_pk_for_legacy_identifiers_test_logic( |
| 565 | + "YGA", "https://tpp-uk.com/Id/ve/vacc" |
| 566 | + ) |
| 567 | + |
| 568 | + def test_delete_immunization_uses_separate_identifier_pk_for_legacy_identifiers_emis(self): |
| 569 | + self.delete_immunization_uses_separate_identifier_pk_for_legacy_identifiers_test_logic( |
| 570 | + "YGJ", "https://emishealth.com/identifiers/vacc" |
| 571 | + ) |
| 572 | + |
416 | 573 |
|
417 | 574 | @mock_aws |
418 | 575 | @patch.dict(os.environ, {"DYNAMODB_TABLE_NAME": "TestTable"}) |
|
0 commit comments