tests.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import asyncio
  2. import os
  3. from unittest import mock
  4. from asgiref.sync import async_to_sync, iscoroutinefunction
  5. from django.core.cache import DEFAULT_CACHE_ALIAS, caches
  6. from django.core.exceptions import ImproperlyConfigured, SynchronousOnlyOperation
  7. from django.http import HttpResponse, HttpResponseNotAllowed
  8. from django.test import RequestFactory, SimpleTestCase
  9. from django.utils.asyncio import async_unsafe
  10. from django.views.generic.base import View
  11. from .models import SimpleModel
  12. class CacheTest(SimpleTestCase):
  13. def test_caches_local(self):
  14. @async_to_sync
  15. async def async_cache():
  16. return caches[DEFAULT_CACHE_ALIAS]
  17. cache_1 = async_cache()
  18. cache_2 = async_cache()
  19. self.assertIs(cache_1, cache_2)
  20. class DatabaseConnectionTest(SimpleTestCase):
  21. """A database connection cannot be used in an async context."""
  22. async def test_get_async_connection(self):
  23. with self.assertRaises(SynchronousOnlyOperation):
  24. list(SimpleModel.objects.all())
  25. class AsyncUnsafeTest(SimpleTestCase):
  26. """
  27. async_unsafe decorator should work correctly and returns the correct
  28. message.
  29. """
  30. @async_unsafe
  31. def dangerous_method(self):
  32. return True
  33. async def test_async_unsafe(self):
  34. # async_unsafe decorator catches bad access and returns the right
  35. # message.
  36. msg = (
  37. "You cannot call this from an async context - use a thread or "
  38. "sync_to_async."
  39. )
  40. with self.assertRaisesMessage(SynchronousOnlyOperation, msg):
  41. self.dangerous_method()
  42. @mock.patch.dict(os.environ, {"DJANGO_ALLOW_ASYNC_UNSAFE": "true"})
  43. @async_to_sync # mock.patch() is not async-aware.
  44. async def test_async_unsafe_suppressed(self):
  45. # Decorator doesn't trigger check when the environment variable to
  46. # suppress it is set.
  47. try:
  48. self.dangerous_method()
  49. except SynchronousOnlyOperation:
  50. self.fail("SynchronousOnlyOperation should not be raised.")
  51. class SyncView(View):
  52. def get(self, request, *args, **kwargs):
  53. return HttpResponse("Hello (sync) world!")
  54. class AsyncView(View):
  55. async def get(self, request, *args, **kwargs):
  56. return HttpResponse("Hello (async) world!")
  57. class ViewTests(SimpleTestCase):
  58. def test_views_are_correctly_marked(self):
  59. tests = [
  60. (SyncView, False),
  61. (AsyncView, True),
  62. ]
  63. for view_cls, is_async in tests:
  64. with self.subTest(view_cls=view_cls, is_async=is_async):
  65. self.assertIs(view_cls.view_is_async, is_async)
  66. callback = view_cls.as_view()
  67. self.assertIs(iscoroutinefunction(callback), is_async)
  68. def test_mixed_views_raise_error(self):
  69. class MixedView(View):
  70. def get(self, request, *args, **kwargs):
  71. return HttpResponse("Hello (mixed) world!")
  72. async def post(self, request, *args, **kwargs):
  73. return HttpResponse("Hello (mixed) world!")
  74. msg = (
  75. f"{MixedView.__qualname__} HTTP handlers must either be all sync or all "
  76. "async."
  77. )
  78. with self.assertRaisesMessage(ImproperlyConfigured, msg):
  79. MixedView.as_view()
  80. def test_options_handler_responds_correctly(self):
  81. tests = [
  82. (SyncView, False),
  83. (AsyncView, True),
  84. ]
  85. for view_cls, is_coroutine in tests:
  86. with self.subTest(view_cls=view_cls, is_coroutine=is_coroutine):
  87. instance = view_cls()
  88. response = instance.options(None)
  89. self.assertIs(
  90. asyncio.iscoroutine(response),
  91. is_coroutine,
  92. )
  93. if is_coroutine:
  94. response = asyncio.run(response)
  95. self.assertIsInstance(response, HttpResponse)
  96. def test_http_method_not_allowed_responds_correctly(self):
  97. request_factory = RequestFactory()
  98. tests = [
  99. (SyncView, False),
  100. (AsyncView, True),
  101. ]
  102. for view_cls, is_coroutine in tests:
  103. with self.subTest(view_cls=view_cls, is_coroutine=is_coroutine):
  104. instance = view_cls()
  105. response = instance.http_method_not_allowed(request_factory.post("/"))
  106. self.assertIs(
  107. asyncio.iscoroutine(response),
  108. is_coroutine,
  109. )
  110. if is_coroutine:
  111. response = asyncio.run(response)
  112. self.assertIsInstance(response, HttpResponseNotAllowed)
  113. def test_base_view_class_is_sync(self):
  114. """
  115. View and by extension any subclasses that don't define handlers are
  116. sync.
  117. """
  118. self.assertIs(View.view_is_async, False)