Test: warm up cached lookups before comparing bulk permission-set query counts

The 5-tag vs 50-tag comparison could pick up a one-time cold-cache query
(e.g. Django's ContentType cache) on whichever call ran first, making the
assertion fail independent of actual tag count -- reproduced in CI on
Python 3.11/3.14 only. A throwaway warm-up call before both measured runs
removes that skew.
This commit is contained in:
stumpylog
2026-08-26 13:43:31 -07:00
parent cbac71c165
commit da7154cc89
+8 -2
View File
@@ -840,7 +840,7 @@ class TestBulkEditObjects(APITestCase):
"change": {"users": [self.user1.id], "groups": [group1.id]},
}
def run_with_n_tags(n: int) -> int:
def run_with_n_tags(n: int, *, capture: bool = True) -> int | None:
tags = [Tag.objects.create(name=f"perm-tag-{n}-{i}") for i in range(n)]
with CaptureQueriesContext(connection) as ctx:
response = self.client.post(
@@ -860,7 +860,13 @@ class TestBulkEditObjects(APITestCase):
for tag in tags:
self.assertEqual(get_users_with_perms(tag).count(), 2)
self.assertEqual(get_groups_with_perms(tag).count(), 1)
return len(ctx.captured_queries)
return len(ctx.captured_queries) if capture else None
# Warm up any one-time, process/instance-cached lookups (e.g. Django's
# ContentType cache) on the same code path before measuring, so the
# comparison below isn't skewed by whichever of the two calls happens
# to pay a cold-cache cost that has nothing to do with tag count.
run_with_n_tags(1, capture=False)
small_batch_queries = run_with_n_tags(5)
large_batch_queries = run_with_n_tags(50)