"""Test logging errors.""" from unittest.mock import patch import pytest from payments_generate.error_handling import _raise_service_error from payments_generate.error_handling import OwsServiceException from payments_generate.error_handling import raise_abacus_account_error from payments_generate.error_handling import raise_payment_error @patch('payments_generate.error_handling._raise_service_error') def test_raise_abacus_account_error(mock_service_error): """Test raising an error when making requests to ows-abacus-account fail.""" error_msg = 'ERROR to GET /account/path/' account_id = 123 mock_service_error.return_value = None raise_abacus_account_error(error_msg, account_id=account_id) mock_service_error.asset_called_once_with( error_msg, 'ows-abacus-account', account_id=account_id) @patch('payments_generate.error_handling._raise_service_error') def test_raise_payment_error_get(mock_service_error): """Test raising an error when making a GET request to ows-payment fails.""" error_msg = 'ERROR to GET /payment/path/' mock_service_error.return_value = None raise_payment_error(error_msg) mock_service_error.assert_called_once_with(error_msg, 'ows-payment') @patch('payments_generate.error_handling._raise_service_error') def test_raise_payment_error_post(mock_service_error): """Test raising an error when making a POST request to ows-payment fails.""" data = {'account_id': 123, 'last_payment': '4500.45'} error_msg = 'ERROR from POST /payment/path/' mock_service_error.return_value = None raise_payment_error(error_msg, data=data) mock_service_error.assert_called_once_with(error_msg, 'ows-payment', data=data) def test_raise_service_error(): """Test _raise_service_error logs error and raises exception.""" data = {'account_id': 123, 'last_payment': '4500.45'} error_msg = 'ERROR to POST /service/path/' service = 'ows-service' with pytest.raises(OwsServiceException) as e: _raise_service_error(error_msg, service, **data) assert f'POST to {service} failed' in str(e.value) def test_raise_service_error_no_kwargs(): """Test _raise_service_error logs error and raises exception without kwargs.""" error_msg = 'ERROR to GET /service/path/id/' service = 'ows-service' with pytest.raises(OwsServiceException) as e: _raise_service_error(error_msg, service) assert f'{service} failure: {error_msg}' in str(e.value)