From 0c51b8e1eb5949f027f6a4f9f7288f66396d3919 Mon Sep 17 00:00:00 2001 From: testbot Date: Mon, 24 Apr 2023 12:17:03 +0200 Subject: [PATCH] fix tests for hfh>=0.14 --- tests/utils.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/tests/utils.py b/tests/utils.py index 289cbed9577..b036a82fd97 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -14,6 +14,7 @@ import pyarrow as pa import pytest +import requests from packaging import version from datasets import config @@ -322,9 +323,9 @@ def offline(mode=OfflineSimulationMode.CONNECTION_FAILS, timeout=1e-16): HF_DATASETS_OFFLINE_SET_TO_1: the HF_DATASETS_OFFLINE environment variable is set to 1. This makes the http/ftp calls of the library instantly fail and raise an OfflineModeEmabled error. """ - from requests import request as online_request + online_request = requests.Session().request - def timeout_request(method, url, **kwargs): + def timeout_request(session, method, url, **kwargs): # Change the url to an invalid url so that the connection hangs invalid_url = "https://10.255.255.1" if kwargs.get("timeout") is None: @@ -342,18 +343,16 @@ def timeout_request(method, url, **kwargs): e.args = (max_retry_error,) raise - def offline_socket(*args, **kwargs): - raise OSError("Offline mode is enabled.") + def raise_connection_error(session, prepared_request, **kwargs): + raise requests.ConnectionError("Offline mode is enabled.", request=prepared_request) if mode is OfflineSimulationMode.CONNECTION_FAILS: - # inspired from https://stackoverflow.com/a/18601897 - with patch("socket.socket", offline_socket): + with patch("requests.Session.send", raise_connection_error): yield elif mode is OfflineSimulationMode.CONNECTION_TIMES_OUT: # inspired from https://stackoverflow.com/a/904609 - with patch("requests.request", timeout_request): - with patch("requests.api.request", timeout_request): - yield + with patch("requests.Session.request", timeout_request): + yield elif mode is OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1: with patch("datasets.config.HF_DATASETS_OFFLINE", True): yield