"""Test ows-abacus-state requests.""" from http import HTTPStatus from unittest import mock from unittest.mock import Mock import httpx from owsclient.test import OwsClientMock import pytest from src.connectors import ows_abacus_state from src.connectors.exceptions import OwsAbacusStateException from tests.unit.factories import AbacusStateFactory @pytest.mark.parametrize( 'is_error,action_name', ((True, 'test1'), (False, 'test1'), (False, 'abc')), ) def test_get_abacus_state( ows_client_mock: OwsClientMock, is_error: bool, action_name: str ) -> None: """Test get_abacus_state.""" account_payee_id = 142 states = { f'test{index}': AbacusStateFactory.build( abacus_state_id=index, action_name=f'test{index}', action_status=f'status{index}', ) for index in range(1, 4) } ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock( return_value=httpx.Response( HTTPStatus.BAD_REQUEST if is_error else HTTPStatus.OK, json=[i.model_dump(mode='json') for i in states.values()], ) ) if is_error: with pytest.raises(OwsAbacusStateException): ows_abacus_state.get_abacus_state(account_payee_id, action_name) else: result = ows_abacus_state.get_abacus_state(account_payee_id, action_name) assert result == states.get(action_name) @pytest.mark.parametrize('is_error', (True, False)) def test_update_abacus_state(ows_client_mock: OwsClientMock, is_error: bool) -> None: """Test update_abacus_state.""" abacus_state_id = 1005 message = 'test msg' action_status = 'new status' state = AbacusStateFactory.build( abacus_state_id=abacus_state_id, message=message, action_status=action_status ) ows_client_mock.put( 'ows-abacus-state', f'/abacus-state/{abacus_state_id}', json={ 'action_status': action_status, 'message': message, }, ).mock( return_value=httpx.Response( HTTPStatus.BAD_REQUEST if is_error else HTTPStatus.OK, json=state.model_dump(mode='json'), ) ) if is_error: with pytest.raises(OwsAbacusStateException): ows_abacus_state.update_abacus_state( abacus_state_id, action_status, message ) else: result = ows_abacus_state.update_abacus_state( abacus_state_id, action_status, message ) assert result == state @pytest.mark.parametrize('get_has_result', (True, False)) @mock.patch('src.connectors.ows_abacus_state.update_abacus_state') @mock.patch('src.connectors.ows_abacus_state.get_abacus_state') def test_set_state( mock_get_abacus_state: Mock, mock_update_abacus_state: Mock, get_has_result: bool, ) -> None: """Test set_state.""" account_payee_id = 23 action_name = 'tst action' action_status = 'tst status' message = 'tst msg' state = AbacusStateFactory.build(action_name=action_name) mock_get_abacus_state.return_value = state if get_has_result else None if get_has_result: ows_abacus_state.set_state( account_payee_id, action_name, action_status, message ) else: with pytest.raises(OwsAbacusStateException): ows_abacus_state.set_state( account_payee_id, action_name, action_status, message ) mock_get_abacus_state.assert_called_once_with(account_payee_id, action_name) if get_has_result: mock_update_abacus_state.assert_called_once_with( state.abacus_state_id, action_status, message ) else: mock_update_abacus_state.assert_not_called()