diff options
| author | Simon Charette <charette.s@gmail.com> | 2025-07-02 12:18:22 -0400 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-08-15 10:45:02 +0200 |
| commit | cd0966cd4e37da8e6153cbf57c194dce29caaddc (patch) | |
| tree | 898ef19013f7153594a9d1eef5d91dd04c29d8f6 /tests/bulk_create/tests.py | |
| parent | 5eca562ac3cbfd3b0c6522f3d7ad4be034e1cb8c (diff) | |
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.
Diffstat (limited to 'tests/bulk_create/tests.py')
| -rw-r--r-- | tests/bulk_create/tests.py | 12 |
1 files 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)] |
