schireson / pytest-mock-resources

Pytest Fixtures that let you actually test against external resource (Postgres, Mongo, Redshift...) dependent code.
https://pytest-mock-resources.readthedocs.io/en/latest/quickstart.html
MIT License
179 stars 19 forks source link

psycopg2-binary is not considered as a suitable driver for Postgres. #210

Closed mgedmin closed 5 months ago

mgedmin commented 5 months ago

Describe the bug

My tox.ini lists the following deps:

[testenv]
deps =
  -rrequirements.txt
  pytest-alembic
  pytest-mock-resources[postgres-binary]

and requirements.txt has

# alembic 1.8.1 matches debian bookworm
alembic==1.8.1
# sqlalchemy-citext is not packaged for debian
sqlalchemy-citext==1.8.0
# psycopg2 2.9.5 matches debian bookworm
psycopg2-binary==2.9.5
sqlalchemy >= 1.4, < 2.0

This used to work fine, but with recent pytest-mock-resources releases I'm getting test failures everywhere with this error:

.tox/py3.11/lib/python3.11/site-packages/pytest_mock_resources/container/postgres.py:103: in get_sqlalchemy_engine
    drivername = detect_driver(config.drivername, async_=async_)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

drivername = None, async_ = False

    def detect_driver(drivername: Optional[str] = None, async_: bool = False) -> str:
        if drivername:
            return drivername

        if any(Distribution.discover(name="psycopg")):
            return "postgresql+psycopg"

        if async_:
            if any(Distribution.discover(name="asyncpg")):
                return "postgresql+asyncpg"
        else:
            if any(Distribution.discover(name="psycopg2")):
                return "postgresql+psycopg2"

>       raise ValueError(  # pragma: no cover
            "No suitable driver found for Postgres. Please install `psycopg`, `psycopg2`, "
            "`asyncpg`, or explicitly configure the `drivername=` field of `PostgresConfig`."
        )
E       ValueError: No suitable driver found for Postgres. Please install `psycopg`, `psycopg2`, `asyncpg`, or explicitly configure the `drivername=` field of `PostgresConfig`.

.tox/py3.11/lib/python3.11/site-packages/pytest_mock_resources/container/postgres.py:141: ValueError

Environment

To Reproduce Steps to reproduce the behavior:

  1. virtualenv testenv && testenv/bin/pip install pytest-mock-resources[postgres-binary]
  2. testenv/bin/python -c 'from pytest_mock_resources.container.postgres import detect_driver; print(detect_driver())'
  3. See error

Expected behavior I expected it to print postgresql+psycopg2.

Actual Behavior

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/testenv/lib/python3.11/site-packages/pytest_mock_resources/container/postgres.py", line 141, in detect_driver
    raise ValueError(  # pragma: no cover
ValueError: No suitable driver found for Postgres. Please install `psycopg`, `psycopg2`, `asyncpg`, or explicitly configure the `drivername=` field of `PostgresConfig`.

Additional context BTW testenv/bin/pip list confirms that psycopg2-binary is installed.

DanCardin commented 5 months ago

Hopefully fixed in https://github.com/schireson/pytest-mock-resources/pull/208 a little bit ago? 2.10.3

mgedmin commented 5 months ago

Wow, that PR was opened 30 minutes before I submitted this ticket. I will claim that I searched for existing issues, didn't find one, and then spent 30 minutes composing this one. ;-)