"""Unit tests for accounting_period_close update_abacus_state task.""" from unittest.mock import patch from lib.constants import ABACUS_STATE_STATUSES from lib.constants import ACCOUNTING_PERIOD_ACTIONS from tasks.accounting_period_close.update_abacus_state import update_abacus_state_task @patch('tasks.accounting_period_close.update_abacus_state.ows') @patch('tasks.accounting_period_close.update_abacus_state.get_accounting_period_state') @patch('tasks.accounting_period_close.update_abacus_state.get_event_from_params') def test_update_abacus_state_failure( mock_get_event_from_params, mock_get_accounting_period_state, mock_ows, mock_accounting_period_close_dag_run ): """Test updating accounting_period 'close_period' abacus_state to 'error'.""" accounting_period_id = 1 mock_close_period_state = { 'abacus_state_id': 10, 'action_status': 'running', 'action_name': 'close_period', 'parent_table_id': accounting_period_id, 'parent_table_name': 'accounting_period' } mock_get_event_from_params.return_value.target_id = accounting_period_id mock_get_accounting_period_state.return_value = mock_close_period_state mock_ows.update_abacus_state.return_value = True update_abacus_state_task( mock_accounting_period_close_dag_run, ABACUS_STATE_STATUSES.ERROR ) mock_get_event_from_params.assert_called_once_with( mock_accounting_period_close_dag_run ) mock_get_accounting_period_state.assert_called_once_with( accounting_period_id, ACCOUNTING_PERIOD_ACTIONS.CLOSE_PERIOD ) mock_ows.update_abacus_state.assert_called_once_with( mock_close_period_state['abacus_state_id'], body={'action_status': ABACUS_STATE_STATUSES.ERROR} ) @patch('tasks.accounting_period_close.update_abacus_state.ows') @patch('tasks.accounting_period_close.update_abacus_state.get_accounting_period_state') @patch('tasks.accounting_period_close.update_abacus_state.get_event_from_params') def test_update_abacus_state_started( mock_get_event_from_params, mock_get_accounting_period_state, mock_ows, mock_accounting_period_close_dag_run ): """Test updating accounting_period 'close_period' abacus_state to 'running'.""" accounting_period_id = 1 mock_close_period_state = { 'abacus_state_id': 10, 'action_status': 'init', 'action_name': 'close_period', 'parent_table_id': accounting_period_id, 'parent_table_name': 'accounting_period' } mock_get_event_from_params.return_value.target_id = accounting_period_id mock_get_accounting_period_state.return_value = mock_close_period_state mock_ows.update_abacus_state.return_value = True update_abacus_state_task( mock_accounting_period_close_dag_run, ABACUS_STATE_STATUSES.RUNNING ) mock_get_event_from_params.assert_called_once_with( mock_accounting_period_close_dag_run ) mock_get_accounting_period_state.assert_called_once_with( accounting_period_id, ACCOUNTING_PERIOD_ACTIONS.CLOSE_PERIOD ) mock_ows.update_abacus_state.assert_called_once_with( mock_close_period_state['abacus_state_id'], body={'action_status': ABACUS_STATE_STATUSES.RUNNING} ) @patch('tasks.accounting_period_close.update_abacus_state.ows') @patch('tasks.accounting_period_close.update_abacus_state.get_accounting_period_state') @patch('tasks.accounting_period_close.update_abacus_state.get_event_from_params') def test_update_abacus_state_success( mock_get_event_from_params, mock_get_accounting_period_state, mock_ows, mock_accounting_period_close_dag_run ): """Test updating accounting_period 'close_period' abacus_state to 'complete'.""" accounting_period_id = 1 mock_close_period_state = { 'abacus_state_id': 10, 'action_status': 'running', 'action_name': 'close_period', 'parent_table_id': accounting_period_id, 'parent_table_name': 'accounting_period' } mock_get_event_from_params.return_value.target_id = accounting_period_id mock_get_accounting_period_state.return_value = mock_close_period_state mock_ows.update_abacus_state.return_value = True update_abacus_state_task( mock_accounting_period_close_dag_run, ABACUS_STATE_STATUSES.COMPLETE ) mock_get_event_from_params.assert_called_once_with( mock_accounting_period_close_dag_run ) mock_get_accounting_period_state.assert_called_once_with( accounting_period_id, ACCOUNTING_PERIOD_ACTIONS.CLOSE_PERIOD ) mock_ows.update_abacus_state.assert_called_once_with( mock_close_period_state['abacus_state_id'], body={'action_status': ABACUS_STATE_STATUSES.COMPLETE} )