"""Unit tests for sales_approve helpers.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from lib import constants from tasks.sales_approve import helpers @patch('tasks.sales_approve.helpers.event') def test_get_event_from_params(mock_event, mock_sales_approve_dag_run): """Test getting abacus_event from dag run config.""" mock_event.get_abacus_event.return_value = 'an event' mock_event.validate_event_for_handler.return_value = True res = helpers.get_event_from_params(mock_sales_approve_dag_run) assert res mock_event.get_abacus_event.assert_called_once_with(mock_sales_approve_dag_run) mock_event.validate_event_for_handler.assert_called_once_with( 'an event', event_name=constants.DAG_SALES_APPROVE_EVENT_NAME, target_type=constants.DAG_SALES_APPROVE_TARGET_TYPE ) @patch('tasks.sales_approve.helpers.ows') def test_get_abacus_state_success(mock_ows, mock_sales_file_abacus_states): """Test getting a sales_file's 'approve_sales' abacus_state.""" action_name = constants.SALES_FILE_ACTIONS.APPROVE_SALES mock_ows.get_abacus_states.return_value = mock_sales_file_abacus_states res = helpers.get_abacus_state(123) assert res.get('action_name') == action_name mock_ows.get_abacus_states.assert_called_once_with(helpers.PARENT_TABLE_NAME, 123) @patch('tasks.sales_approve.helpers.ows') def test_get_abacus_states_failure(mock_ows, mock_sales_file_abacus_states): """Test that an error is raised when abacus_state record does not exist.""" mock_ows.get_abacus_states.return_value = [mock_sales_file_abacus_states[0]] with pytest.raises(ValueError): helpers.get_abacus_state(123) mock_ows.get_abacus_states.assert_called_once_with(helpers.PARENT_TABLE_NAME, 123) @patch('tasks.sales_approve.helpers.ows') def test_get_event_records(mock_ows): """Test getting sales_file and accounting_period details from abacus_event.""" accounting_period = { 'accounting_period_id': 456, 'accounting_period_name': 'Month 1999' } sales_file = { 'accounting_period_id': accounting_period['accounting_period_id'], 'sales_file_id': 123, 'file_name': 'eligible sales' } abacus_event = MagicMock() abacus_event.target_id = sales_file['sales_file_id'] mock_ows.get_sales_file_details.return_value = sales_file mock_ows.get_accounting_period_details.return_value = accounting_period res = helpers.get_event_records(abacus_event) assert res == (sales_file, accounting_period) mock_ows.get_sales_file_details.assert_called_once_with(sales_file['sales_file_id']) mock_ows.get_accounting_period_details.assert_called_once_with( accounting_period['accounting_period_id'] ) @patch('tasks.sales_approve.helpers.paths') @patch('tasks.sales_approve.helpers.aws') def test_build_sales_file_main_url(mock_aws, mock_paths): """Test building an S3 path to sales file results.""" accounting_period = { 'accounting_period_id': 456, 'accounting_period_name': 'Month 1999' } sales_file = { 'sales_file_id': 123, 'file_name': 'eligible sales' } accounting_period_slug = '456-Month-1999' sales_file_slug = '123-eligible-sales' key = f'{accounting_period_slug}/eligible-sales/{sales_file_slug}/results/' url = f's3://{key}' mock_location = MagicMock(key=key, url=url) mock_paths.build_period_slug.return_value = accounting_period_slug mock_paths.build_sales_file_slug.return_value = sales_file_slug mock_aws.location.return_value = mock_location res = helpers.build_sales_file_main_url(accounting_period, sales_file) assert res.key == key assert res.url == url mock_paths.build_period_slug.assert_called_once_with( accounting_period.get('accounting_period_id'), accounting_period.get('accounting_period_name') ) mock_paths.build_sales_file_slug.assert_called_once_with( sales_file.get('sales_file_id'), sales_file.get('file_name') ) mock_aws.location.assert_called_once_with( accounting_period_slug, constants.DIRECTORY_ELIGIBLE_SALES, sales_file_slug, constants.FILE_NAME_RESULTS_PARQUET )