_AssertNumQueriesContext not async compatible

Hello

I just tryed to use self.assertNumQueries in full async test.

Code ref: django/django/test/testcases.py at 1feedc8ef8a34484cb5afe33f5c45b543b860210 · django/django · GitHub

I also tryed to hack the issue like:

async with self.assertNumQueries(1):
  await self.async_method(some_kwargs=some_kwargs)

or

await sync_to_async(self.assertNumQueries)(1, func=async_method, some_kwargs=some_kwargs)
...
def assertNumQueries(self, num, func=None, *args, using=DEFAULT_DB_ALIAS, **kwargs):
        conn = connections[using]

        context = _AssertNumQueriesContext(self, num, conn)
        if func is None:
            return context

        with context:
            async_to_sync(func)(*args, **kwargs)

But obviously the event loop is not find event if a new one is created.

It doesn’t look too hard to add aenter and aexit methods to _AssertNumQueriesContext that then call the original method but in a sync context. I may be ablea to do it by myself. Just wanted to check if there is maybe another obvious solution I missed

No, it’s not currently compatible.

See here:

https://code.djangoproject.com/ticket/35388

Pending a proof-of-concept from someone. (I recall @felixxm putting quite a lot of thought into it previously without success)

Thank you for your answer. I see it look more easy that it really is. Will not use it for now.