Open jlariza opened 1 year ago
I created this repository to reproduce the issue https://github.com/jlariza/channels_testing_bug using cookiecutter
To test:
pip install -r requirements/local.txt
to install dependencieschannelstestingbug
pytest channels_testing_bug/myapp
django.db.utils.OperationalError: the connection is closed
error in channels_testing_bug/myapp/tests.py:20
in create_instance call.channels_testing_bug/myapp/consumers.py:14:
when it tries to filter the database.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 django.core.exceptions.SynchronousOnlyOperation
but if you use the database_sync_to_async
decorator, the OperationalError
is raised again.
I tried creating the model using acreate in the testcase but then the OperationalError
is raised in this line if MyModel.objects.filter(id=self.object_id).exists():
again.
it raises django.core.exceptions.SynchronousOnlyOperation
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.
MyModel.objects.filter(id=self.object_id).exists()
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,