Fixed tests for all the environments

This commit is contained in:
enrico 2022-08-19 19:36:03 +08:00
parent 67ebe90742
commit 7d2b73a024
2 changed files with 15 additions and 1 deletions

View File

@ -2,7 +2,8 @@
Provides an APIView class that is the base of all views in REST framework. Provides an APIView class that is the base of all views in REST framework.
""" """
import asyncio import asyncio
from asgiref.sync import async_to_sync
import django
from django.conf import settings from django.conf import settings
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.db import connections, models from django.db import connections, models
@ -20,6 +21,11 @@ from rest_framework.schemas import DefaultSchema
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
from rest_framework.utils import formatting from rest_framework.utils import formatting
if django.VERSION >= (3, 1):
from asgiref.sync import async_to_sync
else:
async_to_sync = None
def get_view_name(view): def get_view_name(view):
""" """
@ -506,6 +512,8 @@ class APIView(View):
handler = self.http_method_not_allowed handler = self.http_method_not_allowed
if asyncio.iscoroutinefunction(handler): if asyncio.iscoroutinefunction(handler):
if not async_to_sync:
raise Exception('Async API views are supported only for django>=3.1.')
response = async_to_sync(handler)(request, *args, **kwargs) response = async_to_sync(handler)(request, *args, **kwargs)
else: else:
response = handler(request, *args, **kwargs) response = handler(request, *args, **kwargs)

View File

@ -1,5 +1,7 @@
import copy import copy
import django
import pytest
from django.test import TestCase from django.test import TestCase
from rest_framework import status from rest_framework import status
@ -136,6 +138,10 @@ class FunctionBasedViewIntegrationTests(TestCase):
assert sanitise_json_error(response.data) == expected assert sanitise_json_error(response.data) == expected
@pytest.mark.skipif(
django.VERSION < (3, 1),
reason="Async view support requires Django 3.1 or higher",
)
class ClassBasedAsyncViewIntegrationTests(TestCase): class ClassBasedAsyncViewIntegrationTests(TestCase):
def setUp(self): def setUp(self):
self.view = BasicAsyncView.as_view() self.view = BasicAsyncView.as_view()