Skip to content

Commit

Permalink
Prepare tests for hfh 0.14 (#5788)
Browse files Browse the repository at this point in the history
* 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 <lhoest.q@gmail.com>
Co-authored-by: testbot <lucainp@hf.co>
Co-authored-by: Albert Villanova del Moral <8515462+albertvillanova@users.noreply.github.com>
  • Loading branch information
4 people authored Apr 25, 2023
1 parent 649d5a3 commit c6015a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
12 changes: 6 additions & 6 deletions tests/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import pyarrow as pa
import pytest
import requests
from packaging import version

from datasets import config
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit c6015a0

Please sign in to comment.