Skip to content

Commit

Permalink
Defaulting to default value in case no variable usage exists for vari…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
aliabbasrizvi committed Oct 11, 2017
1 parent 3182b9b commit 69a0440
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions optimizely/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def get_variable_value_for_variation(self, variable, variation):
variation: The Variation for which we are getting the variable value.
Returns:
The type-casted variable value or None if any of the inputs are invalid.
The variable value or None if any of the inputs are invalid.
"""

if not variable or not variation:
Expand All @@ -438,9 +438,10 @@ def get_variable_value_for_variation(self, variable, variation):
variable_usages = self.variation_variable_usage_map[variation.id]

# Find usage in given variation
variable_usage = variable_usages[variable.id]
variable_usage = variable_usages.get(variable.id)

return variable_usage.value
# Return default value in case there is no variable usage for the variable.
return variable_usage.value if variable_usage else variable.defaultValue

def get_variable_for_feature(self, feature_key, variable_key):
""" Get the variable with the given variable key for the given feature.
Expand Down
11 changes: 11 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,17 @@ def test_get_variable_value_for_variation__no_variables_for_variation(self):
is_working_variable = project_config.get_variable_for_feature('test_feature_in_experiment', 'is_working')
self.assertIsNone(project_config.get_variable_value_for_variation(is_working_variable, variation))

def test_get_variable_value_for_variation__no_usage_of_variable(self):
""" Test that a variable with no usage will return default value for variable. """

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

variation = project_config.get_variation_from_id('test_experiment', '111128')
variable_without_usage_variable = project_config.get_variable_for_feature('test_feature_in_experiment',
'variable_without_usage')
self.assertEqual('45', project_config.get_variable_value_for_variation(variable_without_usage_variable, variation))

def test_get_variable_for_feature__returns_valid_variable(self):
""" Test that the feature variable is returned. """

Expand Down

0 comments on commit 69a0440

Please sign in to comment.