Skip to content

Commit

Permalink
tests: Add caching tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oakbani committed Jan 13, 2020
1 parent 3110e5e commit 9d70543
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
15 changes: 13 additions & 2 deletions tests/test_config_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2019, Optimizely
# Copyright 2019-2020, Optimizely
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand All @@ -18,6 +18,7 @@

from optimizely import config_manager
from optimizely import exceptions as optimizely_exceptions
from optimizely import optimizely_config
from optimizely import project_config
from optimizely.helpers import enums

Expand Down Expand Up @@ -75,13 +76,19 @@ def test_set_config__success(self):
)
mock_notification_center.send_notifications.assert_called_once_with('OPTIMIZELY_CONFIG_UPDATE')

self.assertIsInstance(
project_config_manager.optimizely_config,
optimizely_config.OptimizelyConfig
)

def test_set_config__twice(self):
""" Test calling set_config twice with same content to ensure config is not updated. """
test_datafile = json.dumps(self.config_dict_with_features)
mock_logger = mock.Mock()
mock_notification_center = mock.Mock()

with mock.patch('optimizely.config_manager.BaseConfigManager._validate_instantiation_options'):
with mock.patch('optimizely.config_manager.BaseConfigManager._validate_instantiation_options'), \
mock.patch('optimizely.optimizely_config.OptimizelyConfigService.get_config') as mock_opt_service:
project_config_manager = config_manager.StaticConfigManager(
datafile=test_datafile, logger=mock_logger, notification_center=mock_notification_center,
)
Expand All @@ -92,14 +99,18 @@ def test_set_config__twice(self):
)
self.assertEqual(1, mock_logger.debug.call_count)
mock_notification_center.send_notifications.assert_called_once_with('OPTIMIZELY_CONFIG_UPDATE')
self.assertEqual(1, mock_opt_service.call_count)

mock_logger.reset_mock()
mock_notification_center.reset_mock()
mock_opt_service.reset_mock()

# Call set config again and confirm that no new log message denoting config update is there
project_config_manager._set_config(test_datafile)
self.assertEqual(0, mock_logger.debug.call_count)
self.assertEqual(0, mock_notification_center.call_count)
# Assert that mock_opt_service is not called again.
self.assertEqual(0, mock_opt_service.call_count)

def test_set_config__schema_validation(self):
""" Test set_config calls or does not call schema validation based on skip_json_validation value. """
Expand Down
23 changes: 23 additions & 0 deletions tests/test_optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -3945,6 +3945,29 @@ def test_get_optimizely_config_returns_instance_of_optimizely_config(self):
opt_config = opt_obj.get_optimizely_config()
self.assertIsInstance(opt_config, optimizely_config.OptimizelyConfig)

def test_get_optimizely_config_with_custom_config_manager(self):
""" Test that get_optimizely_config returns a valid instance of OptimizelyConfig
when a custom config manager is used. """

some_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
return_config = some_obj.config_manager.get_config()

class SomeConfigManager(object):
def get_config(self):
return return_config

opt_obj = optimizely.Optimizely(config_manager=SomeConfigManager())
self.assertIsInstance(
opt_obj.get_optimizely_config(),
optimizely_config.OptimizelyConfig
)

with mock.patch('optimizely.optimizely_config.OptimizelyConfigService.get_config') as mock_opt_service:
opt_obj = optimizely.Optimizely(config_manager=SomeConfigManager())
opt_obj.get_optimizely_config()

self.assertEqual(1, mock_opt_service.call_count)


class OptimizelyWithExceptionTest(base.BaseTest):
def setUp(self):
Expand Down

0 comments on commit 9d70543

Please sign in to comment.