swh.core.pytest_plugin module#
- swh.core.pytest_plugin.get_response_cb(request, context, datadir, ignore_urls: List[str] = [], visits: Dict | None = None, response_context_callback: Callable[[object, object], None] | None = None)[source]#
- Mount point callback to fetch on disk the request’s content. The request urls provided are url decoded first to resolve the associated file on disk. - This is meant to be used as ‘body’ argument of the requests_mock.get() method. - It will look for files on the local filesystem based on the requested URL, using the following rules: - files are searched in the datadir/<hostname> directory 
- the local file name is the path part of the URL with path hierarchy markers (aka ‘/’) replaced by ‘_’ 
 - Eg. if you use the requests_mock fixture in your test file as: - requests_mock.get(‘https?://nowhere.com’, body=get_response_cb) # or even requests_mock.get(re.compile(‘https?://’), body=get_response_cb) - then a call requests.get like: - requests.get(’https://nowhere.com/path/to/resource?a=b&c=d’) - will look the content of the response in: - datadir/https_nowhere.com/path_to_resource,a=b,c=d - or a call requests.get like: - requests.get(’http://nowhere.com/path/to/resource?a=b&c=d’) - will look the content of the response in: - datadir/http_nowhere.com/path_to_resource,a=b,c=d - Parameters:
- request – input HTTP request 
- context – Object holding response metadata information (status_code, headers, etc…) 
- datadir – Data files path 
- ignore_urls – urls whose status response should be 404 even if the local file exists 
- visits – Dict of url, number of visits. If None, disable multi visit support (default) 
- response_context_callback – Optional callback function taking request and response context object (having headers, status_code, reason and cookies as attributes) as parameters for extra processing 
 
- Returns:
- Optional[FileDescriptor] on disk file to read from the test context 
 
- swh.core.pytest_plugin.datadir(request: FixtureRequest) str[source]#
- By default, returns the test directory’s data directory. - This can be overridden on a per file tree basis. Add an override definition in the local conftest, for example: - import pytest from os import path @pytest.fixture def datadir(): return path.join(path.abspath(path.dirname(__file__)), 'resources') 
- swh.core.pytest_plugin.requests_mock_datadir_factory(ignore_urls: List[str] = [], has_multi_visit: bool = False, response_context_callback: Callable[[object, object], None] | None = None)[source]#
- This factory generates fixtures which allow to look for files on the local filesystem based on the requested URL, using the following rules: - files are searched in the data/<hostname> directory 
- the local file name is the path part of the URL with path hierarchy markers (aka ‘/’) replaced by ‘_’ 
 - Multiple implementations are possible, for example: - requests_mock_datadir_factory([])- This computes the file name from the query and always returns the same result. - requests_mock_datadir_factory(has_multi_visit=True)- This computes the file name from the query and returns the content of the filename the first time, the next call returning the content of files suffixed with _visit1 and so on and so forth. If the file is not found, returns a 404. - requests_mock_datadir_factory(ignore_urls=['url1', 'url2'])- This will ignore any files corresponding to url1 and url2, always returning 404. - Parameters:
- ignore_urls – List of urls to always returns 404 (whether file exists or not) 
- has_multi_visit – Activate or not the multiple visits behavior 
- response_context_callback – Optional callback function taking request and response context object (having headers, status_code, reason and cookies as attributes) as parameters for extra processing 
 
 
- swh.core.pytest_plugin.requests_mock_datadir(requests_mock, datadir)#
- Instance of - requests_mock_datadir_factory(), with the default arguments.
- swh.core.pytest_plugin.requests_mock_datadir_visits(requests_mock, datadir)#
- Instance of - requests_mock_datadir_factory(), with the default arguments, but has_multi_visit=True.
- swh.core.pytest_plugin.swh_rpc_client(swh_rpc_client_class, swh_rpc_adapter)[source]#
- This fixture generates an RPCClient instance that uses the class generated by the rpc_client_class fixture as backend. - Since it uses the swh_rpc_adapter, HTTP queries will be intercepted and routed directly to the current Flask app (as provided by the app fixture). - So this stack of fixtures allows to test the RPCClient -> RPCServerApp communication path using a real RPCClient instance and a real Flask (RPCServerApp) app instance. - To use this fixture: - ensure an app fixture exists and generate a Flask application, 
- implement an swh_rpc_client_class fixtures that returns the RPCClient-based class to use as client side for the tests, 
- implement your tests using this swh_rpc_client fixture. 
 - See swh/core/api/tests/test_rpc_client_server.py for an example of usage. 
- swh.core.pytest_plugin.swh_rpc_adapter(app)[source]#
- Fixture that generates a requests.Adapter instance that can be used to test client/servers code based on swh.core.api classes. - See swh/core/api/tests/test_rpc_client_server.py for an example of usage. 
- swh.core.pytest_plugin.statsd()[source]#
- Simple fixture giving a Statsd instance suitable for tests - The Statsd instance uses a FakeSocket as .socket attribute in which one can get the accumulated statsd messages in a deque in .socket.payloads. 
- swh.core.pytest_plugin.mock_get_swh_backend_module(request, mocker, datadir, mock_import_module)#