Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/conftest.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 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 | ||||
from django.urls import reverse | from django.urls import reverse | ||||
from django.test.utils import setup_databases # type: ignore | |||||
# mypy is asked to ignore the import statement above because setup_databases | |||||
# 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.tests.conftest import * # noqa | from swh.scheduler.tests.conftest import * # noqa | ||||
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 SWHDefaultConfig | |||||
from swh.deposit.config import ( | from swh.deposit.config import ( | ||||
COL_IRI, EDIT_SE_IRI, DEPOSIT_STATUS_DEPOSITED, DEPOSIT_STATUS_REJECTED, | COL_IRI, EDIT_SE_IRI, DEPOSIT_STATUS_DEPOSITED, DEPOSIT_STATUS_REJECTED, | ||||
DEPOSIT_STATUS_PARTIAL, DEPOSIT_STATUS_LOAD_SUCCESS, | DEPOSIT_STATUS_PARTIAL, DEPOSIT_STATUS_LOAD_SUCCESS, | ||||
DEPOSIT_STATUS_VERIFIED, DEPOSIT_STATUS_LOAD_FAILURE | DEPOSIT_STATUS_VERIFIED, 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': { | 'collection': { | ||||
'name': 'test' | 'name': 'test' | ||||
}, | }, | ||||
} | } | ||||
def execute_sql(sql): | TEST_CONFIG = { | ||||
"""Execute sql to postgres db""" | 'max_upload_size': 500, | ||||
with psycopg2.connect(database='postgres') as conn: | 'extraction_dir': '/tmp/swh-deposit/test/extraction-dir', | ||||
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) | 'checks': False, | ||||
cur = conn.cursor() | 'provider': { | ||||
cur.execute(sql) | 'provider_name': '', | ||||
'provider_type': 'deposit_client', | |||||
'provider_url': '', | |||||
'metadata': { | |||||
} | |||||
}, | |||||
'tool': { | |||||
'name': 'swh-deposit', | |||||
'version': '0.0.1', | |||||
'configuration': { | |||||
'sword_version': '2' | |||||
} | |||||
}, | |||||
} | |||||
@pytest.hookimpl(tryfirst=True) | def pytest_configure(): | ||||
def pytest_load_initial_conftests(early_config, parser, args): | setup_django_for('testing') | ||||
"""This hook is done prior to django loading. | |||||
Used to initialize the deposit's server db. | |||||
""" | |||||
import project.app.signals # type: ignore | |||||
def prepare_db(*args, **kwargs): | @pytest.fixture() | ||||
def deposit_config(): | |||||
return TEST_CONFIG | |||||
@pytest.fixture(autouse=True) | |||||
def deposit_autoconfig(monkeypatch, deposit_config, swh_scheduler_config): | |||||
"""Enforce config for deposit classes inherited from SWHDefaultConfig.""" | |||||
def mock_parse_config(*args, **kw): | |||||
config = deposit_config.copy() | |||||
config['scheduler'] = { | |||||
'cls': 'local', | |||||
'args': swh_scheduler_config, | |||||
} | |||||
return config | |||||
monkeypatch.setattr( | |||||
SWHDefaultConfig, "parse_config_file", | |||||
mock_parse_config) | |||||
scheduler = get_scheduler('local', swh_scheduler_config) | |||||
task_type = { | |||||
'type': 'load-deposit', | |||||
ardumont: `.package.`
not that it matters i think. | |||||
'backend_name': 'swh.loader.packages.deposit.tasks.LoadDeposit', | |||||
Not Done Inline Actionsbecause why not? i don't know, maybe to ease readability... so maybe this can be dropped now that we know. ardumont: because why not?
i don't know, maybe to ease readability...
but in the end, task types usually… | |||||
'description': 'why does this have not-null constraint?'} | |||||
scheduler.create_task_type(task_type) | |||||
@pytest.fixture(scope='session') | |||||
def django_db_setup( | |||||
request, | |||||
django_db_blocker, | |||||
postgresql_proc): | |||||
from django.conf import settings | from django.conf import settings | ||||
db_name = 'tests' | settings.DATABASES['default'].update({ | ||||
# work around db settings for django | |||||
for k, v in [ | |||||
('ENGINE', 'django.db.backends.postgresql'), | ('ENGINE', 'django.db.backends.postgresql'), | ||||
('NAME', 'tests'), | ('NAME', 'tests'), | ||||
('USER', postgresql_proc.user), # noqa | ('USER', postgresql_proc.user), # noqa | ||||
('HOST', postgresql_proc.host), # noqa | ('HOST', postgresql_proc.host), # noqa | ||||
('PORT', postgresql_proc.port), # noqa | ('PORT', postgresql_proc.port), # noqa | ||||
]: | }) | ||||
settings.DATABASES['default'][k] = v | with django_db_blocker.unblock(): | ||||
setup_databases( | |||||
verbosity=request.config.option.verbose, | |||||
interactive=False, | |||||
keepdb=False) | |||||
execute_sql('DROP DATABASE IF EXISTS %s' % db_name) | |||||
execute_sql('CREATE DATABASE %s TEMPLATE template0' % db_name) | |||||
project.app.signals.something = prepare_db | def execute_sql(sql): | ||||
"""Execute sql to postgres db""" | |||||
with psycopg2.connect(database='postgres') as conn: | |||||
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) | |||||
cur = conn.cursor() | |||||
cur.execute(sql) | |||||
@pytest.fixture(autouse=True, scope='session') | @pytest.fixture(autouse=True, scope='session') | ||||
def swh_proxy(): | def swh_proxy(): | ||||
"""Automatically inject this fixture in all tests to ensure no outside | """Automatically inject this fixture in all tests to ensure no outside | ||||
connection takes place. | connection takes place. | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 259 Lines • Show Last 20 Lines |
.package.
not that it matters i think.