Skip to content

Commit

Permalink
prmdr-242 added more validation and test
Browse files Browse the repository at this point in the history
  • Loading branch information
NogaNHS committed Oct 9, 2023
1 parent bb2dc2d commit b3bd130
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
18 changes: 16 additions & 2 deletions lambdas/services/lloyd_george_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@ class LGFileNameException(ValueError):
"""One or more of the files do not match naming convention"""
pass

def validate_lg_file_type(file_type):
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

def validate_file_name(name):
def validate_file_name(name: str):
lg_regex = '[0-9]+of[0-9]+_Lloyd_George_Record_\\[[A-Za-z]+\\s[A-Za-z]+]_\\[[0-9]{10}]_\\[\\d\\d-\\d\\d-\\d\\d\\d\\d].pdf'
if not re.fullmatch(lg_regex, name):
raise LGFileNameException

def check_for_duplicate_files(file_list: list[str]):
if len(file_list) > len(set(file_list)):
raise LGInvalidFilesException

def check_for_number_of_files_match_expected(file_name: str, total_files_number: str):
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:] == total_files_number:
raise LGInvalidFilesException

39 changes: 38 additions & 1 deletion lambdas/tests/unit/services/test_lloyd_george_validator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from services.lloyd_george_validator import validate_lg_file_type, LGFileTypeException, LGFileNameException, validate_file_name
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


def test_catching_error_when_file_type_not_pdf():
with pytest.raises(LGFileTypeException):
Expand All @@ -25,3 +26,39 @@ def test_valid_file_name():
validate_file_name(file_name)
except LGFileNameException:
assert False, 'One or more of the files do not match naming convention'

def test_files_with_duplication():
with pytest.raises(LGInvalidFilesException):
lg_file_list = [
'1of1_Lloyd_George_Record_[Joe Bloggs]_[1111111111]_[25-12-2019].pdf',
'1of1_Lloyd_George_Record_[Joe Bloggs]_[1111111111]_[25-12-2019].pdf'
]
check_for_duplicate_files(lg_file_list)

def test_files_without_duplication():
try:
lg_file_list = [
'1of2_Lloyd_George_Record_[Joe Bloggs]_[1111111111]_[25-12-2019].pdf',
'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'

def test_files_list_with_missing_files():
with pytest.raises(LGInvalidFilesException):
lg_file_list = [
'1of3_Lloyd_George_Record_[Joe Bloggs]_[1111111111]_[25-12-2019].pdf',
'2of3_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)))

def test_files_without_missing_files():
try:
lg_file_list = [
'1of2_Lloyd_George_Record_[Joe Bloggs]_[1111111111]_[25-12-2019].pdf',
'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'

0 comments on commit b3bd130

Please sign in to comment.