Skip to content

Commit

Permalink
fix: Avoid deprecated sqlalchemy URL constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin committed Sep 20, 2021
1 parent 9187fee commit 3fa64d3
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/source/async.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ For example:
@pytest.fixture
def async_pg(pmr_postgres_config):
# or `URL.create` in sqlalchemy 1.4+
create_async_engine(URL(host=pmr_postgres_config.host, database=pmr_postgres_config.database, ...))
Expand Down
2 changes: 1 addition & 1 deletion src/pytest_mock_resources/compat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pytest_mock_resources.compat.import_ import ImportAdaptor

# isort: split
from pytest_mock_resources.compat import import_, sqlalchemy # flake8: ignore
from pytest_mock_resources.compat import sqlalchemy # noqa

try:
import psycopg2
Expand Down
15 changes: 13 additions & 2 deletions src/pytest_mock_resources/compat/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import sqlalchemy.engine.url

from pytest_mock_resources.compat.import_ import ImportAdaptor

try:
from sqlalchemy.ext import asyncio as asyncio # type: ignore
from sqlalchemy.ext import asyncio # type: ignore
except ImportError:
asyncio = ImportAdaptor( # type: ignore
asyncio = ImportAdaptor(
"SQLAlchemy",
"SQLAlchemy >= 1.4",
fail_message="Cannot use sqlalchemy async features with SQLAlchemy < 1.4.\n",
)

URL = sqlalchemy.engine.url.URL
try:
# Attempt to use the newly recommended `URL.create` method when available
URL = URL.create # type: ignore
except AttributeError:
# But if it's not available, the top-level contructor should still exist,
# as this is the only available option in sqlalchemy<1.4 versions.
pass
4 changes: 2 additions & 2 deletions src/pytest_mock_resources/container/mysql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
import sqlalchemy
from sqlalchemy.engine.url import URL
from pytest_mock_resources import compat

from pytest_mock_resources.config import DockerContainerConfig, fallback
from pytest_mock_resources.container.base import ContainerCheckFailed, get_container
Expand Down Expand Up @@ -52,7 +52,7 @@ def root_database(self):

def get_sqlalchemy_engine(config, database_name, isolation_level=None):
DB_URI = str(
URL(
compat.sqlalchemy.URL(
"mysql+pymysql",
username=config.username,
password=config.password,
Expand Down
4 changes: 2 additions & 2 deletions src/pytest_mock_resources/fixture/database/generic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy.engine.url import URL
from pytest_mock_resources import compat


class Credentials:
Expand Down Expand Up @@ -31,7 +31,7 @@ def as_url(self):

def as_sqlalchemy_url(self):
"""Return a sqlalchemy :class:`sqlalchemy.engine.url.URL`."""
return URL(
return compat.sqlalchemy.URL(
drivername=self.drivername,
host=self.host,
port=self.port,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import attr
import six
from sqlalchemy import MetaData
from sqlalchemy.engine.url import URL
from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.sql.ddl import CreateSchema
Expand Down Expand Up @@ -200,7 +199,7 @@ async def manage_async(self, session=None):
self.engine.dispose()

def _get_async_engine(self, isolation_level=None):
url = URL(
url = compat.sqlalchemy.URL(
drivername="postgresql+asyncpg",
username=self.engine.pmr_credentials.username,
password=self.engine.pmr_credentials.password,
Expand Down
4 changes: 2 additions & 2 deletions tests/fixture/database/test_database.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from sqlalchemy import create_engine, text
from sqlalchemy.engine.url import URL

from pytest_mock_resources import (
compat,
create_mysql_fixture,
create_postgres_fixture,
create_redshift_fixture,
Expand Down Expand Up @@ -89,7 +89,7 @@ def test_create_custom_connection_from_dict(postgres_3):


def test_create_custom_connection_url(postgres_3):
url = URL(**postgres_3.pmr_credentials.as_sqlalchemy_url_kwargs())
url = compat.sqlalchemy.URL(**postgres_3.pmr_credentials.as_sqlalchemy_url_kwargs())
engine = create_engine(url, isolation_level="AUTOCOMMIT")
engine.execute("select 1")

Expand Down

0 comments on commit 3fa64d3

Please sign in to comment.