Skip to content

Commit 84c3266

Browse files
committed
interim - for merge II
1 parent 5399174 commit 84c3266

File tree

10 files changed

+64
-47
lines changed

10 files changed

+64
-47
lines changed

lambdas/backend/src/controller/aws_apig_event_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from aws_lambda_typing.events import APIGatewayProxyEventV1
66

77
from controller.constants import E_TAG_HEADER_NAME, SUPPLIER_SYSTEM_HEADER_NAME
8-
from models.errors import ResourceVersionNotProvided, UnauthorizedError
8+
from common.models.errors import ResourceVersionNotProvided, UnauthorizedError
99
from utils import dict_utils
1010

1111

lambdas/backend/src/controller/fhir_api_exception_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from clients import logger
88
from constants import GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE
99
from controller.aws_apig_response_utils import create_response
10-
from models.errors import (
10+
from common.models.errors import (
1111
Code,
1212
CustomValidationError,
1313
IdentifierDuplicationError,

lambdas/backend/src/controller/fhir_controller.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from controller.aws_apig_response_utils import create_response
1919
from controller.constants import E_TAG_HEADER_NAME
2020
from controller.fhir_api_exception_handler import fhir_api_exception_handler
21-
from models.errors import (
21+
from common.models.errors import (
2222
Code,
2323
InconsistentIdError,
2424
InvalidImmunizationId,
@@ -30,7 +30,7 @@
3030
UnauthorizedVaxError,
3131
create_operation_outcome,
3232
)
33-
from models.utils.generic_utils import check_keys_in_sources
33+
from common.models.utils.generic_utils import check_keys_in_sources
3434
from parameter_parser import create_query_string, process_params, process_search_params
3535
from repository.fhir_repository import ImmunizationRepository, create_table
3636
from service.fhir_service import FhirService, get_service_url

lambdas/backend/src/repository/fhir_repository.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
from mypy_boto3_dynamodb.service_resource import DynamoDBServiceResource, Table
1414
from responses import logger
1515

16-
from models.constants import Constants
17-
from models.errors import (
16+
from common.models.constants import Constants
17+
from common.models.errors import (
1818
ResourceNotFoundError,
1919
UnhandledResponseError,
2020
)
21-
from models.immunization_record_metadata import ImmunizationRecordMetadata
22-
from models.utils.generic_utils import get_contained_patient
23-
from models.utils.validation_utils import (
21+
from common.models.immunization_record_metadata import ImmunizationRecordMetadata
22+
from common.models.utils.generic_utils import get_contained_patient
23+
from common.models.utils.validation_utils import (
2424
get_vaccine_type,
2525
)
2626

lambdas/backend/src/service/fhir_service.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@
2222
from authorisation.api_operation_code import ApiOperationCode
2323
from authorisation.authoriser import Authoriser
2424
from filter import Filter
25-
from models.errors import (
25+
from common.models.errors import (
2626
CustomValidationError,
2727
IdentifierDuplicationError,
2828
MandatoryError,
2929
ResourceNotFoundError,
3030
UnauthorizedVaxError,
3131
)
32-
from models.fhir_immunization import ImmunizationValidator
33-
from models.utils.generic_utils import (
32+
from common.models.fhir_immunization import ImmunizationValidator
33+
from common.models.utils.generic_utils import (
3434
form_json,
3535
get_contained_patient,
3636
get_occurrence_datetime,
3737
)
38-
from models.utils.validation_utils import get_vaccine_type, validate_identifiers_match, validate_resource_versions_match
38+
from common.models.utils.validation_utils import get_vaccine_type, validate_identifiers_match, validate_resource_versions_match
3939
from repository.fhir_repository import ImmunizationRepository
4040

4141
logging.basicConfig(level="INFO")

lambdas/backend/tests/controller/test_fhir_api_exception_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from unittest.mock import patch
44

55
from controller.fhir_api_exception_handler import fhir_api_exception_handler
6-
from models.errors import (
6+
from common.models.errors import (
77
CustomValidationError,
88
IdentifierDuplicationError,
99
InconsistentIdentifierError,

lambdas/backend/tests/controller/test_fhir_controller.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from controller.aws_apig_response_utils import create_response
1414
from controller.fhir_controller import FhirController
15-
from models.errors import (
15+
from common.models.errors import (
1616
CustomValidationError,
1717
ParameterException,
1818
ResourceNotFoundError,
@@ -31,13 +31,14 @@ class TestFhirControllerBase(unittest.TestCase):
3131

3232
def setUp(self):
3333
super().setUp()
34-
self.redis_patcher = patch("parameter_parser.redis_client")
35-
self.mock_redis_client = self.redis_patcher.start()
34+
self.mock_redis = Mock()
35+
self.redis_getter_patcher = patch("parameter_parser.get_redis_client")
36+
self.mock_redis_getter = self.redis_getter_patcher.start()
3637
self.logger_info_patcher = patch("logging.Logger.info")
3738
self.mock_logger_info = self.logger_info_patcher.start()
3839

3940
def tearDown(self):
40-
self.redis_patcher.stop()
41+
self.redis_getter_patcher.stop()
4142
self.logger_info_patcher.stop()
4243
super().tearDown()
4344

@@ -1270,7 +1271,8 @@ def setUp(self):
12701271
self.date_to_key = "-date.to"
12711272
self.nhs_number_valid_value = "9000000009"
12721273
self.patient_identifier_valid_value = f"{patient_identifier_system}|{self.nhs_number_valid_value}"
1273-
self.mock_redis_client.hkeys.return_value = self.MOCK_REDIS_V2D_HKEYS
1274+
self.mock_redis.hkeys.return_value = self.MOCK_REDIS_V2D_HKEYS
1275+
self.mock_redis_getter.return_value = self.mock_redis
12741276

12751277
def test_get_search_immunizations(self):
12761278
"""it should search based on patient_identifier and immunization_target"""
@@ -1537,7 +1539,8 @@ def test_post_search_immunizations_for_unauthorized_vaccine_type_search_403(self
15371539

15381540
@patch("controller.fhir_controller.process_search_params", wraps=process_search_params)
15391541
def test_uses_parameter_parser(self, process_search_params: Mock):
1540-
self.mock_redis_client.hkeys.return_value = self.MOCK_REDIS_V2D_HKEYS
1542+
self.mock_redis.hkeys.return_value = self.MOCK_REDIS_V2D_HKEYS
1543+
self.mock_redis_getter.return_value = self.mock_redis
15411544
lambda_event = {
15421545
"multiValueQueryStringParameters": {
15431546
self.patient_identifier_key: ["https://fhir.nhs.uk/Id/nhs-number|9000000009"],

lambdas/backend/tests/models/utils/test_validation_utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33

44
from jsonpath_ng.ext import parse
55

6-
from models.errors import InconsistentIdentifierError, InconsistentResourceVersion
7-
from models.fhir_immunization import ImmunizationValidator
8-
from models.obtain_field_value import ObtainFieldValue
9-
from models.utils.generic_utils import (
6+
from common.models.errors import InconsistentIdentifierError, InconsistentResourceVersion
7+
from common.models.fhir_immunization import ImmunizationValidator
8+
from common.models.obtain_field_value import ObtainFieldValue
9+
from common.models.utils.generic_utils import (
1010
get_current_name_instance,
1111
obtain_current_name_period,
1212
obtain_name_field_location,
1313
patient_and_practitioner_value_and_index,
1414
)
15-
from models.utils.validation_utils import validate_identifiers_match, validate_resource_versions_match
15+
from common.models.utils.validation_utils import validate_identifiers_match, validate_resource_versions_match
1616
from testing_utils.generic_utils import (
1717
load_json_data,
1818
)

lambdas/backend/tests/repository/test_fhir_repository.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
from boto3.dynamodb.conditions import Attr, Key
99
from fhir.resources.R4B.immunization import Immunization
1010

11-
from models.errors import (
11+
from common.models.errors import (
1212
ResourceNotFoundError,
1313
UnhandledResponseError,
1414
)
15-
from models.immunization_record_metadata import ImmunizationRecordMetadata
16-
from models.utils.validation_utils import get_vaccine_type
15+
from common.models.immunization_record_metadata import ImmunizationRecordMetadata
16+
from common.models.utils.validation_utils import get_vaccine_type
1717
from repository.fhir_repository import ImmunizationRepository
1818
from testing_utils.generic_utils import update_target_disease_code
1919
from testing_utils.immunization_utils import VALID_NHS_NUMBER, create_covid_immunization_dict
@@ -32,8 +32,9 @@ class TestFhirRepositoryBase(unittest.TestCase):
3232

3333
def setUp(self):
3434
super().setUp()
35-
self.redis_patcher = patch("models.utils.validation_utils.redis_client")
36-
self.mock_redis_client = self.redis_patcher.start()
35+
self.mock_redis = Mock()
36+
self.redis_getter_patcher = patch("common.models.utils.validation_utils.get_redis_client")
37+
self.mock_redis_getter = self.redis_getter_patcher.start()
3738
self.logger_info_patcher = patch("logging.Logger.info")
3839
self.mock_logger_info = self.logger_info_patcher.start()
3940

@@ -252,7 +253,8 @@ def test_create_immunization(self):
252253
imms = Immunization.parse_obj(create_covid_immunization_dict(imms_id=self._MOCK_CREATED_UUID))
253254

254255
self.table.put_item = MagicMock(return_value={"ResponseMetadata": {"HTTPStatusCode": 200}})
255-
self.mock_redis_client.hget.return_value = "COVID"
256+
self.mock_redis.hget.return_value = "COVID"
257+
self.mock_redis_getter.return_value = self.mock_redis
256258

257259
created_id = self.repository.create_immunization(imms, "Test")
258260

lambdas/backend/tests/service/test_fhir_service.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
from authorisation.api_operation_code import ApiOperationCode
1515
from authorisation.authoriser import Authoriser
1616
from constants import NHS_NUMBER_USED_IN_SAMPLE_DATA
17-
from models.errors import (
17+
from common.models.errors import (
1818
CustomValidationError,
1919
IdentifierDuplicationError,
2020
InconsistentIdentifierError,
2121
InconsistentResourceVersion,
2222
ResourceNotFoundError,
2323
UnauthorizedVaxError,
2424
)
25-
from models.fhir_immunization import ImmunizationValidator
26-
from models.immunization_record_metadata import ImmunizationRecordMetadata
25+
from common.models.fhir_immunization import ImmunizationValidator
26+
from common.models.immunization_record_metadata import ImmunizationRecordMetadata
2727
from repository.fhir_repository import ImmunizationRepository
2828
from service.fhir_service import FhirService, get_service_url
2929
from testing_utils.generic_utils import load_json_data
@@ -40,8 +40,9 @@ class TestFhirServiceBase(unittest.TestCase):
4040

4141
def setUp(self):
4242
super().setUp()
43-
self.redis_patcher = patch("models.utils.validation_utils.redis_client")
44-
self.mock_redis_client = self.redis_patcher.start()
43+
self.mock_redis = Mock()
44+
self.redis_getter_patcher = patch("common.models.utils.validation_utils.get_redis_client")
45+
self.mock_redis_getter = self.redis_getter_patcher.start()
4546
self.logger_info_patcher = patch("logging.Logger.info")
4647
self.mock_logger_info = self.logger_info_patcher.start()
4748

@@ -99,7 +100,8 @@ def tearDown(self):
99100
def test_get_immunization_by_id(self):
100101
"""it should find an Immunization by id"""
101102
imms_id = "an-id"
102-
self.mock_redis_client.hget.return_value = "COVID"
103+
self.mock_redis.hget.return_value = "COVID"
104+
self.mock_redis_getter.return_value = self.mock_redis
103105
self.authoriser.authorise.return_value = True
104106
self.imms_repo.get_immunization_and_resource_meta_by_id.return_value = (
105107
create_covid_immunization(imms_id).dict(),
@@ -140,7 +142,8 @@ def test_get_immunization_by_id_patient_not_restricted(self):
140142
imms_id = "non_restricted_id"
141143

142144
immunization_data = load_json_data("completed_covid_immunization_event.json")
143-
self.mock_redis_client.hget.return_value = "COVID"
145+
self.mock_redis.hget.return_value = "COVID"
146+
self.mock_redis_getter.return_value = self.mock_redis
144147
self.authoriser.authorise.return_value = True
145148
self.imms_repo.get_immunization_and_resource_meta_by_id.return_value = (
146149
immunization_data,
@@ -162,7 +165,8 @@ def test_get_immunization_by_id_patient_not_restricted(self):
162165
def test_unauthorised_error_raised_when_user_lacks_permissions(self):
163166
"""it should throw an exception when user lacks permissions"""
164167
imms_id = "an-id"
165-
self.mock_redis_client.hget.return_value = "COVID"
168+
self.mock_redis.hget.return_value = "COVID"
169+
self.mock_redis_getter.return_value = self.mock_redis
166170
self.authoriser.authorise.return_value = False
167171
self.imms_repo.get_immunization_and_resource_meta_by_id.return_value = (
168172
create_covid_immunization(imms_id).dict(),
@@ -288,7 +292,8 @@ def setUp(self):
288292

289293
def test_create_immunization(self):
290294
"""it should create Immunization and validate it"""
291-
self.mock_redis_client.hget.return_value = "COVID"
295+
self.mock_redis.hget.return_value = "COVID"
296+
self.mock_redis_getter.return_value = self.mock_redis
292297
self.authoriser.authorise.return_value = True
293298
self.imms_repo.check_immunization_identifier_exists.return_value = False
294299
self.imms_repo.create_immunization.return_value = self._MOCK_NEW_UUID
@@ -338,7 +343,8 @@ def test_pre_validation_failed(self):
338343

339344
def test_post_validation_failed_create_invalid_target_disease(self):
340345
"""it should raise CustomValidationError for invalid target disease code on create"""
341-
self.mock_redis_client.hget.return_value = None
346+
self.mock_redis.hget.return_value = None
347+
self.mock_redis_getter.return_value = self.mock_redis
342348
valid_imms = create_covid_immunization_dict_no_id(VALID_NHS_NUMBER)
343349

344350
bad_target_disease_imms = deepcopy(valid_imms)
@@ -358,7 +364,8 @@ def test_post_validation_failed_create_invalid_target_disease(self):
358364

359365
def test_post_validation_failed_create_missing_patient_name(self):
360366
"""it should raise CustomValidationError for missing patient name on create"""
361-
self.mock_redis_client.hget.return_value = "COVID"
367+
self.mock_redis.hget.return_value = "COVID"
368+
self.mock_redis_getter.return_value = self.mock_redis
362369
valid_imms = create_covid_immunization_dict_no_id(VALID_NHS_NUMBER)
363370

364371
bad_patient_name_imms = deepcopy(valid_imms)
@@ -391,7 +398,8 @@ def test_patient_error(self):
391398

392399
def test_unauthorised_error_raised_when_user_lacks_permissions(self):
393400
"""it should raise error when user lacks permissions"""
394-
self.mock_redis_client.hget.return_value = "COVID"
401+
self.mock_redis.hget.return_value = "COVID"
402+
self.mock_redis_getter.return_value = self.mock_redis
395403
self.authoriser.authorise.return_value = False
396404
self.imms_repo.create_immunization.return_value = create_covid_immunization_dict_no_id()
397405

@@ -409,7 +417,8 @@ def test_unauthorised_error_raised_when_user_lacks_permissions(self):
409417

410418
def test_raises_duplicate_error_if_identifier_already_exits(self):
411419
"""it should raise a duplicate error if the immunisation identifier (system + local ID) already exists"""
412-
self.mock_redis_client.hget.return_value = "COVID"
420+
self.mock_redis.hget.return_value = "COVID"
421+
self.mock_redis_getter.return_value = self.mock_redis
413422
self.authoriser.authorise.return_value = True
414423
self.imms_repo.check_immunization_identifier_exists.return_value = True
415424

@@ -441,7 +450,8 @@ def setUp(self):
441450
self.authoriser = create_autospec(Authoriser)
442451
self.imms_repo = create_autospec(ImmunizationRepository)
443452
self.fhir_service = FhirService(self.imms_repo, self.authoriser)
444-
self.mock_redis_client.hget.return_value = "COVID"
453+
self.mock_redis.hget.return_value = "COVID"
454+
self.mock_redis_getter.return_value = self.mock_redis
445455

446456
def test_update_immunization(self):
447457
"""it should update Immunization and validate NHS number"""
@@ -588,7 +598,8 @@ def setUp(self):
588598
def test_delete_immunization(self):
589599
"""it should delete Immunization record"""
590600
imms = json.loads(create_covid_immunization(self.TEST_IMMUNISATION_ID).json())
591-
self.mock_redis_client.hget.return_value = "COVID"
601+
self.mock_redis.hget.return_value = "COVID"
602+
self.mock_redis_getter.return_value = self.mock_redis
592603
self.authoriser.authorise.return_value = True
593604
self.imms_repo.get_immunization_and_resource_meta_by_id.return_value = (
594605
imms,
@@ -621,7 +632,8 @@ def test_delete_immunization_throws_authorisation_exception_if_does_not_have_req
621632
):
622633
"""it should raise an UnauthorizedVaxError when the client does not have permissions for the given vacc type"""
623634
imms = json.loads(create_covid_immunization(self.TEST_IMMUNISATION_ID).json())
624-
self.mock_redis_client.hget.return_value = "FLU"
635+
self.mock_redis.hget.return_value = "FLU"
636+
self.mock_redis_getter.return_value = self.mock_redis
625637
self.authoriser.authorise.return_value = False
626638
self.imms_repo.get_immunization_and_resource_meta_by_id.return_value = (
627639
imms,

0 commit comments

Comments
 (0)