swh.core.pytest_plugin module

swh.core.pytest_plugin.get_response_cb(request: requests.models.Request, context, datadir, ignore_urls: List[str] = [], visits: Optional[Dict] = 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:

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:

will look the content of the response in:

datadir/http_nowhere.com/path_to_resource,a=b,c=d

Parameters
  • request – Object requests

  • context (requests.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)

Returns

Optional[FileDescriptor] on disk file to read from the test context

swh.core.pytest_plugin.datadir(*args, **kwargs)[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)[source]

This factory generates fixture which allow to 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 ‘_’

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

swh.core.pytest_plugin.requests_mock_datadir(*args, **kwargs)
swh.core.pytest_plugin.requests_mock_datadir_visits(*args, **kwargs)
swh.core.pytest_plugin.swh_rpc_client(*args, **kwargs)[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(*args, **kwargs)[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.

class swh.core.pytest_plugin.RPCTestAdapter(client)[source]

Bases: requests.adapters.BaseAdapter

build_response(req, resp)[source]
send(request, **kw)[source]

Overrides requests.adapters.BaseAdapter.send

swh.core.pytest_plugin.flask_app_client(*args, **kwargs)[source]