Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/conftest.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-2020 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 General Public License version 3, or any later version | # License: GNU 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 os | import os | ||||
import base64 | import base64 | ||||
import pytest | import pytest | ||||
import psycopg2 | import psycopg2 | ||||
import yaml | |||||
from django.urls import reverse | from django.urls import reverse | ||||
from django.test.utils import setup_databases # type: ignore | from django.test.utils import setup_databases # type: ignore | ||||
# mypy is asked to ignore the import statement above because setup_databases | # mypy is asked to ignore the import statement above because setup_databases | ||||
# is not part of the d.t.utils.__all__ variable. | # is not part of the d.t.utils.__all__ variable. | ||||
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT | from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT | ||||
from rest_framework import status | from rest_framework import status | ||||
from rest_framework.test import APIClient | from rest_framework.test import APIClient | ||||
from typing import Mapping | from typing import Mapping | ||||
from swh.scheduler import get_scheduler | from swh.scheduler import get_scheduler | ||||
from swh.model.identifiers import DIRECTORY, swhid, REVISION, SNAPSHOT | from swh.model.identifiers import DIRECTORY, swhid, REVISION, SNAPSHOT | ||||
from swh.deposit.config import setup_django_for | from swh.deposit.config import setup_django_for | ||||
from swh.deposit.parsers import parse_xml | from swh.deposit.parsers import parse_xml | ||||
from swh.deposit.config import APIConfig | |||||
from swh.deposit.config import ( | from swh.deposit.config import ( | ||||
COL_IRI, | COL_IRI, | ||||
EDIT_SE_IRI, | EDIT_SE_IRI, | ||||
DEPOSIT_STATUS_DEPOSITED, | DEPOSIT_STATUS_DEPOSITED, | ||||
DEPOSIT_STATUS_REJECTED, | DEPOSIT_STATUS_REJECTED, | ||||
DEPOSIT_STATUS_PARTIAL, | DEPOSIT_STATUS_PARTIAL, | ||||
DEPOSIT_STATUS_LOAD_SUCCESS, | DEPOSIT_STATUS_LOAD_SUCCESS, | ||||
DEPOSIT_STATUS_VERIFIED, | DEPOSIT_STATUS_VERIFIED, | ||||
DEPOSIT_STATUS_LOAD_FAILURE, | DEPOSIT_STATUS_LOAD_FAILURE, | ||||
) | ) | ||||
from swh.deposit.tests.common import create_arborescence_archive | from swh.deposit.tests.common import create_arborescence_archive | ||||
TEST_USER = { | TEST_USER = { | ||||
"username": "test", | "username": "test", | ||||
"password": "password", | "password": "password", | ||||
"email": "test@example.org", | "email": "test@example.org", | ||||
"provider_url": "https://hal-test.archives-ouvertes.fr/", | "provider_url": "https://hal-test.archives-ouvertes.fr/", | ||||
"domain": "archives-ouvertes.fr/", | "domain": "archives-ouvertes.fr/", | ||||
"collection": {"name": "test"}, | "collection": {"name": "test"}, | ||||
} | } | ||||
TEST_CONFIG = { | def pytest_configure(): | ||||
setup_django_for("testing") | |||||
@pytest.fixture() | |||||
def deposit_config(swh_scheduler_config): | |||||
return { | |||||
"max_upload_size": 500, | "max_upload_size": 500, | ||||
"extraction_dir": "/tmp/swh-deposit/test/extraction-dir", | "extraction_dir": "/tmp/swh-deposit/test/extraction-dir", | ||||
"checks": False, | "checks": False, | ||||
"provider": { | "provider": { | ||||
"provider_name": "", | "provider_name": "", | ||||
"provider_type": "deposit_client", | "provider_type": "deposit_client", | ||||
"provider_url": "", | "provider_url": "", | ||||
"metadata": {}, | "metadata": {}, | ||||
}, | }, | ||||
"tool": { | "tool": { | ||||
"name": "swh-deposit", | "name": "swh-deposit", | ||||
"version": "0.0.1", | "version": "0.0.1", | ||||
"configuration": {"sword_version": "2"}, | "configuration": {"sword_version": "2"}, | ||||
}, | }, | ||||
"scheduler": {"cls": "local", "args": swh_scheduler_config,}, | |||||
} | } | ||||
def pytest_configure(): | |||||
setup_django_for("testing") | |||||
@pytest.fixture() | @pytest.fixture() | ||||
def deposit_config(): | def deposit_config_path(tmp_path, monkeypatch, deposit_config): | ||||
return TEST_CONFIG | conf_path = os.path.join(tmp_path, "deposit.yml") | ||||
with open(conf_path, "w") as f: | |||||
f.write(yaml.dump(deposit_config)) | |||||
monkeypatch.setenv("SWH_CONFIG_FILENAME", conf_path) | |||||
return conf_path | |||||
@pytest.fixture(autouse=True) | @pytest.fixture(autouse=True) | ||||
def deposit_autoconfig(monkeypatch, deposit_config, swh_scheduler_config): | def deposit_autoconfig(deposit_config_path, swh_scheduler_config): | ||||
"""Enforce config for deposit classes inherited from APIConfig.""" | """Enforce config for deposit classes inherited from APIConfig.""" | ||||
def mock_parse_config(*args, **kw): | |||||
config = deposit_config.copy() | |||||
config["scheduler"] = { | |||||
"cls": "local", | |||||
"args": swh_scheduler_config, | |||||
} | |||||
return config | |||||
monkeypatch.setattr(APIConfig, "parse_config_file", mock_parse_config) | |||||
scheduler = get_scheduler("local", swh_scheduler_config) | scheduler = get_scheduler("local", swh_scheduler_config) | ||||
task_type = { | task_type = { | ||||
"type": "load-deposit", | "type": "load-deposit", | ||||
"backend_name": "swh.loader.packages.deposit.tasks.LoadDeposit", | "backend_name": "swh.loader.packages.deposit.tasks.LoadDeposit", | ||||
"description": "why does this have not-null constraint?", | "description": "Load deposit task", | ||||
} | } | ||||
scheduler.create_task_type(task_type) | scheduler.create_task_type(task_type) | ||||
@pytest.fixture(scope="session") | @pytest.fixture(scope="session") | ||||
def django_db_setup(request, django_db_blocker, postgresql_proc): | def django_db_setup(request, django_db_blocker, postgresql_proc): | ||||
from django.conf import settings | from django.conf import settings | ||||
▲ Show 20 Lines • Show All 324 Lines • Show Last 20 Lines |