10000 fix: Forced variation not available in experiment by ozayr-zaviar · Pull Request #367 · optimizely/python-sdk · GitHub
[go: up one dir, main page]

Skip to content

fix: Forced variation not available in experiment #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions optimizely/event/user_event_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ def create_impression_event(
if activated_experiment:
experiment_id = activated_experiment.id

if variation_id and experiment_id:
variation = project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id)
# need this condition when we send events involving forced decisions
elif variation_id and flag_key:
if variation_id and flag_key:
# need this condition when we send events involving forced decisions
# (F-to-D or E-to-D with any ruleKey/variationKey combinations)
variation = project_config.get_flag_variation(flag_key, 'id', variation_id)
8000 elif variation_id and experiment_id:
variation = project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id)

event_context = user_event.EventContext(
project_config.account_id, project_config.project_id, project_config.revision, project_config.anonymize_ip,
)
Expand Down
15 changes: 8 additions & 7 deletions optimizely/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,8 @@ def get_variation_from_id_by_experiment_id(self, experiment_id, variation_id):
variation_id in self.variation_id_map_by_experiment_id[experiment_id]):
return self.variation_id_map_by_experiment_id[experiment_id][variation_id]

self.logger.error('Variation with id "%s" not defined in the datafile for experiment "%s".',
variation_id, experiment_id)
self.logger.error('Variation with id "%s" not defined in the datafile for experiment "%s".' %
(variation_id, experiment_id))

return {}

Expand All @@ -628,8 +628,8 @@ def get_variation_from_key_by_experiment_id(self, experiment_id, variation_key):
variation_key in self.variation_key_map_by_experiment_id[experiment_id]):
return self.variation_key_map_by_experiment_id[experiment_id][variation_key]

self.logger.error('Variation with key "%s" not defined in the datafile for experiment "%s".',
variation_key, experiment_id)
self.logger.error('Variation with key "%s" not defined in the datafile for experiment "%s".' %
(variation_key, experiment_id))

return {}

Expand Down Expand Up @@ -661,8 +661,9 @@ def get_flag_variation(self, flag_key, variation_attribute, target_value):
return None

variations = self.flag_variations_map.get(flag_key)
for variation in variations:
if getattr(variation, variation_attribute) == target_value:
return variation
if variations:
for variation in variations:
if getattr(variation, variation_attribute) == target_value:
return variation

return None
25 changes: 25 additions & 0 deletions tests/test_user_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,31 @@ def test_should_return_valid_experiment_decision_after_setting_forced_decision(s

self.assertEqual(decide_decision.reasons, expected_reasons)

def test_should_return_valid_decision_after_setting_variation_of_different_experiment_in_forced_decision(self):
"""
Should return valid decision after setting setting variation of different experiment in forced decision.
"""
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
user_context = opt_obj.create_user_context("test_user", {})

context = OptimizelyUserContext.OptimizelyDecisionContext('test_feature_in_experiment_and_rollout',
'group_exp_2')
decision = OptimizelyUserContext.OptimizelyForcedDecision('211129')

status = user_context.set_forced_decision(context, decision)
self.assertTrue(status)
status = user_context.get_forced_decision(context)
self.assertEqual(status.variation_key, '211129')

decide_decision = user_context.decide('test_feature_in_experiment_and_rollout', ['INCLUDE_REASONS'])

self.assertEqual(decide_decision.variation_key, '211129')
self.assertEqual(decide_decision.rule_key, 'group_exp_2')
self.assertTrue(decide_decision.enabled)
self.assertEqual(decide_decision.flag_key, 'test_feature_in_experiment_and_rollout')
self.assertEqual(decide_decision.user_context.user_id, 'test_user')
self.assertEqual(decide_decision.user_context.get_user_attributes(), {})

def test_should_return_valid_decision_after_setting_invalid_delivery_rule_variation_in_forced_decision(self):
"""
Should return valid decision after setting invalid delivery rule variation in forced decision.
Expand Down
0