Skip to content

Commit

Permalink
Add more test cases about fsspec integration
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghua committed Nov 14, 2024
1 parent afc6c2c commit 64053a0
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tosfs/tests/test_fsspec_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
from fsspec.registry import known_implementations
from tos import EnvCredentialsProvider

from tosfs import TosFileSystem
from tosfs.utils import random_str


def test_fssepc_register():
fsspec.register_implementation("tos", "tosfs.TosFileSystem")
Expand All @@ -41,3 +44,54 @@ def test_set_known_implementations():
credentials_provider=EnvCredentialsProvider(),
)
assert len(tosfs.ls("")) > 0


def test_fsspec_open(bucket, temporary_workspace):
known_implementations["tos"] = {"class": "tosfs.core.TosFileSystem"}

file = f"{random_str()}.txt"
content = "Hello TOSFS."
with fsspec.open(
f"tos://{bucket}/{temporary_workspace}/{file}",
endpoint_url=os.environ.get("TOS_ENDPOINT"),
region=os.environ.get("TOS_REGION"),
credentials_provider=EnvCredentialsProvider(),
mode="w",
) as f:
f.write(content)

with fsspec.open(
f"tos://{bucket}/{temporary_workspace}/{file}",
endpoint_url=os.environ.get("TOS_ENDPOINT"),
region=os.environ.get("TOS_REGION"),
credentials_provider=EnvCredentialsProvider(),
mode="r",
) as f:
assert f.read() == content


class MyTosFileSystem(TosFileSystem):
"""A sub class of TosFileSystem."""

def __init__(self):
"""Init MyTosFileSystem."""
super().__init__(
endpoint_url=os.environ.get("TOS_ENDPOINT"),
region=os.environ.get("TOS_REGION"),
credentials_provider=EnvCredentialsProvider(),
)


def test_customized_class(bucket, temporary_workspace):
known_implementations["tos"] = {
"class": "tosfs.tests.test_fsspec_integration.MyTosFileSystem"
}

file = f"{random_str()}.txt"
content = "Hello TOSFS."

with fsspec.open(f"tos://{bucket}/{temporary_workspace}/{file}", mode="w") as f:
f.write(content)

with fsspec.open(f"tos://{bucket}/{temporary_workspace}/{file}", mode="r") as f:
assert f.read() == content

0 comments on commit 64053a0

Please sign in to comment.