Skip to content

Commit

Permalink
fix(5044/mvfsv): handle deleted feature states (#5050)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
gagantrivedi and pre-commit-ci[bot] authored Jan 29, 2025
1 parent 5431585 commit b325998
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api/features/multivariate/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
AbstractBaseExportableModel,
abstract_base_auditable_model_factory,
)
from django.core.exceptions import ObjectDoesNotExist
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django_lifecycle import (
Expand Down Expand Up @@ -143,6 +144,14 @@ def clone(self, feature_state: "FeatureState", persist: bool = True):

return clone

def get_skip_create_audit_log(self) -> bool:
try:
if self.feature_state.deleted_at:
return True
return self.feature_state.get_skip_create_audit_log()
except ObjectDoesNotExist:
return True

def get_update_log_message(self, history_instance) -> typing.Optional[str]:
feature_state = self.feature_state
feature = feature_state.feature
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from unittest.mock import MagicMock

from environments.identities.models import Identity
from environments.models import Environment
from features.feature_types import MULTIVARIATE, STANDARD
from features.models import FeatureSegment, FeatureState
from features.multivariate.models import (
MultivariateFeatureOption,
MultivariateFeatureStateValue,
)
from segments.models import Segment


def test_multivariate_feature_option_get_create_log_message(feature):
Expand Down Expand Up @@ -160,3 +163,79 @@ def test_adding_mv_option_to_standard_feature_converts_it_into_multivariate(feat
# Then
feature.refresh_from_db()
assert feature.type == MULTIVARIATE


def test_multivariate_feature_state_value__get_skip_create_audit_log_for_identity_delete(
multivariate_feature: MultivariateFeatureOption,
environment: Environment,
identity: Identity,
) -> None:
# Given
identity_override = FeatureState.objects.create(
feature=multivariate_feature, identity=identity, environment=environment
)
mvfsv = identity_override.multivariate_feature_state_values.create(
multivariate_feature_option=multivariate_feature.multivariate_options.first(),
percentage_allocation=100,
)

# When
identity.delete()

# Then
mvfsv_history_instance = MultivariateFeatureStateValue.history.filter(
id=mvfsv.id, history_type="-"
).first()

assert mvfsv_history_instance.instance.get_skip_create_audit_log() is True


def test_multivariate_feature_state_value__get_skip_create_audit_log_for_segment_delete(
multivariate_feature: MultivariateFeatureOption,
environment: Environment,
segment: Segment,
) -> None:
# Given
feature_segment = FeatureSegment.objects.create(
segment=segment, feature=multivariate_feature, environment=environment
)
segment_override = FeatureState.objects.create(
feature=multivariate_feature,
feature_segment=feature_segment,
environment=environment,
)
mvfsv = segment_override.multivariate_feature_state_values.create(
multivariate_feature_option=multivariate_feature.multivariate_options.first(),
percentage_allocation=100,
)

# When
segment.delete()

# Then
mvfsv_history_instance = MultivariateFeatureStateValue.history.filter(
id=mvfsv.id, history_type="-"
).first()

assert mvfsv_history_instance.instance.get_skip_create_audit_log() is True


def test_multivariate_feature_state_value__get_skip_create_audit_log_for_feature_delete(
multivariate_feature: MultivariateFeatureOption,
environment: Environment,
) -> None:

# Given
mvfsv = MultivariateFeatureStateValue.objects.filter(
feature_state__environment=environment
).first()

# When
multivariate_feature.delete()

# Then
mvfsv_history_instance = MultivariateFeatureStateValue.history.filter(
id=mvfsv.id, history_type="-"
).first()

assert mvfsv_history_instance.instance.get_skip_create_audit_log() is True

0 comments on commit b325998

Please sign in to comment.