0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-12-01 04:12:23 +01:00
posthog/ee/clickhouse/queries/test/test_person_query.py
Karl-Aksel Puulmann 457e151f58
Push person predicates down (#6346)
* Refactor column_optimizer to work differently

* WIP: Use counter over set

* Handle person filters in person query

* Remove a dead argument

* Use enum over parameter for determining behavior

* Allow excluding person properties mode when handled in person query

* Fix _get_person_query type

* Use correct table for funnel_event_query

* Remove unneeded override

* Add extra typing

* Filter by entity.properties in person query for trends

* Handle error 184 due to naming clash

* Better default for prop_filter_json_extract

* Update column_optimizer tests for Counter

* Handle person_props as extra_fields

* Handle breakdowns and person property filter pushdown

* Transform values correctly

* Simplify get_entity_filtering_params

* Fix funnel correlations

* Solve caching issues in trend people queries

* Remove @skip test

* Add syrupy tests for parse_prop_clauses

Can update these via --snapshot-update

* Add snapshot tests for person queries

* Add a few notes

* Update test to avoid collision

* Kill dead code

* Handle PR comments

* Update ee/clickhouse/queries/person_query.py

Co-authored-by: Neil Kakkar <neilkakkar@gmail.com>

Co-authored-by: Neil Kakkar <neilkakkar@gmail.com>
2021-10-13 14:00:47 +00:00

100 lines
3.4 KiB
Python

import pytest
from ee.clickhouse.client import sync_execute
from ee.clickhouse.materialized_columns import materialize
from ee.clickhouse.queries.person_query import ClickhousePersonQuery
from posthog.models.filters import Filter
from posthog.models.person import Person
from posthog.models.team import Team
def _create_person(**kwargs):
person = Person.objects.create(**kwargs)
return Person(id=person.uuid, uuid=person.uuid)
def person_query(team: Team, filter: Filter, **kwargs):
return ClickhousePersonQuery(filter, team.pk, **kwargs).get_query()[0]
def run_query(team: Team, filter: Filter, **kwargs):
query, params = ClickhousePersonQuery(filter, team.pk, **kwargs).get_query()
rows = sync_execute(query, {**params, "team_id": team.pk})
if len(rows) > 0:
return {"rows": len(rows), "columns": len(rows[0])}
else:
return {"rows": 0}
@pytest.fixture
def testdata(db, team):
materialize("person", "email")
_create_person(distinct_ids=["1"], team_id=team.pk, properties={"email": "tim@posthog.com"})
_create_person(distinct_ids=["2"], team_id=team.pk, properties={"email": "marius@posthog.com"})
_create_person(distinct_ids=["3"], team_id=team.pk, properties={"email": "karl@example.com"})
def test_person_query(testdata, team, snapshot):
filter = Filter(data={"properties": []})
assert person_query(team, filter) == snapshot
assert run_query(team, filter) == {"rows": 3, "columns": 1}
filter = Filter(
data={
"properties": [
{"key": "event_prop", "value": "value"},
{"key": "email", "type": "person", "value": "posthog", "operator": "icontains"},
],
}
)
assert person_query(team, filter) == snapshot
assert run_query(team, filter) == {"rows": 2, "columns": 1}
def test_person_query_with_extra_requested_fields(testdata, team, snapshot):
filter = Filter(
data={
"properties": [{"key": "email", "type": "person", "value": "posthog", "operator": "icontains"},],
"breakdown": "person_prop_4326",
"breakdown_type": "person",
},
)
assert person_query(team, filter) == snapshot
assert run_query(team, filter) == {"rows": 2, "columns": 2}
filter = filter.with_data({"breakdown": "email", "breakdown_type": "person"})
assert person_query(team, filter) == snapshot
assert run_query(team, filter) == {"rows": 2, "columns": 2}
def test_person_query_with_entity_filters(testdata, team, snapshot):
filter = Filter(
data={
"events": [
{
"id": "$pageview",
"properties": [{"key": "email", "type": "person", "value": "karl", "operator": "icontains"}],
}
]
}
)
assert person_query(team, filter) == snapshot
assert run_query(team, filter) == {"rows": 3, "columns": 2}
assert person_query(team, filter, entity=filter.entities[0]) == snapshot
assert run_query(team, filter, entity=filter.entities[0]) == {"rows": 1, "columns": 1}
def test_person_query_with_extra_fields(testdata, team, snapshot):
filter = Filter(
data={"properties": [{"key": "email", "type": "person", "value": "posthog", "operator": "icontains"},]},
)
assert person_query(team, filter, extra_fields=["person_props", "pmat_email"]) == snapshot
assert run_query(team, filter, extra_fields=["person_props", "pmat_email"]) == {"rows": 2, "columns": 3}