summaryrefslogtreecommitdiff
path: root/tests/composite_pk/tests.py
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2025-06-03 21:53:10 -0400
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-08-21 16:47:41 +0200
commitd3cf24e9b415b41f570c9f426b2cd113b5fdb4de (patch)
treea3f5ba9a228551d83014dc6d9a47fc0002c14a73 /tests/composite_pk/tests.py
parenta2ce4900a63f91f0cc685ac157762610c199c391 (diff)
Refs #36430, #36416, #34378 -- Simplified batch size calculation in QuerySet.in_bulk().
Diffstat (limited to 'tests/composite_pk/tests.py')
-rw-r--r--tests/composite_pk/tests.py30
1 files changed, 17 insertions, 13 deletions
diff --git a/tests/composite_pk/tests.py b/tests/composite_pk/tests.py
index cc78f3495a..c4a8e6ca8c 100644
--- a/tests/composite_pk/tests.py
+++ b/tests/composite_pk/tests.py
@@ -147,20 +147,24 @@ class CompositePKTests(TestCase):
result = Comment.objects.in_bulk([self.comment.pk])
self.assertEqual(result, {self.comment.pk: self.comment})
- @unittest.mock.patch.object(
- type(connection.features), "max_query_params", new_callable=lambda: 10
- )
- def test_in_bulk_batching(self, mocked_max_query_params):
+ def test_in_bulk_batching(self):
Comment.objects.all().delete()
- num_requiring_batching = (connection.features.max_query_params // 2) + 1
- comments = [
- Comment(id=i, tenant=self.tenant, user=self.user)
- for i in range(1, num_requiring_batching + 1)
- ]
- Comment.objects.bulk_create(comments)
- id_list = list(Comment.objects.values_list("pk", flat=True))
- with self.assertNumQueries(2):
- comment_dict = Comment.objects.in_bulk(id_list=id_list)
+ batching_required = connection.features.max_query_params is not None
+ expected_queries = 2 if batching_required else 1
+ with unittest.mock.patch.object(
+ type(connection.features), "max_query_params", 10
+ ):
+ num_requiring_batching = (
+ connection.ops.bulk_batch_size([Comment._meta.pk], []) + 1
+ )
+ comments = [
+ Comment(id=i, tenant=self.tenant, user=self.user)
+ for i in range(1, num_requiring_batching + 1)
+ ]
+ Comment.objects.bulk_create(comments)
+ id_list = list(Comment.objects.values_list("pk", flat=True))
+ with self.assertNumQueries(expected_queries):
+ comment_dict = Comment.objects.in_bulk(id_list=id_list)
self.assertQuerySetEqual(comment_dict, id_list)
def test_iterator(self):