"""Tests for ows-abacus-state requests.""" from typing import Any import httpx import pytest from owsclient.test import OwsClientMock from sync_contract_sap.error_handling import OwsServiceException from sync_contract_sap.ows_abacus_state import ( create_abacus_states, get_abacus_states, update_abacus_state_by_id, ) def test_get_abacus_states_success( mock_abacus_states: list[dict[str, Any]], ows_client_mock: OwsClientMock ) -> None: """Test get abacus states. success case. """ parent_table_name = 'contract' parent_table_id = 1 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_states(parent_table_name, parent_table_id) assert res == mock_abacus_states def test_get_abacus_states_failure(ows_client_mock: OwsClientMock) -> None: """Test get_abacus_states raises on non-200 response.""" parent_table_name = 'contract' parent_table_id = 1 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(404, json='not found') ) with pytest.raises( OwsServiceException, match=f'ERROR in GET /abacus-state/{parent_table_name}/{parent_table_id}', ): get_abacus_states(parent_table_name, parent_table_id) def test_update_abacus_state_by_id_success(ows_client_mock: OwsClientMock) -> None: """Test successfully updates abacus state.""" abacus_state_id = 111 put_body = {'action_status': 'success', 'message': 'Great success!'} mock_response = { 'action_name': 'sap_sync', 'action_state_id': abacus_state_id, 'action_status': 'success', 'parent_table_id': '1', 'parent_table_name': 'contract', } 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 def test_update_abacus_state_by_id_failure(ows_client_mock: OwsClientMock) -> None: """Test update_abacus_state_by_id raises on non-200 response.""" 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') ) with pytest.raises( OwsServiceException, match=f'ERROR in PUT /abacus-state/{abacus_state_id}' ): update_abacus_state_by_id(abacus_state_id, put_body) def test_create_abacus_states_success( mock_abacus_states: list[dict[str, Any]], ows_client_mock: OwsClientMock ) -> None: """Test create_abacus_states returns state list on 201 response.""" contract_id = 1 contract_type = 'distribution' path = f'/abacus-states/contract/{contract_id}/?contract_type={contract_type}' ows_client_mock.post('ows-abacus-state', path).mock( return_value=httpx.Response(201, json=mock_abacus_states) ) result = create_abacus_states(contract_id, contract_type) assert result == mock_abacus_states def test_create_abacus_states_failure(ows_client_mock: OwsClientMock) -> None: """Test create_abacus_states raises on non-2xx response.""" contract_id = 1 contract_type = 'distribution' path = f'/abacus-states/contract/{contract_id}/?contract_type={contract_type}' ows_client_mock.post('ows-abacus-state', path).mock( return_value=httpx.Response(500, json='error') ) with pytest.raises(OwsServiceException, match='ERROR in POST'): create_abacus_states(contract_id, contract_type)