Skip to content

Commit

Permalink
pytest-postgresql requires psycopg
Browse files Browse the repository at this point in the history
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` .
  • Loading branch information
nsoranzo committed Jul 28, 2023
1 parent 6a08474 commit fbe8489
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions test/unit/app/queue_worker/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

import pytest

try:
import psycopg
except ImportError:
psycopg = None

try:
import psycopg2
except ImportError:
psycopg2 = None

from galaxy.app_unittest_utils import galaxy_mock
from galaxy.util import which


def create_base_test(connection, amqp_type: str, amqp_connection: Optional[str] = None):
Expand Down Expand Up @@ -48,9 +52,6 @@ def create_app():
def postgres_app(postgresql_proc):
connection = "postgresql://{p.user}@{p.host}:{p.port}/".format(p=postgresql_proc)

if not psycopg2:
pytest.skip("psycopg2 must be installed for postgresql fixture")

def create_app():
return create_base_test(connection, amqp_type="postgres")

Expand All @@ -60,12 +61,11 @@ def create_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 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")
try:
return request.getfixturevalue(request.param)
except ImportError:
pytest.skip("psycopg2 must be installed for postgresql fixture")
return request.getfixturevalue(request.param)

0 comments on commit fbe8489

Please sign in to comment.