"""Unit tests for accounting_period_sales_approve helpers.""" from unittest.mock import patch import pytest from lib import constants from tasks.accounting_period_sales_approve import helpers @patch('tasks.accounting_period_sales_approve.helpers.event') def test_get_event_from_params( mock_event, mock_accounting_period_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_accounting_period_sales_approve_dag_run) assert res mock_event.get_abacus_event.assert_called_once_with( mock_accounting_period_sales_approve_dag_run ) mock_event.validate_event_for_handler.assert_called_once_with( 'an event', event_name=constants.DAG_ACCOUNTING_PERIOD_SALES_APPROVE_EVENT_NAME, target_type=constants.DAG_ACCOUNTING_PERIOD_SALES_APPROVE_TARGET_TYPE ) @patch('tasks.accounting_period_sales_approve.helpers.ows') def test_get_abacus_state_success(mock_ows, mock_accounting_period_abacus_states): """Test getting an accounting_period's 'approve_sales_files' abacus_state.""" action_name = constants.ACCOUNTING_PERIOD_ACTIONS.APPROVE_SALES_FILES mock_ows.get_abacus_states.return_value = mock_accounting_period_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.accounting_period_sales_approve.helpers.ows') def test_get_abacus_state_failure(mock_ows, mock_accounting_period_abacus_states): """Test that an error is raised when abacus-state record does not exist.""" mock_ows.get_abacus_states.return_value = [mock_accounting_period_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)