diff --git a/api/features/multivariate/models.py b/api/features/multivariate/models.py index 665555bbf7c4..cec100e7db7f 100644 --- a/api/features/multivariate/models.py +++ b/api/features/multivariate/models.py @@ -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 ( @@ -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 diff --git a/api/tests/unit/features/multivariate/test_unit_multivariate_models.py b/api/tests/unit/features/multivariate/test_unit_multivariate_models.py index 1febb4ccd39c..c8de8b642233 100644 --- a/api/tests/unit/features/multivariate/test_unit_multivariate_models.py +++ b/api/tests/unit/features/multivariate/test_unit_multivariate_models.py @@ -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): @@ -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