Skip to content

Commit

Permalink
Stub out requests.Session.get instead of requests.get
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul V Craven committed Feb 24, 2025
1 parent 80bc26c commit f4c8173
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
24 changes: 12 additions & 12 deletions tests/test_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def test_get_config_blocks(self):
self.assertEqual(1, round(end_time - start_time))


@mock.patch('requests.get')
@mock.patch('requests.Session.get')
class PollingConfigManagerTest(base.BaseTest):
def test_init__no_sdk_key_no_datafile__fails(self, _):
""" Test that initialization fails if there is no sdk_key or datafile provided. """
Expand Down Expand Up @@ -379,7 +379,7 @@ def test_fetch_datafile(self, _):
test_response.status_code = 200
test_response.headers = test_headers
test_response._content = test_datafile
with mock.patch('requests.get', return_value=test_response) as mock_request:
with mock.patch('requests.Session.get', return_value=test_response) as mock_request:
project_config_manager = config_manager.PollingConfigManager(sdk_key=sdk_key)
project_config_manager.stop()

Expand All @@ -392,7 +392,7 @@ def test_fetch_datafile(self, _):
self.assertIsInstance(project_config_manager.get_config(), project_config.ProjectConfig)

# Call fetch_datafile again and assert that request to URL is with If-Modified-Since header.
with mock.patch('requests.get', return_value=test_response) as mock_requests:
with mock.patch('requests.Session.get', return_value=test_response) as mock_requests:
project_config_manager._initialize_thread()
project_config_manager.start()
project_config_manager.stop()
Expand Down Expand Up @@ -421,7 +421,7 @@ def raise_for_status(self):
test_response.headers = test_headers
test_response._content = test_datafile

with mock.patch('requests.get', return_value=test_response) as mock_request:
with mock.patch('requests.Session.get', return_value=test_response) as mock_request:
project_config_manager = config_manager.PollingConfigManager(sdk_key=sdk_key, logger=mock_logger)
project_config_manager.stop()

Expand All @@ -434,7 +434,7 @@ def raise_for_status(self):
self.assertIsInstance(project_config_manager.get_config(), project_config.ProjectConfig)

# Call fetch_datafile again, but raise exception this time
with mock.patch('requests.get', return_value=MockExceptionResponse()) as mock_requests:
with mock.patch('requests.Session.get', return_value=MockExceptionResponse()) as mock_requests:
project_config_manager._initialize_thread()
project_config_manager.start()
project_config_manager.stop()
Expand Down Expand Up @@ -462,7 +462,7 @@ def test_fetch_datafile__request_exception_raised(self, _):
test_response.status_code = 200
test_response.headers = test_headers
test_response._content = test_datafile
with mock.patch('requests.get', return_value=test_response) as mock_request:
with mock.patch('requests.Session.get', return_value=test_response) as mock_request:
project_config_manager = config_manager.PollingConfigManager(sdk_key=sdk_key, logger=mock_logger)
project_config_manager.stop()

Expand All @@ -476,7 +476,7 @@ def test_fetch_datafile__request_exception_raised(self, _):

# Call fetch_datafile again, but raise exception this time
with mock.patch(
'requests.get',
'requests.Session.get',
side_effect=requests.exceptions.RequestException('Error Error !!'),
) as mock_requests:
project_config_manager._initialize_thread()
Expand Down Expand Up @@ -506,7 +506,7 @@ def test_fetch_datafile__exception_polling_thread_failed(self, _):
test_response.headers = test_headers
test_response._content = test_datafile

with mock.patch('requests.get', return_value=test_response):
with mock.patch('requests.Session.get', return_value=test_response):
project_config_manager = config_manager.PollingConfigManager(sdk_key=sdk_key,
logger=mock_logger,
update_interval=12345678912345)
Expand All @@ -529,7 +529,7 @@ def test_is_running(self, _):
project_config_manager.stop()


@mock.patch('requests.get')
@mock.patch('requests.Session.get')
class AuthDatafilePollingConfigManagerTest(base.BaseTest):
def test_init__datafile_access_token_none__fails(self, _):
""" Test that initialization fails if datafile_access_token is None. """
Expand Down Expand Up @@ -569,7 +569,7 @@ def test_fetch_datafile(self, _):
test_response._content = test_datafile

# Call fetch_datafile and assert that request was sent with correct authorization header
with mock.patch('requests.get',
with mock.patch('requests.Session.get',
return_value=test_response) as mock_request:
project_config_manager.fetch_datafile()

Expand All @@ -596,7 +596,7 @@ def test_fetch_datafile__request_exception_raised(self, _):
test_response._content = test_datafile

# Call fetch_datafile and assert that request was sent with correct authorization header
with mock.patch('requests.get', return_value=test_response) as mock_request:
with mock.patch('requests.Session.get', return_value=test_response) as mock_request:
project_config_manager = config_manager.AuthDatafilePollingConfigManager(
datafile_access_token=datafile_access_token,
sdk_key=sdk_key,
Expand All @@ -614,7 +614,7 @@ def test_fetch_datafile__request_exception_raised(self, _):

# Call fetch_datafile again, but raise exception this time
with mock.patch(
'requests.get',
'requests.Session.get',
side_effect=requests.exceptions.RequestException('Error Error !!'),
) as mock_requests:
project_config_manager._initialize_thread()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_event_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_dispatch_event__get_request(self):
params = {'a': '111001', 'n': 'test_event', 'g': '111028', 'u': 'oeutest_user'}
event = event_builder.Event(url, params)

with mock.patch('requests.get') as mock_request_get:
with mock.patch('requests.Session.get') as mock_request_get:
event_dispatcher.EventDispatcher.dispatch_event(event)

mock_request_get.assert_called_once_with(url, params=params, timeout=EventDispatchConfig.REQUEST_TIMEOUT)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_notification_center_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_remove_notification_center(self):
test_response = self.fake_server_response(status_code=200, content=test_datafile)
notification_center = _NotificationCenterRegistry.get_notification_center(sdk_key, logger)

with mock.patch('requests.get', return_value=test_response), \
with mock.patch('requests.Session.get', return_value=test_response), \
mock.patch.object(notification_center, 'send_notifications') as mock_send:

client = Optimizely(sdk_key=sdk_key, logger=logger)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -4696,7 +4696,7 @@ def delay(*args, **kwargs):
time.sleep(.5)
return mock.DEFAULT

with mock.patch('requests.get', return_value=test_response, side_effect=delay):
with mock.patch('requests.Session.get', return_value=test_response, side_effect=delay):
# initialize config_manager with delay, so it will receive the datafile after client initialization
custom_config_manager = config_manager.PollingConfigManager(sdk_key='segments-test', logger=logger)
client = optimizely.Optimizely(config_manager=custom_config_manager)
Expand Down Expand Up @@ -5428,7 +5428,7 @@ def test_send_odp_event__send_event_with_static_config_manager(self):
def test_send_odp_event__send_event_with_polling_config_manager(self):
mock_logger = mock.Mock()
with mock.patch(
'requests.get',
'requests.Session.get',
return_value=self.fake_server_response(
status_code=200,
content=json.dumps(self.config_dict_with_audience_segments)
Expand Down Expand Up @@ -5467,7 +5467,7 @@ def test_send_odp_event__log_debug_if_datafile_not_ready(self):
def test_send_odp_event__log_error_if_odp_not_enabled_with_polling_config_manager(self):
mock_logger = mock.Mock()
with mock.patch(
'requests.get',
'requests.Session.get',
return_value=self.fake_server_response(
status_code=200,
content=json.dumps(self.config_dict_with_audience_segments)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_optimizely_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from . import base


@mock.patch('requests.get')
@mock.patch('requests.Session.get')
class OptimizelyFactoryTest(base.BaseTest):
def delay(*args, **kwargs):
time.sleep(.5)
Expand Down Expand Up @@ -171,7 +171,7 @@ def test_set_batch_size_and_set_flush_interval___should_set_values_valid_or_inva
self.assertEqual(optimizely_instance.event_processor.batch_size, 10)

def test_update_odp_config_correctly(self, _):
with mock.patch('requests.get') as mock_request_post:
with mock.patch('requests.Session.get') as mock_request_post:
mock_request_post.return_value = self.fake_server_response(
status_code=200,
content=json.dumps(self.config_dict_with_audience_segments)
Expand All @@ -194,7 +194,7 @@ def test_update_odp_config_correctly_with_custom_config_manager_and_delay(self,
test_datafile = json.dumps(self.config_dict_with_audience_segments)
test_response = self.fake_server_response(status_code=200, content=test_datafile)

with mock.patch('requests.get', return_value=test_response, side_effect=self.delay):
with mock.patch('requests.Session.get', return_value=test_response, side_effect=self.delay):
# initialize config_manager with delay, so it will receive the datafile after client initialization
config_manager = PollingConfigManager(sdk_key='test', logger=logger)
client = OptimizelyFactory.default_instance_with_config_manager(config_manager=config_manager)
Expand All @@ -221,7 +221,7 @@ def test_update_odp_config_correctly_with_delay(self, _):
test_datafile = json.dumps(self.config_dict_with_audience_segments)
test_response = self.fake_server_response(status_code=200, content=test_datafile)

with mock.patch('requests.get', return_value=test_response, side_effect=self.delay):
with mock.patch('requests.Session.get', return_value=test_response, side_effect=self.delay):
# initialize config_manager with delay, so it will receive the datafile after client initialization
client = OptimizelyFactory.default_instance(sdk_key='test')
odp_manager = client.odp_manager
Expand All @@ -247,7 +247,7 @@ def test_odp_updated_with_custom_instance(self, _):
test_datafile = json.dumps(self.config_dict_with_audience_segments)
test_response = self.fake_server_response(status_code=200, content=test_datafile)

with mock.patch('requests.get', return_value=test_response, side_effect=self.delay):
with mock.patch('requests.Session.get', return_value=test_response, side_effect=self.delay):
# initialize config_manager with delay, so it will receive the datafile after client initialization
client = OptimizelyFactory.custom_instance(sdk_key='test')
odp_manager = client.odp_manager
Expand Down

0 comments on commit f4c8173

Please sign in to comment.