Hi there!
I am experimenting with asynchronous Django, specifically I’m trying to make this test pass:
class SomeAsyncTest(TestCase):
def _make_user(self):
return User.objects.create_user(
username="John", email="Doe", password="not-secret"
)
def _force_login(self, user):
return self.async_client.force_login(user)
@override_settings(
MIDDLEWARE=[
"django.contrib.sessions.middleware.SessionMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
]
)
async def test_it_sends_email_to_admins(self):
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
user = await sync_to_async(self._make_user)()
await sync_to_async(self._force_login)(user)
await self.async_client.get("/secret-area/")
# and more
The database for this test is Sqlite3, and without DJANGO_ALLOW_ASYNC_UNSAFE
I get django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async
, which I understand where it comes from.
If I make it “true” instead, I get django.db.utils.OperationalError: database table is locked: django_session
.
I understand the ORM is not async at this moment, but there is a way to make this test pass? Could depend from the Sqlite backend? What am I doing wrong?
EDIT: this should be Sqlite. It can’t handle concurrent connections, I forgot! It seems to work with Postgres, but I need to debug one last thing: PytestWarning: Error when trying to teardown test databases: RuntimeError("generator didn't stop after throw()")
.
Thanks in advance!