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

Allow using `psycopg` instead of `psycopg2` #205

Closed Popkornium18 closed 5 months ago

Popkornium18 commented 5 months ago

Right now testing non-async with postgres requires the use of the legacy psycopg2 package. psycopg is the successor to psycopg2 with a very unfortunate name. It is recommended to use the newer package unless you are maintaining an existing application which can't be migrated. It would be great if PMR would allow (and maybe default to) the use of the more modern psycopg package.

DanCardin commented 5 months ago

I believe the psycopg driver should work, if you configure it: https://pytest-mock-resources.readthedocs.io/en/latest/postgres.html

With that said, our code is already doing some automatic swapping of the default driver based on heuristics. I suppose we could introspect whether one or the other driver is installed and make a more intelligent dynamic selection when it's unset.

Popkornium18 commented 5 months ago

How would I configure create_postgres_fixture() to use the modified PostgresConfig? Just including the pmr_postgres_config fixture results in a fixture scope mismatch.

Edit: I see, modifying the scope seems to be the way to go.

Popkornium18 commented 5 months ago

The example in https://pytest-mock-resources.readthedocs.io/en/latest/postgres.html is missing a return.

DanCardin commented 5 months ago

i shall fix the docs alongside the improved default heuristics, should probably look like:

      from pytest_mock_resources import PostgresConfig

      @pytest.fixture(scope='session')
      def pmr_postgres_config():
          return PostgresConfig(drivername='postgresql+psycopg2')  # but whatever driver you require.
DanCardin commented 5 months ago

If you're interested in it doing the "right" thing by default, feel free to try out the above branch before I merge it.

Popkornium18 commented 5 months ago

I'll do that tomorrow.