Getting an error trying to use `pytest_psql` when ...
# general
w
Getting an error trying to use
pytest_psql
when running tests within the pex sandbox:
Copy code
/Users/nasron/.cache/pants/named_caches/pex_root/venvs/s/e101a3aa/venv/lib/python3.8/site-packages/pytest_pgsql/plugin.py:88: in database_snapshot
    return database.create_database_snapshot(pg_engine)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

connectable = Engine(<postgresql://postgres@127.0.0.1:63347/test>)

    def create_database_snapshot(connectable):
        """Create a snapshot of the current state of the database so that we can
        restore it to this state when the test exits.
    
        Arguments:
            connectable (`sqlalchemy.engine.Connectable`):
                The engine, connection, or other Connectable to use to take this
                snapshot.
    
        Returns (dict):
            A dictionary with three keys:
    
            * ``schemas``: A tuple of the names of all the schemas present.
            * ``tables``: A list of all of the tables present. Each item in the list
              is a dictionary with the schema name, table name, and table OID.
            * ``extensions``: A tuple of the names of all the extensions currently
              installed.
        """
>       execute = connectable.execute
E       AttributeError: 'Engine' object has no attribute 'execute'
I think
pyscopg2-binary
isn’t getting installed despite being in the dependency list - but I’m note sure.
t
I am relatively new to using pants but perhaps you could check to make sure you have added
psycopg2-binary
to your test targets? The following is a snippet from my
project/tests/BUILD
file. The same directory which has my
conftest.py
file:
Copy code
python_test_utils(
    name="test_utils",
    dependencies=[
        "//:monorepodeps#psycopg2-binary",
        "//:monorepodeps#python-multipart",
        "//:app-env",
    ],
)
I had to explicitly add the dependency to my test_utils target. Hope this helps!
w
I too am new to pants! Fairly certain the tests include the
pyscopg2
because
pants dependencies
indicates it’s there
Copy code
$ ./pants dependencies --transitive iai_fl_gateway/tests:tests | grep psy
3rdparty:reqs#psycopg2-binary
👍 1
b
That error message doesn't immediately suggest it's a psycopg2 problem. Have you confirmed that, for instance,
import psycopg2
doesn't work? If you haven't seen it before, https://www.pantsbuild.org/docs/python-test-goal#debugging-tests discusses a few ways to attach a debugger to be able to check these things.
w
yup - i’ll dig a bit, thx
This ended up being a conflict in versions between the requirements/venv of the pytest plugin and the requirements/venv of the code being tested. in my toml:
Copy code
[pytest]
version = 'pytest==7.2.1'
extra_requirements = [
   'SQLAlchemy[mypy]==1.4.46', # to match 3rdparty/all_requirements.txt
   'pytest-cov',
   'pytest-env',
   'pytest-pgsql',
   'pytest-xdist'
]
lockfile='pytest.lock'
I had to add the SQLAlchemy requirement there otherwise it installs version 2 (for some reason) which is incompatible with
pytest-pqsql
There may be other libs that are conflicting with the ones in my 3rdparty/all_requirements.txt as well…
b
ah, yeah, I've suffered from that too 😦 I think https://github.com/pantsbuild/pants/issues/12449 is vaguely related
👍 1