:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
refactor(experiments): add facade functions for core callers (#125581)
Anders382d578a96c5
diff --git a/products/experiments/backend/facade/api.py b/products/experiments/backend/facade/api.pyindex f08b46aa1a0f..784dd479dffd 100644--- a/products/experiments/backend/facade/api.py+++ b/products/experiments/backend/facade/api.py@@ -5,16 +5,22 @@ This module provides the public interface for creating and managing experimentsusing framework-free DTOs, wrapping the existing ExperimentService."""+import dataclasses+from collections.abc import Collection, Sequencefrom datetime import datetime+from decimal import Decimalfrom typing import Anyfrom uuid import UUID+from django.core.exceptions import ValidationError as DjangoValidationError+import pydanticimport structlogfrom posthog.schema import ExperimentEventExposureConfigfrom posthog.models.team import Team+from posthog.models.team.extensions import get_or_create_team_extensionfrom posthog.models.user import Userfrom products.experiments.backend.experiment_service import ExperimentService@@ -38,11 +44,17 @@ from products.experiments.backend.metric_calculation.config import ()from products.experiments.backend.metric_calculation.results import MetricResultStorefrom products.experiments.backend.models.experiment import Experiment as ExperimentModel+from products.experiments.backend.models.team_experiments_config import (+ TeamExperimentsConfig,+ validate_recalculation_times as _validate_stored_recalculation_times,+)from products.experiments.backend.running_time_target import load_running_time_targetfrom products.experiments.stats.shared.enums import DifferenceTypefrom .contracts import (CreateExperimentInput,+ DigestCompletedExperiment,+ DigestLaunchedExperiment,EndedExperiment,Experiment,ExperimentDetectableEffectCheckpoint,@@ -55,7 +67,11 @@ from .contracts import (RunningTimeMode,StoredArmResult,StoredPrimaryResult,+ TeamExperimentsSettings,+ TeamExperimentsSettingsUpdate,+ Unset,)+from .enums import MAX_DEFAULT_CONFIDENCE_LEVEL, MIN_DEFAULT_CONFIDENCE_LEVELlogger = structlog.get_logger(__name__)@@ -304,6 +320,177 @@ def count_enrolled_people(*, team: Team, user: User, flag_keys: list[str]) -> direturn enrolled_people_by_flag(team, user, flag_keys)+def team_has_launched_experiments(team_id: int) -> bool:+ """Whether the team has a running or stopped experiment, deleted and archived ones included. Experiments are saved+ on the project's root team, so pass that team's id."""+ # `team=` matches the id exactly. RootTeamQuerySet rewrites a `team_id=` filter to the project's root team.+ return ExperimentModel.objects.filter(+ team=team_id, status__in=[ExperimentModel.Status.RUNNING, ExperimentModel.Status.STOPPED]+ ).exists()+++def experiment_flag_ids(team_ids: Collection[int], *, include_deleted: bool) -> set[int]:+ """Ids of the flags that the teams' experiments use, archived experiments included."""+ experiments = ExperimentModel.objects.filter(team_id__in=team_ids)+ if not include_deleted:+ experiments = experiments.filter(deleted=False)+ return set(experiments.values_list("feature_flag_id", flat=True))+++def list_launched_experiments_for_digest(+ *, period_start: datetime, period_end: datetime, team_ids: range+) -> list[DigestLaunchedExperiment]:+ """Experiments of the teams in `team_ids` that started after `period_start` and by `period_end`, and did not also+ end in that period. Deleted and archived experiments are included, in no set order."""+ rows = (+ ExperimentModel.objects.filter(+ team_id__gte=team_ids.start,+ team_id__lt=team_ids.stop,+ start_date__gt=period_start,+ start_date__lte=period_end,+ )+ .exclude(end_date__gt=period_start, end_date__lte=period_end)+ .values_list("team_id", "id", "name", "start_date")+ )+ return [+ DigestLaunchedExperiment(team_id=team_id, id=id, name=name, start_date=start_date)+ for team_id, id, name, start_date in rows+ ]+++def list_completed_experiments_for_digest(+ *, period_start: datetime, period_end: datetime, team_ids: range+) -> list[DigestCompletedExperiment]:+ """Experiments of the teams in `team_ids` that ended after `period_start` and by `period_end`. Deleted and+ archived experiments are included, in no set order."""+ rows = ExperimentModel.objects.filter(+ team_id__gte=team_ids.start,+ team_id__lt=team_ids.stop,+ end_date__gt=period_start,+ end_date__lte=period_end,+ ).values_list("team_id", "id", "name", "start_date", "end_date")+ return [+ DigestCompletedExperiment(team_id=team_id, id=id, name=name, start_date=start_date, end_date=end_date)+ for team_id, id, name, start_date, end_date in rows+ ]+++def list_precomputation_enabled_team_ids(team_ids: Collection[int] | None = None) -> set[int]:+ """Ids of the teams that have experiment precomputation enabled, among `team_ids` when it is given."""+ configs = TeamExperimentsConfig.objects.filter(experiment_precomputation_enabled=True)+ if team_ids is not None:+ configs = configs.filter(team_id__in=team_ids)+ return set(configs.values_list("team_id", flat=True))+++def _team_experiments_config(team_id: int) -> TeamExperimentsConfig:+ return get_or_create_team_extension(Team.objects.get(id=team_id), TeamExperimentsConfig)+++def set_precomputation_enabled(*, team_id: int, enabled: bool) -> None:+ config = _team_experiments_config(team_id)+ config.experiment_precomputation_enabled = enabled+ # A human toggling precomputation must stick: the auto-enrollment job only+ # writes when precomputation_enabled_set_by is null or "auto".+ config.precomputation_enabled_set_by = TeamExperimentsConfig.PrecomputationEnabledSetBy.MANUAL+ config.save(update_fields=["experiment_precomputation_enabled", "precomputation_enabled_set_by"])+++def _to_team_experiments_settings(config: TeamExperimentsConfig) -> TeamExperimentsSettings:+ times = config.experiment_recalculation_times+ return TeamExperimentsSettings(+ experiment_recalculation_times=None if times is None else tuple(times),+ default_experiment_confidence_level=config.default_experiment_confidence_level,+ default_experiment_stats_method=config.default_experiment_stats_method,+ experiment_precomputation_enabled=config.experiment_precomputation_enabled,+ default_only_count_matured_users=config.default_only_count_matured_users,+ default_cuped_enabled=config.default_cuped_enabled,+ default_cuped_lookback_days=config.default_cuped_lookback_days,+ default_minimum_detectable_effect=config.default_minimum_detectable_effect,+ default_sequential_testing_enabled=config.default_sequential_testing_enabled,+ default_sequential_tuning_parameter=config.default_sequential_tuning_parameter,+ flag_cleanup_repository=config.flag_cleanup_repository,+ )+++def get_team_experiments_settings(team_id: int) -> TeamExperimentsSettings:+ """The first read creates the team's settings row with the defaults."""+ return _to_team_experiments_settings(_team_experiments_config(team_id))+++def validate_recalculation_times(times: Sequence[str] | None) -> None:+ try:+ _validate_stored_recalculation_times(None if times is None else list(times))+ except DjangoValidationError as error:+ raise ExperimentValidationError(error.messages[0], field="experiment_recalculation_times") from error+++def _connected_cleanup_repository(team: Team, repository: str | None) -> str | None:+ # Keeps the sandbox/LLM runtime the repo-selection module pulls in off the+ # request import path.+ from products.tasks.backend.facade import repo_selection as tasks_repo_selection # noqa: PLC0415++ if not repository:+ return None+ parts = repository.split("/")+ if len(parts) != 2 or not parts[0] or not parts[1]:+ raise ExperimentValidationError(+ "Repository must be in the format organization/repository", field="flag_cleanup_repository"+ )+ repository = repository.lower()+ # Reject repos outside the team's GitHub installation now rather than storing a+ # default the cleanup resolution would silently ignore.+ github = tasks_repo_selection.resolve_team_github_integration(team.id, team=team, team_only=True)+ cached = {+ full_name.lower()+ for repo in (github.list_all_cached_repositories(max_repos=1000) if github else [])+ if (full_name := repo.get("full_name"))+ }+ if repository not in cached:+ raise ExperimentValidationError(+ "This repository is not connected to the project's GitHub integration.", field="flag_cleanup_repository"+ )+ return repository+++def update_team_experiments_settings(*, team_id: int, update: TeamExperimentsSettingsUpdate) -> TeamExperimentsSettings:+ """Applies the settings that `update` sets, with the checks of the project settings endpoint. Raises+ ExperimentValidationError for the first setting that fails, with the setting's name in `field`."""+ config = _team_experiments_config(team_id)+ changes: dict[str, object] = {+ setting.name: getattr(update, setting.name)+ for setting in dataclasses.fields(update)+ if not isinstance(getattr(update, setting.name), Unset)+ }+ if not isinstance(times := update.experiment_recalculation_times, Unset):+ validate_recalculation_times(times)+ # The JSON column holds a list. A tuple never equals the stored list, so the activity log would report an+ # unchanged schedule as a change.+ changes["experiment_recalculation_times"] = None if times is None else list(times)+ if isinstance(level := update.default_experiment_confidence_level, Decimal):+ if level < MIN_DEFAULT_CONFIDENCE_LEVEL:+ raise ExperimentValidationError(+ f"Ensure this value is greater than or equal to {MIN_DEFAULT_CONFIDENCE_LEVEL}.",+ field="default_experiment_confidence_level",+ )+ if level > MAX_DEFAULT_CONFIDENCE_LEVEL:+ raise ExperimentValidationError(+ f"Ensure this value is less than or equal to {MAX_DEFAULT_CONFIDENCE_LEVEL}.",+ field="default_experiment_confidence_level",+ )+ for name, value in changes.items():+ setattr(config, name, value)+ try:+ config.clean_fields(exclude={field.name for field in TeamExperimentsConfig._meta.fields} - changes.keys())+ except DjangoValidationError as error:+ name, messages = next(iter(error.message_dict.items()))+ raise ExperimentValidationError(messages[0], field=name) from error+ if not isinstance(repository := update.flag_cleanup_repository, Unset):+ config.flag_cleanup_repository = _connected_cleanup_repository(config.team, repository)+ config.save()+ return _to_team_experiments_settings(config)++def _experiment_model_to_dto(experiment: ExperimentModel) -> Experiment:"""Convert Django model to DTO."""return Experiment(diff --git a/products/experiments/backend/facade/contracts.py b/products/experiments/backend/facade/contracts.pyindex e3d57444e5fb..b53eedc8a26b 100644--- a/products/experiments/backend/facade/contracts.py+++ b/products/experiments/backend/facade/contracts.py@@ -8,6 +8,7 @@ between the experiments product and the rest of the system.from collections.abc import Mappingfrom dataclasses import dataclass, fieldfrom datetime import datetime+from decimal import Decimalfrom typing import TYPE_CHECKING, Anyfrom posthog.dataclasses import frozen@@ -280,6 +281,12 @@ class ExperimentValidationError(Exception):"""Raised when the facade refuses an experiment write for its input. The message is written for the person whosent the input, so a caller at an HTTP boundary can return it as a 400."""+ def __init__(self, message: str, *, field: str | None = None) -> None:+ super().__init__(message)+ # The input field that the message is about, so that an HTTP boundary can report the error on that field.+ # None when the message is about the input as a whole.+ self.field = field+class LegacyMigrationError(Exception):"""Raised when a legacy experiment can't move onto the new engine. The message is written for the person who asked@@ -530,3 +537,62 @@ class ExperimentChanged:archived: boolcreated_at: datetimeupdated_at: datetime+++@frozen+class DigestLaunchedExperiment:+ team_id: int+ id: int+ name: str+ start_date: datetime+++@frozen+class DigestCompletedExperiment:+ team_id: int+ id: int+ name: str+ start_date: datetime | None+ end_date: datetime+++@frozen+class TeamExperimentsSettings:+ """A team's experiment settings, as the project settings show them."""++ # Times of day (UTC) as 'HH:00:00' strings. None means the default time.+ experiment_recalculation_times: tuple[str, ...] | None+ default_experiment_confidence_level: Decimal | None+ default_experiment_stats_method: str | None+ experiment_precomputation_enabled: bool+ default_only_count_matured_users: bool+ default_cuped_enabled: bool+ default_cuped_lookback_days: int | None+ default_minimum_detectable_effect: int | None+ default_sequential_testing_enabled: bool+ default_sequential_tuning_parameter: int | None+ flag_cleanup_repository: str | None+++class Unset:+ """Marks a setting that an update leaves as it is, because None is a valid value."""+++UNSET = Unset()+++@frozen+class TeamExperimentsSettingsUpdate:+ """The settings that an update changes. UNSET leaves a setting as it is. Precomputation is not here, because+ PostHog manages it."""++ experiment_recalculation_times: tuple[str, ...] | None | Unset = UNSET+ default_experiment_confidence_level: Decimal | None | Unset = UNSET+ default_experiment_stats_method: str | None | Unset = UNSET+ default_only_count_matured_users: bool | Unset = UNSET+ default_cuped_enabled: bool | Unset = UNSET+ default_cuped_lookback_days: int | None | Unset = UNSET+ default_minimum_detectable_effect: int | None | Unset = UNSET+ default_sequential_testing_enabled: bool | Unset = UNSET+ default_sequential_tuning_parameter: int | None | Unset = UNSET+ flag_cleanup_repository: str | None | Unset = UNSETdiff --git a/products/experiments/backend/facade/enums.py b/products/experiments/backend/facade/enums.pyindex 4313903d6ae6..d307dfda7ac6 100644--- a/products/experiments/backend/facade/enums.py+++ b/products/experiments/backend/facade/enums.py@@ -1,11 +1,17 @@"""Exported enums and constants for experiments."""+from decimal import Decimalfrom enum import StrEnumMAX_RECALCULATION_TIMES = 2# frontend/src/scenes/settings/environment/ExperimentRecalculationTime.tsx repeats this value, so change both together.MIN_RECALCULATION_GAP_HOURS = 6+# The frequentist engine derives alpha as 1 - confidence level, so anything outside (0, 1) is meaningless. The setting+# stores two decimal places, so these bounds are that open range.+MIN_DEFAULT_CONFIDENCE_LEVEL = Decimal("0.01")+MAX_DEFAULT_CONFIDENCE_LEVEL = Decimal("0.99")+class PrecomputeSkipReason(StrEnum):"""Why a metric read never attempted precompute. Tagged on the query asdiff --git a/products/experiments/backend/test/test_facade.py b/products/experiments/backend/test/test_facade.pyindex ccba68e81bb0..34deefac332b 100644--- a/products/experiments/backend/test/test_facade.py+++ b/products/experiments/backend/test/test_facade.py@@ -2,6 +2,9 @@import uuidfrom datetime import UTC, datetime, timedelta+from decimal import Decimal+from types import SimpleNamespace+from typing import Anyimport pytestimport time_machine@@ -13,6 +16,7 @@ from posthog.test.base import (_create_flag_evaluations,flush_persons_and_events,)+from unittest.mock import MagicMock, patchfrom django.utils import timezone@@ -27,13 +31,18 @@ from products.experiments.backend.facade import api, count_running_experiments_ofrom products.experiments.backend.facade.change_signals import connect_experiment_changed, experiment_changedfrom products.experiments.backend.facade.contracts import (CreateExperimentInput,+ DigestCompletedExperiment,+ DigestLaunchedExperiment,EndedExperiment,ExperimentChanged,ExperimentValidationError,StoredPrimaryResult,+ TeamExperimentsSettings,+ TeamExperimentsSettingsUpdate,)from products.experiments.backend.metric_calculation.config import build_primary_calculation_configsfrom products.experiments.backend.models.experiment import Experiment, ExperimentHoldout, ExperimentMetricResult+from products.experiments.backend.models.team_experiments_config import TeamExperimentsConfigfrom products.feature_flags.backend.facade.enums import FlagEvaluationsModefrom products.feature_flags.backend.models.feature_flag import FeatureFlagfrom products.feature_flags.backend.models.organization_feature_flags_config import OrganizationFeatureFlagsConfig@@ -452,3 +461,287 @@ class TestGetStoredPrimaryResult(BaseTest):stats_config = {"method": "frequentist", "frequentist": {"difference_type": "ABSOLUTE"}}assert self._stored({"metric_type": "funnel", "series": [_CHECKOUT]}, stats_config, 20) is None+++class TestTeamHasLaunchedExperiments(BaseTest):+ @parameterized.expand(+ [+ ("draft", None, None, False, False, False),+ ("running", datetime(2026, 9, 1, tzinfo=UTC), None, False, False, True),+ ("stopped", datetime(2026, 9, 1, tzinfo=UTC), datetime(2026, 9, 20, tzinfo=UTC), False, False, True),+ ("deleted_still_counts", datetime(2026, 9, 1, tzinfo=UTC), None, True, False, True),+ ("another_team", datetime(2026, 9, 1, tzinfo=UTC), None, False, True, False),+ ]+ )+ def test_counts_running_and_stopped_experiments_of_the_team(+ self,+ _name: str,+ start_date: datetime | None,+ end_date: datetime | None,+ deleted: bool,+ other_team: bool,+ expected: bool,+ ) -> None:+ team = Team.objects.create(organization=self.organization, name="Other") if other_team else self.team+ flag = FeatureFlag.objects.create(team=team, key="experiment-flag", created_by=self.user)+ Experiment.objects.create(+ team=team, name="Experiment", feature_flag=flag, start_date=start_date, end_date=end_date, deleted=deleted+ )++ assert api.team_has_launched_experiments(self.team.id) is expected+++class TestExperimentFlagIds(BaseTest):+ @parameterized.expand(+ [+ ("with_deleted", True, {"live", "archived", "deleted", "deleted_null", "listed_team"}),+ ("without_deleted", False, {"live", "archived", "listed_team"}),+ ]+ )+ def test_returns_the_flags_of_the_listed_teams_experiments(+ self, _name: str, include_deleted: bool, expected: set[str]+ ) -> None:+ listed_team = Team.objects.create(organization=self.organization, name="Listed")+ unlisted_team = Team.objects.create(organization=self.organization, name="Unlisted")+ flag_ids: dict[str, int] = {}+ for key, team, deleted, archived in [+ ("live", self.team, False, False),+ ("archived", self.team, False, True),+ ("deleted", self.team, True, False),+ ("deleted_null", self.team, None, False),+ ("listed_team", listed_team, False, False),+ ("unlisted_team", unlisted_team, False, False),+ ]:+ flag = FeatureFlag.objects.create(team=team, key=key, created_by=self.user)+ Experiment.objects.create(team=team, name=key, feature_flag=flag, deleted=deleted, archived=archived)+ flag_ids[key] = flag.id++ assert api.experiment_flag_ids([self.team.id, listed_team.id], include_deleted=include_deleted) == {+ flag_ids[key] for key in expected+ }+++class TestDigestExperiments(BaseTest):+ def test_lists_the_experiments_launched_and_completed_in_the_period(self) -> None:+ period_start = datetime(2026, 10, 19, tzinfo=UTC)+ period_end = datetime(2026, 10, 26, tzinfo=UTC)+ day = timedelta(days=1)+ next_batch_team = Team.objects.create(organization=self.organization, name="Next batch")+ ids: dict[str, int] = {}+ for name, team, start_date, end_date, deleted in [+ ("started at the period start", self.team, period_start, None, False),+ ("started in the period", self.team, period_start + day, None, False),+ ("started at the period end", self.team, period_end, None, False),+ ("started and ended in the period", self.team, period_start + day, period_start + 2 * day, False),+ ("ended after the period", self.team, period_start + day, period_end + day, False),+ ("ended at the period start", self.team, period_start - 10 * day, period_start, False),+ ("ended at the period end", self.team, period_start - 10 * day, period_end, False),+ ("ended without a launch", self.team, None, period_start + 3 * day, False),+ ("deleted", self.team, period_start + day, None, True),+ ("launched in the next batch", next_batch_team, period_start + day, None, False),+ ("ended in the next batch", next_batch_team, period_start - 10 * day, period_end, False),+ ]:+ flag = FeatureFlag.objects.create(team=team, key=f"flag-{len(ids)}", created_by=self.user)+ experiment = Experiment.objects.create(+ team=team, name=name, feature_flag=flag, start_date=start_date, end_date=end_date, deleted=deleted+ )+ ids[name] = experiment.id+ batch = range(self.team.id, next_batch_team.id)++ launched = api.list_launched_experiments_for_digest(+ period_start=period_start, period_end=period_end, team_ids=batch+ )+ completed = api.list_completed_experiments_for_digest(+ period_start=period_start, period_end=period_end, team_ids=batch+ )++ assert sorted(launched, key=lambda experiment: experiment.id) == [+ DigestLaunchedExperiment(team_id=self.team.id, id=ids[name], name=name, start_date=start_date)+ for name, start_date in [+ ("started in the period", period_start + day),+ ("started at the period end", period_end),+ ("ended after the period", period_start + day),+ ("deleted", period_start + day),+ ]+ ]+ assert sorted(completed, key=lambda experiment: experiment.id) == [+ DigestCompletedExperiment(+ team_id=self.team.id, id=ids[name], name=name, start_date=start_date, end_date=end_date+ )+ for name, start_date, end_date in [+ ("started and ended in the period", period_start + day, period_start + 2 * day),+ ("ended at the period end", period_start - 10 * day, period_end),+ ("ended without a launch", None, period_start + 3 * day),+ ]+ ]+++class TestPrecomputationSettings(BaseTest):+ def test_lists_the_teams_with_precomputation_enabled(self) -> None:+ enabled_team = Team.objects.create(organization=self.organization, name="Enabled")+ disabled_team = Team.objects.create(organization=self.organization, name="Disabled")+ unconfigured_team = Team.objects.create(organization=self.organization, name="Unconfigured")+ TeamExperimentsConfig.objects.create(team=self.team, experiment_precomputation_enabled=True)+ TeamExperimentsConfig.objects.create(team=enabled_team, experiment_precomputation_enabled=True)+ TeamExperimentsConfig.objects.create(team=disabled_team, experiment_precomputation_enabled=False)++ assert api.list_precomputation_enabled_team_ids() == {self.team.id, enabled_team.id}+ assert api.list_precomputation_enabled_team_ids([self.team.id, disabled_team.id, unconfigured_team.id]) == {+ self.team.id+ }++ @parameterized.expand([("turns_off_an_auto_enrollment", True, False), ("creates_the_missing_row", False, True)])+ def test_setting_precomputation_marks_it_as_a_manual_decision(+ self, _name: str, auto_enrolled: bool, enabled: bool+ ) -> None:+ if auto_enrolled:+ TeamExperimentsConfig.objects.create(+ team=self.team,+ experiment_precomputation_enabled=True,+ precomputation_enabled_set_by=TeamExperimentsConfig.PrecomputationEnabledSetBy.AUTO,+ )++ api.set_precomputation_enabled(team_id=self.team.id, enabled=enabled)++ config = TeamExperimentsConfig.objects.get(team=self.team)+ assert (config.experiment_precomputation_enabled, config.precomputation_enabled_set_by) == (+ enabled,+ TeamExperimentsConfig.PrecomputationEnabledSetBy.MANUAL,+ )+++def _github_with_repositories(*full_names: str) -> MagicMock:+ return MagicMock(+ return_value=SimpleNamespace(+ list_all_cached_repositories=lambda max_repos: [{"full_name": full_name} for full_name in full_names]+ )+ )+++class TestTeamExperimentsSettings(BaseTest):+ def test_reads_every_setting(self) -> None:+ TeamExperimentsConfig.objects.create(+ team=self.team,+ experiment_recalculation_times=["02:00:00", "14:00:00"],+ default_experiment_confidence_level=Decimal("0.90"),+ default_experiment_stats_method="frequentist",+ experiment_precomputation_enabled=False,+ default_only_count_matured_users=True,+ default_cuped_enabled=False,+ default_cuped_lookback_days=14,+ default_minimum_detectable_effect=7,+ default_sequential_testing_enabled=True,+ default_sequential_tuning_parameter=5000,+ flag_cleanup_repository="posthog/posthog",+ )++ assert api.get_team_experiments_settings(self.team.id) == TeamExperimentsSettings(+ experiment_recalculation_times=("02:00:00", "14:00:00"),+ default_experiment_confidence_level=Decimal("0.90"),+ default_experiment_stats_method="frequentist",+ experiment_precomputation_enabled=False,+ default_only_count_matured_users=True,+ default_cuped_enabled=False,+ default_cuped_lookback_days=14,+ default_minimum_detectable_effect=7,+ default_sequential_testing_enabled=True,+ default_sequential_tuning_parameter=5000,+ flag_cleanup_repository="posthog/posthog",+ )++ def test_update_changes_only_the_settings_it_sets(self) -> None:+ TeamExperimentsConfig.objects.create(+ team=self.team,+ experiment_recalculation_times=["02:00:00"],+ default_cuped_lookback_days=14,+ default_minimum_detectable_effect=7,+ )++ with patch(+ "products.tasks.backend.facade.repo_selection.resolve_team_github_integration",+ _github_with_repositories("PostHog/posthog"),+ ):+ updated = api.update_team_experiments_settings(+ team_id=self.team.id,+ update=TeamExperimentsSettingsUpdate(+ experiment_recalculation_times=("14:00:00", "02:00:00"),+ default_experiment_confidence_level=Decimal("0.90"),+ default_cuped_lookback_days=None,+ flag_cleanup_repository="PostHog/PostHog",+ ),+ )++ stored = api.get_team_experiments_settings(self.team.id)+ assert updated == stored+ assert (+ stored.experiment_recalculation_times,+ stored.default_experiment_confidence_level,+ stored.default_cuped_lookback_days,+ stored.default_minimum_detectable_effect,+ stored.flag_cleanup_repository,+ ) == (("14:00:00", "02:00:00"), Decimal("0.90"), None, 7, "posthog/posthog")++ @parameterized.expand(+ [+ (+ "recalculation_times_too_close",+ {"experiment_recalculation_times": ("08:00:00", "09:00:00")},+ "experiment_recalculation_times",+ "Recalculation times must be at least 6 hours apart.",+ ),+ (+ "recalculation_times_empty",+ {"experiment_recalculation_times": ()},+ "experiment_recalculation_times",+ "Recalculation times must be a non-empty list of 'HH:00:00' strings.",+ ),+ (+ "confidence_level_of_one",+ {"default_experiment_confidence_level": Decimal("1.00")},+ "default_experiment_confidence_level",+ "Ensure this value is less than or equal to 0.99.",+ ),+ (+ "unknown_stats_method",+ {"default_experiment_stats_method": "magic"},+ "default_experiment_stats_method",+ "Value 'magic' is not a valid choice.",+ ),+ (+ "cuped_lookback_too_long",+ {"default_cuped_lookback_days": 31},+ "default_cuped_lookback_days",+ "Ensure this value is less than or equal to 30.",+ ),+ (+ "malformed_repository",+ {"flag_cleanup_repository": "not-a-repo"},+ "flag_cleanup_repository",+ "Repository must be in the format organization/repository",+ ),+ (+ "repository_outside_the_installation",+ {"flag_cleanup_repository": "other/repo"},+ "flag_cleanup_repository",+ "This repository is not connected to the project's GitHub integration.",+ ),+ ]+ )+ def test_update_refuses_an_invalid_setting_and_saves_nothing(+ self, _name: str, invalid: dict[str, Any], field: str, message: str+ ) -> None:+ before = api.get_team_experiments_settings(self.team.id)++ with (+ patch(+ "products.tasks.backend.facade.repo_selection.resolve_team_github_integration",+ _github_with_repositories("PostHog/posthog"),+ ),+ pytest.raises(ExperimentValidationError) as raised,+ ):+ api.update_team_experiments_settings(+ team_id=self.team.id, update=TeamExperimentsSettingsUpdate(default_cuped_enabled=True, **invalid)+ )++ assert (raised.value.field, str(raised.value)) == (field, message)+ assert api.get_team_experiments_settings(self.team.id) == before