-
-
Notifications
You must be signed in to change notification settings - Fork 798
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
How to test sync WebsocketConsumer that queries the database #2043
Comments
I created this repository to reproduce the issue https://github.com/jlariza/channels_testing_bug using cookiecutter To test:
The consumer works; I already tested it in another project. However, the tests fail because they cannot handle the async connection to the database. Any ideas of what I'm missing or doing incorrectly? Thank you, |
I suspect this is about how pytest manages the connection between tests. I'd try inclining the model creation in the test case, to see if that makes progress and then adjust out from there. |
@carltongibson no luck. If you try to run the model creation in the test case directly, it raises I tried creating the model using acreate in the testcase but then the |
Yes, you'd need to wrap it in sync_to_async(). The idea is to reduce the number of different context so you can identify when the DB connection is being closed. |
This will be because of the transaction test case usage. From the other transaction the object isn't yet saved, I'd guess. |
Good day,
I have a
WebsocketConsumer
that queries the databased based on a parameter. The code works as expected; however, tests keep failing with this messagedjango.db.utils.OperationalError: the connection is closed
.This is the consumer implementation based on the tutorial
and this is the test I'm writing:
However, it always raises
django.db.utils.OperationalError: the connection is closed
in this lineMyModel.objects.create()
. It tries to access the database but cannot do it because the database connection is closed.I tried creating the model instance synchronously in
setUp
method; it works but then theOperationalError
is raised in this lineif MyModel.objects.filter(id=self.object_id).exists():
.Any ideas of what I'm missing or doing incorrectly?
Thank you,
The text was updated successfully, but these errors were encountered: