Skip to content

Commit

Permalink
Merge 4f27743 into c3b191b
Browse files Browse the repository at this point in the history
  • Loading branch information
The-inside-man committed Jun 22, 2021
2 parents c3b191b + 4f27743 commit fc523b9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
30 changes: 28 additions & 2 deletions optimizely/optimizely_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@


class OptimizelyConfig(object):
def __init__(self, revision, experiments_map, features_map, datafile=None):
def __init__(self, revision, experiments_map, features_map, datafile=None, sdk_key=None, environment_key=None):
self.revision = revision
self.experiments_map = experiments_map
self.features_map = features_map
self._datafile = datafile
self.sdk_key = sdk_key
self.environment_key = environment_key

def get_datafile(self):
""" Get the datafile associated with OptimizelyConfig.
Expand All @@ -31,6 +33,22 @@ def get_datafile(self):
"""
return self._datafile

def get_sdk_key(self):
""" Get the sdk key associated with OptimizelyConfig.
Returns:
A string containing sdk key.
"""
return self.sdk_key

def get_environment_key(self):
""" Get the environemnt key associated with OptimizelyConfig.
Returns:
A string containing environment key.
"""
return self.environment_key


class OptimizelyExperiment(object):
def __init__(self, id, key, variations_map):
Expand Down Expand Up @@ -82,6 +100,8 @@ def __init__(self, project_config):
self.feature_flags = project_config.feature_flags
self.groups = project_config.groups
self.revision = project_config.revision
self.sdk_key = project_config.sdk_key
self.environment_key = project_config.environment_key

self._create_lookup_maps()

Expand All @@ -98,7 +118,13 @@ def get_config(self):
experiments_key_map, experiments_id_map = self._get_experiments_maps()
features_map = self._get_features_map(experiments_id_map)

return OptimizelyConfig(self.revision, experiments_key_map, features_map, self._datafile)
return OptimizelyConfig(
self.revision,
experiments_key_map,
features_map,
self._datafile,
self.sdk_key,
self.environment_key)

def _create_lookup_maps(self):
""" Creates lookup maps to avoid redundant iteration of config objects. """
Expand Down
20 changes: 20 additions & 0 deletions optimizely/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def __init__(self, datafile, logger, error_handler):
self.account_id = config.get('accountId')
self.project_id = config.get('projectId')
self.revision = config.get('revision')
self.sdk_key = config.get('sdkKey', None)
self.environment_key = config.get('environmentKey', None)
self.groups = config.get('groups', [])
self.experiments = config.get('experiments', [])
self.events = config.get('events', [])
Expand Down Expand Up @@ -213,6 +215,24 @@ def get_revision(self):

return self.revision

def get_sdk_key(self):
""" Get sdk key from the datafile.
Returns:
Revision of the sdk key.
"""

return self.sdk_key

def get_environment_key(self):
""" Get environment key from the datafile.
Returns:
Revision of the environment key.
"""

return self.environment_key

def get_account_id(self):
""" Get account ID from the config.
Expand Down
13 changes: 13 additions & 0 deletions tests/test_optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,19 @@ def test_init__sdk_key_and_datafile_access_token(self):

self.assertIs(type(opt_obj.config_manager), config_manager.AuthDatafilePollingConfigManager)

def test_init__invalid_default_decide_options(self):
"""
Test to confirm that default decide options passed not as a list will trigger setting
self.deafulat_decide_options as an empty list.
"""
invalid_decide_options = {"testKey": "testOption"}

mock_client_logger = mock.MagicMock()
with mock.patch('optimizely.logger.reset_logger', return_value=mock_client_logger):
opt_obj = optimizely.Optimizely(default_decide_options=invalid_decide_options)

self.assertEqual(opt_obj.default_decide_options, [])

def test_invalid_json_raises_schema_validation_off(self):
""" Test that invalid JSON logs error if schema validation is turned off. """

Expand Down
2 changes: 2 additions & 0 deletions tests/test_optimizely_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def setUp(self):
self.opt_config_service = optimizely_config.OptimizelyConfigService(self.project_config)

self.expected_config = {
'sdk_key': None,
'environment_key': None,
'experiments_map': {
'test_experiment2': {
'variations_map': {
Expand Down
17 changes: 17 additions & 0 deletions tests/test_user_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ def test_user_context(self):
self.assertEqual("firefox", uc.get_user_attributes()["browser"])
self.assertEqual("red", uc.get_user_attributes()["color"])

def test_user_and_attributes_as_json(self):
"""
tests user context as json
"""
uc = OptimizelyUserContext(self.optimizely, "test_user")

# set an attribute
uc.set_attribute("browser", "safari")

# set expected json obj
expected_json = {
"user_id": uc.user_id,
"attributes": uc.get_user_attributes(),
}

self.assertEqual(uc.as_json(), expected_json)

def test_attributes_are_cloned_when_passed_to_user_context(self):
user_id = 'test_user'
attributes = {"browser": "chrome"}
Expand Down

0 comments on commit fc523b9

Please sign in to comment.