: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(product-analytics): move stickiness actor tests out of core (#88867)
Thomas Obermüller459c8403c8df
diff --git a/posthog/hogql_queries/insights/test/__snapshots__/test_insight_actors_query_runner.ambr b/posthog/hogql_queries/insights/test/__snapshots__/test_insight_actors_query_runner.ambrindex ee56b34f607b..2df43d3dd977 100644--- a/posthog/hogql_queries/insights/test/__snapshots__/test_insight_actors_query_runner.ambr+++ b/posthog/hogql_queries/insights/test/__snapshots__/test_insight_actors_query_runner.ambr@@ -166,122 +166,6 @@use_hive_partitioning=0'''# ----# name: TestInsightActorsQueryRunner.test_insight_persons_stickiness_groups_query- '''- SELECT name AS name- FROM- (SELECT groups.properties___name AS name- FROM- (SELECT aggregation_target AS group_key- FROM- (SELECT aggregation_target AS aggregation_target,- count() AS num_intervals- FROM- (SELECT e.`$group_0` AS aggregation_target,- toStartOfInterval(toTimeZone(e.timestamp, 'US/Pacific'), toIntervalDay(1)) AS start_of_interval- FROM events AS e- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2020-01-01 00:00:00', 'US/Pacific')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2020-01-19 23:59:59', 'US/Pacific'))), equals(e.event, '$pageview'), notEquals(e.`$group_0`, ''))- GROUP BY aggregation_target,- start_of_interval- HAVING greater(count(), 0))- GROUP BY aggregation_target)- WHERE equals(num_intervals, 7)) AS source- INNER JOIN- (SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, 'name'), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, 'US/Pacific')), 1) AS properties___name,- groups.group_type_index AS index,- groups.group_key AS key- FROM groups- WHERE equals(groups.team_id, 99999)- GROUP BY groups.group_type_index,- groups.group_key) AS groups ON equals(groups.key, source.group_key)- ORDER BY groups.properties___name ASC SETTINGS optimize_aggregation_in_order=1,- join_algorithm='auto')- LIMIT 100 SETTINGS readonly=2,- max_execution_time=60,- allow_experimental_object_type=1,- max_ast_elements=4000000,- max_expanded_ast_elements=4000000,- max_bytes_before_external_group_by=0,- transform_null_in=1,- optimize_min_equality_disjunction_chain_length=4294967295,- optimize_rewrite_aggregate_function_with_if=0,- optimize_min_inequality_conjunction_chain_length=4294967295,- allow_experimental_join_condition=1,- use_hive_partitioning=0- '''-# ----# name: TestInsightActorsQueryRunner.test_insight_persons_stickiness_query- '''- SELECT name AS name- FROM- (SELECT persons.properties___name AS name- FROM- (SELECT aggregation_target AS actor_id- FROM- (SELECT aggregation_target AS aggregation_target,- count() AS num_intervals- FROM- (SELECT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS aggregation_target,- toStartOfInterval(toTimeZone(e.timestamp, 'US/Pacific'), toIntervalDay(1)) AS start_of_interval- FROM events AS e- LEFT OUTER JOIN- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,- person_distinct_id_overrides.distinct_id AS distinct_id- FROM person_distinct_id_overrides- WHERE equals(person_distinct_id_overrides.team_id, 99999)- GROUP BY person_distinct_id_overrides.distinct_id- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2020-01-09 00:00:00', 'US/Pacific')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2020-01-19 23:59:59', 'US/Pacific'))), equals(e.event, '$pageview'))- GROUP BY aggregation_target,- start_of_interval- HAVING greater(count(), 0))- GROUP BY aggregation_target)- WHERE equals(num_intervals, 2)) AS source- INNER JOIN- (SELECT person.id AS id,- replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'name'), ''), 'null'), '^"|"$', '') AS properties___name- FROM person- WHERE and(equals(person.team_id, 99999), globalIn(tuple(person.id, person.version),- (SELECT person.id AS id, max(person.version) AS version- FROM person- WHERE and(equals(person.team_id, 99999), globalIn(person.id,- (SELECT source.actor_id AS actor_id- FROM- (SELECT aggregation_target AS actor_id- FROM- (SELECT aggregation_target AS aggregation_target, count() AS num_intervals- FROM- (SELECT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS aggregation_target, toStartOfInterval(toTimeZone(e.timestamp, 'US/Pacific'), toIntervalDay(1)) AS start_of_interval- FROM events AS e- LEFT OUTER JOIN- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id- FROM person_distinct_id_overrides- WHERE equals(person_distinct_id_overrides.team_id, 99999)- GROUP BY person_distinct_id_overrides.distinct_id- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2020-01-09 00:00:00', 'US/Pacific')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2020-01-19 23:59:59', 'US/Pacific'))), equals(e.event, '$pageview'))- GROUP BY aggregation_target, start_of_interval- HAVING greater(count(), 0))- GROUP BY aggregation_target)- WHERE equals(num_intervals, 2)) AS source)))- GROUP BY person.id- HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, 'US/Pacific'), person.version), plus(now64(6, 'US/Pacific'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS persons ON equals(persons.id, source.actor_id)- ORDER BY persons.properties___name ASC SETTINGS optimize_aggregation_in_order=1,- join_algorithm='auto')- LIMIT 100 SETTINGS readonly=2,- max_execution_time=60,- allow_experimental_object_type=1,- max_ast_elements=4000000,- max_expanded_ast_elements=4000000,- max_bytes_before_external_group_by=0,- transform_null_in=1,- optimize_min_equality_disjunction_chain_length=4294967295,- optimize_rewrite_aggregate_function_with_if=0,- optimize_min_inequality_conjunction_chain_length=4294967295,- allow_experimental_join_condition=1,- use_hive_partitioning=0- '''-# ---# name: TestInsightActorsQueryRunner.test_insight_persons_trends_groups_query'''SELECT name AS namediff --git a/posthog/hogql_queries/insights/test/test_insight_actors_query_runner.py b/posthog/hogql_queries/insights/test/test_insight_actors_query_runner.pyindex 9e8e81552139..fbd9cfc9a2d4 100644--- a/posthog/hogql_queries/insights/test/test_insight_actors_query_runner.py+++ b/posthog/hogql_queries/insights/test/test_insight_actors_query_runner.py@@ -183,53 +183,6 @@ class TestInsightActorsQueryRunner(ClickhouseTestMixin, APIBaseTest):self.assertEqual([("p1",), ("p2",)], response.results)- @snapshot_clickhouse_queries- def test_insight_persons_stickiness_query(self):- self._create_test_events()- self.team.timezone = "US/Pacific"- self.team.save()-- response = self.select(- """- select * from (- <ActorsQuery select={['properties.name']}>- <InsightActorsQuery day={2}>- <StickinessQuery- dateRange={<DateRange date_from='2020-01-09' date_to='2020-01-19' />}- series={[<EventsNode event='$pageview' />]}- />- </InsightActorsQuery>- </ActorsQuery>- )- """- )-- self.assertEqual([("p2",)], response.results)-- @snapshot_clickhouse_queries- def test_insight_persons_stickiness_groups_query(self):- self._create_test_groups()- self._create_test_events()- self.team.timezone = "US/Pacific"- self.team.save()-- response = self.select(- """- select * from (- <ActorsQuery select={['properties.name']}>- <InsightActorsQuery day={7}>- <StickinessQuery- dateRange={<DateRange date_from='2020-01-01' date_to='2020-01-19' />}- series={[<EventsNode event='$pageview' math='unique_group' math_group_type_index={0} />]}- />- </InsightActorsQuery>- </ActorsQuery>- )- """- )-- self.assertEqual([("org1",)], response.results)-def test_insight_persons_trends_query_with_argmaxV1_calculate_adds_event_distinct_ids(self):self._create_test_events()self.team.timezone = "US/Pacific"diff --git a/products/product_analytics/backend/hogql_queries/stickiness/test/__snapshots__/test_stickiness_insight_actors.ambr b/products/product_analytics/backend/hogql_queries/stickiness/test/__snapshots__/test_stickiness_insight_actors.ambrnew file mode 100644index 000000000000..073ae2e73cd1--- /dev/null+++ b/products/product_analytics/backend/hogql_queries/stickiness/test/__snapshots__/test_stickiness_insight_actors.ambr@@ -0,0 +1,117 @@+# serializer version: 1+# name: TestStickinessInsightActors.test_insight_persons_stickiness_groups_query+ '''+ SELECT name AS name+ FROM+ (SELECT groups.properties___name AS name+ FROM+ (SELECT aggregation_target AS group_key+ FROM+ (SELECT aggregation_target AS aggregation_target,+ count() AS num_intervals+ FROM+ (SELECT e.`$group_0` AS aggregation_target,+ toStartOfInterval(toTimeZone(e.timestamp, 'US/Pacific'), toIntervalDay(1)) AS start_of_interval+ FROM events AS e+ WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2020-01-01 00:00:00', 'US/Pacific')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2020-01-19 23:59:59', 'US/Pacific'))), equals(e.event, '$pageview'), notEquals(e.`$group_0`, ''))+ GROUP BY aggregation_target,+ start_of_interval+ HAVING greater(count(), 0))+ GROUP BY aggregation_target)+ WHERE equals(num_intervals, 7)) AS source+ INNER JOIN+ (SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, 'name'), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, 'US/Pacific')), 1) AS properties___name,+ groups.group_type_index AS index,+ groups.group_key AS key+ FROM groups+ WHERE equals(groups.team_id, 99999)+ GROUP BY groups.group_type_index,+ groups.group_key) AS groups ON equals(groups.key, source.group_key)+ ORDER BY groups.properties___name ASC SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto')+ LIMIT 100 SETTINGS readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestStickinessInsightActors.test_insight_persons_stickiness_query+ '''+ SELECT name AS name+ FROM+ (SELECT persons.properties___name AS name+ FROM+ (SELECT aggregation_target AS actor_id+ FROM+ (SELECT aggregation_target AS aggregation_target,+ count() AS num_intervals+ FROM+ (SELECT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS aggregation_target,+ toStartOfInterval(toTimeZone(e.timestamp, 'US/Pacific'), toIntervalDay(1)) AS start_of_interval+ FROM events AS e+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)+ WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2020-01-09 00:00:00', 'US/Pacific')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2020-01-19 23:59:59', 'US/Pacific'))), equals(e.event, '$pageview'))+ GROUP BY aggregation_target,+ start_of_interval+ HAVING greater(count(), 0))+ GROUP BY aggregation_target)+ WHERE equals(num_intervals, 2)) AS source+ INNER JOIN+ (SELECT person.id AS id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'name'), ''), 'null'), '^"|"$', '') AS properties___name+ FROM person+ WHERE and(equals(person.team_id, 99999), globalIn(tuple(person.id, person.version),+ (SELECT person.id AS id, max(person.version) AS version+ FROM person+ WHERE and(equals(person.team_id, 99999), globalIn(person.id,+ (SELECT source.actor_id AS actor_id+ FROM+ (SELECT aggregation_target AS actor_id+ FROM+ (SELECT aggregation_target AS aggregation_target, count() AS num_intervals+ FROM+ (SELECT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS aggregation_target, toStartOfInterval(toTimeZone(e.timestamp, 'US/Pacific'), toIntervalDay(1)) AS start_of_interval+ FROM events AS e+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)+ WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2020-01-09 00:00:00', 'US/Pacific')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2020-01-19 23:59:59', 'US/Pacific'))), equals(e.event, '$pageview'))+ GROUP BY aggregation_target, start_of_interval+ HAVING greater(count(), 0))+ GROUP BY aggregation_target)+ WHERE equals(num_intervals, 2)) AS source)))+ GROUP BY person.id+ HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, 'US/Pacific'), person.version), plus(now64(6, 'US/Pacific'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS persons ON equals(persons.id, source.actor_id)+ ORDER BY persons.properties___name ASC SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto')+ LIMIT 100 SETTINGS readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/product_analytics/backend/hogql_queries/stickiness/test/test_stickiness_insight_actors.py b/products/product_analytics/backend/hogql_queries/stickiness/test/test_stickiness_insight_actors.pynew file mode 100644index 000000000000..2e7ed900ea0e--- /dev/null+++ b/products/product_analytics/backend/hogql_queries/stickiness/test/test_stickiness_insight_actors.py@@ -0,0 +1,120 @@+from freezegun import freeze_time+from posthog.test.base import (+ APIBaseTest,+ ClickhouseTestMixin,+ _create_event,+ _create_person,+ snapshot_clickhouse_queries,+)++from posthog.hogql.query import execute_hogql_query++from posthog.models.group.util import create_group+from posthog.test.test_utils import create_group_type_mapping_without_created_at+++class TestStickinessInsightActors(ClickhouseTestMixin, APIBaseTest):+ maxDiff = None++ def _create_test_groups(self):+ create_group_type_mapping_without_created_at(+ team=self.team, project_id=self.team.project_id, group_type="organization", group_type_index=0+ )+ create_group(+ team_id=self.team.pk,+ group_type_index=0,+ group_key="org:1",+ properties={"name": "org1"},+ )+ create_group(+ team_id=self.team.pk,+ group_type_index=0,+ group_key="org:2",+ properties={"name": "org2"},+ )++ def _create_test_events(self):+ data = [+ (+ "p1",+ [+ "2020-01-11T12:00:00Z",+ "2020-01-12T12:00:00Z",+ "2020-01-13T12:00:00Z",+ "2020-01-15T12:00:00Z",+ "2020-01-17T12:00:00Z",+ "2020-01-19T12:00:00Z",+ ],+ ),+ ("p2", ["2020-01-09T12:00:00Z", "2020-01-12T12:00:00Z"]),+ ("p3", ["2020-01-12T12:00:00Z"]),+ ("p4", ["2020-01-15T12:00:00Z"]),+ ]+ for distinct_id, timestamps in data:+ with freeze_time(timestamps[0]):+ _create_person(+ team_id=self.team.pk,+ distinct_ids=[distinct_id],+ properties={+ "name": distinct_id,+ **({"email": "[email protected]"} if distinct_id == "p1" else {}),+ },+ )+ for timestamp in timestamps:+ _create_event(+ team=self.team,+ event="$pageview",+ distinct_id=distinct_id,+ timestamp=timestamp,+ properties={"$group_0": "org:1"},+ )++ def select(self, query: str):+ return execute_hogql_query(query=query, team=self.team)++ @snapshot_clickhouse_queries+ def test_insight_persons_stickiness_query(self):+ self._create_test_events()+ self.team.timezone = "US/Pacific"+ self.team.save()++ response = self.select(+ """+ select * from (+ <ActorsQuery select={['properties.name']}>+ <InsightActorsQuery day={2}>+ <StickinessQuery+ dateRange={<DateRange date_from='2020-01-09' date_to='2020-01-19' />}+ series={[<EventsNode event='$pageview' />]}+ />+ </InsightActorsQuery>+ </ActorsQuery>+ )+ """+ )++ self.assertEqual([("p2",)], response.results)++ @snapshot_clickhouse_queries+ def test_insight_persons_stickiness_groups_query(self):+ self._create_test_groups()+ self._create_test_events()+ self.team.timezone = "US/Pacific"+ self.team.save()++ response = self.select(+ """+ select * from (+ <ActorsQuery select={['properties.name']}>+ <InsightActorsQuery day={7}>+ <StickinessQuery+ dateRange={<DateRange date_from='2020-01-01' date_to='2020-01-19' />}+ series={[<EventsNode event='$pageview' math='unique_group' math_group_type_index={0} />]}+ />+ </InsightActorsQuery>+ </ActorsQuery>+ )+ """+ )++ self.assertEqual([("org1",)], response.results)