summaryrefslogtreecommitdiff
path: root/tests/async
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2019-04-12 06:15:18 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-06-20 12:29:43 +0200
commita415ce70bef6d91036b00dd2c8544aed7aeeaaed (patch)
tree3583cef22e9b56d2ed52456ab586d9c47620bc51 /tests/async
parentcce47ff65a4dd3786c049ec14ee889e128ca7de9 (diff)
Fixed #30451 -- Added ASGI handler and coroutine-safety.
This adds an ASGI handler, asgi.py file for the default project layout, a few async utilities and adds async-safety to many parts of Django.
Diffstat (limited to 'tests/async')
-rw-r--r--tests/async/__init__.py0
-rw-r--r--tests/async/models.py5
-rw-r--r--tests/async/tests.py36
3 files changed, 41 insertions, 0 deletions
diff --git a/tests/async/__init__.py b/tests/async/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/async/__init__.py
diff --git a/tests/async/models.py b/tests/async/models.py
new file mode 100644
index 0000000000..0fd606b07e
--- /dev/null
+++ b/tests/async/models.py
@@ -0,0 +1,5 @@
+from django.db import models
+
+
+class SimpleModel(models.Model):
+ field = models.IntegerField()
diff --git a/tests/async/tests.py b/tests/async/tests.py
new file mode 100644
index 0000000000..1e1cabc1c6
--- /dev/null
+++ b/tests/async/tests.py
@@ -0,0 +1,36 @@
+from asgiref.sync import async_to_sync
+
+from django.core.exceptions import SynchronousOnlyOperation
+from django.test import SimpleTestCase
+from django.utils.asyncio import async_unsafe
+
+from .models import SimpleModel
+
+
+class DatabaseConnectionTest(SimpleTestCase):
+ """A database connection cannot be used in an async context."""
+ @async_to_sync
+ async def test_get_async_connection(self):
+ with self.assertRaises(SynchronousOnlyOperation):
+ list(SimpleModel.objects.all())
+
+
+class AsyncUnsafeTest(SimpleTestCase):
+ """
+ async_unsafe decorator should work correctly and returns the correct
+ message.
+ """
+ @async_unsafe
+ def dangerous_method(self):
+ return True
+
+ @async_to_sync
+ async def test_async_unsafe(self):
+ # async_unsafe decorator catches bad access and returns the right
+ # message.
+ msg = (
+ 'You cannot call this from an async context - use a thread or '
+ 'sync_to_async.'
+ )
+ with self.assertRaisesMessage(SynchronousOnlyOperation, msg):
+ self.dangerous_method()