Skip to content

Commit

Permalink
add addtional comments for context
Browse files Browse the repository at this point in the history
  • Loading branch information
salty-ivy committed Sep 9, 2024
1 parent d86e7c2 commit 048fb94
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 75 deletions.
2 changes: 2 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class AsyncToolbarTestClient(AsyncClient):
async def request(self, **request):
# Use a thread/async task context-local variable to guard against a
# concurrent _created signal from a different thread/task.
# In cases testsuite will have both regular and async tests or
# multiple async tests running in an eventloop making async_client calls.
data_contextvar.set(None)

def handle_toolbar_created(sender, toolbar=None, **kwargs):
Expand Down
80 changes: 10 additions & 70 deletions tests/test_integration_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ def content(self):
class DebugToolbarTestCase(BaseTestCase):
_is_async = True

async def test_show_toolbar(self):
self.assertTrue(show_toolbar(self.request))
def test_show_toolbar(self):
"""
Just to verify that show_toolbar() works with an ASGIRequest too
"""

async def test_show_toolbar_DEBUG(self):
with self.settings(DEBUG=False):
self.assertFalse(show_toolbar(self.request))
self.assertTrue(show_toolbar(self.request))

async def test_show_toolbar_INTERNAL_IPS(self):
with self.settings(INTERNAL_IPS=[]):
Expand Down Expand Up @@ -86,67 +86,6 @@ def __contains__(self, x):
self.assertEqual(response.status_code, 200)
self.assertContains(response, "djDebug") # toolbar

# async def test_should_render_panels_RENDER_PANELS(self):
# """
# The toolbar should force rendering panels on each request
# based on the RENDER_PANELS setting.
# """
# toolbar = DebugToolbar(self.request, self.get_response)
# self.assertFalse(toolbar.should_render_panels())
# toolbar.config["RENDER_PANELS"] = True
# self.assertTrue(toolbar.should_render_panels())
# toolbar.config["RENDER_PANELS"] = None
# self.assertTrue(toolbar.should_render_panels())

# def test_should_render_panels_multiprocess(self):
# """
# The toolbar should render the panels on each request when wsgi.multiprocess
# is True or missing.
# """
# request = rf.get("/")
# request.META["wsgi.multiprocess"] = True
# toolbar = DebugToolbar(request, self.get_response)
# toolbar.config["RENDER_PANELS"] = None
# self.assertTrue(toolbar.should_render_panels())

# request.META["wsgi.multiprocess"] = False
# self.assertFalse(toolbar.should_render_panels())

# request.META.pop("wsgi.multiprocess")
# self.assertTrue(toolbar.should_render_panels())

# def _resolve_stats(self, path):
# # takes stats from Request panel
# request = rf.get(path)
# panel = self.toolbar.get_panel_by_id("RequestPanel")
# response = panel.process_request(request)
# panel.generate_stats(request, response)
# return panel.get_stats()

# def test_url_resolving_positional(self):
# stats = self._resolve_stats("/resolving1/a/b/")
# self.assertEqual(stats["view_urlname"], "positional-resolving")
# self.assertEqual(stats["view_func"], "tests.views.resolving_view")
# self.assertEqual(stats["view_args"], ("a", "b"))
# self.assertEqual(stats["view_kwargs"], {})

# def test_url_resolving_named(self):
# stats = self._resolve_stats("/resolving2/a/b/")
# self.assertEqual(stats["view_args"], ())
# self.assertEqual(stats["view_kwargs"], {"arg1": "a", "arg2": "b"})

# def test_url_resolving_mixed(self):
# stats = self._resolve_stats("/resolving3/a/")
# self.assertEqual(stats["view_args"], ("a",))
# self.assertEqual(stats["view_kwargs"], {"arg2": "default"})

# def test_url_resolving_bad(self):
# stats = self._resolve_stats("/non-existing-url/")
# self.assertEqual(stats["view_urlname"], "None")
# self.assertEqual(stats["view_args"], "None")
# self.assertEqual(stats["view_kwargs"], "None")
# self.assertEqual(stats["view_func"], "<no view>")

async def test_middleware_response_insertion(self):
async def get_response(request):
return regular_view(request, "İ")
Expand Down Expand Up @@ -266,11 +205,12 @@ async def test_async_sql_page(self):


# Concurrent database queries are not fully supported by Django's backend with
# psycopg2 and psycopg3's async support isn't integrated yet. As a result, in
# ASGI environments(especially with the Django Debug Toolbar), either
# deadlocks can occure in certain cases like when running
# current integrated database drivers like psycopg2, mysqlclient etc and
# support for async drivers like psycopg3's isn't integrated yet.
# As a result, in ASGI environments(especially with the Django Debug Toolbar),
# either deadlocks can occure in certain cases like when running
# tests/views/async_execute_sql_concurrently in ASGI environment
# or queries will execute synchronously. Check ou
# or queries will execute synchronously. Check out
# https://forum.djangoproject.com/t/are-concurrent-database-queries-in-asgi-a-thing/24136/2
# https://forum.djangoproject.com/t/are-concurrent-database-queries-in-asgi-a-thing/24136/2
# https://github.com/django/deps/blob/main/accepted/0009-async.rst#the-orm
Expand Down
16 changes: 11 additions & 5 deletions tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ def execute_sql(request):


async def async_execute_sql(request):
list_stor = []
"""
Some query API can be executed asynchronously but some requires
async version of itself.
# make async query filter, which is async compatible with async for.
https://docs.djangoproject.com/en/5.1/topics/db/queries/#asynchronous-queries
"""
list_store = []

# make async query with filter, which is compatible with async for.
async for user in User.objects.filter(username="test"):
list_stor.append(user)
list_store.append(user)

# make async query aget
# make async query with afirst
async_fetched_user = await User.objects.filter(username="test").afirst()
list_stor.append(async_fetched_user)
list_store.append(async_fetched_user)
return render(request, "base.html")


Expand Down

0 comments on commit 048fb94

Please sign in to comment.