Skip to content

Commit

Permalink
add test for updating product intent
Browse files Browse the repository at this point in the history
  • Loading branch information
raquelmsmith committed Oct 30, 2024
1 parent db2ecd4 commit cc4113f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion posthog/api/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def get_live_events_token(self, team: Team) -> Optional[str]:
)

def get_product_intents(self, obj):
calculate_product_activation.delay(obj, only_calc_if_days_since_last_checked=1)
calculate_product_activation.delay(obj.id, only_calc_if_days_since_last_checked=1)
return ProductIntent.objects.filter(team=obj).values(
"product_type", "created_at", "onboarding_completed_at", "updated_at"
)
Expand Down
45 changes: 45 additions & 0 deletions posthog/api/test/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,51 @@ def test_can_add_product_intent(
team=self.team,
)

@patch("posthog.api.team.calculate_product_activation.delay", MagicMock())
@patch("posthog.models.product_intent.ProductIntent.check_and_update_activation")
@patch("posthog.api.project.report_user_action")
@patch("posthog.api.team.report_user_action")
@freeze_time("2024-01-01T00:00:00Z")
def test_can_update_product_intent_if_already_exists(
self,
mock_report_user_action: MagicMock,
mock_report_user_action_legacy_endpoint: MagicMock,
mock_check_and_update_activation: MagicMock,
) -> None:
intent = ProductIntent.objects.create(team=self.team, product_type="product_analytics")
original_created_at = intent.created_at
assert original_created_at == datetime(2024, 1, 1, 0, 0, 0, tzinfo=UTC)
# change the time of the existing intent
with freeze_time("2024-01-02T00:00:00Z"):
if self.client_class is EnvironmentToProjectRewriteClient:
mock_report_user_action = mock_report_user_action_legacy_endpoint
response = self.client.patch(
f"/api/environments/{self.team.id}/add_product_intent/",
{"product_type": "product_analytics"},
headers={"Referer": "https://posthogtest.com/my-url", "X-Posthog-Session-Id": "test_session_id"},
)
assert response.status_code == status.HTTP_201_CREATED
product_intent = ProductIntent.objects.get(team=self.team, product_type="product_analytics")
assert product_intent.updated_at == datetime(2024, 1, 2, 0, 0, 0, tzinfo=UTC)
assert product_intent.created_at == original_created_at
mock_check_and_update_activation.assert_called_once()
mock_report_user_action.assert_called_once_with(
self.user,
"user showed product intent",
{
"product_key": "product_analytics",
"$current_url": "https://posthogtest.com/my-url",
"$session_id": "test_session_id",
"intent_context": None,
"$set_once": {"first_onboarding_product_selected": "product_analytics"},
"is_first_intent_for_product": False,
"intent_created_at": datetime(2024, 1, 1, 0, 0, 0, tzinfo=UTC),
"intent_updated_at": datetime(2024, 1, 2, 0, 0, 0, tzinfo=UTC),
"realm": get_instance_realm(),
},
team=self.team,
)

@patch("posthog.api.project.report_user_action")
@patch("posthog.api.team.report_user_action")
def test_can_complete_product_onboarding(
Expand Down
8 changes: 5 additions & 3 deletions posthog/models/product_intent/product_intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from celery import shared_task
from django.db import models

from posthog.event_usage import report_team_action
from posthog.models.insight import Insight
from posthog.models.team.team import Team
from posthog.models.utils import UUIDModel
Expand Down Expand Up @@ -83,8 +82,10 @@ def check_and_update_activation(self) -> None:
self.report_activation("data_warehouse")

def report_activation(self, product_key: str) -> None:
from posthog.event_usage import report_team_action

report_team_action(
self.team.organization,
self.team,
"product intent marked activated",
{
"product_key": product_key,
Expand All @@ -96,11 +97,12 @@ def report_activation(self, product_key: str) -> None:


@shared_task(ignore_result=True)
def calculate_product_activation(team: Team, only_calc_if_days_since_last_checked: int = 1) -> None:
def calculate_product_activation(team_id: int, only_calc_if_days_since_last_checked: int = 1) -> None:
"""
Calculate product activation for a team.
Only calculate if it's been more than `only_calc_if_days_since_last_checked` days since the last activation check.
"""
team = Team.objects.get(id=team_id)
product_intents = ProductIntent.objects.filter(team=team)
for product_intent in product_intents:
if product_intent.activated_at:
Expand Down

0 comments on commit cc4113f

Please sign in to comment.