Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflection does not work #55

Open
fgregg opened this issue Sep 24, 2021 · 0 comments · May be fixed by #56
Open

Reflection does not work #55

fgregg opened this issue Sep 24, 2021 · 0 comments · May be fixed by #56

Comments

@fgregg
Copy link
Contributor

fgregg commented Sep 24, 2021

Code that uses SQLAlchemy's Reflection facilities won't work with a monkey patched database engine.

code like this:

messages = Table('messages', meta, autoload_with=engine)

raises the error sqlalchemy.exc.NoInspectionAvailable: No inspection system is available for object of type <class 'unittest.mock.MagicMock'>

The relevant SQLAlchemy code is here: https://github.com/sqlalchemy/sqlalchemy/blob/52e8545b2df312898d46f6a5b119675e8d0aa956/lib/sqlalchemy/sql/schema.py#L693-L718

it might be enough to

Register the mocked engine in the sqlalchemy.inspections._registrars dictionary. You would need to specialize the MagickMock class when setting up the db_engine to make sure we get a handle on just the mocked engine. something like:

class EngineMock(mocker.MagickMock):
    pass
engine = EngineMock(spec=sa.engine.Engine)
sqlalchemy.inspections._registrars[EngineMock] = sqlalchemy.engine.Inspector._engine_insp

=

@fgregg fgregg linked a pull request Sep 24, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant