Skip to content

Commit

Permalink
fix(misc): remove variable allocation when not needed (part #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guido Iaquinti committed Jun 27, 2022
1 parent 4ea626a commit c4ac909
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 223 deletions.
16 changes: 7 additions & 9 deletions posthog/models/test/test_event_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def test_filter_events_by_url(self):
event="$autocapture", action=action4, url="/123$", url_matching=ActionStep.REGEX,
)

event1_uuid = _create_event(team=self.team, distinct_id="whatever", event="$autocapture")
_create_event(team=self.team, distinct_id="whatever", event="$autocapture")
event2_uuid = _create_event(
event="$autocapture",
team=self.team,
Expand Down Expand Up @@ -314,8 +314,8 @@ def test_person_with_different_distinct_id(self):
action_watch_movie = Action.objects.create(team=self.team, name="watched movie")
ActionStep.objects.create(action=action_watch_movie, tag_name="a", href="/movie", event="$autocapture")

person = _create_person(distinct_ids=["anonymous_user", "is_now_signed_up"], team=self.team)
event_watched_movie_anonymous_uuid = _create_event(
_create_person(distinct_ids=["anonymous_user", "is_now_signed_up"], team=self.team)
_create_event(
distinct_id="anonymous_user",
team=self.team,
elements=[Element(tag_name="a", href="/movie")],
Expand All @@ -337,13 +337,11 @@ def test_no_person_leakage_from_other_teams(self):
action_watch_movie = Action.objects.create(team=self.team, name="watched movie")
ActionStep.objects.create(action=action_watch_movie, event="user signed up")

person = _create_person(distinct_ids=["anonymous_user"], team=self.team)
event_watched_movie_anonymous = _create_event(
event="user signed up", distinct_id="anonymous_user", team=self.team
)
_create_person(distinct_ids=["anonymous_user"], team=self.team)
_create_event(event="user signed up", distinct_id="anonymous_user", team=self.team)

team2 = Organization.objects.bootstrap(None)[2]
person2 = _create_person(distinct_ids=["anonymous_user2"], team=team2)
_create_person(distinct_ids=["anonymous_user2"], team=team2)

events = _get_events_for_action(action_watch_movie)
self.assertEqual(len(events), 1)
Expand All @@ -363,7 +361,7 @@ def test_person_property(self):

def test_no_steps(self):
_create_person(distinct_ids=["whatever"], team=self.team)
event1 = _create_event(
_create_event(
event="$autocapture",
team=self.team,
distinct_id="whatever",
Expand Down
2 changes: 1 addition & 1 deletion posthog/models/test/test_user_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_analytics_metadata(self):
self.team.completed_snippet_onboarding = True
self.team.ingested_event = True
self.team.save()
team_2: Team = Team.objects.create(organization=self.organization)
_: Team = Team.objects.create(organization=self.organization)
user_2: User = User.objects.create(email="test_org_2@posthog.com", email_opt_in=True)
user_2.join(organization=self.organization)

Expand Down
84 changes: 36 additions & 48 deletions posthog/queries/funnels/test/test_funnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,10 @@ def test_funnel_default(self):

with freeze_time("2012-01-01T03:21:34.000Z"):
# event
person1_stopped_after_signup = person_factory(
distinct_ids=["stopped_after_signup1"], team_id=self.team.pk
)
person_factory(distinct_ids=["stopped_after_signup1"], team_id=self.team.pk)
self._signup_event(distinct_id="stopped_after_signup1")

person2_stopped_after_signup = person_factory(
distinct_ids=["stopped_after_signup2"], team_id=self.team.pk
)
person_factory(distinct_ids=["stopped_after_signup2"], team_id=self.team.pk)
self._signup_event(distinct_id="stopped_after_signup2")

result = funnel.run()
Expand All @@ -132,10 +128,10 @@ def test_funnel_with_single_step(self):
funnel = self._single_step_funnel()

# event
person1_stopped_after_signup = person_factory(distinct_ids=["stopped_after_signup1"], team_id=self.team.pk)
person_factory(distinct_ids=["stopped_after_signup1"], team_id=self.team.pk)
self._signup_event(distinct_id="stopped_after_signup1")

person2_stopped_after_signup = person_factory(distinct_ids=["stopped_after_signup2"], team_id=self.team.pk)
person_factory(distinct_ids=["stopped_after_signup2"], team_id=self.team.pk)
self._signup_event(distinct_id="stopped_after_signup2")

result = funnel.run()
Expand All @@ -146,24 +142,22 @@ def test_funnel_events(self):
funnel = self._basic_funnel()

# events
person_stopped_after_signup = person_factory(distinct_ids=["stopped_after_signup"], team_id=self.team.pk)
person_factory(distinct_ids=["stopped_after_signup"], team_id=self.team.pk)
self._signup_event(distinct_id="stopped_after_signup")

person_stopped_after_pay = person_factory(distinct_ids=["stopped_after_pay"], team_id=self.team.pk)
person_factory(distinct_ids=["stopped_after_pay"], team_id=self.team.pk)
self._signup_event(distinct_id="stopped_after_pay")
self._pay_event(distinct_id="stopped_after_pay")

person_stopped_after_movie = person_factory(
distinct_ids=["had_anonymous_id", "completed_movie"], team_id=self.team.pk
)
person_factory(distinct_ids=["had_anonymous_id", "completed_movie"], team_id=self.team.pk)
self._signup_event(distinct_id="had_anonymous_id")
self._pay_event(distinct_id="completed_movie")
self._movie_event(distinct_id="completed_movie")

person_that_just_did_movie = person_factory(distinct_ids=["just_did_movie"], team_id=self.team.pk)
person_factory(distinct_ids=["just_did_movie"], team_id=self.team.pk)
self._movie_event(distinct_id="just_did_movie")

person_wrong_order = person_factory(distinct_ids=["wrong_order"], team_id=self.team.pk)
person_factory(distinct_ids=["wrong_order"], team_id=self.team.pk)
self._pay_event(distinct_id="wrong_order")
self._signup_event(distinct_id="wrong_order")
self._movie_event(distinct_id="wrong_order")
Expand All @@ -190,23 +184,21 @@ def test_funnel_with_messed_up_order(self):
)

# events
person_stopped_after_signup = person_factory(distinct_ids=["stopped_after_signup"], team_id=self.team.pk)
person_factory(distinct_ids=["stopped_after_signup"], team_id=self.team.pk)
self._signup_event(distinct_id="stopped_after_signup")

person_stopped_after_pay = person_factory(distinct_ids=["stopped_after_pay"], team_id=self.team.pk)
person_factory(distinct_ids=["stopped_after_pay"], team_id=self.team.pk)
self._signup_event(distinct_id="stopped_after_pay")
self._movie_event(distinct_id="completed_movie")

person_stopped_after_movie = person_factory(
distinct_ids=["had_anonymous_id", "completed_movie"], team_id=self.team.pk
)
person_factory(distinct_ids=["had_anonymous_id", "completed_movie"], team_id=self.team.pk)
self._signup_event(distinct_id="had_anonymous_id")
self._movie_event(distinct_id="completed_movie")

person_that_just_did_movie = person_factory(distinct_ids=["just_did_movie"], team_id=self.team.pk)
person_factory(distinct_ids=["just_did_movie"], team_id=self.team.pk)
self._movie_event(distinct_id="just_did_movie")

person_wrong_order = person_factory(distinct_ids=["wrong_order"], team_id=self.team.pk)
person_factory(distinct_ids=["wrong_order"], team_id=self.team.pk)
self._movie_event(distinct_id="wrong_order")
self._signup_event(distinct_id="wrong_order")

Expand Down Expand Up @@ -234,23 +226,21 @@ def test_funnel_with_new_entities_that_mess_up_order(self):
)

# events
person_stopped_after_signup = person_factory(distinct_ids=["stopped_after_signup"], team_id=self.team.pk)
person_factory(distinct_ids=["stopped_after_signup"], team_id=self.team.pk)
self._signup_event(distinct_id="stopped_after_signup")

person_stopped_after_pay = person_factory(distinct_ids=["stopped_after_pay"], team_id=self.team.pk)
person_factory(distinct_ids=["stopped_after_pay"], team_id=self.team.pk)
self._signup_event(distinct_id="stopped_after_pay")
self._movie_event(distinct_id="completed_movie")

person_stopped_after_movie = person_factory(
distinct_ids=["had_anonymous_id", "completed_movie"], team_id=self.team.pk
)
person_factory(distinct_ids=["had_anonymous_id", "completed_movie"], team_id=self.team.pk)
self._signup_event(distinct_id="had_anonymous_id")
self._movie_event(distinct_id="completed_movie")

person_that_just_did_movie = person_factory(distinct_ids=["just_did_movie"], team_id=self.team.pk)
person_factory(distinct_ids=["just_did_movie"], team_id=self.team.pk)
self._movie_event(distinct_id="just_did_movie")

person_wrong_order = person_factory(distinct_ids=["wrong_order"], team_id=self.team.pk)
person_factory(distinct_ids=["wrong_order"], team_id=self.team.pk)
self._movie_event(distinct_id="wrong_order")
self._signup_event(distinct_id="wrong_order")

Expand All @@ -268,7 +258,7 @@ def test_funnel_no_events(self):
def test_funnel_skipped_step(self):
funnel = self._basic_funnel()

person_wrong_order = person_factory(distinct_ids=["wrong_order"], team_id=self.team.pk)
person_factory(distinct_ids=["wrong_order"], team_id=self.team.pk)
self._signup_event(distinct_id="wrong_order")
self._movie_event(distinct_id="wrong_order")

Expand All @@ -281,17 +271,17 @@ def test_funnel_prop_filters(self):
funnel = self._basic_funnel(properties={"$browser": "Safari"})

# events
with_property = person_factory(distinct_ids=["with_property"], team_id=self.team.pk)
person_factory(distinct_ids=["with_property"], team_id=self.team.pk)
self._signup_event(distinct_id="with_property", properties={"$browser": "Safari"})
self._pay_event(distinct_id="with_property", properties={"$browser": "Safari"})

# should not add a count
without_property = person_factory(distinct_ids=["without_property"], team_id=self.team.pk)
person_factory(distinct_ids=["without_property"], team_id=self.team.pk)
self._signup_event(distinct_id="without_property")
self._pay_event(distinct_id="without_property")

# will add to first step
half_property = person_factory(distinct_ids=["half_property"], team_id=self.team.pk)
person_factory(distinct_ids=["half_property"], team_id=self.team.pk)
self._signup_event(distinct_id="half_property", properties={"$browser": "Safari"})
self._pay_event(distinct_id="half_property")

Expand Down Expand Up @@ -338,20 +328,20 @@ def test_funnel_prop_filters_per_entity(self):
funnel = self._basic_funnel(filters=filters)

# events
with_property = person_factory(
person_factory(
distinct_ids=["with_property"], team_id=self.team.pk, properties={"$browser": "Safari"},
)
self._signup_event(distinct_id="with_property", properties={"$browser": "Safari"})
self._pay_event(distinct_id="with_property", properties={"$browser": "Safari"})
self._movie_event(distinct_id="with_property")

# should not add a count
without_property = person_factory(distinct_ids=["without_property"], team_id=self.team.pk)
person_factory(distinct_ids=["without_property"], team_id=self.team.pk)
self._signup_event(distinct_id="without_property")
self._pay_event(distinct_id="without_property", properties={"$browser": "Safari"})

# will add to first step
half_property = person_factory(distinct_ids=["half_property"], team_id=self.team.pk)
person_factory(distinct_ids=["half_property"], team_id=self.team.pk)
self._signup_event(distinct_id="half_property")
self._pay_event(distinct_id="half_property")
self._movie_event(distinct_id="half_property")
Expand Down Expand Up @@ -388,7 +378,7 @@ def test_funnel_person_prop(self):
funnel = self._basic_funnel(filters=filters)

# events
with_property = person_factory(
person_factory(
distinct_ids=["with_property"], team_id=self.team.pk, properties={"email": "hello@posthog.com"},
)
self._signup_event(distinct_id="with_property")
Expand Down Expand Up @@ -714,7 +704,7 @@ def test_funnel_exclusions_full_window(self):
_create_event(team=self.team, event="paid", distinct_id="person1", timestamp="2021-05-01 02:00:00")

# event 2
person2 = _create_person(distinct_ids=["person2"], team_id=self.team.pk)
_create_person(distinct_ids=["person2"], team_id=self.team.pk)
_create_event(
team=self.team, event="user signed up", distinct_id="person2", timestamp="2021-05-01 03:00:00"
)
Expand Down Expand Up @@ -1048,9 +1038,7 @@ def test_advanced_funnel_with_repeat_steps_out_of_order_events(self):
_create_event(team=self.team, event="$pageview", distinct_id="stopped_after_pageview4")
_create_event(team=self.team, event="$pageview", distinct_id="stopped_after_pageview4")

person6_stopped_after_many_pageview_without_signup = _create_person(
distinct_ids=["stopped_after_pageview5"], team_id=self.team.pk
)
_create_person(distinct_ids=["stopped_after_pageview5"], team_id=self.team.pk)
_create_event(team=self.team, event="$pageview", distinct_id="stopped_after_pageview5")
_create_event(team=self.team, event="blaah blaa", distinct_id="stopped_after_pageview5")
_create_event(team=self.team, event="$pageview", distinct_id="stopped_after_pageview5")
Expand Down Expand Up @@ -1326,7 +1314,7 @@ def test_funnel_with_actions_and_events(self):
team=self.team, event="user signed up", distinct_id="person4", timestamp="2021-05-01 00:00:13"
)

person5 = _create_person(distinct_ids=["person5"], team_id=self.team.pk)
_create_person(distinct_ids=["person5"], team_id=self.team.pk)
_create_event(
team=self.team,
event="sign up",
Expand Down Expand Up @@ -1562,15 +1550,15 @@ def test_funnel_exclusion_no_end_event(self):
_create_event(team=self.team, event="paid", distinct_id="person1", timestamp="2021-05-01 02:00:00")

# event 2
person2 = _create_person(distinct_ids=["person2"], team_id=self.team.pk)
_create_person(distinct_ids=["person2"], team_id=self.team.pk)
_create_event(
team=self.team, event="user signed up", distinct_id="person2", timestamp="2021-05-01 03:00:00"
)
_create_event(team=self.team, event="x", distinct_id="person2", timestamp="2021-05-01 03:30:00")
_create_event(team=self.team, event="paid", distinct_id="person2", timestamp="2021-05-01 04:00:00")

# event 3
person3 = _create_person(distinct_ids=["person3"], team_id=self.team.pk)
_create_person(distinct_ids=["person3"], team_id=self.team.pk)
# should be discarded, even if nothing happened after x, since within conversion window
_create_event(
team=self.team, event="user signed up", distinct_id="person3", timestamp="2021-05-01 05:00:00"
Expand Down Expand Up @@ -1632,7 +1620,7 @@ def test_funnel_exclusions_with_actions(self):
_create_event(team=self.team, event="paid", distinct_id="person1", timestamp="2021-05-01 02:00:00")

# event 2
person2 = _create_person(distinct_ids=["person2"], team_id=self.team.pk)
_create_person(distinct_ids=["person2"], team_id=self.team.pk)
_create_event(
team=self.team, event="user signed up", distinct_id="person2", timestamp="2021-05-01 03:00:00"
)
Expand Down Expand Up @@ -1724,7 +1712,7 @@ def test_advanced_funnel_multiple_exclusions_between_steps(self):
],
}

person1 = _create_person(distinct_ids=["person1"], team_id=self.team.pk)
_create_person(distinct_ids=["person1"], team_id=self.team.pk)
_create_event(
team=self.team, event="user signed up", distinct_id="person1", timestamp="2021-05-01 01:00:00"
)
Expand All @@ -1739,7 +1727,7 @@ def test_advanced_funnel_multiple_exclusions_between_steps(self):
)
_create_event(team=self.team, event="pageview2", distinct_id="person1", timestamp="2021-05-01 06:00:00")

person2 = _create_person(distinct_ids=["person2"], team_id=self.team.pk)
_create_person(distinct_ids=["person2"], team_id=self.team.pk)
_create_event(
team=self.team, event="user signed up", distinct_id="person2", timestamp="2021-05-01 01:00:00"
)
Expand All @@ -1755,7 +1743,7 @@ def test_advanced_funnel_multiple_exclusions_between_steps(self):
_create_event(team=self.team, event="x", distinct_id="person2", timestamp="2021-05-01 05:30:00")
_create_event(team=self.team, event="pageview2", distinct_id="person2", timestamp="2021-05-01 06:00:00")

person3 = _create_person(distinct_ids=["person3"], team_id=self.team.pk)
_create_person(distinct_ids=["person3"], team_id=self.team.pk)
_create_event(
team=self.team, event="user signed up", distinct_id="person3", timestamp="2021-05-01 01:00:00"
)
Expand Down
2 changes: 1 addition & 1 deletion posthog/queries/funnels/test/test_funnel_strict.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def test_basic_strict_funnel(self):
_create_event(team=self.team, event="insight viewed", distinct_id="person7")
_create_event(team=self.team, event="blaah blaa", distinct_id="person7")

person8_didnot_signup = _create_person(distinct_ids=["stopped_after_insightview6"], team_id=self.team.pk)
_create_person(distinct_ids=["stopped_after_insightview6"], team_id=self.team.pk)
_create_event(team=self.team, event="insight viewed", distinct_id="stopped_after_insightview6")
_create_event(team=self.team, event="$pageview", distinct_id="stopped_after_insightview6")

Expand Down
Loading

0 comments on commit c4ac909

Please sign in to comment.