File size: 2,179 Bytes
b67c03f | 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 | from types import SimpleNamespace
import pytest
from src import bucket
from src.config import user_bucket_source
def test_user_bucket_source_is_per_user():
assert user_bucket_source(username="alice", bucket_name="space-factory-runs") == "alice/space-factory-runs"
def test_check_user_bucket_ready(monkeypatch):
def fake_bucket_info(bucket_id, token=None):
assert bucket_id == "alice/space-factory-runs"
assert token == "tok"
return SimpleNamespace(name="space-factory-runs", private=True)
monkeypatch.setattr(bucket, "bucket_info", fake_bucket_info)
status = bucket.check_user_bucket(username="alice", bucket_name="space-factory-runs", token="tok")
assert status["ok"] is True
assert status["exists"] is True
assert status["bucket_source"] == "alice/space-factory-runs"
def test_create_user_bucket_private(monkeypatch):
calls = {}
def fake_create_bucket(bucket_id, private=None, exist_ok=False, token=None):
calls.update({"bucket_id": bucket_id, "private": private, "exist_ok": exist_ok, "token": token})
return "hf://buckets/alice/space-factory-runs"
def fake_check_user_bucket(username, bucket_name=None, token=None):
return {"ok": True, "exists": True, "bucket_source": f"{username}/{bucket_name}", "bucket_uri": f"hf://buckets/{username}/{bucket_name}"}
monkeypatch.setattr(bucket, "create_bucket", fake_create_bucket)
monkeypatch.setattr(bucket, "check_user_bucket", fake_check_user_bucket)
status = bucket.create_user_bucket(username="alice", bucket_name="space-factory-runs", token="tok")
assert status["ok"] is True
assert status["created_or_existing"] is True
assert calls == {"bucket_id": "alice/space-factory-runs", "private": True, "exist_ok": True, "token": "tok"}
def test_assert_user_bucket_ready_raises(monkeypatch):
monkeypatch.setattr(bucket, "check_user_bucket", lambda **kwargs: {"ok": False, "bucket_source": "alice/missing", "error": "not found"})
with pytest.raises(ValueError, match="Run bucket `alice/missing` is not ready"):
bucket.assert_user_bucket_ready(username="alice", bucket_name="missing", token="tok")
|