Skip to content

Commit

Permalink
[PRMP-1367] fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NogaNHS committed Jan 24, 2025
1 parent 6b96a9e commit bac5894
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 54 deletions.
9 changes: 9 additions & 0 deletions lambdas/tests/unit/handlers/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,12 @@ def mock_upload_lambda_disabled(mocker):
"uploadLambdaEnabled": False
}
yield mock_upload_lambda_feature_flag


@pytest.fixture
def mock_upload_lambda_disabled(mocker):
mock_function = mocker.patch.object(FeatureFlagService, "get_feature_flags_by_flag")
mock_upload_lambda_feature_flag = mock_function.return_value = {
"lloydGeorgeValidationStrictMode": False
}
yield mock_upload_lambda_feature_flag
8 changes: 4 additions & 4 deletions lambdas/tests/unit/handlers/test_bulk_upload_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def mock_service(mocker):
yield mocker.patch.object(BulkUploadService, "process_message_queue")


def test_can_process_event_with_one_message(mock_service, context, set_env):
def test_can_process_event_with_one_message(mock_service, context, set_env, mock_upload_lambda_disabled):
expected = ApiGatewayResponse(
200, "Finished processing all 1 messages", "GET"
).create_api_gateway_response()
Expand All @@ -24,7 +24,7 @@ def test_can_process_event_with_one_message(mock_service, context, set_env):
assert expected == actual


def test_can_process_event_with_several_messages(mock_service, context, set_env):
def test_can_process_event_with_several_messages(mock_service, context, set_env, mock_upload_lambda_disabled):
expected = ApiGatewayResponse(
200, "Finished processing all 3 messages", "GET"
).create_api_gateway_response()
Expand All @@ -34,7 +34,7 @@ def test_can_process_event_with_several_messages(mock_service, context, set_env)


def test_receive_correct_response_when_service_returns_error(
mock_service, context, set_env
mock_service, context, set_env, mock_upload_lambda_disabled
):
expected = ApiGatewayResponse(
500, "Bulk upload failed with error: ", "GET"
Expand All @@ -46,7 +46,7 @@ def test_receive_correct_response_when_service_returns_error(


def test_receive_correct_response_when_no_records_in_event(
mock_service, context, set_env
mock_service, context, set_env, mock_upload_lambda_disabled
):
expected = ApiGatewayResponse(
400,
Expand Down
74 changes: 39 additions & 35 deletions lambdas/tests/unit/services/test_bulk_upload_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

@pytest.fixture
def repo_under_test(set_env, mocker):
service = BulkUploadService()
service = BulkUploadService(strict_mode=True)
mocker.patch.object(service, "dynamo_repository")
mocker.patch.object(service, "sqs_repository")
mocker.patch.object(service, "s3_repository")
Expand All @@ -76,8 +76,7 @@ def mock_check_virus_result(mocker):

@pytest.fixture
def mock_validate_files(mocker):
validate_files = mocker.patch("services.bulk_upload_service.validate_lg_file_names")
yield validate_files
yield mocker.patch("services.bulk_upload_service.validate_lg_file_names")


@pytest.fixture
Expand Down Expand Up @@ -121,12 +120,17 @@ def mock_pds_service_patient_restricted(mocker):


@pytest.fixture
def mock_pds_validation(mocker):
def mock_pds_validation_lenient(mocker):
yield mocker.patch(
"services.bulk_upload_service.validate_filename_with_patient_details",
"services.bulk_upload_service.validate_filename_with_patient_details_lenient",
return_value=("test string", True),
)

@pytest.fixture
def mock_pds_validation_strict(mocker):
yield mocker.patch(
"services.bulk_upload_service.validate_filename_with_patient_details_strict",
)

@pytest.fixture
def mock_ods_validation(mocker):
Expand All @@ -152,7 +156,7 @@ def build_resolved_file_names_cache(
def test_lambda_handler_process_each_sqs_message_one_by_one(
set_env, mock_handle_sqs_message
):
service = BulkUploadService()
service = BulkUploadService(True)

service.process_message_queue(TEST_SQS_MESSAGES_AS_LIST)

Expand All @@ -170,7 +174,7 @@ def test_lambda_handler_continue_process_next_message_after_handled_error(
InvalidMessageException,
None,
]
service = BulkUploadService()
service = BulkUploadService(True)
service.process_message_queue(TEST_SQS_MESSAGES_AS_LIST)

assert mock_handle_sqs_message.call_count == len(TEST_SQS_MESSAGES_AS_LIST)
Expand All @@ -187,7 +191,7 @@ def test_lambda_handler_handle_pds_too_many_requests_exception(
expected_handled_messages = TEST_SQS_10_MESSAGES_AS_LIST[0:6]
expected_unhandled_message = TEST_SQS_10_MESSAGES_AS_LIST[6:]

service = BulkUploadService()
service = BulkUploadService(True)
with pytest.raises(BulkUploadException):
service.process_message_queue(TEST_SQS_10_MESSAGES_AS_LIST)

Expand All @@ -207,7 +211,7 @@ def test_handle_sqs_message_happy_path(
repo_under_test,
mock_validate_files,
mock_pds_service,
mock_pds_validation,
mock_pds_validation_strict,
mock_ods_validation,
):
TEST_STAGING_METADATA.retries = 0
Expand Down Expand Up @@ -239,7 +243,7 @@ def test_handle_sqs_message_happy_path_single_file(
repo_under_test,
mock_validate_files,
mock_pds_service,
mock_pds_validation,
mock_pds_validation_strict,
mock_ods_validation,
):
TEST_STAGING_METADATA.retries = 0
Expand Down Expand Up @@ -336,7 +340,7 @@ def test_handle_sqs_message_happy_path_with_non_ascii_filenames(
mock_validate_files,
patient_name_on_s3,
patient_name_in_metadata_file,
mock_pds_validation,
mock_pds_validation_strict,
mock_pds_service,
mock_ods_validation,
):
Expand All @@ -362,7 +366,7 @@ def test_handle_sqs_message_calls_report_upload_failure_when_patient_record_alre
mock_uuid,
mock_validate_files,
mock_pds_service,
mock_pds_validation,
mock_pds_validation_strict,
):
TEST_STAGING_METADATA.retries = 0

Expand Down Expand Up @@ -397,7 +401,7 @@ def test_handle_sqs_message_calls_report_upload_failure_when_lg_file_name_invali
mock_uuid,
mock_validate_files,
mock_pds_service,
mock_pds_validation,
mock_pds_validation_strict,
):
TEST_STAGING_METADATA.retries = 0
mock_create_lg_records_and_copy_files = mocker.patch.object(
Expand Down Expand Up @@ -435,7 +439,7 @@ def test_handle_sqs_message_report_failure_when_document_is_infected(
mock_validate_files,
mock_check_virus_result,
mock_pds_service,
mock_pds_validation,
mock_pds_validation_strict,
mock_ods_validation,
):
TEST_STAGING_METADATA.retries = 0
Expand Down Expand Up @@ -473,7 +477,7 @@ def test_handle_sqs_message_report_failure_when_document_not_exist(
mock_validate_files,
mock_check_virus_result,
mock_pds_service,
mock_pds_validation,
mock_pds_validation_strict,
mock_ods_validation,
):
TEST_STAGING_METADATA.retries = 0
Expand Down Expand Up @@ -503,7 +507,7 @@ def test_handle_sqs_message_calls_report_upload_successful_when_patient_is_forma
mock_validate_files,
mock_check_virus_result,
mock_pds_service_patient_deceased_formal,
mock_pds_validation,
mock_pds_validation_strict,
mock_ods_validation,
):
mock_create_lg_records_and_copy_files = mocker.patch.object(
Expand All @@ -512,7 +516,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 = ("Patient matched on full match 3/3", False)
mock_pds_validation_strict.return_value = False
mock_put_staging_metadata_back_to_queue = (
repo_under_test.sqs_repository.put_staging_metadata_back_to_queue
)
Expand All @@ -527,7 +531,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 matched on full match 3/3, Patient is deceased - FORMAL",
"Patient is deceased - FORMAL",
PatientOdsInactiveStatus.DECEASED,
)

Expand All @@ -540,13 +544,13 @@ def test_handle_sqs_message_calls_report_upload_successful_when_patient_is_infor
mock_validate_files,
mock_check_virus_result,
mock_pds_service_patient_deceased_informal,
mock_pds_validation,
mock_pds_validation_strict,
mock_ods_validation,
):
mock_create_lg_records_and_copy_files = mocker.patch.object(
BulkUploadService, "create_lg_records_and_copy_files"
)
mock_pds_validation.return_value = ("Patient matched on mixed match 3/3", True)
mock_pds_validation_strict.return_value = True
mock_remove_ingested_file_from_source_bucket = (
repo_under_test.s3_repository.remove_ingested_file_from_source_bucket
)
Expand All @@ -564,7 +568,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 matched on mixed match 3/3, Patient is deceased - INFORMAL",
"Patient matched on historical name, Patient is deceased - INFORMAL",
"Y12345",
)

Expand All @@ -577,13 +581,13 @@ def test_handle_sqs_message_calls_report_upload_successful_when_patient_has_hist
mock_validate_files,
mock_check_virus_result,
mock_pds_service_patient_restricted,
mock_pds_validation,
mock_pds_validation_strict,
mock_ods_validation,
):
mock_create_lg_records_and_copy_files = mocker.patch.object(
BulkUploadService, "create_lg_records_and_copy_files"
)
mock_pds_validation.return_value = ("Patient matched on mixed match 3/3", True)
mock_pds_validation_strict.return_value = True
mock_remove_ingested_file_from_source_bucket = (
repo_under_test.s3_repository.remove_ingested_file_from_source_bucket
)
Expand All @@ -601,7 +605,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, Patient matched on mixed match 3/3, PDS record is restricted",
"Patient matched on historical name, PDS record is restricted",
"REST",
)

Expand All @@ -614,13 +618,13 @@ def test_handle_sqs_message_calls_report_upload_successful_when_patient_is_infor
mock_validate_files,
mock_check_virus_result,
mock_pds_service_patient_deceased_informal,
mock_pds_validation,
mock_pds_validation_strict,
mock_ods_validation,
):
mock_create_lg_records_and_copy_files = mocker.patch.object(
BulkUploadService, "create_lg_records_and_copy_files"
)
mock_pds_validation.return_value = ("Patient matched on partial match 2/3", False)
mock_pds_validation_strict.return_value = False
mock_remove_ingested_file_from_source_bucket = (
repo_under_test.s3_repository.remove_ingested_file_from_source_bucket
)
Expand All @@ -638,7 +642,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 partial match 2/3, Patient is deceased - INFORMAL",
"Patient is deceased - INFORMAL",
"Y12345",
)

Expand All @@ -651,7 +655,7 @@ def test_handle_sqs_message_put_staging_metadata_back_to_queue_when_virus_scan_r
mock_validate_files,
mock_check_virus_result,
mock_pds_service,
mock_pds_validation,
mock_pds_validation_strict,
mock_ods_validation,
):
TEST_STAGING_METADATA.retries = 0
Expand Down Expand Up @@ -689,7 +693,7 @@ def test_handle_sqs_message_rollback_transaction_when_validation_pass_but_file_t
mock_check_virus_result,
mock_validate_files,
mock_pds_service,
mock_pds_validation,
mock_pds_validation_strict,
mock_ods_validation,
):
TEST_STAGING_METADATA.retries = 0
Expand Down Expand Up @@ -892,7 +896,7 @@ def test_raise_client_error_from_ssm_with_pds_service(
repo_under_test,
mock_validate_files,
mock_pds_service,
mock_pds_validation,
mock_pds_validation_strict,
):
mock_client_error = ClientError(
{"Error": {"Code": "500", "Message": "test error"}}, "testing"
Expand All @@ -909,7 +913,7 @@ def test_mismatch_ods_with_pds_service(
mock_uuid,
mock_validate_files,
mock_pds_service,
mock_pds_validation,
mock_pds_validation_strict,
):
mock_ods_validation.return_value = False

Expand All @@ -931,7 +935,7 @@ def test_create_lg_records_and_copy_files_client_error(
mock_check_virus_result,
mock_validate_files,
mock_pds_service,
mock_pds_validation,
mock_pds_validation_strict,
mock_ods_validation,
):
TEST_STAGING_METADATA.retries = 0
Expand Down Expand Up @@ -967,7 +971,7 @@ def test_handle_sqs_message_happy_path_historical_name(
repo_under_test,
mock_validate_files,
mock_pds_service,
mock_pds_validation,
mock_pds_validation_strict,
mock_ods_validation,
):
TEST_STAGING_METADATA.retries = 0
Expand All @@ -981,7 +985,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 = ("Patient matched on full match 3/3", True)
mock_pds_validation_strict.return_value = True

repo_under_test.handle_sqs_message(message=TEST_SQS_MESSAGE)

Expand All @@ -992,7 +996,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 full match 3/3",
"Patient matched on historical name",
"Y12345",
)
mock_remove_ingested_file_from_source_bucket.assert_called()
Expand Down
Loading

0 comments on commit bac5894

Please sign in to comment.