0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-28 09:16:49 +01:00

reintroduce assertNumQueries (#8487)

* reintroduce assertNumQueries

* Remove and speed up

* fix two more
This commit is contained in:
Tim Glaser 2022-02-09 16:15:17 +00:00 committed by GitHub
parent d5a48fbd06
commit 1aea20c683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 14 additions and 37 deletions

View File

@ -25,14 +25,6 @@ class ClickhouseTestMixin(QueryMatchingTest):
snapshot: Any
@contextmanager
def _assertNumQueries(self, func):
yield
# Ignore assertNumQueries in clickhouse tests
def assertNumQueries(self, num, func=None, *args, using=DEFAULT_DB_ALIAS, **kwargs):
return self._assertNumQueries(func)
def capture_select_queries(self):
return self.capture_queries(("SELECT", "WITH",))

View File

@ -3,6 +3,7 @@ import urllib
from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional, Union
from django.db.models.query import Prefetch
from django.utils.timezone import now
from rest_framework import mixins, request, response, serializers, viewsets
from rest_framework.decorators import action
@ -156,6 +157,7 @@ class EventViewSet(StructuredViewSetMixin, mixins.RetrieveModelMixin, mixins.Lis
def _get_people(self, query_result: List[Dict], team: Team) -> Dict[str, Any]:
distinct_ids = [event[5] for event in query_result]
persons = get_persons_by_distinct_ids(team.pk, distinct_ids)
persons = persons.prefetch_related(Prefetch("persondistinctid_set", to_attr="distinct_ids_cache"))
distinct_to_person: Dict[str, Person] = {}
for person in persons:
for distinct_id in person.distinct_ids:

View File

@ -82,9 +82,8 @@ class TestElement(ClickhouseTestMixin, APIBaseTest):
elements=[Element(tag_name="img")],
)
with self.assertNumQueries(7):
# Django session, PostHog user, PostHog team, PostHog org membership, PostHog event aggregated,
# PostHog element group, PostHog element
with self.assertNumQueries(4):
# Django session, PostHog user, PostHog team, PostHog org membership
response = self.client.get("/api/element/stats/").json()
self.assertEqual(response[0]["count"], 2)
self.assertEqual(response[0]["hash"], event1.elements_hash)

View File

@ -61,11 +61,9 @@ class TestEvents(ClickhouseTestMixin, APIBaseTest):
_create_event(event="$pageview", team=self.team, distinct_id="some-random-uid", properties={"$ip": "8.8.8.8"})
_create_event(event="$pageview", team=self.team, distinct_id="some-other-one", properties={"$ip": "8.8.8.8"})
expected_queries = 4 # Django session, PostHog user, PostHog team, PostHog org membership
if settings.PRIMARY_DB == AnalyticsDBMS.POSTGRES:
# PostHog event, PostHog event, PostHog person, PostHog person distinct ID,
# PostHog element group, PostHog element, PostHog element
expected_queries += 7
expected_queries = (
8 # Django session, PostHog user, PostHog team, PostHog org membership, 2x team(?), person and distinct id
)
with self.assertNumQueries(expected_queries):
response = self.client.get(f"/api/projects/{self.team.id}/events/?distinct_id=2").json()
@ -88,9 +86,9 @@ class TestEvents(ClickhouseTestMixin, APIBaseTest):
event="another event", team=self.team, distinct_id="2", properties={"$ip": "8.8.8.8"},
)
expected_queries = 4 # Django session, PostHog user, PostHog team, PostHog org membership
if settings.PRIMARY_DB == AnalyticsDBMS.POSTGRES:
expected_queries += 4 # PostHog event, PostHog event, PostHog person, PostHog person distinct ID
expected_queries = (
8 # Django session, PostHog user, PostHog team, PostHog org membership, 2x team(?), person and distinct id
)
with self.assertNumQueries(expected_queries):
response = self.client.get(f"/api/projects/{self.team.id}/events/?event=event_name").json()
@ -105,9 +103,7 @@ class TestEvents(ClickhouseTestMixin, APIBaseTest):
)
event2 = _create_event(event="event_name", team=self.team, distinct_id="2", properties={"$browser": "Safari"},)
expected_queries = 4 # Django session, PostHog user, PostHog team, PostHog org membership
if settings.PRIMARY_DB == AnalyticsDBMS.POSTGRES:
expected_queries += 4 # PostHog event, PostHog event, PostHog person, PostHog person distinct ID
expected_queries = 14 # Django session, PostHog user, PostHog team, PostHog org membership, 2x team(?), person and distinct id, couple of constance inserts
with self.assertNumQueries(expected_queries):
response = self.client.get(

View File

@ -281,7 +281,7 @@ class TestInsight(ClickhouseTestMixin, LicensedTestMixin, APIBaseTest, QueryMatc
)
# 4 for request overhead (django sessions/auth), then item count + items + dashboards + users
with self.assertNumQueries(8):
with self.assertNumQueries(10):
response = self.client.get(f"/api/projects/{self.team.id}/insights")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.json()["results"]), 20)

View File

@ -85,8 +85,7 @@ def funnel_test_factory(Funnel, event_factory, person_factory):
)
self._signup_event(distinct_id="stopped_after_signup2")
with self.assertNumQueries(1):
result = funnel.run()
result = funnel.run()
self.assertEqual(result[0]["count"], 0)
def test_funnel_with_single_step(self):
@ -99,8 +98,7 @@ def funnel_test_factory(Funnel, event_factory, person_factory):
person2_stopped_after_signup = person_factory(distinct_ids=["stopped_after_signup2"], team_id=self.team.pk)
self._signup_event(distinct_id="stopped_after_signup2")
with self.assertNumQueries(1):
result = funnel.run()
result = funnel.run()
self.assertEqual(result[0]["name"], "user signed up")
self.assertEqual(result[0]["count"], 2)
@ -141,16 +139,6 @@ def funnel_test_factory(Funnel, event_factory, person_factory):
self.assertEqual(result[2]["name"], "watched movie")
self.assertEqual(result[2]["count"], 1)
# make sure it's O(n)
person_wrong_order = person_factory(distinct_ids=["badalgo"], team_id=self.team.pk)
self._signup_event(distinct_id="badalgo")
with self.assertNumQueries(3):
funnel.run()
self._pay_event(distinct_id="badalgo")
with self.assertNumQueries(3):
funnel.run()
def test_funnel_no_events(self):
funnel = Funnel(filter=Filter(data={"some": "prop"}), team=self.team)
self.assertEqual(funnel.run(), [])