Skip to content

Commit

Permalink
test(hogql): add breakdown tests for unordered funnel (#20335)
Browse files Browse the repository at this point in the history
  • Loading branch information
thmsobrmlr authored Feb 14, 2024
1 parent 6068dd1 commit f6a9e25
Show file tree
Hide file tree
Showing 7 changed files with 3,529 additions and 1,307 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

92 changes: 68 additions & 24 deletions posthog/hogql_queries/insights/funnels/test/breakdown_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,6 @@ def _get_actor_ids_at_step(self, filter, funnel_step, breakdown_value=None):

return [val["id"] for val in serialized_result]

def _create_groups(self):
GroupTypeMapping.objects.create(team=self.team, group_type="organization", group_type_index=0)
GroupTypeMapping.objects.create(team=self.team, group_type="company", group_type_index=1)

create_group(
team_id=self.team.pk,
group_type_index=0,
group_key="org:5",
properties={"industry": "finance"},
)
create_group(
team_id=self.team.pk,
group_type_index=0,
group_key="org:6",
properties={"industry": "technology"},
)
create_group(
team_id=self.team.pk,
group_type_index=1,
group_key="org:5",
properties={"industry": "random"},
)

def _assert_funnel_breakdown_result_is_correct(self, result, steps: List[FunnelStepResult]):
def funnel_result(step: FunnelStepResult, order: int) -> Dict[str, Any]:
return {
Expand Down Expand Up @@ -2681,6 +2658,73 @@ def test_funnel_breakdown_correct_breakdown_props_are_chosen_for_step(self):

self.assertCountEqual([res[0]["breakdown"] for res in results], [["Mac"], ["Safari"]])

return TestFunnelBreakdown


def funnel_breakdown_group_test_factory(FunnelPerson):
funnel_order_type = FunnelOrderType.ORDERED

class TestFunnelBreakdownGroup(APIBaseTest):
def _get_actor_ids_at_step(self, filter, funnel_step, breakdown_value=None):
filter = Filter(data=filter, team=self.team)
person_filter = filter.shallow_clone({"funnel_step": funnel_step, "funnel_step_breakdown": breakdown_value})
_, serialized_result, _ = FunnelPerson(person_filter, self.team).get_actors()

return [val["id"] for val in serialized_result]

def _create_groups(self):
GroupTypeMapping.objects.create(team=self.team, group_type="organization", group_type_index=0)
GroupTypeMapping.objects.create(team=self.team, group_type="company", group_type_index=1)

create_group(
team_id=self.team.pk,
group_type_index=0,
group_key="org:5",
properties={"industry": "finance"},
)
create_group(
team_id=self.team.pk,
group_type_index=0,
group_key="org:6",
properties={"industry": "technology"},
)
create_group(
team_id=self.team.pk,
group_type_index=1,
group_key="org:5",
properties={"industry": "random"},
)

def _assert_funnel_breakdown_result_is_correct(self, result, steps: List[FunnelStepResult]):
def funnel_result(step: FunnelStepResult, order: int) -> Dict[str, Any]:
return {
"action_id": step.name if step.type == "events" else step.action_id,
"name": step.name,
"custom_name": None,
"order": order,
"people": [],
"count": step.count,
"type": step.type,
"average_conversion_time": step.average_conversion_time,
"median_conversion_time": step.median_conversion_time,
"breakdown": step.breakdown,
"breakdown_value": step.breakdown,
**(
{
"action_id": None,
"name": f"Completed {order+1} step{'s' if order > 0 else ''}",
}
if funnel_order_type == FunnelOrderType.UNORDERED
else {}
),
}

step_results = []
for index, step_result in enumerate(steps):
step_results.append(funnel_result(step_result, index))

assert_funnel_results_equal(result, step_results)

@snapshot_clickhouse_queries
def test_funnel_breakdown_group(self):
self._create_groups()
Expand Down Expand Up @@ -3020,7 +3064,7 @@ def test_funnel_aggregate_by_groups_breakdown_group_person_on_events(self):
],
)

return TestFunnelBreakdown
return TestFunnelBreakdownGroup


def sort_breakdown_funnel_results(results: List[Dict[int, Any]]):
Expand Down
12 changes: 11 additions & 1 deletion posthog/hogql_queries/insights/funnels/test/test_funnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
funnel_conversion_time_test_factory,
)
from posthog.hogql_queries.insights.funnels.test.breakdown_cases import (
assert_funnel_results_equal,
funnel_breakdown_test_factory,
funnel_breakdown_group_test_factory,
assert_funnel_results_equal,
)
from posthog.hogql_queries.insights.funnels import Funnel
from posthog.test.test_journeys import journeys_for
Expand Down Expand Up @@ -61,6 +62,15 @@ class TestFunnelBreakdown(
pass


class TestFunnelGroupBreakdown(
ClickhouseTestMixin,
funnel_breakdown_group_test_factory( # type: ignore
ClickhouseFunnelActors,
),
):
pass


class TestFunnelConversionTime(
ClickhouseTestMixin,
funnel_conversion_time_test_factory(FunnelOrderType.ORDERED, ClickhouseFunnelActors), # type: ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
)

from posthog.hogql_queries.insights.funnels.test.breakdown_cases import (
assert_funnel_results_equal,
funnel_breakdown_test_factory,
funnel_breakdown_group_test_factory,
assert_funnel_results_equal,
)
from posthog.hogql_queries.legacy_compatibility.filter_to_query import filter_to_query
from posthog.models.action import Action
Expand Down Expand Up @@ -179,6 +180,15 @@ def test_strict_breakdown_events_with_multiple_properties(self):
self.assertCountEqual(self._get_actor_ids_at_step(filters, 2, ["Safari"]), [people["person2"].uuid])


class TestStrictFunnelGroupBreakdown(
ClickhouseTestMixin,
funnel_breakdown_group_test_factory( # type: ignore
ClickhouseFunnelStrictActors,
),
):
pass


class TestFunnelStrictStepsConversionTime(
ClickhouseTestMixin,
funnel_conversion_time_test_factory(FunnelOrderType.ORDERED, ClickhouseFunnelStrictActors), # type: ignore
Expand Down
Loading

0 comments on commit f6a9e25

Please sign in to comment.