From 0460234f57c99c2f738d72912aaddfb9f2fecef4 Mon Sep 17 00:00:00 2001 From: NogaNHS Date: Tue, 21 Jan 2025 15:28:32 +0000 Subject: [PATCH] [PRMP-1376] all tests passed --- .../unit/services/test_bulk_upload_service.py | 27 +++--- .../unit/utils/test_lloyd_george_validator.py | 85 ++++++++++++------- 2 files changed, 71 insertions(+), 41 deletions(-) diff --git a/lambdas/tests/unit/services/test_bulk_upload_service.py b/lambdas/tests/unit/services/test_bulk_upload_service.py index 3fe0d2425..d9df0f882 100644 --- a/lambdas/tests/unit/services/test_bulk_upload_service.py +++ b/lambdas/tests/unit/services/test_bulk_upload_service.py @@ -7,6 +7,7 @@ from enums.patient_ods_inactive_status import PatientOdsInactiveStatus from enums.snomed_codes import SnomedCodes from enums.upload_status import UploadStatus +from enums.validation_score import ValidationScore from enums.virus_scan_result import SCAN_RESULT_TAG_KEY, VirusScanResult from freezegun import freeze_time from models.fhir.R4.nrl_fhir_document_reference import Attachment @@ -76,7 +77,8 @@ def mock_check_virus_result(mocker): @pytest.fixture def mock_validate_files(mocker): - yield mocker.patch("services.bulk_upload_service.validate_lg_file_names") + validate_files = mocker.patch("services.bulk_upload_service.validate_lg_file_names") + yield validate_files @pytest.fixture @@ -122,7 +124,8 @@ def mock_pds_service_patient_restricted(mocker): @pytest.fixture def mock_pds_validation(mocker): yield mocker.patch( - "services.bulk_upload_service.validate_filename_with_patient_details" + "services.bulk_upload_service.validate_filename_with_patient_details", + return_value=(ValidationScore.FULL_MATCH, True, True), ) @@ -510,7 +513,7 @@ def test_handle_sqs_message_calls_report_upload_successful_when_patient_is_forma mock_remove_ingested_file_from_source_bucket = ( repo_under_test.s3_repository.remove_ingested_file_from_source_bucket ) - mock_pds_validation.return_value = False + mock_pds_validation.return_value = (ValidationScore.FULL_MATCH, True, False) mock_put_staging_metadata_back_to_queue = ( repo_under_test.sqs_repository.put_staging_metadata_back_to_queue ) @@ -525,7 +528,7 @@ def test_handle_sqs_message_calls_report_upload_successful_when_patient_is_forma mock_report_upload.assert_called_with( TEST_STAGING_METADATA, UploadStatus.COMPLETE, - "Patient is deceased - FORMAL", + "Patient matched on full match 3/3, Patient is deceased - FORMAL", PatientOdsInactiveStatus.DECEASED, ) @@ -544,7 +547,7 @@ def test_handle_sqs_message_calls_report_upload_successful_when_patient_is_infor mock_create_lg_records_and_copy_files = mocker.patch.object( BulkUploadService, "create_lg_records_and_copy_files" ) - mock_pds_validation.return_value = True + mock_pds_validation.return_value = (ValidationScore.MIXED_FULL_MATCH, True, True) mock_remove_ingested_file_from_source_bucket = ( repo_under_test.s3_repository.remove_ingested_file_from_source_bucket ) @@ -562,7 +565,7 @@ def test_handle_sqs_message_calls_report_upload_successful_when_patient_is_infor mock_report_upload.assert_called_with( TEST_STAGING_METADATA, UploadStatus.COMPLETE, - "Patient matched on historical name, Patient is deceased - INFORMAL", + "Patient matched on historical name, Patient matched on mixed match 3/3, Patient is deceased - INFORMAL", "Y12345", ) @@ -581,7 +584,7 @@ def test_handle_sqs_message_calls_report_upload_successful_when_patient_has_hist mock_create_lg_records_and_copy_files = mocker.patch.object( BulkUploadService, "create_lg_records_and_copy_files" ) - mock_pds_validation.return_value = True + mock_pds_validation.return_value = (ValidationScore.MIXED_FULL_MATCH, True, True) mock_remove_ingested_file_from_source_bucket = ( repo_under_test.s3_repository.remove_ingested_file_from_source_bucket ) @@ -599,7 +602,7 @@ def test_handle_sqs_message_calls_report_upload_successful_when_patient_has_hist mock_report_upload.assert_called_with( TEST_STAGING_METADATA, UploadStatus.COMPLETE, - "Patient matched on historical name, PDS record is restricted", + "Patient matched on historical name, Patient matched on mixed match 3/3, PDS record is restricted", "REST", ) @@ -618,7 +621,7 @@ def test_handle_sqs_message_calls_report_upload_successful_when_patient_is_infor mock_create_lg_records_and_copy_files = mocker.patch.object( BulkUploadService, "create_lg_records_and_copy_files" ) - mock_pds_validation.return_value = False + mock_pds_validation.return_value = (ValidationScore.PARTIAL_MATCH, True, False) mock_remove_ingested_file_from_source_bucket = ( repo_under_test.s3_repository.remove_ingested_file_from_source_bucket ) @@ -636,7 +639,7 @@ def test_handle_sqs_message_calls_report_upload_successful_when_patient_is_infor mock_report_upload.assert_called_with( TEST_STAGING_METADATA, UploadStatus.COMPLETE, - "Patient is deceased - INFORMAL", + "Patient matched on partial match 2/3, Patient is deceased - INFORMAL", "Y12345", ) @@ -979,7 +982,7 @@ def test_handle_sqs_message_happy_path_historical_name( repo_under_test.s3_repository, "remove_ingested_file_from_source_bucket" ) mocker.patch.object(repo_under_test.s3_repository, "check_virus_result") - mock_pds_validation.return_value = True + mock_pds_validation.return_value = (ValidationScore.FULL_MATCH, True, True) repo_under_test.handle_sqs_message(message=TEST_SQS_MESSAGE) @@ -990,7 +993,7 @@ def test_handle_sqs_message_happy_path_historical_name( mock_report_upload_complete.assert_called_with( TEST_STAGING_METADATA, UploadStatus.COMPLETE, - "Patient matched on historical name", + "Patient matched on historical name, Patient matched on full match 3/3", "Y12345", ) mock_remove_ingested_file_from_source_bucket.assert_called() diff --git a/lambdas/tests/unit/utils/test_lloyd_george_validator.py b/lambdas/tests/unit/utils/test_lloyd_george_validator.py index 1c0e0530e..e2b2f4dbb 100644 --- a/lambdas/tests/unit/utils/test_lloyd_george_validator.py +++ b/lambdas/tests/unit/utils/test_lloyd_george_validator.py @@ -295,10 +295,13 @@ def test_validate_name_with_correct_name(mocker, mock_pds_patient): given_name_match=["Jane"], family_name_match="Smith", ) - - with expect_not_to_raise(LGInvalidFilesException): + actual_score, actual_is_name_validation_based_on_historic_name = ( calculate_validation_score(lg_file_patient_name, mock_pds_patient) + ) + assert mock_validate_name.call_count == 1 + assert actual_is_name_validation_based_on_historic_name is False + assert actual_score == ValidationScore.FULL_MATCH def test_validate_name_with_additional_middle_name_in_file_mismatching_pds(mocker): @@ -307,13 +310,18 @@ def test_validate_name_with_additional_middle_name_in_file_mismatching_pds(mocke "utils.lloyd_george_validator.validate_patient_name" ) patient = Patient.model_validate(PDS_PATIENT_WITH_MIDDLE_NAME) + mock_validate_name.return_value = ValidationResult( + score=ValidationScore.FULL_MATCH, + given_name_match=["Jane"], + family_name_match="Smith", + ) + actual_score, actual_is_name_validation_based_on_historic_name = ( + calculate_validation_score(lg_file_patient_name, patient) + ) - with expect_not_to_raise(LGInvalidFilesException): - actual_is_name_validation_based_on_historic_name = calculate_validation_score( - lg_file_patient_name, patient - ) assert mock_validate_name.call_count == 1 assert actual_is_name_validation_based_on_historic_name is False + assert actual_score == ValidationScore.FULL_MATCH def test_validate_name_with_additional_middle_name_in_file_but_none_in_pds( @@ -323,35 +331,47 @@ def test_validate_name_with_additional_middle_name_in_file_but_none_in_pds( mock_validate_name = mocker.patch( "utils.lloyd_george_validator.validate_patient_name" ) - with expect_not_to_raise(LGInvalidFilesException): - actual_is_name_validation_based_on_historic_name = calculate_validation_score( - lg_file_patient_name, mock_pds_patient - ) + mock_validate_name.return_value = ValidationResult( + score=ValidationScore.FULL_MATCH, + given_name_match=["Jane"], + family_name_match="Smith", + ) + actual_score, actual_is_name_validation_based_on_historic_name = ( + calculate_validation_score(lg_file_patient_name, mock_pds_patient) + ) + assert mock_validate_name.call_count == 1 assert actual_is_name_validation_based_on_historic_name is False + assert actual_score == ValidationScore.FULL_MATCH def test_validate_name_with_wrong_first_name(mocker, mock_pds_patient): lg_file_patient_name = "John Smith" - mock_validate_name = mocker.patch( - "utils.lloyd_george_validator.validate_patient_name" - ) - mock_validate_name.return_value = False - with pytest.raises(LGInvalidFilesException): - calculate_validation_score(lg_file_patient_name, mock_pds_patient) - assert mock_validate_name.call_count == 3 + actual_response = validate_patient_name( + lg_file_patient_name, + mock_pds_patient.name[0].given, + mock_pds_patient.name[0].family, + ) + assert actual_response == ValidationResult( + score=ValidationScore.PARTIAL_MATCH, + given_name_match=[], + family_name_match="Smith", + ) def test_validate_name_with_wrong_family_name(mocker, mock_pds_patient): lg_file_patient_name = "Jane Johnson" - mock_validate_name = mocker.patch( - "utils.lloyd_george_validator.validate_patient_name" + actual_response = validate_patient_name( + lg_file_patient_name, + mock_pds_patient.name[0].given, + mock_pds_patient.name[0].family, + ) + assert actual_response == ValidationResult( + score=ValidationScore.PARTIAL_MATCH, + given_name_match=["Jane"], + family_name_match="", ) - mock_validate_name.return_value = False - with pytest.raises(LGInvalidFilesException): - calculate_validation_score(lg_file_patient_name, mock_pds_patient) - assert mock_validate_name.call_count == 3 def test_validate_name_with_historical_name(mocker, mock_pds_patient): @@ -359,13 +379,20 @@ def test_validate_name_with_historical_name(mocker, mock_pds_patient): mock_validate_name = mocker.patch( "utils.lloyd_george_validator.validate_patient_name" ) - mock_validate_name.side_effect = [False, True] - with expect_not_to_raise(LGInvalidFilesException): - actual_is_name_validation_based_on_historic_name = calculate_validation_score( - lg_file_patient_name, mock_pds_patient - ) + mock_validate_name.side_effect = [ + ValidationResult( + score=ValidationScore.NO_MATCH, + ), + ValidationResult( + score=ValidationScore.FULL_MATCH, + ), + ] + actual_score, actual_is_validate_on_historic = calculate_validation_score( + lg_file_patient_name, mock_pds_patient + ) + assert actual_score == ValidationScore.FULL_MATCH assert mock_validate_name.call_count == 2 - assert actual_is_name_validation_based_on_historic_name is True + assert actual_is_validate_on_historic is True def test_validate_name_without_given_name(mocker, mock_pds_patient):