File: test_hf.py

package info (click to toggle)
universal-pathlib 0.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,656 kB
  • sloc: python: 20,552; makefile: 5
file content (57 lines) | stat: -rw-r--r-- 1,656 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pytest
from fsspec import get_filesystem_class

from upath import UnsupportedOperation
from upath import UPath
from upath.implementations.cloud import HfPath

from ..cases import JoinablePathTests
from ..cases import NonWritablePathTests
from ..cases import ReadablePathTests
from ..utils import OverrideMeta
from ..utils import overrides_base

try:
    get_filesystem_class("hf")
except ImportError:
    pytestmark = pytest.mark.skip


def test_hfpath():
    path = UPath("hf://HuggingFaceTB/SmolLM2-135M")
    assert isinstance(path, HfPath)
    try:
        assert path.exists()
    except AssertionError:
        from httpx import ConnectError
        from huggingface_hub import HfApi

        try:
            HfApi().repo_info("HuggingFaceTB/SmolLM2-135M")
        except ConnectError:
            pytest.xfail("No internet connection")
        except Exception as err:
            if "Service Unavailable" in str(err):
                pytest.xfail("HuggingFace API not reachable")
            raise


class TestUPathHf(
    JoinablePathTests,
    ReadablePathTests,
    NonWritablePathTests,
    metaclass=OverrideMeta,
):
    @pytest.fixture(autouse=True, scope="function")
    def path(self, hf_fixture_with_readonly_mocked_hf_api):
        self.path = UPath(hf_fixture_with_readonly_mocked_hf_api)

    @overrides_base
    def test_is_correct_class(self):
        assert isinstance(self.path, HfPath)

    @overrides_base
    def test_iterdir_parent_iteration(self):
        # HfPath does not support listing all available Repositories
        with pytest.raises(UnsupportedOperation):
            super().test_iterdir_parent_iteration()