-
Notifications
You must be signed in to change notification settings - Fork 45
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
Incompatible with flask-sqlalchemy-3.0 #63
Comments
Yeap, |
I believe a solution is to use SQLAlchemy directly: from sqlalchemy.orm import sessionmaker, scoped_session
db.session = scoped_session(session_factory=sessionmaker(bind=db_connection)) |
Thanks @Midnighter ! If folks are looking for a workaround for now, this is the conftest.py I'm using with latest flask-sqlalchemy for similar purposes - not as comprehensive as this package, but works for a fake session: https://github.com/pamelafox/flask-surveys-container-app/blob/main/backend/tests/conftest.py |
I'm not sure exactly how this library works, but the way config, engines, and the session are managed did change significantly in Flask-SQLAlchemy 3.0. Just monkeypatching The following code sets up transactions for each bind, then rolls them back after each test. This supports multiple binds, in case one request queries multiple databases. @pytest.fixture
def db(app):
with app.app_context():
engines = db.engines
cleanup = []
for key, engine in engines.items():
c = engine.connect()
t = c.begin()
engines[key] = c
cleanup.append((key, engine, c, t))
yield db
for key, engine, c, t in cleanup:
t.rollback()
c.close()
engines[key] = engine |
* replaces the previous not private method `create_scoped_session` with the private method `_make_scoped_session`. Related GitHub issue: jeancochrane/pytest-flask-sqlalchemy#63
* replaces the previous public method `create_scoped_session` from flask-sqlalchemy with the direct way of creating a new session with SQLAlchemy. Related GitHub issue: jeancochrane/pytest-flask-sqlalchemy#63
* replaces the previous public method `create_scoped_session` from flask-sqlalchemy with the direct way of creating a new session with SQLAlchemy. Related GitHub issue: jeancochrane/pytest-flask-sqlalchemy#63
* replaces the previous public method `create_scoped_session` from flask-sqlalchemy with the direct way of creating a new session with SQLAlchemy. Related GitHub issue: jeancochrane/pytest-flask-sqlalchemy#63
* replaces the previous public method `create_scoped_session` from flask-sqlalchemy with the direct way of creating a new session with SQLAlchemy. Related GitHub issue: jeancochrane/pytest-flask-sqlalchemy#63
I'm not sure if this works in more complex situations, but it works in my project. |
If anyone was like me and had problems migrating pytest from SQLAlchemy <= 1.4 to SQLAlchemy 2.0, I created a minimum working example and was able to backtrack through my own test suite to find out why transactional tests weren't working. |
Looks like there has been a new major release for flask-sqlalchemy https://flask-sqlalchemy.palletsprojects.com/en/3.0.x/changes/#version-3-0-0 and they have broken backwards compatibility in a few places.
For example :
Also the
create_scoped_session
method seems to have gone.The text was updated successfully, but these errors were encountered: