Skip to content

Commit ad3d25a

Browse files
authored
fix: exp field patch (#1514)
* fix: property compatible * fix: monkey patch test
1 parent 5b6a756 commit ad3d25a

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

swanlab/core_python/client/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ def cuid(self):
8484
return self.__data["cuid"]
8585

8686
@property
87-
def slug(self):
88-
return self.__data["slug"]
87+
def slug(self) -> Optional[str]:
88+
return self.__data.get("slug")
8989

9090
@property
9191
def name(self):

test/unit/core_python/test_client.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import requests_mock
1111

1212
from swanlab.core_python import create_client, Client, reset_client
13+
from swanlab.core_python.client.utils import ExperimentInfo
1314
from swanlab.core_python.auth import login_by_key
1415
from swanlab.package import get_host_api
1516
from tutils import is_skip_cloud_test, API_KEY
@@ -70,6 +71,39 @@ def test_decode_response():
7071
assert data == "test"
7172

7273

74+
@pytest.mark.parametrize(
75+
"exp_data, expected_url",
76+
[
77+
# slug 不存在,回退到 exp_id
78+
(
79+
{"flagId": "flag-1", "cuid": "exp-123", "name": "mock-exp"},
80+
"https://mock.swanlab.cn/@mock-user/mock-project/runs/exp-123",
81+
),
82+
# slug 存在
83+
(
84+
{"flagId": "flag-1", "cuid": "exp-123", "name": "mock-exp", "slug": "my-slug"},
85+
"https://mock.swanlab.cn/@mock-user/mock-project/runs/my-slug",
86+
),
87+
],
88+
)
89+
def test_web_exp_url_uses_slug_or_falls_back_to_exp_id(monkeypatch, exp_data, expected_url):
90+
client = create_client(mock_login_info(username="mock-user"))
91+
exp = ExperimentInfo(exp_data)
92+
93+
monkeypatch.setattr(
94+
Client,
95+
"web_proj_url",
96+
property(lambda _self: "https://mock.swanlab.cn/@mock-user/mock-project"),
97+
)
98+
monkeypatch.setattr(Client, "exp", property(lambda _self: exp))
99+
monkeypatch.setattr(Client, "exp_id", property(lambda _self: exp.cuid))
100+
101+
try:
102+
assert client.web_exp_url == expected_url
103+
finally:
104+
reset_client()
105+
106+
73107
@pytest.mark.skipif(is_skip_cloud_test, reason="skip cloud test")
74108
class TestExpSuite:
75109
"""

0 commit comments

Comments
 (0)