Skip to content

Commit 2b95621

Browse files
Sending event when variation of an experiment has feature disabled (#128)
1 parent 29515fa commit 2b95621

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

optimizely/optimizely.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,16 +388,20 @@ def is_feature_enabled(self, feature_key, user_id, attributes=None):
388388
return False
389389

390390
decision = self.decision_service.get_variation_for_feature(feature, user_id, attributes)
391-
if decision.variation and decision.variation.featureEnabled:
392-
self.logger.info('Feature "%s" is enabled for user "%s".' % (feature_key, user_id))
391+
if decision.variation:
393392
# Send event if Decision came from an experiment.
394393
if decision.source == decision_service.DECISION_SOURCE_EXPERIMENT:
395394
self._send_impression_event(decision.experiment,
396395
decision.variation,
397396
user_id,
398397
attributes)
399398

400-
return True
399+
if decision.variation.featureEnabled:
400+
self.logger.info('Feature "%s" is enabled for user "%s".' % (feature_key, user_id))
401+
return True
402+
else:
403+
self.logger.info('Feature "%s" is not enabled for user "%s".' % (feature_key, user_id))
404+
return False
401405

402406
self.logger.info('Feature "%s" is not enabled for user "%s".' % (feature_key, user_id))
403407
return False

tests/test_optimizely.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,10 +1181,9 @@ def test_is_feature_enabled__returns_false_for_invalid_feature(self):
11811181
# Check that no event is sent
11821182
self.assertEqual(0, mock_dispatch_event.call_count)
11831183

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

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

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

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

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

1271-
# Check that impression event is not sent
1272-
self.assertEqual(0, mock_dispatch_event.call_count)
1269+
# Check that impression event is sent
1270+
expected_params = {
1271+
'account_id': '12001',
1272+
'project_id': '111111',
1273+
'visitors': [{
1274+
'visitor_id': 'test_user',
1275+
'attributes': [],
1276+
'snapshots': [{
1277+
'decisions': [{
1278+
'variation_id': '111128',
1279+
'experiment_id': '111127',
1280+
'campaign_id': '111182'
1281+
}],
1282+
'events': [{
1283+
'timestamp': 42000,
1284+
'entity_id': '111182',
1285+
'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
1286+
'key': 'campaign_activated',
1287+
}]
1288+
}]
1289+
}],
1290+
'client_version': version.__version__,
1291+
'client_name': 'python-sdk',
1292+
'anonymize_ip': False,
1293+
'revision': '1'
1294+
}
1295+
# Check that impression event is sent
1296+
self.assertEqual(1, mock_dispatch_event.call_count)
1297+
self._validate_event_object(mock_dispatch_event.call_args[0][0],
1298+
'https://logx.optimizely.com/v1/events',
1299+
expected_params, 'POST', {'Content-Type': 'application/json'})
12731300

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

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

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

13101335
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
13111336
project_config = opt_obj.config

0 commit comments

Comments
 (0)