Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(conversations): reduce ticket query test fixtures (#87047)

Paul D'Ambrabaf8a73ddaa9
diff --git a/products/conversations/backend/api/tests/test_tickets.py b/products/conversations/backend/api/tests/test_tickets.py
index 2cf80f6cfdc6..44c135631313 100644
--- a/products/conversations/backend/api/tests/test_tickets.py
+++ b/products/conversations/backend/api/tests/test_tickets.py
@@ -789,8 +789,8 @@ class TestTicketAPI(APIBaseTest):
denormalized on the Ticket model, so no subqueries needed.
Person data is batch-fetched in a single query.
"""
- # Create 10 tickets with messages, assignments, and persons
- for i in range(10):
+ # Two tickets are enough to detect a query that scales with the result count.
+ for i in range(2):
ticket = Ticket.objects.create_with_number(
team=self.team,
channel_source=Channel.WIDGET,
@@ -821,23 +821,25 @@ class TestTicketAPI(APIBaseTest):
created_by=self.user,
)
- # Query count should be constant regardless of number of tickets
- # Includes: session, user, org, team, permissions, feature flag permission org lookup,
- # count query, tickets query, tagged_items prefetch, and the session-activity metadata
- # write (deferred to on_commit, which this test class patches to run synchronously)
- # Note: person reads go through personhog (no DB queries)
- with self.assertNumQueries(12):
- response = self.client.get(f"/api/projects/{self.team.id}/conversations/tickets/")
- self.assertEqual(response.status_code, status.HTTP_200_OK)
- # Should have original ticket + 10 new tickets = 11 total
- self.assertEqual(response.json()["count"], 11)
- # Verify all denormalized fields are present
- for ticket_data in response.json()["results"]:
- self.assertIn("message_count", ticket_data)
- self.assertIn("last_message_at", ticket_data)
- self.assertIn("last_message_text", ticket_data)
- self.assertIn("assignee", ticket_data)
- self.assertIn("person", ticket_data)
+ list_url = f"/api/projects/{self.team.id}/conversations/tickets/"
+ # Warm auth, settings, and session metadata so the measured requests differ only by page size.
+ warmup_response = self.client.get(f"{list_url}?limit=1")
+ self.assertEqual(warmup_response.status_code, status.HTTP_200_OK)
+
+ # Person reads go through personhog, so both page sizes should use the same database queries.
+ for limit in (2, 3):
+ with self.subTest(limit=limit), self.assertNumQueries(10):
+ response = self.client.get(f"{list_url}?limit={limit}")
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertEqual(response.json()["count"], 3)
+ self.assertEqual(len(response.json()["results"]), limit)
+ # Verify all denormalized fields are present
+ for ticket_data in response.json()["results"]:
+ self.assertIn("message_count", ticket_data)
+ self.assertIn("last_message_at", ticket_data)
+ self.assertIn("last_message_text", ticket_data)
+ self.assertIn("assignee", ticket_data)
+ self.assertIn("person", ticket_data)
@patch.object(transaction, "on_commit", side_effect=immediate_on_commit)