Skip to content

Commit

Permalink
add tests for flags and experiment activation checks
Browse files Browse the repository at this point in the history
  • Loading branch information
raquelmsmith committed Dec 4, 2024
1 parent 6d47e1a commit 3c88697
Showing 1 changed file with 75 additions and 1 deletion.
76 changes: 75 additions & 1 deletion posthog/models/test/test_product_intent.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from datetime import datetime, timedelta, UTC
from datetime import UTC, datetime, timedelta

import pytest
from freezegun import freeze_time

from posthog.models.experiment import Experiment
from posthog.models.feature_flag import FeatureFlag
from posthog.models.insight import Insight
from posthog.models.product_intent.product_intent import (
ProductIntent,
Expand Down Expand Up @@ -93,3 +95,75 @@ def test_calculate_product_activation_skips_activated_products(self):
calculate_product_activation(self.team.id)
self.product_intent.refresh_from_db()
assert self.product_intent.activated_at == datetime(2024, 6, 15, 12, 0, 0, tzinfo=UTC)

def test_has_activated_experiments_with_launched_experiment(self):
self.product_intent.product_type = "experiments"
self.product_intent.save()

# Create a feature flag for the experiment
feature_flag = FeatureFlag.objects.create(
team=self.team,
key="test-flag",
name="Test Flag",
filters={"groups": [{"properties": []}]},
)

# Create an experiment without a start date (not launched)
Experiment.objects.create(team=self.team, name="Not launched", feature_flag=feature_flag)
self.assertFalse(self.product_intent.has_activated_experiments())

# Create another feature flag for the launched experiment
launched_flag = FeatureFlag.objects.create(
team=self.team,
key="launched-flag",
name="Launched Flag",
filters={"groups": [{"properties": []}]},
)

# Create an experiment with a start date (launched)
Experiment.objects.create(
team=self.team, name="Launched", start_date=datetime.now(tz=UTC), feature_flag=launched_flag
)
self.assertTrue(self.product_intent.has_activated_experiments())

def test_has_activated_feature_flags(self):
self.product_intent.product_type = "feature_flags"
self.product_intent.save()

# Create a feature flag with one filter group
FeatureFlag.objects.create(
team=self.team,
key="flag-1",
name="Flag 1",
filters={"groups": [{"properties": [{"key": "email", "value": "[email protected]"}]}]},
)
self.assertFalse(self.product_intent.has_activated_feature_flags())

# Create a feature flag with another filter group
FeatureFlag.objects.create(
team=self.team,
key="flag-2",
name="Flag 2",
filters={"groups": [{"properties": [{"key": "country", "value": "US"}]}]},
)
self.assertTrue(self.product_intent.has_activated_feature_flags())

def test_has_activated_feature_flags_excludes_experiment_and_survey_flags(self):
self.product_intent.product_type = "feature_flags"
self.product_intent.save()

# Create excluded feature flags
FeatureFlag.objects.create(
team=self.team,
key="feature-flag-for-experiment-test",
name="Feature Flag for Experiment Test",
filters={"groups": [{"properties": [{"key": "email", "value": "[email protected]"}]}]},
)
FeatureFlag.objects.create(
team=self.team,
key="targeting-flag-for-survey-test",
name="Targeting flag for survey Test",
filters={"groups": [{"properties": [{"key": "country", "value": "US"}]}]},
)

self.assertFalse(self.product_intent.has_activated_feature_flags())

0 comments on commit 3c88697

Please sign in to comment.