"""Unit testcases for request methods.""" import httpx from owsclient.test import OwsClientMock import pytest from contract_lifecycle_automation import requests def test_get(ows_client_mock: OwsClientMock): """Test GET method.""" path = '/object/object_id' service = 'ows-service' options = {'headers': {'foo': 'bar'}} route = ows_client_mock.get( service, path, **options) route.mock( return_value=httpx.Response(200, json={'message': 'OK'}) ) result = requests.get(path=path, service=service, **options) assert result.status_code == 200 assert result.json() == {'message': 'OK'} def test_post(ows_client_mock: OwsClientMock): """Test post method.""" body = {'target_id': 666} path = '/object/object_id' service = 'ows-service' options = {'headers': {'foo': 'bar'}} route = ows_client_mock.post( service, path, json=body, **options) route.mock( return_value=httpx.Response(200, json={'message': 'OK'}) ) result = requests.post(path=path, service=service, body=body, **options) assert result.status_code == 200 assert result.json() == {'message': 'OK'} def test_raise_service_error(): """Test raise_service_error logs error and raises exception.""" error_msg = 'ERROR to GET /object/object_id' service = 'ows-service' with pytest.raises(requests.OwsServiceException) as e: requests.raise_service_error(error_msg, service) assert f'{service} failure: {error_msg}' in str(e.value) def test_raise_service_error_with_kwargs(): """Test raise_service_error logs error and raises exception with kwargs.""" data = {'target_id': 666} error_msg = 'ERROR to GET /object/object_id' service = 'ows-service' with pytest.raises(requests.OwsServiceException) as e: requests.raise_service_error(error_msg, service, **data) assert f'{service} failure: {error_msg} {data}' in str(e.value)