Skip to content

[FSSDK-11017] update: experiment_id and variation_id added to payloads #447

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
# Line break before operand needs to be ignored for line lengths
# greater than max-line-length. Best practice shows W504
ignore = E722, W504
exclude = optimizely/lib/pymmh3.py,*virtualenv*
exclude = optimizely/lib/pymmh3.py,*virtualenv*,tests/testapp/application.py
max-line-length = 120
20 changes: 19 additions & 1 deletion optimizely/optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,22 @@ def _create_optimizely_decision(
if flag_decision is not None and flag_decision.variation is not None
else None
)

experiment_id = None
variation_id = None

try:
if flag_decision.experiment is not None:
experiment_id = flag_decision.experiment.id
except AttributeError:
self.logger.warning("flag_decision.experiment has no attribute 'id'")

try:
if flag_decision.variation is not None:
variation_id = flag_decision.variation.id
except AttributeError:
self.logger.warning("flag_decision.variation has no attribute 'id'")

# Send notification
self.notification_center.send_notifications(
enums.NotificationTypes.DECISION,
Expand All @@ -1215,7 +1231,9 @@ def _create_optimizely_decision(
'variation_key': variation_key,
'rule_key': rule_key,
'reasons': decision_reasons if should_include_reasons else [],
'decision_event_dispatched': decision_event_dispatched
'decision_event_dispatched': decision_event_dispatched,
'experiment_id': experiment_id,
'variation_id': variation_id

},
)
Expand Down
70 changes: 47 additions & 23 deletions tests/test_user_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ def test_decide__feature_test(self):
'reasons': expected.reasons,
'decision_event_dispatched': True,
'variables': expected.variables,
'experiment_id': mock_experiment.id,
'variation_id': mock_variation.id
},
)

Expand Down Expand Up @@ -391,6 +393,24 @@ def test_decide_feature_rollout(self):

self.compare_opt_decisions(expected, actual)

# assert event count
self.assertEqual(1, mock_send_event.call_count)

# assert event payload
expected_experiment = project_config.get_experiment_from_key(expected.rule_key)
expected_var = project_config.get_variation_from_key(expected.rule_key, expected.variation_key)
mock_send_event.assert_called_with(
project_config,
expected_experiment,
expected_var,
expected.flag_key,
expected.rule_key,
'rollout',
expected.enabled,
'test_user',
user_attributes
)

# assert notification count
self.assertEqual(1, mock_broadcast_decision.call_count)

Expand All @@ -408,27 +428,11 @@ def test_decide_feature_rollout(self):
'reasons': expected.reasons,
'decision_event_dispatched': True,
'variables': expected.variables,
'experiment_id': expected_experiment.id,
'variation_id': expected_var.id
},
)

# assert event count
self.assertEqual(1, mock_send_event.call_count)

# assert event payload
expected_experiment = project_config.get_experiment_from_key(expected.rule_key)
expected_var = project_config.get_variation_from_key(expected.rule_key, expected.variation_key)
mock_send_event.assert_called_with(
project_config,
expected_experiment,
expected_var,
expected.flag_key,
expected.rule_key,
'rollout',
expected.enabled,
'test_user',
user_attributes
)

def test_decide_feature_rollout__send_flag_decision_false(self):
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
project_config = opt_obj.config_manager.get_config()
Expand Down Expand Up @@ -467,6 +471,8 @@ def test_decide_feature_rollout__send_flag_decision_false(self):
self.assertEqual(1, mock_broadcast_decision.call_count)

# assert notification
expected_experiment = project_config.get_experiment_from_key(expected.rule_key)
expected_var = project_config.get_variation_from_key(expected.rule_key, expected.variation_key)
mock_broadcast_decision.assert_called_with(
enums.NotificationTypes.DECISION,
'flag',
Expand All @@ -480,6 +486,8 @@ def test_decide_feature_rollout__send_flag_decision_false(self):
'reasons': expected.reasons,
'decision_event_dispatched': False,
'variables': expected.variables,
'experiment_id': expected_experiment.id,
'variation_id': expected_var.id
},
)

Expand Down Expand Up @@ -549,7 +557,9 @@ def test_decide_feature_null_variation(self):
'reasons': expected.reasons,
'decision_event_dispatched': True,
'variables': expected.variables,
},
'experiment_id': None,
'variation_id': None
}
)

# assert event count
Expand Down Expand Up @@ -632,6 +642,8 @@ def test_decide_feature_null_variation__send_flag_decision_false(self):
'reasons': expected.reasons,
'decision_event_dispatched': False,
'variables': expected.variables,
'experiment_id': None,
'variation_id': None
},
)

Expand Down Expand Up @@ -701,6 +713,8 @@ def test_decide__option__disable_decision_event(self):
'reasons': expected.reasons,
'decision_event_dispatched': False,
'variables': expected.variables,
'experiment_id': mock_experiment.id,
'variation_id': mock_variation.id,
},
)

Expand Down Expand Up @@ -773,6 +787,8 @@ def test_decide__default_option__disable_decision_event(self):
'reasons': expected.reasons,
'decision_event_dispatched': False,
'variables': expected.variables,
'experiment_id': mock_experiment.id,
'variation_id': mock_variation.id
},
)

Expand Down Expand Up @@ -834,6 +850,8 @@ def test_decide__option__exclude_variables(self):
'reasons': expected.reasons,
'decision_event_dispatched': True,
'variables': expected.variables,
'experiment_id': mock_experiment.id,
'variation_id': mock_variation.id,
},
)

Expand Down Expand Up @@ -948,6 +966,8 @@ def test_decide__option__enabled_flags_only(self):
'reasons': expected.reasons,
'decision_event_dispatched': True,
'variables': expected.variables,
'experiment_id': expected_experiment.id,
'variation_id': expected_var.id,
},
)

Expand Down Expand Up @@ -1006,7 +1026,7 @@ def test_decide__default_options__with__options(self):
enabled=True,
variables=expected_variables,
flag_key='test_feature_in_experiment',
user_context=user_context
user_context=user_context,
)

self.compare_opt_decisions(expected, actual)
Expand All @@ -1025,6 +1045,8 @@ def test_decide__default_options__with__options(self):
'reasons': expected.reasons,
'decision_event_dispatched': False,
'variables': expected.variables,
'experiment_id': mock_experiment.id,
'variation_id': mock_variation.id
},
)

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

expected_experiment = project_config.get_experiment_from_key(expected.rule_key)
expected_var = project_config.get_variation_from_key('test_experiment', expected.variation_key)

# assert notification count
self.assertEqual(1, mock_broadcast_decision.call_count)

Expand All @@ -1507,12 +1532,11 @@ def test_should_return_valid_decision_after_setting_and_removing_forced_decision
'reasons': expected.reasons,
'decision_event_dispatched': True,
'variables': expected.variables,
'experiment_id': expected_experiment.id,
'variation_id': expected_var.id
},
)

expected_experiment = project_config.get_experiment_from_key(expected.rule_key)
expected_var = project_config.get_variation_from_key('test_experiment', expected.variation_key)

mock_send_event.assert_called_with(
project_config,
expected_experiment,
Expand Down