From 048fb94539d7df41e013c59dc24b4b5f4dc9621b Mon Sep 17 00:00:00 2001 From: Aman Pandey Date: Tue, 10 Sep 2024 02:17:38 +0530 Subject: [PATCH] add addtional comments for context --- tests/base.py | 2 + tests/test_integration_async.py | 80 +++++---------------------------- tests/views.py | 16 ++++--- 3 files changed, 23 insertions(+), 75 deletions(-) diff --git a/tests/base.py b/tests/base.py index a2ccc4f43..073fc9f15 100644 --- a/tests/base.py +++ b/tests/base.py @@ -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): diff --git a/tests/test_integration_async.py b/tests/test_integration_async.py index 926777e6c..0cbb4489e 100644 --- a/tests/test_integration_async.py +++ b/tests/test_integration_async.py @@ -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=[]): @@ -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"], "") - async def test_middleware_response_insertion(self): async def get_response(request): return regular_view(request, "İ") @@ -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 diff --git a/tests/views.py b/tests/views.py index 9bee1f8e5..e8528ff2e 100644 --- a/tests/views.py +++ b/tests/views.py @@ -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")