jeancochrane / jeancochrane/pytest-flask-sqlalchemy
Reflection does not work
- Dominant language
- Python
- Stars
- 253
- Forks
- 45
- PR merge metrics
- No merged PRs in 30d
Description
Code that uses [SQLAlchemy's Reflection facilities](https://docs.sqlalchemy.org/en/14/core/reflection.html) won't work with a monkey patched database engine.
code like this:
```python
messages = Table('messages', meta, autoload_with=engine)
```
raises the error `sqlalchemy.exc.NoInspectionAvailable: No inspection system is available for object of type `
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:
```python
class EngineMock(mocker.MagickMock):
pass
engine = EngineMock(spec=sa.engine.Engine)
sqlalchemy.inspections._registrars[EngineMock] = sqlalchemy.engine.Inspector._engine_insp
```
=
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.