Skip to content

Commit

Permalink
prmdr-242 Pr changes
Browse files Browse the repository at this point in the history
  • Loading branch information
NogaNHS committed Oct 11, 2023
1 parent f6b8901 commit 93af848
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
4 changes: 2 additions & 2 deletions lambdas/handlers/create_document_reference_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from utils.lambda_response import ApiGatewayResponse
from utils.utilities import validate_id

from services.lloyd_george_validator import validate_lg_files, LGInvalidFilesException, LGFileTypeException, LGFileNameException
from services.lloyd_george_validator import validate_lg_files, LGInvalidFilesException

sys.path.append(os.path.join(os.path.dirname(__file__)))

Expand Down Expand Up @@ -156,7 +156,7 @@ def lambda_handler(event, context):
500, "An error occurred when creating document reference", "POST"
).create_api_gateway_response()
return response
except (LGInvalidFilesException, LGFileTypeException, LGFileNameException) as e:
except LGInvalidFilesException as e:
logger.error(e)
response = ApiGatewayResponse(
400, "One or more if the files is not valid", "POST"
Expand Down
18 changes: 4 additions & 14 deletions lambdas/services/lloyd_george_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,27 @@

from lambdas.models.nhs_document_reference import NHSDocumentReference


class LGFileTypeException(ValueError):
"""One or more of the files do not match the required file type"""
pass

class LGFileNameException(ValueError):
"""One or more of the files do not match naming convention"""
pass

class LGInvalidFilesException(Exception):
"""One or more of the files are not valid"""
pass

def validate_lg_file_type(file_type: str):
if file_type != 'application/pdf':
raise LGFileTypeException
raise LGInvalidFilesException('One or more of the files do not match the required file type')

def validate_file_name(name: str):
lg_regex = r'[0-9]+of[0-9]+_Lloyd_George_Record_\[[A-Za-z ]+]_\[[0-9]{10}]_\[\d\d-\d\d-\d\d\d\d].pdf'
if not re.fullmatch(lg_regex, name):
raise LGFileNameException
raise LGInvalidFilesException('One or more of the files do not match naming convention')

def check_for_duplicate_files(file_list: list[str]):
if len(file_list) > len(set(file_list)):
raise LGInvalidFilesException
raise LGInvalidFilesException('One or more of the files has the same filename')

def check_for_number_of_files_match_expected(file_name: str, total_files_number: int):
lg_number_regex = 'of[0-9]+'
expected_number_of_files = re.search(lg_number_regex, file_name)
if expected_number_of_files and not expected_number_of_files.group()[2:] == str(total_files_number):
raise LGInvalidFilesException
raise LGInvalidFilesException('There are missing file(s) in the request')

def validate_lg_files(file_list: list[NHSDocumentReference]):
files_name_list = []
Expand Down
18 changes: 9 additions & 9 deletions lambdas/tests/unit/services/test_lloyd_george_validator.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import pytest

from services.lloyd_george_validator import validate_lg_file_type, LGFileTypeException, LGFileNameException, LGInvalidFilesException, validate_file_name, check_for_duplicate_files, check_for_number_of_files_match_expected
from services.lloyd_george_validator import validate_lg_file_type, LGInvalidFilesException, validate_file_name, check_for_duplicate_files, check_for_number_of_files_match_expected


def test_catching_error_when_file_type_not_pdf():
with pytest.raises(LGFileTypeException):
with pytest.raises(LGInvalidFilesException):
file_type = 'image/png'
validate_lg_file_type(file_type)

def test_valid_file_type():
try:
file_type = 'application/pdf'
validate_lg_file_type(file_type)
except LGFileTypeException:
except LGInvalidFilesException:
assert False, 'One or more of the files do not match the required file type'

def test_invalid_file_name():
with pytest.raises(LGFileNameException):
with pytest.raises(LGInvalidFilesException):
file_name = 'bad_name'
validate_file_name(file_name)

def test_valid_file_name():
try:
file_name = '1of1_Lloyd_George_Record_[Joe Bloggs]_[1111111111]_[25-12-2019].pdf'
validate_file_name(file_name)
except LGFileNameException:
except LGInvalidFilesException:
assert False, 'One or more of the files do not match naming convention'

def test_files_with_duplication():
Expand All @@ -42,8 +42,8 @@ def test_files_without_duplication():
'2of2_Lloyd_George_Record_[Joe Bloggs]_[1111111111]_[25-12-2019].pdf'
]
check_for_duplicate_files(lg_file_list)
except LGFileNameException:
assert False, 'One or more of the files are not valid'
except LGInvalidFilesException:
assert False, 'One or more of the files has the same filename'

def test_files_list_with_missing_files():
with pytest.raises(LGInvalidFilesException):
Expand All @@ -60,5 +60,5 @@ def test_files_without_missing_files():
'2of2_Lloyd_George_Record_[Joe Bloggs]_[1111111111]_[25-12-2019].pdf'
]
check_for_number_of_files_match_expected(lg_file_list[0], str(len(lg_file_list)))
except LGFileNameException:
assert False, 'One or more of the files are not valid'
except LGInvalidFilesException:
assert False, 'There are missing file(s) in the request'

0 comments on commit 93af848

Please sign in to comment.