Skip to content

Commit

Permalink
sending decision on nil variation
Browse files Browse the repository at this point in the history
  • Loading branch information
pawels-optimizely committed Oct 9, 2020
1 parent 4b05245 commit 1cd48cf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion optimizely/optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ def is_feature_enabled(self, feature_key, user_id, attributes=None):
is_source_experiment = decision.source == enums.DecisionSources.FEATURE_TEST
is_source_rollout = decision.source == enums.DecisionSources.ROLLOUT

if is_source_rollout and project_config.get_send_flag_decisions_value():
if (is_source_rollout or not decision.variation) and project_config.get_send_flag_decisions_value():
self._send_impression_event(
project_config, decision.experiment, decision.variation, feature.key, decision.experiment.key if
decision.experiment else '', decision.source, user_id, attributes
Expand Down
42 changes: 42 additions & 0 deletions tests/test_optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -2371,6 +2371,48 @@ def test_is_feature_enabled__returns_false_when_user_is_not_bucketed_into_any_va
# Check that impression event is sent for rollout and send_flag_decisions = True
self.assertEqual(1, mock_process.call_count)

def test_is_feature_enabled__returns_false_when_variation_is_nil(self,):
""" Test that the feature is not enabled with nil variation
Also confirm that impression event is processed. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
project_config = opt_obj.config_manager.get_config()
feature = project_config.get_feature_from_key('test_feature_in_experiment_and_rollout')
with mock.patch(
'optimizely.decision_service.DecisionService.get_variation_for_feature',
return_value=decision_service.Decision(None, None, enums.DecisionSources.ROLLOUT),
) as mock_decision, mock.patch(
'optimizely.event.event_processor.ForwardingEventProcessor.process'
) as mock_process, mock.patch(
'optimizely.notification_center.NotificationCenter.send_notifications'
) as mock_broadcast_decision, mock.patch(
'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'
), mock.patch(
'time.time', return_value=42
):
self.assertFalse(opt_obj.is_feature_enabled("test_feature_in_experiment_and_rollout", 'test_user'))

# Check that impression event is sent for rollout and send_flag_decisions = True
self.assertEqual(1, mock_process.call_count)

mock_decision.assert_called_once_with(opt_obj.config_manager.get_config(), feature, 'test_user', None)

mock_broadcast_decision.assert_called_with(
enums.NotificationTypes.DECISION,
'feature',
'test_user',
{},
{
'feature_key': 'test_feature_in_experiment_and_rollout',
'feature_enabled': False,
'source': 'rollout',
'source_info': {},
},
)

# Check that impression event is sent for rollout and send_flag_decisions = True
self.assertEqual(1, mock_process.call_count)

def test_is_feature_enabled__invalid_object(self):
""" Test that is_feature_enabled returns False and logs error if Optimizely instance is invalid. """

Expand Down

0 comments on commit 1cd48cf

Please sign in to comment.