Skip to content

Commit

Permalink
handle datafile provided as bytes (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewleap-optimizely authored Jun 2, 2022
1 parent eee3aa0 commit 42f6663
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
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

0 comments on commit 42f6663

Please sign in to comment.