"""Tests for OwsMoneyhub.""" import httpx from owsclient.test import OwsClientMock import pytest from moneyhub.connectors.ows_service import OwsService from moneyhub.utils.exceptions import OwsServiceException SERVICE = 'ows-moneyhub' STATEMENT_PERIOD_ID = 1 PATH = f'/statement-period/{STATEMENT_PERIOD_ID}/statement-attachments' def test_post(ows_client_mock: OwsClientMock): """Testing POST requests.""" response_json = {'success': True} OwsService._service = SERVICE ows_client_mock.post(SERVICE, path=PATH).mock( return_value=httpx.Response(status_code=200, json=response_json) ) res = OwsService.post(PATH) assert res == response_json def test_request_error(ows_client_mock: OwsClientMock): """Test a request that fails.""" text = 'Bad Gateway' status = 502 message = f'{SERVICE} error: {status} response from post {PATH} : {text}' OwsService._service = SERVICE ows_client_mock.post(SERVICE, path=PATH).mock( return_value=httpx.Response(status_code=status, text=text) ) with pytest.raises(OwsServiceException) as excinfo: OwsService.post(PATH) assert excinfo.value.status_code == status assert excinfo.value.args[0] == message def test_request_error_with_params_and_body(ows_client_mock: OwsClientMock): """Testing a request that fails with params and body.""" params = {'account_id': 1} body = {'tables': 'fake-tables'} text = 'Bad Gateway' status = 502 new_path = PATH + '?account_id=1' message = f'{SERVICE} error: {status} response from post {new_path} {body}: {text}' OwsService._service = SERVICE ows_client_mock.post( SERVICE, path=PATH, params=params, json=body).mock( return_value=httpx.Response(status_code=status, text=text) ) with pytest.raises(OwsServiceException) as excinfo: OwsService.post(PATH, body, params) assert excinfo.value.status_code == status assert excinfo.value.args[0] == message @pytest.mark.parametrize( 'data, expected_type', [ ([1, 2, 3], list), ({'a': 1, 'b': 2}, dict), ], ) def test_validate_response_type(data, expected_type): """Testing validating the response type.""" result = OwsService.validate_response_type(data, expected_type) assert result == data @pytest.mark.parametrize( 'data, expected_type', [ ([1, 2, 3], dict), ({'a': 1, 'b': 2}, list), ], ) def test_validate_response_type_error(data, expected_type): """Testing validating the response type when an error is raised.""" OwsService._service = SERVICE with pytest.raises(OwsServiceException) as excinfo: OwsService.validate_response_type(data, expected_type) assert excinfo.value.status_code == 500 assert excinfo.value.args[0] == f'{SERVICE} error: Unexpected response type {type(data)}'