"""Test payments_generate_export tasks' helpers.""" from unittest.mock import patch import pytest from lib.config import S3_PAYMENTS_BUCKET_NAME from lib.constants import FILE_NAME_PAYMENT_SUMMARY_SUFFIX from tasks.payments_generate_export import helpers def test_build_report_summary_archive_location(mock_payment_group_payment): """Test helper that builds S3 location of an archived payment summary report.""" payment_group_payment_id = mock_payment_group_payment['payment_group_payment_id'] payment_name = mock_payment_group_payment['payment_name'] sanitized_name = payment_name.lower().replace(' ', '-') bucket = f's3://{S3_PAYMENTS_BUCKET_NAME}' archive_key = f'{payment_group_payment_id}-{sanitized_name}/summary/archive' res = helpers.build_report_summary_archive_location( payment_group_payment_id, payment_name ) assert res.key.startswith(archive_key) assert res.url.startswith(f'{bucket}/{archive_key}/{sanitized_name}-') assert res.url.endswith(FILE_NAME_PAYMENT_SUMMARY_SUFFIX) def test_build_report_summary_location(mock_payment_group_payment): """Test helper that builds S3 location of payment summary report.""" payment_group_payment_id = mock_payment_group_payment['payment_group_payment_id'] payment_name = mock_payment_group_payment['payment_name'] res = helpers.build_report_summary_location(payment_group_payment_id, payment_name) sanitized_name = payment_name.lower().replace(' ', '-') bucket = f's3://{S3_PAYMENTS_BUCKET_NAME}' summary_key = f'{payment_group_payment_id}-{sanitized_name}/summary' assert res.key == \ f'{summary_key}/{sanitized_name}-{FILE_NAME_PAYMENT_SUMMARY_SUFFIX}' assert res.url == \ f'{bucket}/{summary_key}/{sanitized_name}-{FILE_NAME_PAYMENT_SUMMARY_SUFFIX}' def test_build_report_summary_path(mock_payment_group_payment): """Test helper that builds S3 path prefix to a payment summary directory.""" payment_group_payment_id = mock_payment_group_payment['payment_group_payment_id'] payment_name = mock_payment_group_payment['payment_name'] res = helpers.build_report_summary_path(payment_group_payment_id, payment_name) assert res == \ f'{payment_group_payment_id}-{payment_name.lower().replace(" ", "-")}/summary' @patch('tasks.payments_generate_export.helpers.ows') def test_get_abacus_state_success(mock_ows, mock_payment_group_payment_states): """Test getting a specified abacus_state record for a payment_group_payment.""" action_name = 'generate_export' mock_ows.get_abacus_states.return_value = mock_payment_group_payment_states res = helpers.get_abacus_state(action_name, 123) assert res mock_ows.get_abacus_states.assert_called_once_with('payment_group_payment', 123) @patch('tasks.payments_generate_export.helpers.ows') def test_get_abacus_state_failure(mock_ows, mock_payment_group_payment_states): """Test getting a specified abacus_state record raises and error when not found.""" action_name = 'not_an_action' mock_ows.get_abacus_states.return_value = mock_payment_group_payment_states with pytest.raises(ValueError): helpers.get_abacus_state(action_name, 123) mock_ows.get_abacus_states.assert_called_once_with('payment_group_payment', 123)