Skip to content

Commit cd0966c

Browse files
charettessarahboyce
authored andcommitted
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.
1 parent 5eca562 commit cd0966c

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

tests/bulk_create/tests.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
from django.test import (
1616
TestCase,
1717
TransactionTestCase,
18-
override_settings,
1918
skipIfDBFeature,
2019
skipUnlessDBFeature,
2120
)
21+
from django.test.utils import CaptureQueriesContext
2222
from django.utils import timezone
2323

2424
from .models import (
@@ -217,12 +217,11 @@ def test_large_single_field_batch(self):
217217

218218
@skipUnlessDBFeature("has_bulk_insert")
219219
def test_large_batch_efficiency(self):
220-
with override_settings(DEBUG=True):
221-
connection.queries_log.clear()
220+
with CaptureQueriesContext(connection) as ctx:
222221
TwoFields.objects.bulk_create(
223222
[TwoFields(f1=i, f2=i + 1) for i in range(0, 1001)]
224223
)
225-
self.assertLess(len(connection.queries), 10)
224+
self.assertLess(len(ctx), 10)
226225

227226
def test_large_batch_mixed(self):
228227
"""
@@ -248,15 +247,14 @@ def test_large_batch_mixed_efficiency(self):
248247
Test inserting a large batch with objects having primary key set
249248
mixed together with objects without PK set.
250249
"""
251-
with override_settings(DEBUG=True):
252-
connection.queries_log.clear()
250+
with CaptureQueriesContext(connection) as ctx:
253251
TwoFields.objects.bulk_create(
254252
[
255253
TwoFields(id=i if i % 2 == 0 else None, f1=i, f2=i + 1)
256254
for i in range(100000, 101000)
257255
]
258256
)
259-
self.assertLess(len(connection.queries), 10)
257+
self.assertLess(len(ctx), 10)
260258

261259
def test_explicit_batch_size(self):
262260
objs = [TwoFields(f1=i, f2=i) for i in range(0, 4)]

0 commit comments

Comments
 (0)