Skip to content

Commit 60dba53

Browse files
optimizely/optimizely.py -> Removed experiment_id and variation_id from legacy apis.
optimizely/project_config.py -> Enhanced comments for clarity. tests/test_user_context.py -> Updated test assertions for experiments.
1 parent 969be5f commit 60dba53

File tree

3 files changed

+65
-52
lines changed

3 files changed

+65
-52
lines changed

optimizely/optimizely.py

+13-24
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,7 @@ def _get_feature_variable_for_type(
340340
user_context = OptimizelyUserContext(self, self.logger, user_id, attributes, False)
341341

342342
decision, _ = self.decision_service.get_variation_for_feature(project_config, feature_flag, user_context)
343-
experiment_id = decision.experiment.id if decision.experiment else None
344-
variation_id = decision.variation.id if decision.variation else None
345-
343+
346344
if decision.variation:
347345

348346
feature_enabled = decision.variation.featureEnabled
@@ -388,8 +386,6 @@ def _get_feature_variable_for_type(
388386
'variable_value': actual_value,
389387
'variable_type': variable_type,
390388
'source_info': source_info,
391-
'experiment_id': experiment_id,
392-
'variation_id': variation_id
393389
},
394390
)
395391
return actual_value
@@ -431,9 +427,7 @@ def _get_all_feature_variables_for_type(
431427
user_context = OptimizelyUserContext(self, self.logger, user_id, attributes, False)
432428

433429
decision, _ = self.decision_service.get_variation_for_feature(project_config, feature_flag, user_context)
434-
experiment_id = decision.experiment.id if decision.experiment else None
435-
variation_id = decision.variation.id if decision.variation else None
436-
430+
437431
if decision.variation:
438432

439433
feature_enabled = decision.variation.featureEnabled
@@ -486,8 +480,6 @@ def _get_all_feature_variables_for_type(
486480
'variable_values': all_variables,
487481
'source': decision.source,
488482
'source_info': source_info,
489-
'experiment_id': experiment_id,
490-
'variation_id': variation_id
491483
},
492484
)
493485
return all_variables
@@ -654,10 +646,7 @@ def get_variation(
654646
decision_notification_type = enums.DecisionNotificationTypes.FEATURE_TEST
655647
else:
656648
decision_notification_type = enums.DecisionNotificationTypes.AB_TEST
657-
658-
experiment_id = experiment.id if experiment else None
659-
variation_id = variation.id if variation else None
660-
649+
661650
self.notification_center.send_notifications(
662651
enums.NotificationTypes.DECISION,
663652
decision_notification_type,
@@ -666,8 +655,6 @@ def get_variation(
666655
{
667656
'experiment_key': experiment_key,
668657
'variation_key': variation_key,
669-
'experiment_id': experiment_id,
670-
'variation_id': variation_id
671658
},
672659
)
673660

@@ -754,8 +741,6 @@ def is_feature_enabled(self, feature_key: str, user_id: str, attributes: Optiona
754741
'feature_enabled': feature_enabled,
755742
'source': decision.source,
756743
'source_info': source_info,
757-
'experiment_id': decision.experiment.id,
758-
'variation_id': decision.variation.id
759744
},
760745
)
761746

@@ -1220,15 +1205,19 @@ def _create_optimizely_decision(
12201205
if flag_decision is not None and flag_decision.variation is not None
12211206
else None
12221207
)
1223-
1224-
rollout_id = feature_flag.rolloutId if decision_source == DecisionSources.ROLLOUT else None
1225-
experiment_id = project_config.get_experiment_id_by_key_or_rollout_id(rule_key, rollout_id)
1208+
1209+
rollout_id = None
1210+
if decision_source == DecisionSources.ROLLOUT and feature_flag is not None:
1211+
rollout_id = feature_flag.rolloutId
1212+
experiment_id = None
1213+
if rule_key is not None:
1214+
experiment_id = project_config.get_experiment_id_by_key_or_rollout_id(rule_key, rollout_id)
12261215
variation_id = None
1227-
if variation_key:
1216+
if experiment_id and variation_key:
12281217
variation = project_config.get_variation_from_key_by_experiment_id(experiment_id, variation_key)
12291218
if variation:
1230-
variation_id = variation.id
1231-
1219+
variation_id = variation.id
1220+
12321221
# Send notification
12331222
self.notification_center.send_notifications(
12341223
enums.NotificationTypes.DECISION,

optimizely/project_config.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,8 @@ def get_experiment_id_by_key_or_rollout_id(self, key: str, rollout_id: Optional[
722722
Retrieves the experiment ID associated with a given rule key or a specific rollout.
723723
724724
Args:
725-
key: The key associated with the experiment rule.
726-
rollout_id: The ID of the rollout to search if the key is not found.
725+
key: The key associated with the experiment rule. It can be experiment key or rule key.
726+
rollout_id: The ID of the rollout to be searched if the key is not found in the experiment key map.
727727
728728
Returns:
729729
Optional[str]: The experiment ID if found, otherwise None.
@@ -738,9 +738,9 @@ def get_experiment_id_by_key_or_rollout_id(self, key: str, rollout_id: Optional[
738738
if rollout_id:
739739
rollout = self.get_rollout_from_id(rollout_id)
740740
if rollout:
741-
for experiment in rollout.experiments:
742-
experiment = entities.Experiment(**experiment)
741+
for experiment_data in rollout.experiments:
742+
experiment = entities.Experiment(**experiment_data)
743743
if experiment.key == key:
744-
return experiment.id
744+
return experiment.id
745745

746746
return None

tests/test_user_context.py

+47-23
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ def test_decide__feature_test(self):
283283
'reasons': expected.reasons,
284284
'decision_event_dispatched': True,
285285
'variables': expected.variables,
286+
'experiment_id': mock_experiment.id,
287+
'variation_id': mock_variation.id
286288
},
287289
)
288290

@@ -391,6 +393,24 @@ def test_decide_feature_rollout(self):
391393

392394
self.compare_opt_decisions(expected, actual)
393395

396+
# assert event count
397+
self.assertEqual(1, mock_send_event.call_count)
398+
399+
# assert event payload
400+
expected_experiment = project_config.get_experiment_from_key(expected.rule_key)
401+
expected_var = project_config.get_variation_from_key(expected.rule_key, expected.variation_key)
402+
mock_send_event.assert_called_with(
403+
project_config,
404+
expected_experiment,
405+
expected_var,
406+
expected.flag_key,
407+
expected.rule_key,
408+
'rollout',
409+
expected.enabled,
410+
'test_user',
411+
user_attributes
412+
)
413+
394414
# assert notification count
395415
self.assertEqual(1, mock_broadcast_decision.call_count)
396416

@@ -408,27 +428,11 @@ def test_decide_feature_rollout(self):
408428
'reasons': expected.reasons,
409429
'decision_event_dispatched': True,
410430
'variables': expected.variables,
431+
'experiment_id': expected_experiment.id,
432+
'variation_id': expected_var.id
411433
},
412434
)
413435

414-
# assert event count
415-
self.assertEqual(1, mock_send_event.call_count)
416-
417-
# assert event payload
418-
expected_experiment = project_config.get_experiment_from_key(expected.rule_key)
419-
expected_var = project_config.get_variation_from_key(expected.rule_key, expected.variation_key)
420-
mock_send_event.assert_called_with(
421-
project_config,
422-
expected_experiment,
423-
expected_var,
424-
expected.flag_key,
425-
expected.rule_key,
426-
'rollout',
427-
expected.enabled,
428-
'test_user',
429-
user_attributes
430-
)
431-
432436
def test_decide_feature_rollout__send_flag_decision_false(self):
433437
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
434438
project_config = opt_obj.config_manager.get_config()
@@ -467,6 +471,8 @@ def test_decide_feature_rollout__send_flag_decision_false(self):
467471
self.assertEqual(1, mock_broadcast_decision.call_count)
468472

469473
# assert notification
474+
expected_experiment = project_config.get_experiment_from_key(expected.rule_key)
475+
expected_var = project_config.get_variation_from_key(expected.rule_key, expected.variation_key)
470476
mock_broadcast_decision.assert_called_with(
471477
enums.NotificationTypes.DECISION,
472478
'flag',
@@ -480,6 +486,8 @@ def test_decide_feature_rollout__send_flag_decision_false(self):
480486
'reasons': expected.reasons,
481487
'decision_event_dispatched': False,
482488
'variables': expected.variables,
489+
'experiment_id': expected_experiment.id,
490+
'variation_id': expected_var.id
483491
},
484492
)
485493

@@ -549,7 +557,9 @@ def test_decide_feature_null_variation(self):
549557
'reasons': expected.reasons,
550558
'decision_event_dispatched': True,
551559
'variables': expected.variables,
552-
},
560+
'experiment_id': None,
561+
'variation_id': None
562+
}
553563
)
554564

555565
# assert event count
@@ -632,6 +642,8 @@ def test_decide_feature_null_variation__send_flag_decision_false(self):
632642
'reasons': expected.reasons,
633643
'decision_event_dispatched': False,
634644
'variables': expected.variables,
645+
'experiment_id': None,
646+
'variation_id': None
635647
},
636648
)
637649

@@ -701,6 +713,8 @@ def test_decide__option__disable_decision_event(self):
701713
'reasons': expected.reasons,
702714
'decision_event_dispatched': False,
703715
'variables': expected.variables,
716+
'experiment_id': mock_experiment.id,
717+
'variation_id': mock_variation.id,
704718
},
705719
)
706720

@@ -773,6 +787,8 @@ def test_decide__default_option__disable_decision_event(self):
773787
'reasons': expected.reasons,
774788
'decision_event_dispatched': False,
775789
'variables': expected.variables,
790+
'experiment_id': mock_experiment.id,
791+
'variation_id': mock_variation.id
776792
},
777793
)
778794

@@ -834,6 +850,8 @@ def test_decide__option__exclude_variables(self):
834850
'reasons': expected.reasons,
835851
'decision_event_dispatched': True,
836852
'variables': expected.variables,
853+
'experiment_id': mock_experiment.id,
854+
'variation_id': mock_variation.id,
837855
},
838856
)
839857

@@ -948,6 +966,8 @@ def test_decide__option__enabled_flags_only(self):
948966
'reasons': expected.reasons,
949967
'decision_event_dispatched': True,
950968
'variables': expected.variables,
969+
'experiment_id': expected_experiment.id,
970+
'variation_id': expected_var.id,
951971
},
952972
)
953973

@@ -1006,7 +1026,7 @@ def test_decide__default_options__with__options(self):
10061026
enabled=True,
10071027
variables=expected_variables,
10081028
flag_key='test_feature_in_experiment',
1009-
user_context=user_context
1029+
user_context=user_context,
10101030
)
10111031

10121032
self.compare_opt_decisions(expected, actual)
@@ -1025,6 +1045,8 @@ def test_decide__default_options__with__options(self):
10251045
'reasons': expected.reasons,
10261046
'decision_event_dispatched': False,
10271047
'variables': expected.variables,
1048+
'experiment_id': mock_experiment.id,
1049+
'variation_id': mock_variation.id
10281050
},
10291051
)
10301052

@@ -1490,6 +1512,9 @@ def test_should_return_valid_decision_after_setting_and_removing_forced_decision
14901512
'User "test_user" is in variation "control" of experiment test_experiment.']
14911513
)
14921514

1515+
expected_experiment = project_config.get_experiment_from_key(expected.rule_key)
1516+
expected_var = project_config.get_variation_from_key('test_experiment', expected.variation_key)
1517+
14931518
# assert notification count
14941519
self.assertEqual(1, mock_broadcast_decision.call_count)
14951520

@@ -1507,12 +1532,11 @@ def test_should_return_valid_decision_after_setting_and_removing_forced_decision
15071532
'reasons': expected.reasons,
15081533
'decision_event_dispatched': True,
15091534
'variables': expected.variables,
1535+
'experiment_id': expected_experiment.id,
1536+
'variation_id': expected_var.id
15101537
},
15111538
)
15121539

1513-
expected_experiment = project_config.get_experiment_from_key(expected.rule_key)
1514-
expected_var = project_config.get_variation_from_key('test_experiment', expected.variation_key)
1515-
15161540
mock_send_event.assert_called_with(
15171541
project_config,
15181542
expected_experiment,

0 commit comments

Comments
 (0)