Skip to content

Commit 76698f7

Browse files
sending decision on nil variation (#302)
1 parent 4b05245 commit 76698f7

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

optimizely/optimizely.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ def is_feature_enabled(self, feature_key, user_id, attributes=None):
580580
is_source_experiment = decision.source == enums.DecisionSources.FEATURE_TEST
581581
is_source_rollout = decision.source == enums.DecisionSources.ROLLOUT
582582

583-
if is_source_rollout and project_config.get_send_flag_decisions_value():
583+
if (is_source_rollout or not decision.variation) and project_config.get_send_flag_decisions_value():
584584
self._send_impression_event(
585585
project_config, decision.experiment, decision.variation, feature.key, decision.experiment.key if
586586
decision.experiment else '', decision.source, user_id, attributes

tests/test_optimizely.py

+42
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,48 @@ def test_is_feature_enabled__returns_false_when_user_is_not_bucketed_into_any_va
23712371
# Check that impression event is sent for rollout and send_flag_decisions = True
23722372
self.assertEqual(1, mock_process.call_count)
23732373

2374+
def test_is_feature_enabled__returns_false_when_variation_is_nil(self,):
2375+
""" Test that the feature is not enabled with nil variation
2376+
Also confirm that impression event is processed. """
2377+
2378+
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
2379+
project_config = opt_obj.config_manager.get_config()
2380+
feature = project_config.get_feature_from_key('test_feature_in_experiment_and_rollout')
2381+
with mock.patch(
2382+
'optimizely.decision_service.DecisionService.get_variation_for_feature',
2383+
return_value=decision_service.Decision(None, None, enums.DecisionSources.ROLLOUT),
2384+
) as mock_decision, mock.patch(
2385+
'optimizely.event.event_processor.ForwardingEventProcessor.process'
2386+
) as mock_process, mock.patch(
2387+
'optimizely.notification_center.NotificationCenter.send_notifications'
2388+
) as mock_broadcast_decision, mock.patch(
2389+
'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'
2390+
), mock.patch(
2391+
'time.time', return_value=42
2392+
):
2393+
self.assertFalse(opt_obj.is_feature_enabled("test_feature_in_experiment_and_rollout", 'test_user'))
2394+
2395+
# Check that impression event is sent for rollout and send_flag_decisions = True
2396+
self.assertEqual(1, mock_process.call_count)
2397+
2398+
mock_decision.assert_called_once_with(opt_obj.config_manager.get_config(), feature, 'test_user', None)
2399+
2400+
mock_broadcast_decision.assert_called_with(
2401+
enums.NotificationTypes.DECISION,
2402+
'feature',
2403+
'test_user',
2404+
{},
2405+
{
2406+
'feature_key': 'test_feature_in_experiment_and_rollout',
2407+
'feature_enabled': False,
2408+
'source': 'rollout',
2409+
'source_info': {},
2410+
},
2411+
)
2412+
2413+
# Check that impression event is sent for rollout and send_flag_decisions = True
2414+
self.assertEqual(1, mock_process.call_count)
2415+
23742416
def test_is_feature_enabled__invalid_object(self):
23752417
""" Test that is_feature_enabled returns False and logs error if Optimizely instance is invalid. """
23762418

0 commit comments

Comments
 (0)