"""Tests for ows-abacus-state requests.""" from typing import Dict from unittest import mock import httpx from owsclient import OwsClient from owsclient.test import OwsClientMock import pytest from src.connectors.ows_state import get_abacus_states, update_abacus_state_by_id from src.exceptions import OwsStateException from src.models import AbacusState from tests.unit.factories import AbacusStateFactory def test_get_abacus_states_success(ows_client_mock: OwsClientMock) -> None: """Test get abacus states. success case. """ states = [AbacusStateFactory.build(), AbacusStateFactory.build()] parent_table_name = 'worksheet_payment_contract_advance' 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( status_code=200, json=[AbacusState.model_dump(state) for state in states] ) ) res = get_abacus_states(parent_table_name, parent_table_id) assert res == states def test_get_abacus_states_failure(ows_client_mock: OwsClientMock) -> None: """Test get abacus states. failure case. """ parent_table_name = 'worksheet_payment_contract_advance' 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(status_code=404, json='not found')) with pytest.raises( OwsStateException, 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: Dict[str, str] = {'action_status': 'success', 'message': 'Great success!'} mock_request = ows_client_mock.put( 'ows-abacus-state', f'/abacus-state/{abacus_state_id}', json=put_body ) mock_request.mock(return_value=httpx.Response(status_code=200, json={})) update_abacus_state_by_id(abacus_state_id, put_body) def test_update_abacus_state_by_id_failure(ows_client_mock: OwsClientMock) -> None: """Test failure updating abacus state.""" abacus_state_id = 111 put_body: Dict[str, str] = {'action_status': 'complete'} ows_client_mock.put( 'ows-abacus-state', f'/abacus-state/{abacus_state_id}', json=put_body ).mock(return_value=httpx.Response(status_code=400, json='error')) with pytest.raises( OwsStateException, match=f'ERROR in PUT /abacus-state/{abacus_state_id}' ): update_abacus_state_by_id(abacus_state_id, put_body)