From cd0966cd4e37da8e6153cbf57c194dce29caaddc Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Wed, 2 Jul 2025 12:18:22 -0400 Subject: [PATCH] Avoided usage of DEBUG setting override in bulk_create tests. Asserting an upper bound for the number of executed queries can be achieved by using CaptureQueriesContext instead of enabling the whole DEBUG machinery. --- tests/bulk_create/tests.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/bulk_create/tests.py b/tests/bulk_create/tests.py index 35180bf487..4fd9e6c7bf 100644 --- a/tests/bulk_create/tests.py +++ b/tests/bulk_create/tests.py @@ -15,10 +15,10 @@ from django.db.models.functions import Lower, Now from django.test import ( TestCase, TransactionTestCase, - override_settings, skipIfDBFeature, skipUnlessDBFeature, ) +from django.test.utils import CaptureQueriesContext from django.utils import timezone from .models import ( @@ -217,12 +217,11 @@ class BulkCreateTests(TestCase): @skipUnlessDBFeature("has_bulk_insert") def test_large_batch_efficiency(self): - with override_settings(DEBUG=True): - connection.queries_log.clear() + with CaptureQueriesContext(connection) as ctx: TwoFields.objects.bulk_create( [TwoFields(f1=i, f2=i + 1) for i in range(0, 1001)] ) - self.assertLess(len(connection.queries), 10) + self.assertLess(len(ctx), 10) def test_large_batch_mixed(self): """ @@ -248,15 +247,14 @@ class BulkCreateTests(TestCase): Test inserting a large batch with objects having primary key set mixed together with objects without PK set. """ - with override_settings(DEBUG=True): - connection.queries_log.clear() + with CaptureQueriesContext(connection) as ctx: TwoFields.objects.bulk_create( [ TwoFields(id=i if i % 2 == 0 else None, f1=i, f2=i + 1) for i in range(100000, 101000) ] ) - self.assertLess(len(connection.queries), 10) + self.assertLess(len(ctx), 10) def test_explicit_batch_size(self): objs = [TwoFields(f1=i, f2=i) for i in range(0, 4)]