mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #31090 -- Logged transaction management queries.
Thanks to Petter Strandmark for the original idea and Mariusz Felisiak for advice during the DjangoConUS 2022 Sprint!
This commit is contained in:
committed by
Mariusz Felisiak
parent
c0a93d3941
commit
798e38c2b9
@@ -1,10 +1,16 @@
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from django.db import DEFAULT_DB_ALIAS, connection, connections
|
||||
from django.db import DEFAULT_DB_ALIAS, connection, connections, transaction
|
||||
from django.db.backends.base.base import BaseDatabaseWrapper
|
||||
from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
|
||||
from django.test import (
|
||||
SimpleTestCase,
|
||||
TestCase,
|
||||
TransactionTestCase,
|
||||
skipUnlessDBFeature,
|
||||
)
|
||||
from django.test.utils import CaptureQueriesContext, override_settings
|
||||
|
||||
from ..models import Square
|
||||
from ..models import Person, Square
|
||||
|
||||
|
||||
class DatabaseWrapperTests(SimpleTestCase):
|
||||
@@ -55,6 +61,57 @@ class DatabaseWrapperTests(SimpleTestCase):
|
||||
connection.check_database_version_supported()
|
||||
|
||||
|
||||
class DatabaseWrapperLoggingTests(TransactionTestCase):
|
||||
available_apps = []
|
||||
|
||||
@override_settings(DEBUG=True)
|
||||
def test_commit_debug_log(self):
|
||||
conn = connections[DEFAULT_DB_ALIAS]
|
||||
with CaptureQueriesContext(conn):
|
||||
with self.assertLogs("django.db.backends", "DEBUG") as cm:
|
||||
with transaction.atomic():
|
||||
Person.objects.create(first_name="first", last_name="last")
|
||||
|
||||
self.assertGreaterEqual(len(conn.queries_log), 3)
|
||||
self.assertEqual(conn.queries_log[-3]["sql"], "BEGIN")
|
||||
self.assertRegex(
|
||||
cm.output[0],
|
||||
r"DEBUG:django.db.backends:\(\d+.\d{3}\) "
|
||||
rf"BEGIN; args=None; alias={DEFAULT_DB_ALIAS}",
|
||||
)
|
||||
self.assertEqual(conn.queries_log[-1]["sql"], "COMMIT")
|
||||
self.assertRegex(
|
||||
cm.output[-1],
|
||||
r"DEBUG:django.db.backends:\(\d+.\d{3}\) "
|
||||
rf"COMMIT; args=None; alias={DEFAULT_DB_ALIAS}",
|
||||
)
|
||||
|
||||
@override_settings(DEBUG=True)
|
||||
def test_rollback_debug_log(self):
|
||||
conn = connections[DEFAULT_DB_ALIAS]
|
||||
with CaptureQueriesContext(conn):
|
||||
with self.assertLogs("django.db.backends", "DEBUG") as cm:
|
||||
with self.assertRaises(Exception), transaction.atomic():
|
||||
Person.objects.create(first_name="first", last_name="last")
|
||||
raise Exception("Force rollback")
|
||||
|
||||
self.assertEqual(conn.queries_log[-1]["sql"], "ROLLBACK")
|
||||
self.assertRegex(
|
||||
cm.output[-1],
|
||||
r"DEBUG:django.db.backends:\(\d+.\d{3}\) "
|
||||
rf"ROLLBACK; args=None; alias={DEFAULT_DB_ALIAS}",
|
||||
)
|
||||
|
||||
def test_no_logs_without_debug(self):
|
||||
with self.assertNoLogs("django.db.backends", "DEBUG"):
|
||||
with self.assertRaises(Exception), transaction.atomic():
|
||||
Person.objects.create(first_name="first", last_name="last")
|
||||
raise Exception("Force rollback")
|
||||
|
||||
conn = connections[DEFAULT_DB_ALIAS]
|
||||
self.assertEqual(len(conn.queries_log), 0)
|
||||
|
||||
|
||||
class ExecuteWrapperTests(TestCase):
|
||||
@staticmethod
|
||||
def call_execute(connection, params=None):
|
||||
|
||||
Reference in New Issue
Block a user