Skip to content

Commit

Permalink
refactor: Extract sqlalchemy compat into its own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin committed Sep 20, 2021
1 parent abceb40 commit 9187fee
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
35 changes: 3 additions & 32 deletions src/pytest_mock_resources/compat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
class ImportAdaptor(object):
__wrapped__ = False

def __init__(self, package, recommended_extra, fail_message=None, **attrs):
self.package = package
self.recommended_extra = recommended_extra
self.fail_message = fail_message

for key, value in attrs.items():
setattr(self, key, value)

def fail(self):
if self.fail_message:
fail_message = self.fail_message
else:
fail_message = "Cannot use {recommended_extra} fixtures without {package}. pip install pytest-mock-resources[{recommended_extra}]".format(
package=self.package, recommended_extra=self.recommended_extra
)

raise RuntimeError(fail_message)

def __getattr__(self, attr):
self.fail()
from pytest_mock_resources.compat.import_ import ImportAdaptor

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

try:
import psycopg2
Expand Down Expand Up @@ -88,12 +68,3 @@ def __getattr__(self, attr):
import pymysql
except ImportError:
pymysql = ImportAdaptor("pymysql", "mysql") # type: ignore

try:
from sqlalchemy.ext import asyncio as sqlalchemy_asyncio # type: ignore
except ImportError:
sqlalchemy_asyncio = ImportAdaptor( # type: ignore
"SQLAlchemy",
"SQLAlchemy >= 1.4",
fail_message="Cannot use sqlalchemy async features with SQLAlchemy < 1.4.\n",
)
23 changes: 23 additions & 0 deletions src/pytest_mock_resources/compat/import_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class ImportAdaptor(object):
__wrapped__ = False

def __init__(self, package, recommended_extra, fail_message=None, **attrs):
self.package = package
self.recommended_extra = recommended_extra
self.fail_message = fail_message

for key, value in attrs.items():
setattr(self, key, value)

def fail(self):
if self.fail_message:
fail_message = self.fail_message
else:
fail_message = "Cannot use {recommended_extra} fixtures without {package}. pip install pytest-mock-resources[{recommended_extra}]".format(
package=self.package, recommended_extra=self.recommended_extra
)

raise RuntimeError(fail_message)

def __getattr__(self, attr):
self.fail()
10 changes: 10 additions & 0 deletions src/pytest_mock_resources/compat/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pytest_mock_resources.compat.import_ import ImportAdaptor

try:
from sqlalchemy.ext import asyncio as asyncio # type: ignore
except ImportError:
asyncio = ImportAdaptor( # type: ignore
"SQLAlchemy",
"SQLAlchemy >= 1.4",
fail_message="Cannot use sqlalchemy async features with SQLAlchemy < 1.4.\n",
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sqlalchemy.sql.ddl import CreateSchema
from sqlalchemy.sql.schema import Table

from pytest_mock_resources.compat import sqlalchemy_asyncio
from pytest_mock_resources import compat


@six.add_metaclass(abc.ABCMeta)
Expand Down Expand Up @@ -188,7 +188,9 @@ async def manage_async(self, session=None):
session_factory = session
else:
session_factory = sessionmaker(
async_engine, expire_on_commit=False, class_=sqlalchemy_asyncio.AsyncSession
async_engine,
expire_on_commit=False,
class_=compat.sqlalchemy.asyncio.AsyncSession,
)
async with session_factory() as session:
yield session
Expand All @@ -210,7 +212,7 @@ def _get_async_engine(self, isolation_level=None):
options = {}
if isolation_level:
options["isolation_level"] = isolation_level
return sqlalchemy_asyncio.create_async_engine(url, **options)
return compat.sqlalchemy.asyncio.create_async_engine(url, **options)


def identify_matching_tables(metadata, table_specifier):
Expand Down

0 comments on commit 9187fee

Please sign in to comment.