def test_connection_error_response(mocker): client = mocker.MagicMock() mocker.patch('ocds_client.utils.sleep') client.get_resource_items.side_effect = ConnectionError('ConnectionError') with pytest.raises(ConnectionError) as e: get_response(client, {}) assert e.value.args == ('ConnectionError',)
def test_exception_response(mocker): client = mocker.MagicMock() mocker.patch('ocds_client.utils.sleep') client.get_resource_items.side_effect = InvalidHeader('Missing X-Token') with pytest.raises(InvalidHeader) as e: get_response(client, {}) assert e.value.args == ('Missing X-Token',)
def _run(self): LOGGER.info(f'{self.name}: Start job...') response = get_response(self.client, self.params) records_len = len(response[f'{self.resource}s']) LOGGER.debug( f'Retriever response length {records_len} items', extra={f'{self.name.upper()}_RESPONSE_LENGTH': records_len}) while not self.exit_successful: self.sync_client.heartbeat = time() while (response[f'{self.resource}s']): self.sync_client.heartbeat = time() self.handle_response_data(response[f'{self.resource}s']) params = self.get_params(response) self.params.update(params) self.log_state() response = get_response(self.client, self.params) records_len = len(response[f'{self.resource}s']) LOGGER.debug( f'{self.name} response length {records_len} items', extra={ f'{self.name.upper()}_RESPONSE_LENGTH': records_len }) if records_len != 0: timeout = self.retrievers_params.get( 'up_requests_sleep', 5.0) LOGGER.info( f'{self.name}: pause between requests {timeout} sec.') sleep(timeout) if self.name == 'BackwardWorker': self.exit_successful = True up_wait_sleep = self.retrievers_params.get('up_wait_sleep', 30.0) LOGGER.info( f'{self.name}: pause after empty response {up_wait_sleep} sec.', extra={f'{self.name}_WAIT_SLEEP': up_wait_sleep}) sleep(up_wait_sleep) response = get_response(self.client, self.params) records_len = len(response[f'{self.resource}s']) LOGGER.debug( f'{self.name} response length {records_len} items', extra={f'{self.name.upper()}_RESPONSE_LENGTH': records_len}) if self.adaptive: if len(response[f'{self.resource}s']) != 0: if self.retrievers_params[ 'up_wait_sleep'] > self.retrievers_params[ 'up_wait_sleep_min']: self.retrievers_params['up_wait_sleep'] -= 1 else: if self.retrievers_params['up_wait_sleep'] < 30: self.retrievers_params['up_wait_sleep'] += 1 return 1
def test_request_failed_response(mocker): client = mocker.MagicMock() mocker.patch('ocds_client.utils.sleep') response = Response('{"error": "Too many requests."}', 429) client.get_resource_items.side_effect = RequestFailed(response) with pytest.raises(RequestFailed) as e: get_response(client, {}) assert e.value.message == '{"error": "Too many requests."}' assert e.value.status_code == 429 response = Response('{"error": "Internal Server Error."}', 500) client.get_resource_items.side_effect = RequestFailed(response) with pytest.raises(RequestFailed) as e: get_response(client, {}) assert e.value.message == '{"error": "Internal Server Error."}' assert e.value.status_code == 500
def test_success_response(mocker): client = mocker.MagicMock() data = [{'ocid': 'ocid_id', 'compiledRelease': {'ocid': 'ocid'}, 'releases': ['release_link']}] client.get_resource_items.return_value = data response = get_response(client, {}) assert response == data