From c6015a070c66a5bbd84603d415ccc57cb668b44b Mon Sep 17 00:00:00 2001 From: Lucain Date: Tue, 25 Apr 2023 16:25:29 +0200 Subject: [PATCH] Prepare tests for hfh 0.14 (#5788) * test hfh 0.14.0rc1 * fix style * fix tests for hfh>=0.14 * fix test token * package version * Remove CI run on push to test-hfh* branch --------- Co-authored-by: Quentin Lhoest Co-authored-by: testbot Co-authored-by: Albert Villanova del Moral <8515462+albertvillanova@users.noreply.github.com> --- tests/test_load.py | 12 ++++++------ tests/utils.py | 17 ++++++++--------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/tests/test_load.py b/tests/test_load.py index 7475e97853c..166e1763a62 100644 --- a/tests/test_load.py +++ b/tests/test_load.py @@ -852,19 +852,19 @@ def test_loading_from_the_datasets_hub(): @pytest.mark.integration def test_loading_from_the_datasets_hub_with_use_auth_token(): - from requests import get + true_request = requests.Session().request - def assert_auth(url, *args, headers, **kwargs): + def assert_auth(method, url, *args, headers, **kwargs): assert headers["authorization"] == "Bearer foo" - return get(url, *args, headers=headers, **kwargs) + return true_request(method, url, *args, headers=headers, **kwargs) - with patch("requests.get") as mock_head: - mock_head.side_effect = assert_auth + with patch("requests.Session.request") as mock_request: + mock_request.side_effect = assert_auth with tempfile.TemporaryDirectory() as tmp_dir: with offline(): with pytest.raises((ConnectionError, requests.exceptions.ConnectionError)): load_dataset(SAMPLE_NOT_EXISTING_DATASET_IDENTIFIER, cache_dir=tmp_dir, use_auth_token="foo") - mock_head.assert_called() + mock_request.assert_called() @pytest.mark.integration 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