Skip to content

Commit

Permalink
[PRMP-1376] all tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
NogaNHS committed Jan 21, 2025
1 parent 59b54fa commit 0460234
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 41 deletions.
27 changes: 15 additions & 12 deletions lambdas/tests/unit/services/test_bulk_upload_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
)


Expand Down Expand Up @@ -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
)
Expand All @@ -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,
)

Expand All @@ -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
)
Expand All @@ -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",
)

Expand All @@ -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
)
Expand All @@ -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",
)

Expand All @@ -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
)
Expand All @@ -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",
)

Expand Down Expand Up @@ -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)

Expand All @@ -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()
Expand Down
85 changes: 56 additions & 29 deletions lambdas/tests/unit/utils/test_lloyd_george_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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(
Expand All @@ -323,49 +331,68 @@ 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):
lg_file_patient_name = "Jim Stevens"
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):
Expand Down

0 comments on commit 0460234

Please sign in to comment.