Skip to content

Commit

Permalink
🖊️ sort enabled feature keys response
Browse files Browse the repository at this point in the history
  • Loading branch information
oakbani committed Mar 5, 2018
1 parent 695ca1c commit 12aa484
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion optimizely/optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def get_enabled_features(self, user_id, attributes=None):
attributes: Dict representing user attributes.
Returns:
A list of the keys of the features that are enabled for the user.
A sorted list of the keys of the features that are enabled for the user.
"""

enabled_features = []
Expand All @@ -433,6 +433,7 @@ def get_enabled_features(self, user_id, attributes=None):
if self.is_feature_enabled(feature.key, user_id, attributes):
enabled_features.append(feature.key)

enabled_features.sort()
return enabled_features

def get_feature_variable_boolean(self, feature_key, variable_key, user_id, attributes=None):
Expand Down
27 changes: 27 additions & 0 deletions tests/test_optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,33 @@ def side_effect(*args, **kwargs):
mock_is_feature_enabled.assert_any_call('test_feature_in_group', 'user_1', None)
mock_is_feature_enabled.assert_any_call('test_feature_in_experiment_and_rollout', 'user_1', None)

def test_get_enabled_features_returns_a_sorted_list(self):
""" Test that get_enabled_features returns a sorted list of enabled feature keys. """

opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))

with mock.patch('optimizely.optimizely.Optimizely.is_feature_enabled',
return_value=True) as mock_is_feature_enabled:
received_features = opt_obj.get_enabled_features('user_1')

calls = [
mock.call('test_feature_in_experiment', 'user_1', None),
mock.call('test_feature_in_group', 'user_1', None),
mock.call('test_feature_in_rollout', 'user_1', None),
mock.call('test_feature_in_experiment_and_rollout', 'user_1', None)
]

mock_is_feature_enabled.assert_has_calls(calls, any_order=False)

expected_enabled_features = [
'test_feature_in_experiment',
'test_feature_in_experiment_and_rollout',
'test_feature_in_group',
'test_feature_in_rollout'
]

self.assertEqual(expected_enabled_features, received_features)

def test_get_enabled_features__invalid_object(self):
""" Test that get_enabled_features returns empty list if Optimizely object is not valid. """

Expand Down

0 comments on commit 12aa484

Please sign in to comment.