Flask Session removal #1325
-
I am getting errors when the flask application exits and the context are popped off. @pytest.fixture(scope="session")
def app():
"""CEUI app pytest fixture."""
_app = create_app()
ctx = _app.app_context()
ctx.push()
yield _app
> ctx.pop()
tests/conftest.py:57:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
venv/lib/python3.8/site-packages/flask/ctx.py:253: in pop
self.app.do_teardown_appcontext(exc)
venv/lib/python3.8/site-packages/flask/app.py:2402: in do_teardown_appcontext
self.ensure_sync(func)(exc)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <SQLAlchemy>, exc = None
def _teardown_session(self, exc: BaseException | None) -> None:
"""Remove the current session at the end of the request.
:meta private:
.. versionadded:: 3.0
"""
> self.session.remove()
E AttributeError: 'Session' object has no attribute 'remove'
venv/lib/python3.8/site-packages/flask_sqlalchemy/extension.py:448: AttributeError
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Could you instead use the context inside a manager? Here's the example that David gives in the issue about testing: And here are some examples of fixtures I use: https://github.com/pamelafox/flask-db-quiz-example/blob/main/tests/conftest.py |
Beta Was this translation helpful? Give feedback.
-
Added! I am puzzle as to why I am unable to rollback and clean up the changes committed during the test run? I've been struggling with this for the last two days without viable solution. I don't know what I am missing exactly, please point out what I am doing wrong. @pytest.fixture(scope="session")
def app():
"""CEUI app pytest fixture."""
# create flask app
_app = create_app()
with _app.app_context():
yield _app @pytest.fixture(scope="module")
def db_session(db, app):
from sqlalchemy.engine import create_engine
Session = scoped_session(sessionmaker(bind=db.engine))
db.session = Session()
connection = db.engine.connect()
transaction = connection.begin()
yield db.session
transaction.rollback()
connection.close()
db.session.close()
Session.remove()
# Session.close()
# db.engine.dispose() I keep getting this message.
|
Beta Was this translation helpful? Give feedback.
-
The key was passing connection to the session maker, and use the factory class directly created by the scoped_session. Also using the context manager What I was doing wrong was creating another instance from the factory class. @pytest.fixture(scope="module")
def db_session(db):
"""Creates a new database session for a test."""
print(f"Pytest connection is {db.engine}")
connection = db.engine.connect()
transaction = connection.begin()
db.session = scoped_session(
session_factory=sessionmaker(
bind=connection,
)
)
yield db.session
transaction.rollback()
db.session.rollback()
db.session.remove()
db.session.close()
connection.close() |
Beta Was this translation helpful? Give feedback.
The key was passing connection to the session maker, and use the factory class directly created by the scoped_session. Also using the context manager
with app.app_context()
What I was doing wrong was creating another instance from the factory class.