-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add experiments and flags product intents (#26661)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
89352a8
commit 5764870
Showing
7 changed files
with
164 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
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.feedback.survey import Survey | ||
from posthog.models.insight import Insight | ||
from posthog.models.product_intent.product_intent import ( | ||
ProductIntent, | ||
|
@@ -93,3 +96,77 @@ 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 | ||
feature_flag = 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]"}]}]}, | ||
) | ||
Experiment.objects.create(team=self.team, name="Experiment Test", feature_flag=feature_flag) | ||
survey_flag = 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"}]}]}, | ||
) | ||
Survey.objects.create(team=self.team, name="Survey Test", targeting_flag=survey_flag) | ||
|
||
self.assertFalse(self.product_intent.has_activated_feature_flags()) |