Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/conftest.py
# Copyright (C) 2018-2021 The Software Heritage developers | # Copyright (C) 2018-2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import json | import json | ||||
import os | import os | ||||
import shutil | import shutil | ||||
from subprocess import PIPE, run | from subprocess import PIPE, run | ||||
import sys | import sys | ||||
from typing import Any, Dict, List, Optional | from typing import Any, Dict, List, Optional | ||||
from _pytest.python import Function | |||||
from hypothesis import HealthCheck, settings | from hypothesis import HealthCheck, settings | ||||
import pytest | import pytest | ||||
from django.core.cache import cache | from django.core.cache import cache | ||||
from rest_framework.test import APIClient, APIRequestFactory | from rest_framework.test import APIClient, APIRequestFactory | ||||
from swh.model.hashutil import ALGORITHMS, hash_to_bytes | from swh.model.hashutil import ALGORITHMS, hash_to_bytes | ||||
from swh.storage.algos.origin import origin_get_latest_visit_status | from swh.storage.algos.origin import origin_get_latest_visit_status | ||||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | |||||
# Fixture to get API request factory from Django REST Framework | # Fixture to get API request factory from Django REST Framework | ||||
@pytest.fixture | @pytest.fixture | ||||
def api_request_factory(): | def api_request_factory(): | ||||
return APIRequestFactory() | return APIRequestFactory() | ||||
# Initialize tests data | # Initialize tests data | ||||
@pytest.fixture(scope="session", autouse=True) | @pytest.fixture(scope="function", autouse=True) | ||||
def tests_data(): | def tests_data(): | ||||
data = get_tests_data(reset=True) | data = get_tests_data(reset=True) | ||||
# Update swh-web configuration to use the in-memory storages | # Update swh-web configuration to use the in-memory storages | ||||
# instantiated in the tests.data module | # instantiated in the tests.data module | ||||
override_storages( | override_storages( | ||||
data["storage"], data["idx_storage"], data["search"], data["counters"] | data["storage"], data["idx_storage"], data["search"], data["counters"] | ||||
) | ) | ||||
return data | return data | ||||
# Fixture to manipulate data from a sample archive used in the tests | # Fixture to manipulate data from a sample archive used in the tests | ||||
@pytest.fixture(scope="session") | @pytest.fixture(scope="function") | ||||
def archive_data(tests_data): | def archive_data(tests_data): | ||||
return _ArchiveData(tests_data) | return _ArchiveData(tests_data) | ||||
# Fixture to manipulate indexer data from a sample archive used in the tests | # Fixture to manipulate indexer data from a sample archive used in the tests | ||||
@pytest.fixture(scope="session") | @pytest.fixture(scope="function") | ||||
def indexer_data(tests_data): | def indexer_data(tests_data): | ||||
return _IndexerData(tests_data) | return _IndexerData(tests_data) | ||||
# Custom data directory for requests_mock | # Custom data directory for requests_mock | ||||
@pytest.fixture | @pytest.fixture | ||||
def datadir(): | def datadir(): | ||||
return os.path.join(os.path.abspath(os.path.dirname(__file__)), "resources") | return os.path.join(os.path.abspath(os.path.dirname(__file__)), "resources") | ||||
▲ Show 20 Lines • Show All 220 Lines • ▼ Show 20 Lines | def keycloak_oidc(keycloak_oidc, mocker): | ||||
keycloak_oidc.server_url = keycloak_config["server_url"] | keycloak_oidc.server_url = keycloak_config["server_url"] | ||||
keycloak_oidc.realm_name = keycloak_config["realm_name"] | keycloak_oidc.realm_name = keycloak_config["realm_name"] | ||||
keycloak_oidc.client_id = OIDC_SWH_WEB_CLIENT_ID | keycloak_oidc.client_id = OIDC_SWH_WEB_CLIENT_ID | ||||
keycloak_oidc_client = mocker.patch("swh.web.auth.views.keycloak_oidc_client") | keycloak_oidc_client = mocker.patch("swh.web.auth.views.keycloak_oidc_client") | ||||
keycloak_oidc_client.return_value = keycloak_oidc | keycloak_oidc_client.return_value = keycloak_oidc | ||||
return keycloak_oidc | return keycloak_oidc | ||||
@pytest.fixture | |||||
def subtest(request): | |||||
"""A hack to explicitly set up and tear down fixtures. | |||||
This fixture allows you to set up and tear down fixtures within the test | |||||
function itself. This is useful (necessary!) for using Hypothesis inside | |||||
pytest, as hypothesis will call the test function multiple times, without | |||||
setting up or tearing down fixture state as it is normally the case. | |||||
Copied from the pytest-subtesthack project, public domain license | |||||
(https://github.com/untitaker/pytest-subtesthack). | |||||
""" | |||||
parent_test = request.node | |||||
def inner(func): | |||||
if hasattr(Function, "from_parent"): | |||||
item = Function.from_parent( | |||||
parent_test, | |||||
name=request.function.__name__ + "[]", | |||||
originalname=request.function.__name__, | |||||
callobj=func, | |||||
) | |||||
else: | |||||
item = Function( | |||||
name=request.function.__name__ + "[]", parent=parent_test, callobj=func | |||||
vlorentz: unused? | |||||
Done Inline ActionsI just copied pasted the code from pytest-subtesthack, looks like that path is not taken in our current tests but it must be there for a reason. anlambert: I just copied pasted the code from pytest-subtesthack, looks like that path is not taken in our… | |||||
) | |||||
nextitem = parent_test # prevents pytest from tearing down module fixtures | |||||
item.ihook.pytest_runtest_setup(item=item) | |||||
item.ihook.pytest_runtest_call(item=item) | |||||
item.ihook.pytest_runtest_teardown(item=item, nextitem=nextitem) | |||||
return inner |
unused?