Skip to content

Commit 0a1f78c

Browse files
authored
Tests without network connection (#492)
* tests: github xfail on broken connection and rate limit * tests: huggingface xfail on broken connection * tests: http xfail on broken connection
1 parent 5a080b0 commit 0a1f78c

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

upath/tests/implementations/test_github.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import os
23
import platform
34
import sys
@@ -17,6 +18,56 @@
1718
)
1819

1920

21+
def has_internet_connection():
22+
import requests
23+
24+
try:
25+
requests.get("http://example.com")
26+
except requests.exceptions.ConnectionError:
27+
return False
28+
else:
29+
return True
30+
31+
32+
def xfail_on_github_rate_limit(func):
33+
"""
34+
Method decorator to mark test as xfail when GitHub rate limit is exceeded.
35+
"""
36+
37+
@functools.wraps(func)
38+
def wrapped_method(self, *args, **kwargs):
39+
import requests
40+
41+
try:
42+
return func(self, *args, **kwargs)
43+
except AssertionError as e:
44+
if "nodename nor servname provided, or not known" in str(e):
45+
pytest.xfail(reason="No internet connection")
46+
raise
47+
except requests.exceptions.ConnectionError:
48+
pytest.xfail(reason="No internet connection")
49+
except Exception as e:
50+
if "rate limit exceeded" in str(e):
51+
pytest.xfail("GitHub API rate limit exceeded")
52+
else:
53+
raise
54+
55+
return wrapped_method
56+
57+
58+
def wrap_github_rate_limit_check(cls):
59+
"""
60+
Class decorator to wrap all test methods with the
61+
xfail_on_github_rate_limit decorator.
62+
"""
63+
for attr_name in dir(cls):
64+
if attr_name.startswith("test_"):
65+
orig_method = getattr(cls, attr_name)
66+
setattr(cls, attr_name, xfail_on_github_rate_limit(orig_method))
67+
return cls
68+
69+
70+
@wrap_github_rate_limit_check
2071
class TestUPathGitHubPath(BaseTests):
2172
"""
2273
Unit-tests for the GitHubPath implementation of UPath.

upath/tests/implementations/test_hf.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import functools
2+
13
import pytest
24
from fsspec import get_filesystem_class
35

@@ -12,13 +14,44 @@
1214
pytestmark = pytest.mark.skip
1315

1416

17+
def xfail_on_hf_service_unavailable(func):
18+
"""
19+
Method decorator to mark test as xfail when HuggingFace service is unavailable.
20+
"""
21+
from httpx import HTTPStatusError
22+
23+
@functools.wraps(func)
24+
def wrapped_method(self, *args, **kwargs):
25+
try:
26+
return func(self, *args, **kwargs)
27+
except HTTPStatusError as err:
28+
if err.response.status_code == 503:
29+
pytest.xfail("HuggingFace API not reachable")
30+
raise
31+
32+
return wrapped_method
33+
34+
1535
def test_hfpath():
1636
path = UPath("hf://HuggingFaceTB/SmolLM2-135M")
1737
assert isinstance(path, HfPath)
18-
assert path.exists()
19-
20-
21-
class TestUPathHttp(BaseTests):
38+
try:
39+
assert path.exists()
40+
except AssertionError:
41+
from httpx import ConnectError
42+
from huggingface_hub import HfApi
43+
44+
try:
45+
HfApi().repo_info("HuggingFaceTB/SmolLM2-135M")
46+
except ConnectError:
47+
pytest.xfail("No internet connection")
48+
except Exception as err:
49+
if "Service Unavailable" in str(err):
50+
pytest.xfail("HuggingFace API not reachable")
51+
raise
52+
53+
54+
class TestUPathHf(BaseTests):
2255
@pytest.fixture(autouse=True, scope="function")
2356
def path(self, hf_fixture_with_readonly_mocked_hf_api):
2457
self.path = UPath(hf_fixture_with_readonly_mocked_hf_api)

upath/tests/implementations/test_http.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@
1717
pytestmark = pytest.mark.skip
1818

1919

20-
def test_httppath():
20+
@pytest.fixture
21+
def internet_connection():
22+
import requests
23+
24+
try:
25+
requests.get("http://example.com")
26+
except requests.exceptions.ConnectionError:
27+
pytest.xfail(reason="No internet connection")
28+
else:
29+
yield
30+
31+
32+
def test_httppath(internet_connection):
2133
path = UPath("http://example.com")
2234
assert isinstance(path, HTTPPath)
2335
assert path.exists()

0 commit comments

Comments
 (0)