Skip to content
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

fix: handle datafile provided as bytes #384

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all 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/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, datafile, logger, error_handler):
"""

config = json.loads(datafile)
self._datafile = u'{}'.format(datafile)
self._datafile = datafile.decode('utf-8') if isinstance(datafile, bytes) else datafile
self.logger = logger
self.error_handler = error_handler
self.version = config.get('version')
Expand Down
13 changes: 13 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,19 @@ def test_to_datafile(self):

self.assertEqual(expected_datafile, actual_datafile)

def test_to_datafile_from_bytes(self):
""" Test that to_datafile returns the expected datafile when given bytes. """

expected_datafile = json.dumps(self.config_dict_with_features)
bytes_datafile = bytes(expected_datafile, 'utf-8')

opt_obj = optimizely.Optimizely(bytes_datafile)
project_config = opt_obj.config_manager.get_config()

actual_datafile = project_config.to_datafile()

self.assertEqual(expected_datafile, actual_datafile)


class ConfigLoggingTest(base.BaseTest):
def setUp(self):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_optimizely_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,18 @@ def test__get_datafile(self):

self.assertEqual(expected_datafile, actual_datafile)

def test__get_datafile_from_bytes(self):
""" Test that get_datafile returns the expected datafile when provided as bytes. """

expected_datafile = json.dumps(self.config_dict_with_features)
bytes_datafile = bytes(expected_datafile, 'utf-8')

opt_instance = optimizely.Optimizely(bytes_datafile)
opt_config = opt_instance.config_manager.optimizely_config
actual_datafile = opt_config.get_datafile()

self.assertEqual(expected_datafile, actual_datafile)

def test__get_sdk_key(self):
""" Test that get_sdk_key returns the expected value. """

Expand Down