Changeset View
Changeset View
Standalone View
Standalone View
swh/core/db/pytest_plugin.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 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 glob | import glob | ||||
import logging | import logging | ||||
import subprocess | import subprocess | ||||
from typing import List, Optional, Set, Union | from typing import List, Optional, Set, Union | ||||
from _pytest.fixtures import FixtureRequest | from _pytest.fixtures import FixtureRequest | ||||
import psycopg2 | import psycopg2 | ||||
import pytest | import pytest | ||||
from pytest_postgresql.janitor import DatabaseJanitor, Version | from pytest_postgresql.janitor import DatabaseJanitor, Version | ||||
try: | try: | ||||
from pytest_postgresql.config import get_config as pytest_postgresql_get_config | from pytest_postgresql.config import get_config as _pytest_postgresql_get_config | ||||
except ImportError: | except ImportError: | ||||
# pytest_postgresql < 3.0.0 | # pytest_postgresql < 3.0.0 | ||||
from pytest_postgresql.factories import get_config as pytest_postgresql_get_config | from pytest_postgresql.factories import get_config as _pytest_postgresql_get_config | ||||
from swh.core.utils import numfile_sortkey as sortkey | from swh.core.utils import numfile_sortkey as sortkey | ||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
class SWHDatabaseJanitor(DatabaseJanitor): | class SWHDatabaseJanitor(DatabaseJanitor): | ||||
"""SWH database janitor implementation with a a different setup/teardown policy than | """SWH database janitor implementation with a a different setup/teardown policy than | ||||
▲ Show 20 Lines • Show All 121 Lines • ▼ Show 20 Lines | ): | ||||
@pytest.fixture | @pytest.fixture | ||||
def postgresql_factory(request: FixtureRequest): | def postgresql_factory(request: FixtureRequest): | ||||
"""Fixture factory for PostgreSQL. | """Fixture factory for PostgreSQL. | ||||
:param FixtureRequest request: fixture request object | :param FixtureRequest request: fixture request object | ||||
:rtype: psycopg2.connection | :rtype: psycopg2.connection | ||||
:returns: postgresql client | :returns: postgresql client | ||||
""" | """ | ||||
config = pytest_postgresql_get_config(request) | config = _pytest_postgresql_get_config(request) | ||||
proc_fixture = request.getfixturevalue(process_fixture_name) | proc_fixture = request.getfixturevalue(process_fixture_name) | ||||
pg_host = proc_fixture.host | pg_host = proc_fixture.host | ||||
pg_port = proc_fixture.port | pg_port = proc_fixture.port | ||||
pg_user = proc_fixture.user | pg_user = proc_fixture.user | ||||
pg_options = proc_fixture.options | pg_options = proc_fixture.options | ||||
pg_db = dbname or config["dbname"] | pg_db = dbname or config["dbname"] | ||||
with SWHDatabaseJanitor( | with SWHDatabaseJanitor( | ||||
Show All 19 Lines |