diff --git a/lambdas/services/lloyd_george_validator.py b/lambdas/services/lloyd_george_validator.py index c612a27d8..39fb9fddd 100644 --- a/lambdas/services/lloyd_george_validator.py +++ b/lambdas/services/lloyd_george_validator.py @@ -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 + diff --git a/lambdas/tests/unit/services/test_lloyd_george_validator.py b/lambdas/tests/unit/services/test_lloyd_george_validator.py index c3ed2f0f5..74e634435 100644 --- a/lambdas/tests/unit/services/test_lloyd_george_validator.py +++ b/lambdas/tests/unit/services/test_lloyd_george_validator.py @@ -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): @@ -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' \ No newline at end of file