-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft: Add warning async is not supported #1911
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,7 @@ | ||||||||||
import io | ||||||||||
import os | ||||||||||
import re | ||||||||||
import sys | ||||||||||
import time | ||||||||||
import unittest | ||||||||||
from unittest.mock import patch | ||||||||||
|
@@ -33,6 +35,12 @@ | |||||||||
webdriver = None | ||||||||||
|
||||||||||
|
||||||||||
try: | ||||||||||
from django.test import AsyncRequestFactory | ||||||||||
except ImportError: | ||||||||||
AsyncRequestFactory = None | ||||||||||
|
||||||||||
|
||||||||||
rf = RequestFactory() | ||||||||||
|
||||||||||
|
||||||||||
|
@@ -843,3 +851,46 @@ def test_theme_toggle(self): | |||||||||
self.get("/regular/basic/") | ||||||||||
toolbar = self.selenium.find_element(By.ID, "djDebug") | ||||||||||
self.assertEqual(toolbar.get_attribute("data-theme"), "light") | ||||||||||
|
||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
https://docs.djangoproject.com/en/stable/topics/testing/advanced/#asyncrequestfactory There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the tip There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You must move that line up near the top of the file with the other imports. Modify line 16 to read |
||||||||||
@unittest.skipUnless( | ||||||||||
AsyncRequestFactory is not None, "Test valid only for django with async requests" | ||||||||||
) | ||||||||||
@override_settings(DEBUG=True) | ||||||||||
class DebugToolbarAsyncTestCase(BaseTestCase): | ||||||||||
@classmethod | ||||||||||
def setUpClass(cls): | ||||||||||
super().setUpClass() | ||||||||||
cls.async_rf = AsyncRequestFactory() | ||||||||||
cls.simple_get_response = lambda *args, **kwargs: HttpResponse( | ||||||||||
"<html><body></body></html>" | ||||||||||
) | ||||||||||
cls._default_stdout = sys.stdout | ||||||||||
|
||||||||||
def setUp(self): | ||||||||||
super().setUp() | ||||||||||
self.captured_output = io.StringIO() | ||||||||||
sys.stdout = self.captured_output | ||||||||||
|
||||||||||
@classmethod | ||||||||||
def tearDownClass(cls): | ||||||||||
super().tearDownClass() | ||||||||||
sys.stdout = cls._default_stdout | ||||||||||
|
||||||||||
def test_do_not_render_toolbar_if_it_was_async_request(self): | ||||||||||
captured_output = io.StringIO() | ||||||||||
sys.stdout = captured_output | ||||||||||
|
||||||||||
request = self.async_rf.get("/") | ||||||||||
response = DebugToolbarMiddleware(self.simple_get_response)(request) | ||||||||||
|
||||||||||
self.assertEqual(response.content, b"<html><body></body></html>") | ||||||||||
|
||||||||||
def test_prints_warning_async_is_not_supported(self): | ||||||||||
request = self.async_rf.get("/") | ||||||||||
DebugToolbarMiddleware(self.simple_get_response)(request) | ||||||||||
|
||||||||||
assert ( | ||||||||||
self.captured_output.getvalue() | ||||||||||
== "----------\nBe caution, django-debug-toolbar does not support async requests!\n----------\n" | ||||||||||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AsyncRequestFactory
is defined in Django >= v3.1 so theImportError
should not happen on all supported versions of Django.Just modify line 16 to read
from django.test import AsyncRequestFactory, RequestFactory