Skip to content

Commit

Permalink
Merge branch 'master' into taxonomy-project
Browse files Browse the repository at this point in the history
  • Loading branch information
Twixes committed Dec 5, 2024
2 parents ef91b56 + 35847fc commit 3804d6a
Show file tree
Hide file tree
Showing 80 changed files with 1,887 additions and 755 deletions.
6 changes: 3 additions & 3 deletions ee/clickhouse/views/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,16 +429,16 @@ def update(self, instance: Experiment, validated_data: dict, *args: Any, **kwarg
{"key": "test", "name": "Test Variant", "rollout_percentage": 50},
]

filters = {
"groups": [{"properties": properties, "rollout_percentage": 100}],
feature_flag_filters = {
"groups": feature_flag.filters.get("groups", []),
"multivariate": {"variants": variants or default_variants},
"aggregation_group_type_index": aggregation_group_type_index,
"holdout_groups": holdout_groups,
}

existing_flag_serializer = FeatureFlagSerializer(
feature_flag,
data={"filters": filters},
data={"filters": feature_flag_filters},
partial=True,
context=self.context,
)
Expand Down
108 changes: 108 additions & 0 deletions ee/clickhouse/views/test/test_clickhouse_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,114 @@ def test_create_draft_experiment_without_filters(self) -> None:
self.assertEqual(response.json()["name"], "Test Experiment")
self.assertEqual(response.json()["feature_flag_key"], ff_key)

def test_feature_flag_and_experiment_sync(self):
# Create an experiment with control and test variants
response = self.client.post(
f"/api/projects/{self.team.id}/experiments/",
{
"name": "Test Experiment",
"description": "My test experiment",
"feature_flag_key": "experiment-test-flag",
"parameters": {
"feature_flag_variants": [
{"key": "control", "name": "Control Group", "rollout_percentage": 50},
{"key": "test", "name": "Test Variant", "rollout_percentage": 50},
]
},
"filters": {"insight": "TRENDS", "events": [{"order": 0, "id": "$pageview"}]},
},
)

self.assertEqual(response.status_code, 201)
experiment_id = response.json()["id"]
feature_flag_id = response.json()["feature_flag"]["id"]

# Fetch the FeatureFlag object
feature_flag = FeatureFlag.objects.get(id=feature_flag_id)

variants = feature_flag.filters["multivariate"]["variants"]

# Verify that the variants are correctly populated
self.assertEqual(len(variants), 2)

self.assertEqual(variants[0]["key"], "control")
self.assertEqual(variants[0]["name"], "Control Group")
self.assertEqual(variants[0]["rollout_percentage"], 50)

self.assertEqual(variants[1]["key"], "test")
self.assertEqual(variants[1]["name"], "Test Variant")
self.assertEqual(variants[1]["rollout_percentage"], 50)

# Change the rollout percentages and groups of the feature flag
response = self.client.patch(
f"/api/projects/{self.team.id}/feature_flags/{feature_flag_id}",
{
"filters": {
"groups": [
{"properties": [], "rollout_percentage": 99},
{"properties": [], "rollout_percentage": 1},
],
"payloads": {},
"multivariate": {
"variants": [
{"key": "control", "rollout_percentage": 10},
{"key": "test", "rollout_percentage": 90},
]
},
"aggregation_group_type_index": 1,
}
},
)

# Verify that Experiment.parameters.feature_flag_variants reflects the updated FeatureFlag.filters.multivariate.variants
experiment = Experiment.objects.get(id=experiment_id)
self.assertEqual(
experiment.parameters["feature_flag_variants"],
[{"key": "control", "rollout_percentage": 10}, {"key": "test", "rollout_percentage": 90}],
)
self.assertEqual(experiment.parameters["aggregation_group_type_index"], 1)

# Update the experiment with an unrelated change
response = self.client.patch(
f"/api/projects/{self.team.id}/experiments/{experiment_id}",
{"name": "Updated Test Experiment"},
)

# Verify that the feature flag variants and groups remain unchanged
feature_flag = FeatureFlag.objects.get(id=feature_flag_id)
self.assertEqual(
feature_flag.filters["multivariate"]["variants"],
[{"key": "control", "rollout_percentage": 10}, {"key": "test", "rollout_percentage": 90}],
)
self.assertEqual(
feature_flag.filters["groups"],
[{"properties": [], "rollout_percentage": 99}, {"properties": [], "rollout_percentage": 1}],
)

# Test removing aggregation_group_type_index
response = self.client.patch(
f"/api/projects/{self.team.id}/feature_flags/{feature_flag_id}",
{
"filters": {
"groups": [
{"properties": [], "rollout_percentage": 99},
{"properties": [], "rollout_percentage": 1},
],
"payloads": {},
"multivariate": {
"variants": [
{"key": "control", "rollout_percentage": 10},
{"key": "test", "rollout_percentage": 90},
]
},
}
},
)

# Verify that aggregation_group_type_index is removed from experiment parameters
experiment = Experiment.objects.get(id=experiment_id)
self.assertNotIn("aggregation_group_type_index", experiment.parameters)


class TestExperimentAuxiliaryEndpoints(ClickhouseTestMixin, APILicensedTest):
def _generate_experiment(self, start_date="2024-01-01T10:23", extra_parameters=None):
Expand Down
Loading

0 comments on commit 3804d6a

Please sign in to comment.