Skip to content

Sending event when variation of an experiment has feature disabled #128

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 4 commits into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all 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: 7 additions & 3 deletions optimizely/optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,16 +388,20 @@ def is_feature_enabled(self, feature_key, user_id, attributes=None):
return False

decision = self.decision_service.get_variation_for_feature(feature, user_id, attributes)
if decision.variation and decision.variation.featureEnabled:
self.logger.info('Feature "%s" is enabled for user "%s".' % (feature_key, user_id))
if decision.variation:
# Send event if Decision came from an experiment.
if decision.source == decision_service.DECISION_SOURCE_EXPERIMENT:
self._send_impression_event(decision.experiment,
decision.variation,
user_id,
attributes)

return True
if decision.variation.featureEnabled:
self.logger.info('Feature "%s" is enabled for user "%s".' % (feature_key, user_id))
return True
else:
self.logger.info('Feature "%s" is not enabled for user "%s".' % (feature_key, user_id))
return False

self.logger.info('Feature "%s" is not enabled for user "%s".' % (feature_key, user_id))
return False
Expand Down
53 changes: 39 additions & 14 deletions tests/test_optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,10 +1181,9 @@ def test_is_feature_enabled__returns_false_for_invalid_feature(self):
# Check that no event is sent
self.assertEqual(0, mock_dispatch_event.call_count)

def test_is_feature_enabled__returns_true_for_feature_experiment_if_property_featureEnabled_is_true(self):
def test_is_feature_enabled__returns_true_for_feature_experiment_if_feature_enabled_for_variation(self):
""" Test that the feature is enabled for the user if bucketed into variation of an experiment and
the variation's featureEnabled property is True.
Also confirm that impression event is dispatched. """
the variation's featureEnabled property is True. Also confirm that impression event is dispatched. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
project_config = opt_obj.config
Expand Down Expand Up @@ -1240,10 +1239,9 @@ def test_is_feature_enabled__returns_true_for_feature_experiment_if_property_fea
'https://logx.optimizely.com/v1/events',
expected_params, 'POST', {'Content-Type': 'application/json'})

def test_is_feature_enabled__returns_false_for_feature_experiment_if_property_featureEnabled_is_false(self):
def test_is_feature_enabled__returns_false_for_feature_experiment_if_feature_disabled_for_variation(self):
""" Test that the feature is disabled for the user if bucketed into variation of an experiment and
the variation's featureEnabled property is False.
Also confirm that impression event is not dispatched. """
the variation's featureEnabled property is False. Also confirm that impression event is dispatched. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
project_config = opt_obj.config
Expand All @@ -1268,13 +1266,41 @@ def test_is_feature_enabled__returns_false_for_feature_experiment_if_property_fe

mock_decision.assert_called_once_with(feature, 'test_user', None)

# Check that impression event is not sent
self.assertEqual(0, mock_dispatch_event.call_count)
# Check that impression event is sent
expected_params = {
'account_id': '12001',
'project_id': '111111',
'visitors': [{
'visitor_id': 'test_user',
'attributes': [],
'snapshots': [{
'decisions': [{
'variation_id': '111128',
'experiment_id': '111127',
'campaign_id': '111182'
}],
'events': [{
'timestamp': 42000,
'entity_id': '111182',
'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
'key': 'campaign_activated',
}]
}]
}],
'client_version': version.__version__,
'client_name': 'python-sdk',
'anonymize_ip': False,
'revision': '1'
}
# Check that impression event is sent
self.assertEqual(1, mock_dispatch_event.call_count)
self._validate_event_object(mock_dispatch_event.call_args[0][0],
'https://logx.optimizely.com/v1/events',
expected_params, 'POST', {'Content-Type': 'application/json'})

def test_is_feature_enabled__returns_true_for_feature_rollout_if_property_featureEnabled_is_true(self):
def test_is_feature_enabled__returns_true_for_feature_rollout_if_feature_enabled(self):
""" Test that the feature is enabled for the user if bucketed into variation of a rollout and
the variation's featureEnabled property is True.
Also confirm that no impression event is dispatched. """
the variation's featureEnabled property is True. Also confirm that no impression event is dispatched. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
project_config = opt_obj.config
Expand Down Expand Up @@ -1302,10 +1328,9 @@ def test_is_feature_enabled__returns_true_for_feature_rollout_if_property_featur
# Check that impression event is not sent
self.assertEqual(0, mock_dispatch_event.call_count)

def test_is_feature_enabled__returns_false_for_feature_rollout_if_property_featureEnabled_is_false(self):
def test_is_feature_enabled__returns_false_for_feature_rollout_if_feature_disabled(self):
""" Test that the feature is disabled for the user if bucketed into variation of a rollout and
the variation's featureEnabled property is False.
Also confirm that no impression event is dispatched. """
the variation's featureEnabled property is False. Also confirm that no impression event is dispatched. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
project_config = opt_obj.config
Expand Down