Skip to content

Commit 6fdf655

Browse files
Update unit tests and add more tests (#73)
1 parent 669bca2 commit 6fdf655

File tree

5 files changed

+158
-137
lines changed

5 files changed

+158
-137
lines changed

optimizely/decision_service.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,17 @@ def get_variation(self, experiment, user_id, attributes, ignore_user_profile=Fal
155155

156156
return None
157157

158-
def get_variation_for_rollout(self, rollout, user_id, attributes=None, ignore_user_profile=False):
158+
def get_variation_for_rollout(self, rollout, user_id, attributes=None):
159159
""" Determine which variation the user is in for a given rollout.
160160
Returns the variation of the first experiment the user qualifies for.
161161
162162
Args:
163163
rollout: Rollout for which we are getting the variation.
164164
user_id: ID for user.
165165
attributes: Dict representing user attributes.
166-
ignore_user_profile: True to ignore the user profile lookup. Defaults to False.
167166
168167
Returns:
169-
Variation the user should see. None if the user is not in any of the layer's experiments.
168+
Variation the user should see. None if the user is not in any of the rollout's targeting rules.
170169
"""
171170

172171
# Go through each experiment in order and try to get the variation for the user
@@ -249,8 +248,8 @@ def get_variation_for_feature(self, feature, user_id, attributes=None):
249248

250249
# Next check if user is part of a rollout
251250
if not variation and feature.rolloutId:
252-
rollout = self.config.get_layer_from_id(feature.rolloutId)
253-
variation = self.get_variation_for_rollout(rollout, user_id, attributes, ignore_user_profile=True)
251+
rollout = self.config.get_rollout_from_id(feature.rolloutId)
252+
variation = self.get_variation_for_rollout(rollout, user_id, attributes)
254253

255254
return variation
256255

optimizely/project_config.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ def __init__(self, datafile, logger, error_handler):
6262
self.event_key_map = self._generate_key_map(self.events, 'key', entities.Event)
6363
self.attribute_key_map = self._generate_key_map(self.attributes, 'key', entities.Attribute)
6464
self.audience_id_map = self._generate_key_map(self.audiences, 'id', entities.Audience)
65-
self.layer_id_map = self._generate_key_map(self.rollouts, 'id', entities.Layer)
66-
for layer in self.layer_id_map.values():
65+
self.rollout_id_map = self._generate_key_map(self.rollouts, 'id', entities.Layer)
66+
for layer in self.rollout_id_map.values():
6767
for experiment in layer.experiments:
6868
self.experiment_key_map[experiment['key']] = entities.Experiment(**experiment)
6969

@@ -399,21 +399,21 @@ def get_feature_from_key(self, feature_key):
399399
self.logger.log(enums.LogLevels.ERROR, 'Feature "%s" is not in datafile.' % feature_key)
400400
return None
401401

402-
def get_layer_from_id(self, layer_id):
403-
""" Get layer for the provided layer id.
402+
def get_rollout_from_id(self, rollout_id):
403+
""" Get rollout for the provided ID.
404404
405405
Args:
406-
layer_id: ID of the layer to be fetched.
406+
rollout_id: ID of the rollout to be fetched.
407407
408408
Returns:
409-
Layer corresponding to the provided layer id.
409+
Rollout corresponding to the provided ID.
410410
"""
411-
layer = self.layer_id_map.get(layer_id)
411+
layer = self.rollout_id_map.get(rollout_id)
412412

413413
if layer:
414414
return layer
415415

416-
self.logger.log(enums.LogLevels.ERROR, 'Layer with ID "%s" is not in datafile.' % layer_id)
416+
self.logger.log(enums.LogLevels.ERROR, 'Rollout with ID "%s" is not in datafile.' % rollout_id)
417417
return None
418418

419419
def get_variable_value_for_variation(self, variable, variation):

tests/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ def setUp(self):
258258
'id': '11159'
259259
}],
260260
'rollouts': [{
261+
'id': '201111',
262+
'experiments': []
263+
}, {
261264
'id': '211111',
262265
'experiments': [{
263266
'id': '211127',
@@ -524,6 +527,9 @@ def setUp(self):
524527
'id': '11159'
525528
}],
526529
'rollouts': [{
530+
'id': '201111',
531+
'experiments': []
532+
}, {
527533
'id': '211111',
528534
'experiments': [{
529535
'id': '211127',

tests/test_config.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ def test_init__with_v4_datafile(self):
913913
'test_feature_in_group': entities.FeatureFlag('91113', 'test_feature_in_group', ['32222'], '', {}, '19228')
914914
}
915915

916-
expected_layer_id_map = {
916+
expected_rollout_id_map = {
917917
'211111': entities.Layer('211111', [{
918918
'key': '211112',
919919
'status': 'Running',
@@ -970,7 +970,7 @@ def test_init__with_v4_datafile(self):
970970
self.assertEqual(expected_variation_key_map, project_config.variation_key_map)
971971
self.assertEqual(expected_variation_id_map, project_config.variation_id_map)
972972
self.assertEqual(expected_feature_key_map, project_config.feature_key_map)
973-
self.assertEqual(expected_layer_id_map, project_config.layer_id_map)
973+
self.assertEqual(expected_rollout_id_map, project_config.rollout_id_map)
974974
self.assertEqual(expected_variation_variable_usage_map, project_config.variation_variable_usage_map)
975975

976976
def test_get_version(self):
@@ -1128,27 +1128,27 @@ def test_get_group__invalid_id(self):
11281128

11291129
def test_get_feature_from_key__valid_feature_key(self):
11301130
""" Test that a valid feature is returned given a valid feature key. """
1131-
optimizely_instance = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1132-
project_config = optimizely_instance.config
1131+
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1132+
project_config = opt_obj.config
11331133

11341134
expected_feature = entities.FeatureFlag('91112', 'test_feature_in_rollout', [], '211111', {})
11351135
self.assertEqual(expected_feature, project_config.get_feature_from_key('test_feature_in_rollout'))
11361136

11371137
def test_get_feature_from_key__invalid_feature_key(self):
11381138
""" Test that None is returned given an invalid feature key. """
11391139

1140-
optimizely_instance = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1141-
project_config = optimizely_instance.config
1140+
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1141+
project_config = opt_obj.config
11421142

11431143
self.assertIsNone(project_config.get_feature_from_key('invalid_feature_key'))
11441144

1145-
def test_get_layer_from_id__valid_layer_id(self):
1146-
""" Test that a valid layer is returned """
1145+
def test_get_rollout_from_id__valid_rollout_id(self):
1146+
""" Test that a valid rollout is returned """
11471147

1148-
optimizely_instance = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1149-
project_config = optimizely_instance.config
1148+
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1149+
project_config = opt_obj.config
11501150

1151-
expected_layer = entities.Layer('211111', [{
1151+
expected_rollout = entities.Layer('211111', [{
11521152
'id': '211127',
11531153
'key': '211127',
11541154
'status': 'Running',
@@ -1194,12 +1194,12 @@ def test_get_layer_from_id__valid_layer_id(self):
11941194
'id': '211149'
11951195
}]
11961196
}])
1197-
self.assertEqual(expected_layer, project_config.get_layer_from_id('211111'))
1197+
self.assertEqual(expected_rollout, project_config.get_rollout_from_id('211111'))
11981198

11991199
def test_get_variable_value_for_variation__returns_valid_value(self):
12001200
""" Test that the right value and type are returned. """
1201-
optimizely_instance = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1202-
project_config = optimizely_instance.config
1201+
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1202+
project_config = opt_obj.config
12031203

12041204
variation = project_config.get_variation_from_id('test_experiment', '111128')
12051205
is_working_variable = project_config.get_variable_for_feature('test_feature_in_experiment', 'is_working')
@@ -1210,17 +1210,17 @@ def test_get_variable_value_for_variation__returns_valid_value(self):
12101210
def test_get_variable_value_for_variation__invalid_variable(self):
12111211
""" Test that an invalid variable key will return None. """
12121212

1213-
optimizely_instance = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1214-
project_config = optimizely_instance.config
1213+
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1214+
project_config = opt_obj.config
12151215

12161216
variation = project_config.get_variation_from_id('test_experiment', '111128')
12171217
self.assertIsNone(project_config.get_variable_value_for_variation(None, variation))
12181218

12191219
def test_get_variable_value_for_variation__no_variables_for_variation(self):
12201220
""" Test that a variation with no variables will return None. """
12211221

1222-
optimizely_instance = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1223-
project_config = optimizely_instance.config
1222+
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1223+
project_config = opt_obj.config
12241224

12251225
variation = entities.Variation('1111281', 'invalid_variation', [])
12261226
is_working_variable = project_config.get_variable_for_feature('test_feature_in_experiment', 'is_working')
@@ -1229,25 +1229,25 @@ def test_get_variable_value_for_variation__no_variables_for_variation(self):
12291229
def test_get_variable_for_feature__returns_valid_variable(self):
12301230
""" Test that the feature variable is returned. """
12311231

1232-
optimizely_instance = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1233-
project_config = optimizely_instance.config
1232+
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1233+
project_config = opt_obj.config
12341234

12351235
variable = project_config.get_variable_for_feature('test_feature_in_experiment', 'is_working')
12361236
self.assertEqual(entities.Variable('127', 'is_working', 'boolean', 'true'), variable)
12371237

12381238
def test_get_variable_for_feature__invalid_feature_key(self):
12391239
""" Test that an invalid feature key will return None. """
12401240

1241-
optimizely_instance = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1242-
project_config = optimizely_instance.config
1241+
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1242+
project_config = opt_obj.config
12431243

12441244
self.assertIsNone(project_config.get_variable_for_feature('invalid_feature', 'is_working'))
12451245

12461246
def test_get_variable_for_feature__invalid_variable_key(self):
12471247
""" Test that an invalid variable key will return None. """
12481248

1249-
optimizely_instance = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1250-
project_config = optimizely_instance.config
1249+
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1250+
project_config = opt_obj.config
12511251

12521252
self.assertIsNone(project_config.get_variable_for_feature('test_feature_in_experiment', 'invalid_variable_key'))
12531253

0 commit comments

Comments
 (0)