mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-24 16:30:27 +08:00
See https://github.com/ClearcodeHQ/pytest-postgresql#how-to-use Fix failing queue_worker unit tests when psycopg2 is installed (otherwise they are skipped): ``` $ pytest test/unit/app/queue_worker/test_database_heartbeat.py::test_database_heartbeat[postgres_app] ... _________________________________________________________________________ ERROR at setup of test_database_heartbeat[postgres_app] _________________________________________________________________________ request = <SubRequest 'database_app' for <Function test_database_heartbeat[postgres_app]>> @pytest.fixture(params=["postgres_app", "sqlite_app", "sqlite_rabbitmq_app"]) def database_app(request): if request.param == "postgres_app": if not which("initdb"): pytest.skip("initdb must be on PATH for postgresql fixture") if not psycopg2: pytest.skip("psycopg2 must be installed for postgresql fixture") if request.param == "sqlite_rabbitmq_app": if not os.environ.get("GALAXY_TEST_AMQP_INTERNAL_CONNECTION"): pytest.skip("rabbitmq tests will be skipped if GALAXY_TEST_AMQP_INTERNAL_CONNECTION env var is unset") > return request.getfixturevalue(request.param) test/unit/app/queue_worker/conftest.py:67: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.10/site-packages/pytest_postgresql/factories/process.py:147: in postgresql_proc_fixture with DatabaseJanitor( .venv/lib/python3.10/site-packages/pytest_postgresql/janitor.py:54: in __init__ check_for_psycopg() _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ def check_for_psycopg() -> None: """ Function checks whether psycopg was imported. Raises ImportError if not. """ if not psycopg: > raise ImportError("No module named psycopg. Please install psycopg.") E ImportError: No module named psycopg. Please install psycopg. .venv/lib/python3.10/site-packages/pytest_postgresql/compat.py:35: ImportError ``` Also, remove check for `initdb` in PATH, because on Ubuntu it's installed in `/usr/lib/postgresql/VERSION/bin/initdb` .
72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
import os
|
|
import tempfile
|
|
from typing import Optional
|
|
|
|
import pytest
|
|
|
|
try:
|
|
import psycopg
|
|
except ImportError:
|
|
psycopg = None
|
|
|
|
try:
|
|
import psycopg2
|
|
except ImportError:
|
|
psycopg2 = None
|
|
|
|
from galaxy.app_unittest_utils import galaxy_mock
|
|
|
|
|
|
def create_base_test(connection, amqp_type: str, amqp_connection: Optional[str] = None):
|
|
app = galaxy_mock.MockApp(database_connection=connection)
|
|
app.config.database_connection = connection
|
|
app.config.amqp_internal_connection = amqp_connection or "sqlalchemy+%s" % app.config.database_connection
|
|
app.amqp_type = amqp_type
|
|
return app
|
|
|
|
|
|
@pytest.fixture()
|
|
def sqlite_connection(request):
|
|
with tempfile.NamedTemporaryFile() as temp:
|
|
yield f"sqlite:////{temp.name}"
|
|
|
|
|
|
@pytest.fixture()
|
|
def sqlite_rabbitmq_app(sqlite_connection):
|
|
def create_app():
|
|
amqp_connection = os.environ.get("GALAXY_TEST_AMQP_INTERNAL_CONNECTION")
|
|
return create_base_test(sqlite_connection, amqp_type="rabbitmq", amqp_connection=amqp_connection)
|
|
|
|
return create_app
|
|
|
|
|
|
@pytest.fixture()
|
|
def sqlite_app(sqlite_connection):
|
|
def create_app():
|
|
return create_base_test(sqlite_connection, amqp_type="sqlite")
|
|
|
|
return create_app
|
|
|
|
|
|
@pytest.fixture()
|
|
def postgres_app(postgresql_proc):
|
|
connection = "postgresql://{p.user}@{p.host}:{p.port}/".format(p=postgresql_proc)
|
|
|
|
def create_app():
|
|
return create_base_test(connection, amqp_type="postgres")
|
|
|
|
return create_app
|
|
|
|
|
|
@pytest.fixture(params=["postgres_app", "sqlite_app", "sqlite_rabbitmq_app"])
|
|
def database_app(request):
|
|
if request.param == "postgres_app":
|
|
if not psycopg:
|
|
pytest.skip("psycopg must be installed for postgresql_proc fixture")
|
|
if not psycopg2:
|
|
pytest.skip("psycopg2 must be installed for database_app fixture")
|
|
if request.param == "sqlite_rabbitmq_app":
|
|
if not os.environ.get("GALAXY_TEST_AMQP_INTERNAL_CONNECTION"):
|
|
pytest.skip("rabbitmq tests will be skipped if GALAXY_TEST_AMQP_INTERNAL_CONNECTION env var is unset")
|
|
return request.getfixturevalue(request.param)
|