"""Lambda test module.""" import pytest from src import app from src.constants import common CURRENT_DATE = '20250101' HFA_ORCHARD_TRACK_LICENSES_JSON = 'hfa_orchard_track_licenses.json' HFA_ORCHARD_TRACK_LICENSES_JSON_WITH_TMP = f'tmp/{HFA_ORCHARD_TRACK_LICENSES_JSON}' PENDING_REQ_TRACK_IDS_DATA_JSON = 'pending_req_track_ids_data.json' PENDING_REQ_TRACK_IDS_DATA_JSON_WITH_TMP = f'tmp/{PENDING_REQ_TRACK_IDS_DATA_JSON}' SSA_REQUEST_TEST_FILE = f'Orchard_M16106_SSA_{CURRENT_DATE}_AAAAA.txt' SSA_REQUEST_TEST_FILE_PATH = f'tmp/{SSA_REQUEST_TEST_FILE}' RGT_REQUEST_TEST_FILE = f'Orchard_M16106_RGT_{CURRENT_DATE}_AAAAA.txt' RGT_REQUEST_TEST_FILE_PATH = f'tmp/{RGT_REQUEST_TEST_FILE}' def test_handler_success(mock_dependencies): """Test handler function success.""" mock_dependencies['upload_file_to_ftp'].return_value = True test_event = { common.STATUS: common.OK, common.GENERATE_HFA_TMP_FILES: { common.PENDING_REQUEST_TRACK_IDS_DATA_FILE: PENDING_REQ_TRACK_IDS_DATA_JSON, common.HFA_ORCHARD_TRACK_LICENSES_FILE: HFA_ORCHARD_TRACK_LICENSES_JSON }, common.GENERATE_HFA_TMP_FILES_LENGTH: 2, common.CREATE_HFA_REQUEST_FILES: { common.SSA_REQUEST_FILE: SSA_REQUEST_TEST_FILE, common.RGT_REQUEST_FILE: RGT_REQUEST_TEST_FILE }, common.CREATE_HFA_REQUEST_FILES_LENGTH: 2, common.FILES_TO_CLEANUP: [ PENDING_REQ_TRACK_IDS_DATA_JSON_WITH_TMP, HFA_ORCHARD_TRACK_LICENSES_JSON_WITH_TMP, SSA_REQUEST_TEST_FILE_PATH, RGT_REQUEST_TEST_FILE_PATH ] } result = app.handler(test_event, None) assert result == test_event assert mock_dependencies['download_file_from_s3'].call_count == 2 assert mock_dependencies['create_zip'].call_count == 2 assert mock_dependencies['transform_license_data'].call_count == 2 assert mock_dependencies['upload_file_to_ftp'].call_count == 2 assert mock_dependencies['update_hfa_orchard_track_licenses_state'].call_count == 2 assert mock_dependencies['insert_hfa_license_request'].call_count == 2 assert mock_dependencies['send_success_email'].call_count == 2 assert mock_dependencies['upload_zip_to_s3'].call_count == 2 mock_dependencies['capture_exception'].assert_not_called() mock_dependencies['send_error_email'].assert_not_called() def test_handler_empty_create_hfa_request_files_event(mock_dependencies): """Test handler function for empty create_hfa_request_files in event..""" mock_dependencies['upload_file_to_ftp'].return_value = True test_event = { common.STATUS: common.OK, common.GENERATE_HFA_TMP_FILES: { common.PENDING_REQUEST_TRACK_IDS_DATA_FILE: PENDING_REQ_TRACK_IDS_DATA_JSON, common.HFA_ORCHARD_TRACK_LICENSES_FILE: HFA_ORCHARD_TRACK_LICENSES_JSON }, common.GENERATE_HFA_TMP_FILES_LENGTH: 2, common.CREATE_HFA_REQUEST_FILES: {}, common.CREATE_HFA_REQUEST_FILES_LENGTH: 0, common.FILES_TO_CLEANUP: [ PENDING_REQ_TRACK_IDS_DATA_JSON_WITH_TMP, HFA_ORCHARD_TRACK_LICENSES_JSON_WITH_TMP ] } result = app.handler(test_event, None) assert result == test_event mock_dependencies['download_file_from_s3'].assert_not_called() mock_dependencies['create_zip'].assert_not_called() mock_dependencies['transform_license_data'].assert_not_called() mock_dependencies['upload_file_to_ftp'].assert_not_called() mock_dependencies['update_hfa_orchard_track_licenses_state'].assert_not_called() mock_dependencies['insert_hfa_license_request'].assert_not_called() mock_dependencies['send_success_email'].assert_not_called() mock_dependencies['upload_zip_to_s3'].assert_not_called() mock_dependencies['capture_exception'].assert_not_called() mock_dependencies['send_error_email'].assert_not_called() def test_handler_upload_to_ftp_failure(mock_dependencies): """Test handler function for ftp upload failure.""" mock_dependencies['upload_file_to_ftp'].return_value = False test_event = { common.STATUS: common.OK, common.GENERATE_HFA_TMP_FILES: { common.PENDING_REQUEST_TRACK_IDS_DATA_FILE: PENDING_REQ_TRACK_IDS_DATA_JSON, common.HFA_ORCHARD_TRACK_LICENSES_FILE: HFA_ORCHARD_TRACK_LICENSES_JSON }, common.GENERATE_HFA_TMP_FILES_LENGTH: 2, common.CREATE_HFA_REQUEST_FILES: { common.SSA_REQUEST_FILE: SSA_REQUEST_TEST_FILE, common.RGT_REQUEST_FILE: RGT_REQUEST_TEST_FILE }, common.CREATE_HFA_REQUEST_FILES_LENGTH: 2, common.FILES_TO_CLEANUP: [ PENDING_REQ_TRACK_IDS_DATA_JSON_WITH_TMP, HFA_ORCHARD_TRACK_LICENSES_JSON_WITH_TMP, SSA_REQUEST_TEST_FILE_PATH, RGT_REQUEST_TEST_FILE_PATH ] } result = app.handler(test_event, None) assert result == test_event assert mock_dependencies['download_file_from_s3'].call_count == 2 assert mock_dependencies['create_zip'].call_count == 2 assert mock_dependencies['transform_license_data'].call_count == 2 assert mock_dependencies['upload_file_to_ftp'].call_count == 2 mock_dependencies['update_hfa_orchard_track_licenses_state'].assert_not_called() mock_dependencies['insert_hfa_license_request'].assert_not_called() mock_dependencies['send_success_email'].assert_not_called() mock_dependencies['upload_zip_to_s3'].assert_not_called() mock_dependencies['capture_exception'].assert_not_called() mock_dependencies['send_error_email'].assert_not_called() def test_handler_exception(mock_dependencies): """Test handler function in exception.""" mock_dependencies['download_file_from_s3'].side_effect = Exception('Unexpected error occurred while downloading') test_event = { common.STATUS: common.OK, common.GENERATE_HFA_TMP_FILES: { common.PENDING_REQUEST_TRACK_IDS_DATA_FILE: PENDING_REQ_TRACK_IDS_DATA_JSON, common.HFA_ORCHARD_TRACK_LICENSES_FILE: HFA_ORCHARD_TRACK_LICENSES_JSON }, common.GENERATE_HFA_TMP_FILES_LENGTH: 2, common.CREATE_HFA_REQUEST_FILES: { common.SSA_REQUEST_FILE: SSA_REQUEST_TEST_FILE, common.RGT_REQUEST_FILE: RGT_REQUEST_TEST_FILE }, common.CREATE_HFA_REQUEST_FILES_LENGTH: 2, common.FILES_TO_CLEANUP: [ PENDING_REQ_TRACK_IDS_DATA_JSON_WITH_TMP, HFA_ORCHARD_TRACK_LICENSES_JSON_WITH_TMP, SSA_REQUEST_TEST_FILE_PATH, RGT_REQUEST_TEST_FILE_PATH ] } with pytest.raises(Exception, match='Unexpected error occurred while downloading'): app.handler(test_event, {}) mock_dependencies['create_zip'].assert_not_called() mock_dependencies['transform_license_data'].assert_not_called() mock_dependencies['upload_file_to_ftp'].assert_not_called() mock_dependencies['update_hfa_orchard_track_licenses_state'].assert_not_called() mock_dependencies['insert_hfa_license_request'].assert_not_called() mock_dependencies['send_success_email'].assert_not_called() mock_dependencies['upload_zip_to_s3'].assert_not_called() mock_dependencies['capture_exception'].assert_called_once() mock_dependencies['send_error_email'].assert_called_once()