Skip to content

Commit

Permalink
Improve comment in wrap_cursor()
Browse files Browse the repository at this point in the history
More fully explain the reasoning for skipping monkey patching when
running under a Django SimpleTestCase.
  • Loading branch information
living180 authored and tim-schilling committed May 18, 2023
1 parent 5d42044 commit b7af52d
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions debug_toolbar/panels/sql/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ class SQLQueryTriggered(Exception):


def wrap_cursor(connection):
# If running a Django SimpleTestCase, which isn't allowed to access the database,
# don't perform any monkey patching.
# When running a SimpleTestCase, Django monkey patches some DatabaseWrapper
# methods, including .cursor() and .chunked_cursor(), to raise an exception
# if the test code tries to access the database, and then undoes the monkey
# patching when the test case is finished. If we monkey patch those methods
# also, Django's process of undoing those monkey patches will fail. To
# avoid this failure, and because database access is not allowed during a
# SimpleTextCase anyway, skip applying our instrumentation monkey patches if
# we detect that Django has already monkey patched DatabaseWrapper.cursor().
if isinstance(connection.cursor, django.test.testcases._DatabaseFailure):
return
if not hasattr(connection, "_djdt_cursor"):
Expand Down

0 comments on commit b7af52d

Please sign in to comment.