Skip to content

sdk_key and environment_key support #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion optimizely/event/event_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(
@property
def is_running(self):
""" Property to check if consumer thread is alive or not. """
return self.executor.isAlive() if self.executor else False
return self.executor.is_alive() if self.executor else False

def _validate_instantiation_props(self, prop, prop_name, default_value):
""" Method to determine if instantiation properties like batch_size, flush_interval
Expand Down
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
6 changes: 6 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
def long(a):
raise NotImplementedError('Tests should only call `long` if running in PY2')

# Check to verify if TestCase has the attribute assertRasesRegex or assertRaisesRegexp
# This check depends on the version of python with assertRaisesRegexp being used by
# python2.7. Later versions of python are using the non-deprecated assertRaisesRegex.
if not hasattr(unittest.TestCase, 'assertRaisesRegex'):
unittest.TestCase.assertRaisesRegex = getattr(unittest.TestCase, 'assertRaisesRegexp')


class BaseTest(unittest.TestCase):
def assertStrictTrue(self, to_assert):
Expand Down
18 changes: 9 additions & 9 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ def setUp(self):
def test_get_experiment_from_key__invalid_key(self):
""" Test that exception is raised when provided experiment key is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidExperimentException,
enums.Errors.INVALID_EXPERIMENT_KEY,
self.project_config.get_experiment_from_key,
Expand All @@ -1127,14 +1127,14 @@ def test_get_experiment_from_key__invalid_key(self):
def test_get_audience__invalid_id(self):
""" Test that message is logged when provided audience ID is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidAudienceException, enums.Errors.INVALID_AUDIENCE, self.project_config.get_audience, '42',
)

def test_get_variation_from_key__invalid_experiment_key(self):
""" Test that exception is raised when provided experiment key is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidExperimentException,
enums.Errors.INVALID_EXPERIMENT_KEY,
self.project_config.get_variation_from_key,
Expand All @@ -1145,7 +1145,7 @@ def test_get_variation_from_key__invalid_experiment_key(self):
def test_get_variation_from_key__invalid_variation_key(self):
""" Test that exception is raised when provided variation key is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidVariationException,
enums.Errors.INVALID_VARIATION,
self.project_config.get_variation_from_key,
Expand All @@ -1156,7 +1156,7 @@ def test_get_variation_from_key__invalid_variation_key(self):
def test_get_variation_from_id__invalid_experiment_key(self):
""" Test that exception is raised when provided experiment key is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidExperimentException,
enums.Errors.INVALID_EXPERIMENT_KEY,
self.project_config.get_variation_from_id,
Expand All @@ -1167,7 +1167,7 @@ def test_get_variation_from_id__invalid_experiment_key(self):
def test_get_variation_from_id__invalid_variation_id(self):
""" Test that exception is raised when provided variation ID is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidVariationException,
enums.Errors.INVALID_VARIATION,
self.project_config.get_variation_from_key,
Expand All @@ -1178,7 +1178,7 @@ def test_get_variation_from_id__invalid_variation_id(self):
def test_get_event__invalid_key(self):
""" Test that exception is raised when provided event key is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidEventException,
enums.Errors.INVALID_EVENT_KEY,
self.project_config.get_event,
Expand All @@ -1188,7 +1188,7 @@ def test_get_event__invalid_key(self):
def test_get_attribute_id__invalid_key(self):
""" Test that exception is raised when provided attribute key is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidAttributeException,
enums.Errors.INVALID_ATTRIBUTE,
self.project_config.get_attribute_id,
Expand All @@ -1198,7 +1198,7 @@ def test_get_attribute_id__invalid_key(self):
def test_get_group__invalid_id(self):
""" Test that exception is raised when provided group ID is invalid. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidGroupException, enums.Errors.INVALID_GROUP_ID, self.project_config.get_group, '42',
)

Expand Down
20 changes: 10 additions & 10 deletions tests/test_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_init__invalid_logger_fails(self):
class InvalidLogger(object):
pass

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
optimizely_exceptions.InvalidInputException, 'Provided "logger" is in an invalid format.',
):
config_manager.StaticConfigManager(logger=InvalidLogger())
Expand All @@ -43,7 +43,7 @@ def test_init__invalid_error_handler_fails(self):
class InvalidErrorHandler(object):
pass

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
optimizely_exceptions.InvalidInputException, 'Provided "error_handler" is in an invalid format.',
):
config_manager.StaticConfigManager(error_handler=InvalidErrorHandler())
Expand All @@ -54,7 +54,7 @@ def test_init__invalid_notification_center_fails(self):
class InvalidNotificationCenter(object):
pass

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
optimizely_exceptions.InvalidInputException, 'Provided "notification_center" is in an invalid format.',
):
config_manager.StaticConfigManager(notification_center=InvalidNotificationCenter())
Expand Down Expand Up @@ -222,7 +222,7 @@ def test_get_config_blocks(self):
class PollingConfigManagerTest(base.BaseTest):
def test_init__no_sdk_key_no_url__fails(self, _):
""" Test that initialization fails if there is no sdk_key or url provided. """
self.assertRaisesRegexp(
self.assertRaisesRegex(
optimizely_exceptions.InvalidInputException,
'Must provide at least one of sdk_key or url.',
config_manager.PollingConfigManager,
Expand All @@ -232,7 +232,7 @@ def test_init__no_sdk_key_no_url__fails(self, _):

def test_get_datafile_url__no_sdk_key_no_url_raises(self, _):
""" Test that get_datafile_url raises exception if no sdk_key or url is provided. """
self.assertRaisesRegexp(
self.assertRaisesRegex(
optimizely_exceptions.InvalidInputException,
'Must provide at least one of sdk_key or url.',
config_manager.PollingConfigManager.get_datafile_url,
Expand All @@ -244,7 +244,7 @@ def test_get_datafile_url__no_sdk_key_no_url_raises(self, _):
def test_get_datafile_url__invalid_url_template_raises(self, _):
""" Test that get_datafile_url raises if url_template is invalid. """
# No url_template provided
self.assertRaisesRegexp(
self.assertRaisesRegex(
optimizely_exceptions.InvalidInputException,
'Invalid url_template None provided',
config_manager.PollingConfigManager.get_datafile_url,
Expand All @@ -255,7 +255,7 @@ def test_get_datafile_url__invalid_url_template_raises(self, _):

# Incorrect url_template provided
test_url_template = 'invalid_url_template_without_sdk_key_field_{key}'
self.assertRaisesRegexp(
self.assertRaisesRegex(
optimizely_exceptions.InvalidInputException,
'Invalid url_template {} provided'.format(test_url_template),
config_manager.PollingConfigManager.get_datafile_url,
Expand Down Expand Up @@ -298,7 +298,7 @@ def test_set_update_interval(self, _):
project_config_manager = config_manager.PollingConfigManager(sdk_key='some_key')

# Assert that if invalid update_interval is set, then exception is raised.
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
optimizely_exceptions.InvalidInputException, 'Invalid update_interval "invalid interval" provided.',
):
project_config_manager.set_update_interval('invalid interval')
Expand All @@ -325,7 +325,7 @@ def test_set_blocking_timeout(self, _):
project_config_manager = config_manager.PollingConfigManager(sdk_key='some_key')

# Assert that if invalid blocking_timeout is set, then exception is raised.
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
optimizely_exceptions.InvalidInputException, 'Invalid blocking timeout "invalid timeout" provided.',
):
project_config_manager.set_blocking_timeout('invalid timeout')
Expand Down Expand Up @@ -484,7 +484,7 @@ def test_is_running(self, _):
class AuthDatafilePollingConfigManagerTest(base.BaseTest):
def test_init__datafile_access_token_none__fails(self, _):
""" Test that initialization fails if datafile_access_token is None. """
self.assertRaisesRegexp(
self.assertRaisesRegex(
optimizely_exceptions.InvalidInputException,
'datafile_access_token cannot be empty or None.',
config_manager.AuthDatafilePollingConfigManager,
Expand Down
21 changes: 17 additions & 4 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 Expand Up @@ -4633,7 +4646,7 @@ def setUp(self):
def test_activate__with_attributes__invalid_attributes(self):
""" Test that activate raises exception if attributes are in invalid format. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidAttributeException,
enums.Errors.INVALID_ATTRIBUTE_FORMAT,
self.optimizely.activate,
Expand All @@ -4645,7 +4658,7 @@ def test_activate__with_attributes__invalid_attributes(self):
def test_track__with_attributes__invalid_attributes(self):
""" Test that track raises exception if attributes are in invalid format. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidAttributeException,
enums.Errors.INVALID_ATTRIBUTE_FORMAT,
self.optimizely.track,
Expand All @@ -4657,7 +4670,7 @@ def test_track__with_attributes__invalid_attributes(self):
def test_track__with_event_tag__invalid_event_tag(self):
""" Test that track raises exception if event_tag is in invalid format. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidEventTagException,
enums.Errors.INVALID_EVENT_TAG_FORMAT,
self.optimizely.track,
Expand All @@ -4669,7 +4682,7 @@ def test_track__with_event_tag__invalid_event_tag(self):
def test_get_variation__with_attributes__invalid_attributes(self):
""" Test that get variation raises exception if attributes are in invalid format. """

self.assertRaisesRegexp(
self.assertRaisesRegex(
exceptions.InvalidAttributeException,
enums.Errors.INVALID_ATTRIBUTE_FORMAT,
self.optimizely.get_variation,
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
Loading