"""Unit tests for archive_existing_summary_report task.""" from datetime import datetime from unittest.mock import patch from lib import constants from lib.config import S3_PAYMENTS_BUCKET_NAME from tasks.payments_generate_export.archive_existing_report \ import archive_existing_summary_report_task @patch('tasks.payments_generate_export.archive_existing_report.aws') @patch('tasks.payments_generate_export.archive_existing_report.ows') @patch('tasks.payments_generate_export.archive_existing_report.helpers') def test_archive_existing_summary_report_task_report_exists( mock_helpers, mock_ows, mock_aws, mock_payment_group_payment, mock_report_payment_group_payment, mock_payments_generate_export_dag_run ): """Test archiving of summary report if one exists.""" payment_group_payment_id = mock_payment_group_payment['payment_group_payment_id'] payment_name = mock_payment_group_payment['payment_name'] summary_report = mock_report_payment_group_payment[0] start_index = len('s3://') + len(S3_PAYMENTS_BUCKET_NAME) sanitized_name = payment_name.lower().replace(' ', '-') today = str(datetime.now()).replace(' ', '-') report_export_url = summary_report['report_export_url'] source_key = report_export_url[start_index:] dest_key = ( f'{payment_group_payment_id}-{sanitized_name}/' f'{constants.DIRECTORY_SUMMARY}/{constants.DIRECTORY_ARCHIVE}/' f'{sanitized_name}-{today}-{constants.FILE_NAME_PAYMENT_SUMMARY_SUFFIX}' ) mock_helpers.get_event_from_params.return_value.target_id = payment_group_payment_id mock_helpers.build_report_summary_archive_location.return_value.key = dest_key mock_ows.get_payment_group_payment_reports.return_value = \ mock_report_payment_group_payment mock_ows.get_payment_group_payment_details.return_value = mock_payment_group_payment mock_aws.file_exists.return_value = True mock_aws.split_path.return_value.key = source_key mock_aws.copy_file.return_value = None mock_aws.delete_files.return_value = None archive_existing_summary_report_task(mock_payments_generate_export_dag_run) mock_helpers.get_event_from_params \ .assert_called_once_with(mock_payments_generate_export_dag_run) mock_ows.get_payment_group_payment_reports \ .assert_called_once_with(payment_group_payment_id) mock_aws.file_exists.assert_called_once_with(report_export_url) mock_ows.get_payment_group_payment_details \ .assert_called_once_with(payment_group_payment_id) mock_aws.split_path.assert_called_once_with(report_export_url) mock_helpers.build_report_summary_archive_location \ .assert_called_once_with(payment_group_payment_id, payment_name) mock_aws.copy_file.assert_called_once_with( source_bucket=S3_PAYMENTS_BUCKET_NAME, source_key=source_key, dest_key=dest_key ) mock_aws.delete_files.assert_called_once_with( bucket=S3_PAYMENTS_BUCKET_NAME, key=source_key ) @patch('tasks.payments_generate_export.archive_existing_report.aws') @patch('tasks.payments_generate_export.archive_existing_report.ows') @patch('tasks.payments_generate_export.archive_existing_report.helpers') def test_archive_existing_summary_report_task_no_report_record( mock_helpers, mock_ows, mock_aws, mock_payment_group_payment, mock_payments_generate_export_dag_run ): """Test file is not archived when no report_payment_group_payment record exists.""" payment_group_payment_id = mock_payment_group_payment['payment_group_payment_id'] mock_helpers.get_event_from_params.return_value.target_id = payment_group_payment_id mock_ows.get_payment_group_payment_reports.return_value = None archive_existing_summary_report_task(mock_payments_generate_export_dag_run) mock_helpers.get_event_from_params \ .assert_called_once_with(mock_payments_generate_export_dag_run) mock_ows.get_payment_group_payment_reports \ .assert_called_once_with(payment_group_payment_id) mock_aws.file_exists.assert_not_called() mock_ows.get_payment_group_payment_details.assert_not_called() mock_helpers.build_report_summary_archive_location.assert_not_called() mock_aws.copy_file.assert_not_called() mock_aws.delete_files.assert_not_called() @patch('tasks.payments_generate_export.archive_existing_report.aws') @patch('tasks.payments_generate_export.archive_existing_report.ows') @patch('tasks.payments_generate_export.archive_existing_report.helpers') def test_archive_existing_summary_report_task_no_s3_report( mock_helpers, mock_ows, mock_aws, mock_payment_group_payment, mock_report_payment_group_payment, mock_payments_generate_export_dag_run ): """Test file is not archived when there is no existing report on S3.""" payment_group_payment_id = mock_payment_group_payment['payment_group_payment_id'] summary_report = mock_report_payment_group_payment[0] report_export_url = summary_report['report_export_url'] mock_helpers.get_event_from_params.return_value.target_id = payment_group_payment_id mock_ows.get_payment_group_payment_reports.return_value = \ mock_report_payment_group_payment mock_aws.file_exists.return_value = False archive_existing_summary_report_task(mock_payments_generate_export_dag_run) mock_helpers.get_event_from_params \ .assert_called_once_with(mock_payments_generate_export_dag_run) mock_ows.get_payment_group_payment_reports \ .assert_called_once_with(payment_group_payment_id) mock_aws.file_exists.assert_called_once_with(report_export_url) mock_ows.get_payment_group_payment_details.assert_not_called() mock_helpers.build_report_summary_archive_location.assert_not_called() mock_aws.copy_file.assert_not_called() mock_aws.delete_files.assert_not_called()