Skip to content

Commit

Permalink
[PRMDR-761] Previous Soft Delete not being considered on Lambda Confi…
Browse files Browse the repository at this point in the history
…rmation (#323)

* [PRMDR-761] replace dynamodb scan with a query which filter out soft-deleted records

* [PRMDR-761] Change param type of validate_number_of_documents
  • Loading branch information
joefong-nhs authored Mar 20, 2024
1 parent 57c0673 commit 3bd5a76
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
19 changes: 10 additions & 9 deletions lambdas/services/upload_confirm_result_service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os

from boto3.dynamodb.conditions import Attr
from botocore.exceptions import ClientError
from enums.lambda_error import LambdaError
from enums.supported_document_types import SupportedDocumentTypes
from services.base.dynamo_service import DynamoDBService
from services.base.s3_service import S3Service
from services.document_service import DocumentService
from utils.audit_logging_setup import LoggingService
from utils.lambda_exceptions import UploadConfirmResultException

Expand All @@ -15,6 +15,7 @@
class UploadConfirmResultService:
def __init__(self, nhs_number):
self.dynamo_service = DynamoDBService()
self.document_service = DocumentService()
self.s3_service = S3Service()
self.nhs_number = nhs_number
self.staging_bucket = os.environ["STAGING_STORE_BUCKET_NAME"]
Expand Down Expand Up @@ -47,7 +48,9 @@ def process_documents(self, documents: dict):
)

if lg_document_references:
self.validate_number_of_documents(lg_table_name, lg_document_references)
self.validate_number_of_documents(
SupportedDocumentTypes.LG, lg_document_references
)
self.move_files_and_update_dynamo(
lg_document_references,
lg_bucket_name,
Expand Down Expand Up @@ -112,19 +115,17 @@ def update_dynamo_table(
{"Uploaded": True, "FileLocation": file_location},
)

def validate_number_of_documents(self, table_name: str, document_references: list):
def validate_number_of_documents(
self, doc_type: SupportedDocumentTypes, document_references: list
):
logger.info(
"Checking number of document references in list matches number of documents in dynamo table"
)

filter_by_nhs_number = Attr("NhsNumber").eq(self.nhs_number)

query_response = self.dynamo_service.scan_table(
table_name=table_name, filter_expression=filter_by_nhs_number
items = self.document_service.fetch_available_document_references_by_type(
nhs_number=self.nhs_number, doc_type=doc_type
)

items = query_response.get("Items", None)

if len(items) != len(document_references):
logger.error(
"Number of document references not equal to number of documents in dynamo table for this nhs number"
Expand Down
22 changes: 15 additions & 7 deletions lambdas/tests/unit/services/test_upload_confirm_result_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ def patched_service(set_env, mocker):
service = UploadConfirmResultService(TEST_NHS_NUMBER)
mock_dynamo_service = mocker.patch.object(service, "dynamo_service")
mocker.patch.object(mock_dynamo_service, "update_item")
mocker.patch.object(mock_dynamo_service, "scan_table")
mock_s3_service = mocker.patch.object(service, "s3_service")
mocker.patch.object(mock_s3_service, "copy_across_bucket")
mocker.patch.object(mock_s3_service, "delete_object")
mock_document_service = mocker.patch.object(service, "document_service")
mocker.patch.object(
mock_document_service, "fetch_available_document_references_by_type"
)
yield service


Expand Down Expand Up @@ -67,7 +70,7 @@ def test_process_documents_with_lg_document_references(
patched_service.process_documents(MOCK_LG_DOCUMENTS)

mock_validate_number_of_documents.assert_called_with(
MOCK_LG_TABLE_NAME, MOCK_LG_DOCUMENT_REFERENCES
SupportedDocumentTypes.LG, MOCK_LG_DOCUMENT_REFERENCES
)
mock_move_files_and_update_dynamo.assert_called_with(
MOCK_LG_DOCUMENT_REFERENCES,
Expand Down Expand Up @@ -101,7 +104,7 @@ def test_process_documents_with_both_types_of_document_references(
patched_service.process_documents(MOCK_BOTH_DOC_TYPES)

mock_validate_number_of_documents.assert_called_once_with(
MOCK_LG_TABLE_NAME, [TEST_FILE_KEY]
SupportedDocumentTypes.LG, [TEST_FILE_KEY]
)
assert mock_move_files_and_update_dynamo.call_count == 2

Expand Down Expand Up @@ -190,21 +193,26 @@ def test_update_dynamo_table(patched_service):


def test_validate_number_of_documents_success(patched_service):
patched_service.dynamo_service.scan_table.return_value = {"Items": ["doc1"]}
patched_service.document_service.fetch_available_document_references_by_type.return_value = [
"doc1"
]

patched_service.validate_number_of_documents(
MOCK_LG_TABLE_NAME, MOCK_LG_DOCUMENT_REFERENCES
)

patched_service.dynamo_service.scan_table.assert_called_once()
patched_service.document_service.fetch_available_document_references_by_type.assert_called_once()


def test_validate_number_of_documents_raises_exception(patched_service):
patched_service.dynamo_service.scan_table.return_value = {"Items": ["doc1", "doc2"]}
patched_service.document_service.fetch_available_document_references_by_type.return_value = [
"doc1",
"doc2",
]

with pytest.raises(UploadConfirmResultException):
patched_service.validate_number_of_documents(
MOCK_LG_TABLE_NAME, MOCK_LG_DOCUMENT_REFERENCES
)

patched_service.dynamo_service.scan_table.assert_called_once()
patched_service.document_service.fetch_available_document_references_by_type.assert_called_once()

0 comments on commit 3bd5a76

Please sign in to comment.