Skip to content

Commit 1c43086

Browse files
committed
Add unit tests
1 parent a782fa2 commit 1c43086

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

lambdas/recordforwarder/tests/repository/test_fhir_batch_repository.py

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import boto3
77
import botocore.exceptions
88
import simplejson as json
9+
from boto3.dynamodb.conditions import Key
910
from moto import mock_aws
1011

1112
from common.models.errors import (
@@ -138,6 +139,53 @@ def test_create_immunization_conditionalcheckfailedexception_error(self):
138139
with self.assertRaises(ResourceFoundError):
139140
self.repository.create_immunization(self.immunization, "supplier", "vax-type", self.table, False)
140141

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+
141189

142190
class TestUpdateImmunization(TestImmunizationBatchRepository):
143191
def test_update_immunization(self):
@@ -300,6 +348,63 @@ def test_update_immunization_conditionalcheckfailedexception_error(self):
300348
)
301349
self.repository.update_immunization(self.immunization, "supplier", "vax-type", self.table, False)
302350

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+
303408

304409
class TestDeleteImmunization(TestImmunizationBatchRepository):
305410
def test_delete_immunization(self):
@@ -413,6 +518,58 @@ def test_delete_immunization_conditionalcheckfailedexception_error(self):
413518
)
414519
self.repository.delete_immunization(self.immunization, "supplier", "vax-type", self.table, False)
415520

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+
416573

417574
@mock_aws
418575
@patch.dict(os.environ, {"DYNAMODB_TABLE_NAME": "TestTable"})

0 commit comments

Comments
 (0)