sqlalchemy.orm.exc.DetachedInstanceError in unit test with socketio.test_client #1616
Replies: 3 comments 9 replies
-
Two important things are missing:
|
Beta Was this translation helpful? Give feedback.
-
I did some further investigation on this and I think I found out why the instances got detached. In the with self.app.app_context():
self.socketio.server._handle_eio_message(self.eio_sid, pkt.encode()) But this calls the @app.teardown_appcontext
def shutdown_session(response_or_exc):
if app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN']:
warnings.warn(
"'COMMIT_ON_TEARDOWN' is deprecated and will be"
" removed in version 3.1. Call"
" 'db.session.commit()'` directly instead.",
DeprecationWarning,
)
if response_or_exc is None:
self.session.commit()
self.session.remove()
return response_or_exc This leads to the So my question is: How can I solve this problem? |
Beta Was this translation helpful? Give feedback.
-
One last question concerning this:
Do you have an example how to do this? You said this is best practice but I dont find a way how to do this. There is also an open question on StackOverflow for this: https://stackoverflow.com/questions/44744183/how-to-isolate-sqlalchemy-session-between-tests-and-tested-code |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
I just included
Flask-SocketIO
in my project which is based onFlask-SQLAlchemy
and wrote some tests. But I in a simple test I get the following error:The error causing test can be reduced to the following:
If I comment out the the first line that creates the socketio test client, the errors does not appear. This is quite strange because I have no idea why the creation of socketio test client can lead to detached instances.
Do you have any idea whats going on here?
The dummy users
self.dummy
andself.dummy2
are Wrappers for an ORM user class. The instances are created inside thesetup()
of the parent test class. I use them in almost any of my 300+ tests and I never had any problems before.I hope you can help me here.
Additional context
I do not think that I use
Flask-SocketIO
wrong because if I start the server and my client everything works perfectly fine. Additionally, my other simple socketio tests also work perfectly:Beta Was this translation helpful? Give feedback.
All reactions