1
- # Copyright 2019, Optimizely
1
+ # Copyright 2019-2020 , Optimizely
2
2
# Licensed under the Apache License, Version 2.0 (the "License");
3
3
# you may not use this file except in compliance with the License.
4
4
# You may obtain a copy of the License at
18
18
19
19
from optimizely import config_manager
20
20
from optimizely import exceptions as optimizely_exceptions
21
+ from optimizely import optimizely_config
21
22
from optimizely import project_config
22
23
from optimizely .helpers import enums
23
24
@@ -75,13 +76,19 @@ def test_set_config__success(self):
75
76
)
76
77
mock_notification_center .send_notifications .assert_called_once_with ('OPTIMIZELY_CONFIG_UPDATE' )
77
78
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 ):
79
85
""" Test calling set_config twice with same content to ensure config is not updated. """
80
86
test_datafile = json .dumps (self .config_dict_with_features )
81
87
mock_logger = mock .Mock ()
82
88
mock_notification_center = mock .Mock ()
83
89
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 :
85
92
project_config_manager = config_manager .StaticConfigManager (
86
93
datafile = test_datafile , logger = mock_logger , notification_center = mock_notification_center ,
87
94
)
@@ -92,14 +99,49 @@ def test_set_config__twice(self):
92
99
)
93
100
self .assertEqual (1 , mock_logger .debug .call_count )
94
101
mock_notification_center .send_notifications .assert_called_once_with ('OPTIMIZELY_CONFIG_UPDATE' )
102
+ self .assertEqual (1 , mock_opt_service .call_count )
95
103
96
104
mock_logger .reset_mock ()
97
105
mock_notification_center .reset_mock ()
106
+ mock_opt_service .reset_mock ()
98
107
99
108
# Call set config again and confirm that no new log message denoting config update is there
100
109
project_config_manager ._set_config (test_datafile )
101
110
self .assertEqual (0 , mock_logger .debug .call_count )
102
111
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 )
103
145
104
146
def test_set_config__schema_validation (self ):
105
147
""" Test set_config calls or does not call schema validation based on skip_json_validation value. """
0 commit comments