import os
import pytest
pytest.importorskip("weedforge")
from weedforge import WeedClient, FileId
class TestFileId:
def test_parse_valid(self):
fid = FileId.parse("3,0000016300007037")
assert fid.volume_id == 3
assert isinstance(fid.file_key, int)
assert isinstance(fid.cookie, int)
def test_parse_invalid(self):
with pytest.raises(RuntimeError):
FileId.parse("invalid")
def test_render(self):
fid = FileId.parse("3,0000016300007037")
rendered = fid.render()
assert rendered.startswith("3,")
def test_str(self):
fid = FileId.parse("3,0000016300007037")
assert str(fid).startswith("3,")
def test_repr(self):
fid = FileId.parse("3,0000016300007037")
assert "FileId" in repr(fid)
assert "volume_id=" in repr(fid)
class TestWeedClient:
def test_create_client(self):
client = WeedClient(master_urls=["http://localhost:9333"])
assert client is not None
def test_create_client_with_strategy(self):
for strategy in ["round_robin", "failover", "random"]:
client = WeedClient(
master_urls=["http://localhost:9333"],
strategy=strategy,
)
assert client is not None
def test_create_client_invalid_strategy(self):
with pytest.raises(RuntimeError):
WeedClient(
master_urls=["http://localhost:9333"],
strategy="invalid",
)
def test_parse_file_id(self):
fid = WeedClient.parse_file_id("3,0000016300007037")
assert fid.volume_id == 3
@pytest.mark.skipif(
not os.environ.get("SEAWEEDFS_MASTER"),
reason="SEAWEEDFS_MASTER not set",
)
class TestIntegration:
@pytest.fixture
def client(self):
master_url = os.environ.get("SEAWEEDFS_MASTER", "http://localhost:9333")
return WeedClient(master_urls=[master_url])
def test_write_read_delete(self, client):
data = b"Hello, SeaweedFS!"
fid = client.write(data, filename="test.txt")
assert fid is not None
assert fid.volume_id > 0
downloaded = client.read(fid)
assert downloaded == data
downloaded2 = client.read(str(fid))
assert downloaded2 == data
url = client.public_url(fid)
assert url.startswith("http")
client.delete(fid)
def test_upload_bytes_alias(self, client):
data = b"Test data"
fid = client.upload_bytes(data, filename="alias_test.txt")
assert fid is not None
client.delete(fid)
def test_public_url_resized(self, client):
data = b"fake image data"
fid = client.write(data, filename="image.jpg")
url = client.public_url_resized(fid, width=200, height=200)
assert "width=200" in url
assert "height=200" in url
client.delete(fid)
if __name__ == "__main__":
pytest.main([__file__, "-v"])