"""Update Sales File State logic tests.""" from unittest.mock import patch import pytest from abacus_state.constants.constants import ( ACCOUNTING_PERIOD_ACTION_NAMES, ACTION_STATUSES, SALES_FILES_ACTION_NAMES, ) from abacus_state.constants.error import ( ERROR_PERIOD_IS_NOT_LOCKED, ERROR_SALES_FILE_GET_ELIGIBLE_SALES_IS_NOT_COMPLETE, ) from abacus_state.logic import sales_file_state as logic from abacus_state.utils.format_error import validation_error from tests.utils.factories import AbacusStateFactory @patch('abacus_state.logic.sales_file_state.SalesFile') @patch('abacus_state.logic.sales_file_state._is_valid_for_approval') @patch('abacus_state.logic.sales_file_state.AbacusState') def test_update_sales_file_state_success( mock_model, mock_validation, mock_model_sales_file, mock_sales_file ): """Test update_sales_file_state method.""" get_eligible_sales_abacus_state = AbacusStateFactory.create( action_name=SALES_FILES_ACTION_NAMES.GET_ELIGIBLE_SALES, action_status=ACTION_STATUSES.INIT, parent_table_name='sales_file', parent_table_id=789, ) AbacusStateFactory.create( action_name=SALES_FILES_ACTION_NAMES.APPROVE_SALES, action_status=ACTION_STATUSES.INIT, parent_table_name='sales_file', parent_table_id=789, ) mock_put_request = {'action_status': ACTION_STATUSES.RUNNING, 'message': 'testing'} accounting_period_states = { ACCOUNTING_PERIOD_ACTION_NAMES.DELIVER_SALES_FILES: ACTION_STATUSES.COMPLETE } mock_validation.return_value = True mock_model.get_formatted_action_statuses.return_value = accounting_period_states mock_model_sales_file.get_sales_file_by_id.return_value = mock_sales_file[0] res = logic.update_sales_file_state( get_eligible_sales_abacus_state, **mock_put_request ) assert res.status == 200 mock_model.commit_changes.assert_called_once() @patch('abacus_state.logic.sales_file_state.SalesFile') @patch('abacus_state.logic.sales_file_state._is_valid_for_approval') @patch('abacus_state.logic.sales_file_state.AbacusState') def test_update_sales_file_state_error( mock_model, mock_validation, mock_model_sales_file, mock_sales_file ): """Test update_sales_file_state method. when deliver_sales_files action is not completed. """ AbacusStateFactory.create( action_name=SALES_FILES_ACTION_NAMES.GET_ELIGIBLE_SALES, action_status=ACTION_STATUSES.INIT, parent_table_name='sales_file', parent_table_id=789, ) approve_sales_abacus_state = AbacusStateFactory.create( action_name=SALES_FILES_ACTION_NAMES.APPROVE_SALES, action_status=ACTION_STATUSES.INIT, parent_table_name='sales_file', parent_table_id=789, ) mock_put_request = {'action_status': ACTION_STATUSES.COMPLETE, 'message': 'testing'} accounting_period_states = { ACCOUNTING_PERIOD_ACTION_NAMES.DELIVER_SALES_FILES: ACTION_STATUSES.RUNNING } mock_model.get_formatted_action_statuses.return_value = accounting_period_states mock_validation.return_value = validation_error('mocked error') mock_model_sales_file.get_sales_file_by_id.return_value = mock_sales_file[0] res = logic.update_sales_file_state(approve_sales_abacus_state, **mock_put_request) assert res.status == 400 mock_model.commit_changes.assert_not_called() @patch('abacus_state.logic.sales_file_state.SalesFile') def test_update_sales_file_state_no_sales_file_found(mock_model_sales_file): """Test update_sales_file_state method. when specified sales file doesn't exist. """ AbacusStateFactory.create( action_name=SALES_FILES_ACTION_NAMES.GET_ELIGIBLE_SALES, action_status=ACTION_STATUSES.INIT, parent_table_name='sales_file', parent_table_id=789, ) approve_sales_abacus_state = AbacusStateFactory.create( action_name=SALES_FILES_ACTION_NAMES.APPROVE_SALES, action_status=ACTION_STATUSES.INIT, parent_table_name='sales_file', parent_table_id=789, ) mock_put_request = {'action_status': ACTION_STATUSES.COMPLETE, 'message': 'testing'} mock_model_sales_file.get_sales_file_by_id.return_value = None res = logic.update_sales_file_state(approve_sales_abacus_state, **mock_put_request) assert res.status == 404 assert res.errors['message'] == 'No sales file found' def test_is_valid_for_approval_file_not_delivered(): """Test _is_valid_for_approval method. when accounting_period's deliver_sales_files action is not completed. """ accounting_period_states = { ACCOUNTING_PERIOD_ACTION_NAMES.DELIVER_SALES_FILES: ACTION_STATUSES.RUNNING } get_eligible_sales_abacus_state = AbacusStateFactory.create( action_name=SALES_FILES_ACTION_NAMES.GET_ELIGIBLE_SALES, action_status=ACTION_STATUSES.COMPLETE, parent_table_name='sales_file', parent_table_id=789, ) approve_sales_abacus_state = AbacusStateFactory.create( action_name=SALES_FILES_ACTION_NAMES.APPROVE_SALES, action_status=ACTION_STATUSES.INIT, parent_table_name='sales_file', parent_table_id=789, ) sales_file_actions = { SALES_FILES_ACTION_NAMES.GET_ELIGIBLE_SALES: get_eligible_sales_abacus_state, SALES_FILES_ACTION_NAMES.APPROVE_SALES: approve_sales_abacus_state, } res = logic._is_valid_for_approval(accounting_period_states, sales_file_actions) assert res.status == 400 assert res.errors['message'] == ERROR_PERIOD_IS_NOT_LOCKED @pytest.mark.parametrize( 'get_eligible_sales_action_status', [(ACTION_STATUSES.INIT), (ACTION_STATUSES.RUNNING), (ACTION_STATUSES.ERROR)], ) def test_is_valid_for_approval_file_not_completed(get_eligible_sales_action_status): """Test _is_valid_for_approval method. when sales_file's get_eligible_sales state is not complete. """ accounting_period_states = { ACCOUNTING_PERIOD_ACTION_NAMES.DELIVER_SALES_FILES: ACTION_STATUSES.COMPLETE } approve_sales_abacus_state = AbacusStateFactory.create( action_name=SALES_FILES_ACTION_NAMES.APPROVE_SALES, action_status=ACTION_STATUSES.INIT, parent_table_name='sales_file', parent_table_id=789, ) sales_file_actions = { SALES_FILES_ACTION_NAMES.GET_ELIGIBLE_SALES: get_eligible_sales_action_status, SALES_FILES_ACTION_NAMES.APPROVE_SALES: approve_sales_abacus_state, } res = logic._is_valid_for_approval(accounting_period_states, sales_file_actions) assert res.status == 400 assert res.errors['message'] == ERROR_SALES_FILE_GET_ELIGIBLE_SALES_IS_NOT_COMPLETE def test_is_valid_for_approval_success(): """Test _is_valid_for_approval method.""" accounting_period_states = { ACCOUNTING_PERIOD_ACTION_NAMES.DELIVER_SALES_FILES: ACTION_STATUSES.COMPLETE } sales_file_actions = { SALES_FILES_ACTION_NAMES.GET_ELIGIBLE_SALES: ACTION_STATUSES.COMPLETE, SALES_FILES_ACTION_NAMES.APPROVE_SALES: ACTION_STATUSES.INIT, } res = logic._is_valid_for_approval(accounting_period_states, sales_file_actions) assert res is True