"""Test ows-abacus-state requests.""" from unittest.mock import patch import httpx from owsclient.test import OwsClientMock from payoneer_payments_payout.ows_abacus_state import \ create_abacus_state_by_parent_table, get_abacus_state from payoneer_payments_payout.ows_abacus_state import create_abacus_states from payoneer_payments_payout.ows_abacus_state import format_abacus_state_put_params from payoneer_payments_payout.ows_abacus_state import update_abacus_state_by_id @patch('payoneer_payments_payout.ows_abacus_state.raise_service_error') def test_create_abacus_states_success( mock_raise_service_error, ows_client_mock: OwsClientMock ): """Test successfully created abacus states.""" post_body = [{ 'action_name': 'send_payment', 'action_status': 'init', 'parent_table_id': '1', 'parent_table_name': 'payment_group_payment_account' }] mock_response = [{ 'action_name': 'send_payment', 'action_state_id': 1, 'action_status': 'init', 'parent_table_id': '1', 'parent_table_name': 'payment_group_payment_account' }] request_url = '/abacus-states' ows_client_mock.post('ows-abacus-state', request_url).mock( return_value=httpx.Response( 201, json=mock_response ) ) res = create_abacus_states(post_body) assert res == mock_response mock_raise_service_error.assert_not_called() @patch('payoneer_payments_payout.ows_abacus_state.app_logger') @patch('payoneer_payments_payout.ows_abacus_state.raise_service_error') def test_create_abacus_state_failure( mock_raise_service_error, mock_app_logger, ows_client_mock: OwsClientMock ): """Test failure creating abacus states.""" request_url = '/abacus-states' ows_client_mock.post('ows-abacus-state', request_url).mock( return_value=httpx.Response( 400, json='error' ) ) post_body = [{ 'action_name': 'send_payment', 'action_status': 'init', 'parent_table_id': '1', 'parent_table_name': 'payment_group_payment_account' }] create_abacus_states(post_body) mock_raise_service_error.assert_called_once_with( 'ERROR in POST /abacus-states', 'ows-abacus-state' ) @patch('payoneer_payments_payout.ows_abacus_state.raise_service_error') def test_create_abacus_state_by_parent_table_success( mock_raise_service_error, ows_client_mock: OwsClientMock ): """Test successfully created abacus state.""" mock_response = [{ 'action_name': 'send_payment', 'action_state_id': 1, 'action_status': 'init', 'parent_table_id': '1', 'parent_table_name': 'payment_group_payment_batch' }] request_url = '/abacus-states/payment_group_payment_batch/1' ows_client_mock.post('ows-abacus-state', request_url).mock( return_value=httpx.Response( 201, json=mock_response ) ) res = create_abacus_state_by_parent_table('payment_group_payment_batch', 1) assert res == mock_response mock_raise_service_error.assert_not_called() @patch('payoneer_payments_payout.ows_abacus_state.app_logger') @patch('payoneer_payments_payout.ows_abacus_state.raise_service_error') def test_create_abacus_state_by_parent_table_failure( mock_raise_service_error, mock_app_logger, ows_client_mock: OwsClientMock ): """Test failure creating abacus state.""" request_url = '/abacus-states/payment_group_payment_batch/1' ows_client_mock.post('ows-abacus-state', request_url).mock( return_value=httpx.Response( 400, json='error' ) ) create_abacus_state_by_parent_table('payment_group_payment_batch', 1) mock_raise_service_error.assert_called_once_with( 'ERROR in POST /abacus-states/payment_group_payment_batch/1', 'ows-abacus-state' ) @patch('payoneer_payments_payout.ows_abacus_state.raise_service_error') def test_update_abacus_state_by_id_success( mock_raise_service_error, ows_client_mock: OwsClientMock ): """Test successfully updates abacus state.""" abacus_state_id = 111 put_body = { 'action_status': 'error', 'message': 'Some error.' } mock_response = { 'action_name': 'send_payment', 'action_state_id': 111, 'action_status': 'error', 'parent_table_id': '1', 'parent_table_name': 'payment_group_payment_batch' } request_url = f'/abacus-state/{abacus_state_id}' ows_client_mock.put('ows-abacus-state', request_url).mock( return_value=httpx.Response( 200, json=mock_response ) ) res = update_abacus_state_by_id(abacus_state_id, put_body) assert res == mock_response mock_raise_service_error.assert_not_called() @patch('payoneer_payments_payout.ows_abacus_state.app_logger') @patch('payoneer_payments_payout.ows_abacus_state.raise_service_error') def test_update_abacus_state_by_id_failure( mock_raise_service_error, mock_app_logger, ows_client_mock: OwsClientMock ): """Test failure updating abacus state.""" abacus_state_id = 111 put_body = { 'action_status': 'complete' } request_url = f'/abacus-state/{abacus_state_id}' ows_client_mock.put('ows-abacus-state', request_url).mock( return_value=httpx.Response( 400, json='error' ) ) update_abacus_state_by_id(abacus_state_id, put_body) mock_raise_service_error.assert_called_once_with( f'ERROR in PUT /abacus-state/{abacus_state_id}', 'ows-abacus-state' ) def test_format_abacus_state_put_params(): """Test to format abacus states PUT request parameters.""" action_status = 'init' parent_table_id = 1 message = 'Test Message' res = format_abacus_state_put_params(action_status, parent_table_id, message) assert res == { 'action_status': action_status, 'parent_table_id': parent_table_id, 'message': message } @patch('payoneer_payments_payout.ows_abacus_state.raise_service_error') def test_get_abacus_state_success( mock_raise_service_error, mock_abacus_states, ows_client_mock: OwsClientMock ): """Test successfully getting abacus_state.""" parent_table_name = 'payment_group_payment_account' parent_table_id = '1' action_name = 'calculate_payments' request_url = f'/abacus-state/{parent_table_name}/{parent_table_id}' ows_client_mock.get('ows-abacus-state', request_url).mock( return_value=httpx.Response( 200, json=mock_abacus_states ) ) res = get_abacus_state(parent_table_name, parent_table_id, action_name) assert res['action_name'] == action_name assert res['parent_table_name'] == parent_table_name assert res['parent_table_id'] == parent_table_id mock_raise_service_error.assert_not_called() @patch('payoneer_payments_payout.ows_abacus_state.raise_service_error') def test_get_abacus_state_failure( mock_raise_service_error, ows_client_mock: OwsClientMock ): """Test failure getting abacus_state.""" parent_table_name = 'payment_group_payment_account' parent_table_id = '1' action_name = 'calculate_payments' request_url = f'/abacus-state/{parent_table_name}/{parent_table_id}' ows_client_mock.get('ows-abacus-state', request_url).mock( return_value=httpx.Response( 400, json='error' ) ) res = get_abacus_state(parent_table_name, parent_table_id, action_name) assert res is None mock_raise_service_error.assert_called_once_with( f'ERROR in GET /abacus-state/{parent_table_name}/{parent_table_id}', 'ows-abacus-state' )