"""Tests for ows-abacus-state requests.""" from unittest.mock import patch import httpx from owsclient.test import OwsClientMock from commit_royalties.ows_abacus_state import get_abacus_states, \ update_abacus_state_by_id @patch('commit_royalties.ows_abacus_state.raise_service_error') def test_get_abacus_states_success( mock_raise_service_error, mock_abacus_states, ows_client_mock: OwsClientMock, ): """Test get abacus states. success case. """ parent_table_name = 'accounting_run' parent_table_id = 1 ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/{parent_table_name}/{parent_table_id}' ).mock(return_value=httpx.Response(200, json=mock_abacus_states)) res = get_abacus_states(parent_table_name, parent_table_id) assert res == mock_abacus_states mock_raise_service_error.assert_not_called() @patch('commit_royalties.ows_abacus_state.raise_service_error') def test_get_abacus_states_failure( mock_raise_service_error, ows_client_mock: OwsClientMock ): """Test get abacus states. failure case. """ parent_table_name = 'accounting_run' parent_table_id = 1 ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/{parent_table_name}/{parent_table_id}' ).mock(return_value=httpx.Response(404, text='not found')) get_abacus_states(parent_table_name, parent_table_id) mock_raise_service_error.assert_called_once_with( f'ERROR in GET /abacus-state/{parent_table_name}/{parent_table_id}', 'ows-abacus-state' ) @patch('commit_royalties.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': 'success', 'message': 'Great success!' } mock_response = { 'action_name': 'commit_royalties', 'action_state_id': abacus_state_id, 'action_status': 'success', 'parent_table_id': '1', 'parent_table_name': 'accounting_run' } ows_client_mock.put( 'ows-abacus-state', f'/abacus-state/{abacus_state_id}', json=put_body, ).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('commit_royalties.ows_abacus_state.raise_service_error') def test_update_abacus_state_by_id_failure( mock_raise_service_error, ows_client_mock: OwsClientMock ): """Test failure updating abacus state.""" abacus_state_id = 111 put_body = { 'action_status': 'complete' } ows_client_mock.put( 'ows-abacus-state', f'/abacus-state/{abacus_state_id}', json=put_body, ).mock(return_value=httpx.Response(400, text='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' )