Changeset View
Changeset View
Standalone View
Standalone View
swh/core/pytest_plugin.py
Show First 20 Lines • Show All 134 Lines • ▼ Show 20 Lines | def datadir(request: FixtureRequest) -> str: | ||||
""" | """ | ||||
return path.join(path.dirname(str(request.fspath)), "data") | return path.join(path.dirname(str(request.fspath)), "data") | ||||
def requests_mock_datadir_factory( | def requests_mock_datadir_factory( | ||||
ignore_urls: List[str] = [], has_multi_visit: bool = False | ignore_urls: List[str] = [], has_multi_visit: bool = False | ||||
): | ): | ||||
"""This factory generates fixture which allow to look for files on the | """This factory generates fixtures which allow to look for files on the | ||||
local filesystem based on the requested URL, using the following rules: | local filesystem based on the requested URL, using the following rules: | ||||
- files are searched in the datadir/<hostname> directory | - files are searched in the data/<hostname> directory | ||||
- the local file name is the path part of the URL with path hierarchy | - the local file name is the path part of the URL with path hierarchy | ||||
markers (aka '/') replaced by '_' | markers (aka '/') replaced by '_' | ||||
Multiple implementations are possible, for example: | Multiple implementations are possible, for example: | ||||
- requests_mock_datadir_factory([]): | ``requests_mock_datadir_factory([])`` | ||||
This computes the file name from the query and always returns the same | This computes the file name from the query and always returns the same | ||||
result. | result. | ||||
- requests_mock_datadir_factory(has_multi_visit=True): | ``requests_mock_datadir_factory(has_multi_visit=True)`` | ||||
This computes the file name from the query and returns the content of | 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 | 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 | files suffixed with _visit1 and so on and so forth. If the file is not | ||||
found, returns a 404. | found, returns a 404. | ||||
- requests_mock_datadir_factory(ignore_urls=['url1', 'url2']): | ``requests_mock_datadir_factory(ignore_urls=['url1', 'url2'])`` | ||||
This will ignore any files corresponding to url1 and url2, always | This will ignore any files corresponding to url1 and url2, always | ||||
returning 404. | returning 404. | ||||
Args: | Args: | ||||
ignore_urls: List of urls to always returns 404 (whether file | ignore_urls: List of urls to always returns 404 (whether file | ||||
exists or not) | exists or not) | ||||
has_multi_visit: Activate or not the multiple visits behavior | has_multi_visit: Activate or not the multiple visits behavior | ||||
Show All 17 Lines | def requests_mock_datadir(requests_mock, datadir): | ||||
) | ) | ||||
return requests_mock | return requests_mock | ||||
return requests_mock_datadir | return requests_mock_datadir | ||||
# Default `requests_mock_datadir` implementation | # Default `requests_mock_datadir` implementation | ||||
requests_mock_datadir = requests_mock_datadir_factory([]) | requests_mock_datadir = requests_mock_datadir_factory() | ||||
""" | |||||
Instance of :py:func:`requests_mock_datadir_factory`, | |||||
with the default arguments. | |||||
""" | |||||
# Implementation for multiple visits behavior: | # Implementation for multiple visits behavior: | ||||
# - first time, it checks for a file named `filename` | # - first time, it checks for a file named `filename` | ||||
# - second time, it checks for a file named `filename`_visit1 | # - second time, it checks for a file named `filename`_visit1 | ||||
# etc... | # etc... | ||||
requests_mock_datadir_visits = requests_mock_datadir_factory(has_multi_visit=True) | requests_mock_datadir_visits = requests_mock_datadir_factory(has_multi_visit=True) | ||||
""" | |||||
Instance of :py:func:`requests_mock_datadir_factory`, | |||||
with the default arguments, but `has_multi_visit=True`. | |||||
""" | |||||
@pytest.fixture | @pytest.fixture | ||||
def swh_rpc_client(swh_rpc_client_class, swh_rpc_adapter): | def swh_rpc_client(swh_rpc_client_class, swh_rpc_adapter): | ||||
"""This fixture generates an RPCClient instance that uses the class generated | """This fixture generates an RPCClient instance that uses the class generated | ||||
by the rpc_client_class fixture as backend. | by the rpc_client_class fixture as backend. | ||||
Since it uses the swh_rpc_adapter, HTTP queries will be intercepted and | Since it uses the swh_rpc_adapter, HTTP queries will be intercepted and | ||||
▲ Show 20 Lines • Show All 107 Lines • Show Last 20 Lines |