"""Tests for updating worksheet payment custom state.""" from unittest.mock import patch from abacus_state.constants.constants import WORKSHEET_PAYMENT_CUSTOM_ACTION_NAMES from abacus_state.logic import worksheet_payment_custom_state as logic from tests.utils.factories import AbacusStateFactory LOGIC_IMPORT_PATH = 'abacus_state.logic.worksheet_payment_custom_state' @patch(f'{LOGIC_IMPORT_PATH}._are_previous_actions_completed') @patch(f'{LOGIC_IMPORT_PATH}.AbacusState') def test_worksheet_payment_custom_state_success(mock_model, mock_validation): """Test update_worksheet_payment_custom_state method.""" parent_table_id = 123 mock_validation.return_value = True abacus_state = AbacusStateFactory.create( action_name=WORKSHEET_PAYMENT_CUSTOM_ACTION_NAMES.UPLOAD_APPROVAL, action_status='init', parent_table_name='worksheet_payment_custom_state', parent_table_id=parent_table_id, ) mock_put_request = {'action_status': 'complete', 'message': 'testing'} res = logic.update_worksheet_payment_custom_state(abacus_state, **mock_put_request) assert res.status == 200 mock_model.commit_changes.assert_called_once() @patch(f'{LOGIC_IMPORT_PATH}._are_previous_actions_completed') @patch(f'{LOGIC_IMPORT_PATH}.AbacusState') def test_worksheet_payment_custom_state_failure(mock_model, mock_validation): """Test update_worksheet_payment_custom_state method failure.""" parent_table_id = 123 mock_validation.return_value = False abacus_state = AbacusStateFactory.create( action_name=WORKSHEET_PAYMENT_CUSTOM_ACTION_NAMES.FUNDS_CONFIRMED, action_status='init', parent_table_name='worksheet_payment_custom_state', parent_table_id=parent_table_id, ) mock_put_request = {'action_status': 'complete', 'message': 'testing'} res = logic.update_worksheet_payment_custom_state(abacus_state, **mock_put_request) assert res.status == 400 assert not mock_model.commit_changes.called def test_are_previous_actions_completed_failure(): """Test _are_previous_actions_completed method. when earlier actions are not completed """ actions = dict() for index, action in enumerate(WORKSHEET_PAYMENT_CUSTOM_ACTION_NAMES): actions.update({action: 'init'}) action_name = WORKSHEET_PAYMENT_CUSTOM_ACTION_NAMES.FUNDS_CONFIRMED res = logic._are_previous_actions_completed(actions, action_name) assert res is False def test_are_previous_actions_completed_success(): """Test _are_previous_actions_completed method. when earlier actions are completed """ actions = dict() for index, action in enumerate(WORKSHEET_PAYMENT_CUSTOM_ACTION_NAMES): actions.update({action: 'complete'}) action_name = WORKSHEET_PAYMENT_CUSTOM_ACTION_NAMES.FUNDS_CONFIRMED res = logic._are_previous_actions_completed(actions, action_name) assert res is True