"""Tests fixture plugin for pytest.""" import pytest from owsrequest import request from owsrequest.utils import fixture_plugin from owsrequest.utils import mock_request pytest_plugins = 'owsrequest.utils.fixture_plugin' def test_fixture_added(): """Check that engine is mocked even fixture not specified.""" with pytest.raises(fixture_plugin.OwsrequestMockException): request.process( 'test_app', 'test', 'GET', 'service', '/path', correlation_id='a') def test_any_service_exist(request_engine): """Test services autocreation in fixture.""" assert isinstance(request_engine['foo'], fixture_plugin.ServiceRouter) def test_direct_path_match(): """Test mocking by path exact matching.""" mock = mock_request.post('service', '/path/smth', {'foo': 'bar'}) response = request.process( 'test_app', 'test', 'POST', 'service', '/path/smth', correlation_id='a') assert mock.called assert response.status_code == 200 assert response.text == '{"foo": "bar"}' with pytest.raises(fixture_plugin.OwsrequestMockException): request.process( 'test_app', 'test', 'POST', 'service', '/path/other', correlation_id='a') def test_regexp_path(): """Test mocking by regexp path.""" mock = mock_request.post( 'service', r'/path/\w+', {'foo': 'bar'}, status=418) response = request.process( 'test_app', 'test', 'POST', 'service', '/path/smth', correlation_id='a') assert mock.called assert response.status_code == 418 assert response.text == '{"foo": "bar"}' with pytest.raises(fixture_plugin.OwsrequestMockException): request.process( 'test_app', 'test', 'POST', 'service', '/other', correlation_id='a') def test_invalid_method(): """Test calling mock with invalid method.""" mock_request.post('service', '/path/smth', {'foo': 'bar'}) with pytest.raises(fixture_plugin.OwsrequestMockException): request.process( 'test_app', 'test', 'GET', 'service', '/path/smth', correlation_id='a') class Effect(Exception): """Exception class for test side effect.""" def test_exception_side_effect(): """Test request side effect exception instance.""" mock = mock_request.post('service', '/path/smth', Effect()) with pytest.raises(Effect): request.process( 'test_app', 'test', 'POST', 'service', '/path/smth', correlation_id='a') assert mock.called def test_exception_class_side_effect(): """Test request side effect exception class.""" mock = mock_request.post('service', '/path/smth', Effect) with pytest.raises(Effect): request.process( 'test_app', 'test', 'POST', 'service', '/path/smth', correlation_id='a') assert mock.called def test_empty_response(): """Test empty mock response body.""" mock = mock_request.post('service', '/path/smth') response = request.process( 'test_app', 'test', 'POST', 'service', '/path/smth', correlation_id='a') assert mock.called assert response.status_code == 200 assert response.text == '{}' def test_any_request_method(request_engine): """Test any method call spec.""" mock = request_engine['service'].add_spec('*', '/path/smth') response = request.process( 'test_app', 'test', 'GET', 'service', '/path/smth', correlation_id='a') assert response.status_code == 200 response = request.process( 'test_app', 'test', 'POST', 'service', '/path/smth', correlation_id='a') assert mock.called assert response.status_code == 200 assert len(mock.calls) == 2 assert mock.calls[0]['method'] == 'GET' assert mock.calls[1]['method'] == 'POST' @pytest.mark.parametrize( 'method', ['GET', 'HEAD', 'OPTIONS', 'POST', 'PUT', 'DELETE', '*']) def test_mock_request(method): """Test mock_request shortcuts.""" func = getattr(mock_request, 'any' if method == '*' else method.lower()) mock = func('service', '/path/smth') test_method = 'GET' if method == '*' else method response = request.process( 'test_app', 'test', test_method, 'service', '/path/smth', correlation_id='a') assert mock.called assert response.status_code == 200