|
10 | 10 | import requests_mock |
11 | 11 |
|
12 | 12 | from swanlab.core_python import create_client, Client, reset_client |
| 13 | +from swanlab.core_python.client.utils import ExperimentInfo |
13 | 14 | from swanlab.core_python.auth import login_by_key |
14 | 15 | from swanlab.package import get_host_api |
15 | 16 | from tutils import is_skip_cloud_test, API_KEY |
@@ -70,6 +71,39 @@ def test_decode_response(): |
70 | 71 | assert data == "test" |
71 | 72 |
|
72 | 73 |
|
| 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 | + |
73 | 107 | @pytest.mark.skipif(is_skip_cloud_test, reason="skip cloud test") |
74 | 108 | class TestExpSuite: |
75 | 109 | """ |
|
0 commit comments