Skip to content

Commit

Permalink
Fix test_sessions_model
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Oct 28, 2024
1 parent a7d635e commit 822655a
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions posthog/clickhouse/test/test_sessions_model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from posthog.clickhouse.client import sync_execute, query_with_columns
from posthog.models import Team
from posthog.test.base import (
_create_event,
ClickhouseTestMixin,
BaseTest,
ClickhouseDestroyTablesMixin,
)

distinct_id_counter = 0
Expand All @@ -21,7 +23,12 @@ def create_session_id():
return f"s{session_id_counter}"


class TestSessionsModel(ClickhouseTestMixin, BaseTest):
# only certain team ids can insert events into this legacy sessions table
TEAM_ID = 1
TEAM = Team(id=TEAM_ID)


class TestSessionsModel(ClickhouseDestroyTablesMixin, ClickhouseTestMixin, BaseTest):
def select_by_session_id(self, session_id):
return query_with_columns(
"""
Expand All @@ -34,15 +41,15 @@ def select_by_session_id(self, session_id):
""",
{
"session_id": session_id,
"team_id": self.team.id,
"team_id": TEAM_ID,
},
)

def test_it_creates_session_when_creating_event(self):
distinct_id = create_distinct_id()
session_id = create_session_id()
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$current_url": "/", "$session_id": session_id},
Expand All @@ -60,7 +67,7 @@ def test_it_creates_session_when_creating_event(self):
""",
{
"distinct_id": distinct_id,
"team_id": self.team.id,
"team_id": TEAM_ID,
},
)

Expand All @@ -72,14 +79,14 @@ def test_handles_different_distinct_id_across_same_session(self):
session_id = create_session_id()

_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id1,
properties={"$session_id": session_id},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id2,
properties={"$session_id": session_id},
Expand All @@ -96,28 +103,28 @@ def test_handles_entry_and_exit_urls(self):
session_id = create_session_id()

_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$current_url": "/entry", "$session_id": session_id},
timestamp="2024-03-08:01",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$current_url": "/middle", "$session_id": session_id},
timestamp="2024-03-08:02",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$current_url": "/middle", "$session_id": session_id},
timestamp="2024-03-08:03",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$current_url": "/exit", "$session_id": session_id},
Expand All @@ -136,14 +143,14 @@ def test_handles_initial_utm_properties(self):
session_id = create_session_id()

_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id, "utm_source": "source"},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id, "utm_source": "other_source"},
Expand All @@ -159,35 +166,35 @@ def test_counts_pageviews_autocaptures_and_events(self):
session_id = create_session_id()

_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="$autocapture",
distinct_id=distinct_id,
properties={"$session_id": session_id},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="$autocapture",
distinct_id=distinct_id,
properties={"$session_id": session_id},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="other event",
distinct_id=distinct_id,
properties={"$session_id": session_id},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageleave",
distinct_id=distinct_id,
properties={"$session_id": session_id},
Expand All @@ -209,14 +216,14 @@ def test_separates_sessions_across_same_user(self):
session_id3 = create_session_id()

_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id1},
timestamp="2024-03-08",
)
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id2},
Expand All @@ -235,7 +242,7 @@ def test_select_from_sessions(self):
distinct_id = create_distinct_id()
session_id = create_session_id()
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id},
Expand All @@ -260,7 +267,7 @@ def test_select_from_sessions(self):
""",
{
"session_id": session_id,
"team_id": self.team.id,
"team_id": TEAM_ID,
},
)
self.assertEqual(len(responses), 1)
Expand All @@ -270,7 +277,7 @@ def test_select_from_sessions_mv(self):
distinct_id = create_distinct_id()
session_id = create_session_id()
_create_event(
team=self.team,
team=TEAM,
event="$pageview",
distinct_id=distinct_id,
properties={"$session_id": session_id},
Expand All @@ -295,7 +302,7 @@ def test_select_from_sessions_mv(self):
""",
{
"session_id": session_id,
"team_id": self.team.id,
"team_id": TEAM_ID,
},
)
self.assertEqual(len(responses), 1)

0 comments on commit 822655a

Please sign in to comment.