"""conftest. This file gets picked up when running py.test tests: http://pytest.org/latest/writing_plugins.html#conftest """ import application from owsrequest import test_utils from owsrequest import request import pytest from tests.fixtures import * # noqa from tests.utils import db_operations @pytest.fixture def client(): """Return flask test client. Returns: flask client: flask test client """ return application.app.test_client() def pytest_sessionstart(session): """Before running all tests.""" db_operations.create_mkt_program_info_table() def pytest_sessionfinish(session, exitstatus): """After running all tests.""" db_operations.drop_mkt_program_info_table() class OwsRequestMockBuilder: """Utility to build mocking setup for owsrequest.""" def __init__(self, mocker): """Initializer.""" self.specs = {method: [] for method in ['get', 'post', 'head', 'put']} self.mocker = mocker def mock_response( self, service, path, method='get', status=200, json=None): """Add a spec to the list of available methods.""" spec = {'service': service, 'path': path, 'status': status} if json is not None: spec['json'] = json self.specs[method].append(spec) self.apply() return self def add(self, spec_with_method): """Legacy implementation of mock_response.""" method = spec_with_method.pop('method') self.specs[method].append(spec_with_method) self.apply() return self def apply(self): """Set up actual mock with added specs.""" for method in ['get', 'post', 'head', 'put']: specs = self.specs[method] requests = test_utils.mock_ows_requests(specs) self.mocker.patch.object(request, method, requests) @pytest.fixture def ows_request_mocker(mocker): """Fixture for mocking requests.""" return OwsRequestMockBuilder(mocker) @pytest.fixture def app_context(): """Create an api test app fixture.""" with application.app.app_context(): yield