Skip to content

Commit 47e7b4f

Browse files
[FSSDK-11017] update: experiment_id and variation_id added to payloads (#447)
* experiment_id and variation_id added to payloads * 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. * .flake8 -> redundant checks being performed in tests/testapp/application.py so added it to exclusions * reverting to previous code * change in logic to get experiment_id by key or rollout_id * update project_config.py * fetching experiment_id and variation_id from flag_decision * -updated experiment_id and variation_id fetching logic -removed redundant function from project_config.py * chore: trigger workflow
1 parent fd0930c commit 47e7b4f

File tree

3 files changed

+67
-25
lines changed

3 files changed

+67
-25
lines changed

.flake8

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
# Line break before operand needs to be ignored for line lengths
55
# greater than max-line-length. Best practice shows W504
66
ignore = E722, W504
7-
exclude = optimizely/lib/pymmh3.py,*virtualenv*
7+
exclude = optimizely/lib/pymmh3.py,*virtualenv*,tests/testapp/application.py
88
max-line-length = 120

optimizely/optimizely.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,22 @@ def _create_optimizely_decision(
12021202
if flag_decision is not None and flag_decision.variation is not None
12031203
else None
12041204
)
1205+
1206+
experiment_id = None
1207+
variation_id = None
1208+
1209+
try:
1210+
if flag_decision.experiment is not None:
1211+
experiment_id = flag_decision.experiment.id
1212+
except AttributeError:
1213+
self.logger.warning("flag_decision.experiment has no attribute 'id'")
1214+
1215+
try:
1216+
if flag_decision.variation is not None:
1217+
variation_id = flag_decision.variation.id
1218+
except AttributeError:
1219+
self.logger.warning("flag_decision.variation has no attribute 'id'")
1220+
12051221
# Send notification
12061222
self.notification_center.send_notifications(
12071223
enums.NotificationTypes.DECISION,
@@ -1215,7 +1231,9 @@ def _create_optimizely_decision(
12151231
'variation_key': variation_key,
12161232
'rule_key': rule_key,
12171233
'reasons': decision_reasons if should_include_reasons else [],
1218-
'decision_event_dispatched': decision_event_dispatched
1234+
'decision_event_dispatched': decision_event_dispatched,
1235+
'experiment_id': experiment_id,
1236+
'variation_id': variation_id
12191237

12201238
},
12211239
)

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)