1- # Copyright 2019, Optimizely
1+ # Copyright 2019-2020 , Optimizely
22# Licensed under the Apache License, Version 2.0 (the "License");
33# you may not use this file except in compliance with the License.
44# You may obtain a copy of the License at
1818
1919from optimizely import config_manager
2020from optimizely import exceptions as optimizely_exceptions
21+ from optimizely import optimizely_config
2122from optimizely import project_config
2223from optimizely .helpers import enums
2324
@@ -75,13 +76,19 @@ def test_set_config__success(self):
7576 )
7677 mock_notification_center .send_notifications .assert_called_once_with ('OPTIMIZELY_CONFIG_UPDATE' )
7778
78- def test_set_config__twice (self ):
79+ self .assertIsInstance (
80+ project_config_manager .optimizely_config ,
81+ optimizely_config .OptimizelyConfig
82+ )
83+
84+ def test_set_config__twice__with_same_content (self ):
7985 """ Test calling set_config twice with same content to ensure config is not updated. """
8086 test_datafile = json .dumps (self .config_dict_with_features )
8187 mock_logger = mock .Mock ()
8288 mock_notification_center = mock .Mock ()
8389
84- with mock .patch ('optimizely.config_manager.BaseConfigManager._validate_instantiation_options' ):
90+ with mock .patch ('optimizely.config_manager.BaseConfigManager._validate_instantiation_options' ), \
91+ mock .patch ('optimizely.optimizely_config.OptimizelyConfigService.get_config' ) as mock_opt_service :
8592 project_config_manager = config_manager .StaticConfigManager (
8693 datafile = test_datafile , logger = mock_logger , notification_center = mock_notification_center ,
8794 )
@@ -92,14 +99,49 @@ def test_set_config__twice(self):
9299 )
93100 self .assertEqual (1 , mock_logger .debug .call_count )
94101 mock_notification_center .send_notifications .assert_called_once_with ('OPTIMIZELY_CONFIG_UPDATE' )
102+ self .assertEqual (1 , mock_opt_service .call_count )
95103
96104 mock_logger .reset_mock ()
97105 mock_notification_center .reset_mock ()
106+ mock_opt_service .reset_mock ()
98107
99108 # Call set config again and confirm that no new log message denoting config update is there
100109 project_config_manager ._set_config (test_datafile )
101110 self .assertEqual (0 , mock_logger .debug .call_count )
102111 self .assertEqual (0 , mock_notification_center .call_count )
112+ # Assert that mock_opt_service is not called again.
113+ self .assertEqual (0 , mock_opt_service .call_count )
114+
115+ def test_set_config__twice__with_diff_content (self ):
116+ """ Test calling set_config twice with different content to ensure config is updated. """
117+ test_datafile = json .dumps (self .config_dict_with_features )
118+ mock_logger = mock .Mock ()
119+ mock_notification_center = mock .Mock ()
120+
121+ with mock .patch ('optimizely.config_manager.BaseConfigManager._validate_instantiation_options' ):
122+ project_config_manager = config_manager .StaticConfigManager (
123+ datafile = test_datafile , logger = mock_logger , notification_center = mock_notification_center ,
124+ )
125+
126+ mock_logger .debug .assert_called_with (
127+ 'Received new datafile and updated config. ' 'Old revision number: None. New revision number: 1.'
128+ )
129+ self .assertEqual (1 , mock_logger .debug .call_count )
130+ mock_notification_center .send_notifications .assert_called_once_with ('OPTIMIZELY_CONFIG_UPDATE' )
131+ self .assertEqual ('1' , project_config_manager .optimizely_config .revision )
132+
133+ mock_logger .reset_mock ()
134+ mock_notification_center .reset_mock ()
135+
136+ # Call set config again
137+ other_datafile = json .dumps (self .config_dict_with_multiple_experiments )
138+ project_config_manager ._set_config (other_datafile )
139+ mock_logger .debug .assert_called_with (
140+ 'Received new datafile and updated config. ' 'Old revision number: 1. New revision number: 42.'
141+ )
142+ self .assertEqual (1 , mock_logger .debug .call_count )
143+ mock_notification_center .send_notifications .assert_called_once_with ('OPTIMIZELY_CONFIG_UPDATE' )
144+ self .assertEqual ('42' , project_config_manager .optimizely_config .revision )
103145
104146 def test_set_config__schema_validation (self ):
105147 """ Test set_config calls or does not call schema validation based on skip_json_validation value. """
0 commit comments