Skip to content

Commit

Permalink
PRMDR-180
Browse files Browse the repository at this point in the history
Adding final bits for comprehensive unit tests
  • Loading branch information
AlexHerbertNHS committed Oct 10, 2023
1 parent cbed6c6 commit bda7459
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 11 deletions.
16 changes: 16 additions & 0 deletions lambdas/tests/unit/handlers/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ def valid_id_and_both_doctype_event():
return api_gateway_proxy_event


@pytest.fixture
def valid_id_and_arf_doctype_event():
api_gateway_proxy_event = {
"queryStringParameters": {"patientId": "9000000009", "docType": "ARF"},
}
return api_gateway_proxy_event


@pytest.fixture
def valid_id_and_lg_doctype_event():
api_gateway_proxy_event = {
"queryStringParameters": {"patientId": "9000000009", "docType": "LG"},
}
return api_gateway_proxy_event


@pytest.fixture
def valid_id_and_invalid_doctype_event():
api_gateway_proxy_event = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from unittest.mock import call

from enums.metadata_field_names import DocumentReferenceMetadataFields
from enums.supported_document_types import SupportedDocumentTypes
from handlers.document_manifest_by_nhs_number_handler import lambda_handler
from tests.unit.helpers.data.test_documents import TEST_DS_DOCS, TEST_LG_DOCS
from utils.lambda_response import ApiGatewayResponse
Expand All @@ -11,7 +14,7 @@


def test_lambda_handler_returns_204_when_no_documents_returned_from_dynamo_response(
mocker, set_env, valid_id_and_both_doctype_event, context
mocker, set_env, valid_id_and_arf_doctype_event, context
):
mock_document_query = mocker.patch(
"services.manifest_dynamo_service.ManifestDynamoService.discover_uploaded_documents"
Expand All @@ -22,7 +25,7 @@ def test_lambda_handler_returns_204_when_no_documents_returned_from_dynamo_respo
204, "No documents found for given NHS number and document type", "GET"
).create_api_gateway_response()

actual = lambda_handler(valid_id_and_both_doctype_event, context)
actual = lambda_handler(valid_id_and_arf_doctype_event, context)

assert expected == actual

Expand All @@ -44,15 +47,71 @@ def test_lambda_handler_returns_400_when_doc_type_invalid_response(
assert expected == actual


def test_lambda_handler_valid_parameters_returns_200(
def manifest_service_side_effect(nhs_number, doc_type):
if doc_type == SupportedDocumentTypes.ARF:
return [TEST_DS_DOCS]
if doc_type == SupportedDocumentTypes.LG:
return [TEST_LG_DOCS]
return None


def test_lambda_handler_valid_parameters_arf_doc_type_request_returns_200(
mocker, set_env, valid_id_and_arf_doctype_event, context
):
expected_url = "test-url"

mock_dynamo = mocker.patch(
"services.manifest_dynamo_service.ManifestDynamoService.discover_uploaded_documents"
)
mock_dynamo.side_effect = manifest_service_side_effect

mock_doc_manifest_url = mocker.patch(
"services.document_manifest_service.DocumentManifestService.create_document_manifest_presigned_url"
)
mock_doc_manifest_url.return_value = expected_url

expected = ApiGatewayResponse(
200, expected_url, "GET"
).create_api_gateway_response()

actual = lambda_handler(valid_id_and_arf_doctype_event, context)
mock_dynamo.assert_called_once_with("9000000009", SupportedDocumentTypes.ARF)
assert expected == actual


def test_lambda_handler_valid_parameters_lg_doc_type_request_returns_200(
mocker, set_env, valid_id_and_lg_doctype_event, context
):
expected_url = "test-url"

mock_dynamo = mocker.patch(
"services.manifest_dynamo_service.ManifestDynamoService.discover_uploaded_documents"
)
mock_dynamo.side_effect = manifest_service_side_effect

mock_doc_manifest_url = mocker.patch(
"services.document_manifest_service.DocumentManifestService.create_document_manifest_presigned_url"
)
mock_doc_manifest_url.return_value = expected_url

expected = ApiGatewayResponse(
200, expected_url, "GET"
).create_api_gateway_response()

actual = lambda_handler(valid_id_and_lg_doctype_event, context)
mock_dynamo.assert_called_once_with("9000000009", SupportedDocumentTypes.LG)
assert expected == actual


def test_lambda_handler_valid_parameters_both_doc_type_request_returns_200(
mocker, set_env, valid_id_and_both_doctype_event, context
):
expected_url = "test-url"

mock_dynamo = mocker.patch(
"services.manifest_dynamo_service.ManifestDynamoService.discover_uploaded_documents"
)
mock_dynamo.side_effect = [TEST_DS_DOCS, TEST_LG_DOCS]
mock_dynamo.side_effect = manifest_service_side_effect

mock_doc_manifest_url = mocker.patch(
"services.document_manifest_service.DocumentManifestService.create_document_manifest_presigned_url"
Expand All @@ -64,20 +123,23 @@ def test_lambda_handler_valid_parameters_returns_200(
).create_api_gateway_response()

actual = lambda_handler(valid_id_and_both_doctype_event, context)

mock_dynamo.assert_has_calls([
call("9000000009", SupportedDocumentTypes.ARF),
call("9000000009", SupportedDocumentTypes.LG)
])
assert expected == actual


def test_lambda_handler_missing_environment_variables_returns_500(
set_env, monkeypatch, valid_id_and_both_doctype_event, context
set_env, monkeypatch, valid_id_and_arf_doctype_event, context
):
monkeypatch.delenv("DOCUMENT_STORE_DYNAMODB_NAME")
expected = ApiGatewayResponse(
500,
"An error occurred due to missing key: 'DOCUMENT_STORE_DYNAMODB_NAME'",
"GET",
).create_api_gateway_response()
actual = lambda_handler(valid_id_and_both_doctype_event, context)
actual = lambda_handler(valid_id_and_arf_doctype_event, context)
assert expected == actual


Expand Down Expand Up @@ -107,7 +169,3 @@ def test_lambda_handler_returns_400_when_doc_type_not_supplied(
).create_api_gateway_response()
actual = lambda_handler(valid_id_event, context)
assert expected == actual

# only gets LG docs
# only gets ARF docs
# gets all docs

0 comments on commit bda7459

Please sign in to comment.