#32409: TestCase async tests are not transaction-aware
-------------------------------------+-------------------------------------
     Reporter:  David                |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Testing framework    |                  Version:  3.1
     Severity:  Normal               |               Resolution:
     Keywords:  TestCase, async,     |             Triage Stage:  Accepted
  transaction                        |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

 * type:  Bug => New feature
 * stage:  Unreviewed => Accepted


Comment:

 Hi David. Thanks for the report.

 The following diff gives an `OperationalError` ''database table is
 locked'', so yes.

 {{{
 (django) ~/Documents/Django-Stack/django/tests (master)$ GIT_PAGER="" git
 diff
 diff --git a/tests/async/tests.py b/tests/async/tests.py
 index 783fbdd080..076cd9bc1d 100644
 --- a/tests/async/tests.py
 +++ b/tests/async/tests.py
 @@ -2,11 +2,11 @@ import os
  import sys
  from unittest import mock, skipIf

 -from asgiref.sync import async_to_sync
 +from asgiref.sync import async_to_sync, sync_to_async

  from django.core.cache import DEFAULT_CACHE_ALIAS, caches
  from django.core.exceptions import SynchronousOnlyOperation
 -from django.test import SimpleTestCase
 +from django.test import SimpleTestCase, TestCase
  from django.utils.asyncio import async_unsafe

  from .models import SimpleModel
 @@ -61,3 +61,19 @@ class AsyncUnsafeTest(SimpleTestCase):
              self.dangerous_method()
          except SynchronousOnlyOperation:
              self.fail('SynchronousOnlyOperation should not be raised.')
 +
 +
 +class SyncToAsyncTestCase(TestCase):
 +
 +    @classmethod
 +    def setUpTestData(cls):
 +        SimpleModel.objects.create(field=1)
 +
 +    async def test_using_sync_to_async(self):
 +        @sync_to_async
 +        def fetch():
 +            return list(SimpleModel.objects.all())
 +
 +        data = await fetch()
 +        self.assertEqual(len(data), 1)
 +        self.assertEqual(data[0].field, 1)
 }}}

 I'm inclined to think this a documentation issue — that nested
 sync_to_async calls are not yet supported — plus a new feature request to
 add that support.
 i.e. that the usage (whilst presumably reasonable) is beyond where
 Django's async support has yet reached. (I will provisionally accept on
 that basis, but happy if we want to adjust that.)

 Can I ask what the use-case is? What are you trying to do using this
 pattern?

 It looks like a tough one to actually address until we can get the async
 wrapper layer around the ORM. 🤔

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32409#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.046471b0a945cf0dc3a1235c805290bd%40djangoproject.com.

Reply via email to