Skip to content

Commit

Permalink
sort enabled feature keys (#104)
Browse files Browse the repository at this point in the history
* 🖊️ sort enabled feature keys response

* 🖊️ Dictionary values are called in different orders in python versions
  • Loading branch information
oakbani authored and kellyroach-optimizely committed Mar 12, 2018
1 parent 695ca1c commit cb28ea4
Show file tree
Hide file tree
Showing 2 changed files with 25 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
23 changes: 23 additions & 0 deletions tests/test_optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,29 @@ 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')

mock_is_feature_enabled.assert_any_call('test_feature_in_experiment', 'user_1', None)
mock_is_feature_enabled.assert_any_call('test_feature_in_rollout', 'user_1', None)
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)

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

self.assertEqual(expected_sorted_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 cb28ea4

Please sign in to comment.