Skip to content

Commit 1dfc7ad

Browse files
committed
test(api): add unit tests for various control APIs
Added comprehensive unit tests for the AgentRuntimeControlAPI, CredentialControlAPI, KnowledgeBaseControlAPI, MemoryCollectionControlAPI, ModelControlAPI, SandboxControlAPI, ToolControlAPI, and ToolsetControlAPI. These tests ensure that the API methods correctly match the underlying SDK client method signatures, improving code reliability and maintainability. --- 新增各控制 API 的单元测试 为 AgentRuntimeControlAPI、CredentialControlAPI、KnowledgeBaseControlAPI、MemoryCollectionControlAPI、ModelControlAPI、SandboxControlAPI、ToolControlAPI 和 ToolsetControlAPI 添加了全面的单元测试。这些测试确保 API 方法与底层 SDK 客户端方法签名正确匹配,提高了代码的可靠性和可维护性。 Change-Id: I1d1f89ae1d9bc4c67e5d31bded1864b9790daa25 Signed-off-by: OhYee <oyohyee@oyohyee.com>
1 parent 03bf81d commit 1dfc7ad

13 files changed

Lines changed: 1030 additions & 10 deletions

File tree

agentrun/agent_runtime/api/control.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
CreateAgentRuntimeEndpointRequest,
2222
CreateAgentRuntimeInput,
2323
CreateAgentRuntimeRequest,
24-
DeleteAgentRuntimeEndpointRequest,
25-
DeleteAgentRuntimeRequest,
26-
GetAgentRuntimeEndpointRequest,
2724
GetAgentRuntimeRequest,
2825
ListAgentRuntimeEndpointsOutput,
2926
ListAgentRuntimeEndpointsRequest,
@@ -196,7 +193,6 @@ def delete_agent_runtime(
196193
client = self._get_client(config)
197194
response = client.delete_agent_runtime_with_options(
198195
agent_id,
199-
DeleteAgentRuntimeRequest(),
200196
headers=headers or {},
201197
runtime=RuntimeOptions(),
202198
)
@@ -252,7 +248,6 @@ async def delete_agent_runtime_async(
252248
client = self._get_client(config)
253249
response = await client.delete_agent_runtime_with_options_async(
254250
agent_id,
255-
DeleteAgentRuntimeRequest(),
256251
headers=headers or {},
257252
runtime=RuntimeOptions(),
258253
)
@@ -783,7 +778,6 @@ def delete_agent_runtime_endpoint(
783778
response = client.delete_agent_runtime_endpoint_with_options(
784779
agent_id,
785780
endpoint_id,
786-
DeleteAgentRuntimeEndpointRequest(),
787781
headers=headers or {},
788782
runtime=RuntimeOptions(),
789783
)
@@ -844,7 +838,6 @@ async def delete_agent_runtime_endpoint_async(
844838
await client.delete_agent_runtime_endpoint_with_options_async(
845839
agent_id,
846840
endpoint_id,
847-
DeleteAgentRuntimeEndpointRequest(),
848841
headers=headers or {},
849842
runtime=RuntimeOptions(),
850843
)
@@ -1035,7 +1028,6 @@ def get_agent_runtime_endpoint(
10351028
response = client.get_agent_runtime_endpoint_with_options(
10361029
agent_id,
10371030
endpoint_id,
1038-
GetAgentRuntimeEndpointRequest(),
10391031
headers=headers or {},
10401032
runtime=RuntimeOptions(),
10411033
)
@@ -1096,7 +1088,6 @@ async def get_agent_runtime_endpoint_async(
10961088
await client.get_agent_runtime_endpoint_with_options_async(
10971089
agent_id,
10981090
endpoint_id,
1099-
GetAgentRuntimeEndpointRequest(),
11001091
headers=headers or {},
11011092
runtime=RuntimeOptions(),
11021093
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies = [
1313
"litellm>=1.79.3",
1414
"alibabacloud-devs20230714>=2.4.1",
1515
"pydash>=8.0.5",
16-
"alibabacloud-agentrun20250910>=5.6.1",
16+
"alibabacloud-agentrun20250910>=5.6.3",
1717
"alibabacloud_tea_openapi>=0.4.2",
1818
"alibabacloud_bailian20231229>=2.6.2",
1919
"agentrun-mem0ai>=0.0.10",
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
"""
2+
测试 AgentRuntimeControlAPI 对底层 SDK client 的调用是否与 SDK 方法签名匹配。
3+
"""
4+
5+
from unittest.mock import create_autospec, MagicMock, patch
6+
7+
from alibabacloud_agentrun20250910.client import Client as AgentRunClient
8+
from alibabacloud_agentrun20250910.models import (
9+
CreateAgentRuntimeEndpointInput,
10+
CreateAgentRuntimeInput,
11+
GetAgentRuntimeRequest,
12+
ListAgentRuntimeEndpointsRequest,
13+
ListAgentRuntimesRequest,
14+
ListAgentRuntimeVersionsRequest,
15+
UpdateAgentRuntimeEndpointInput,
16+
UpdateAgentRuntimeInput,
17+
)
18+
import pytest
19+
20+
from agentrun.agent_runtime.api.control import AgentRuntimeControlAPI
21+
from agentrun.utils.config import Config
22+
23+
24+
@pytest.fixture
25+
def mock_config():
26+
return Config(
27+
access_key_id="test-ak",
28+
access_key_secret="test-sk",
29+
region_id="cn-hangzhou",
30+
control_endpoint="https://agentrun.cn-hangzhou.aliyuncs.com",
31+
)
32+
33+
34+
@pytest.fixture
35+
def mock_response():
36+
response = MagicMock()
37+
response.body.request_id = "test-request-id"
38+
response.body.data = MagicMock()
39+
return response
40+
41+
42+
@pytest.fixture
43+
def api_and_client(mock_config, mock_response):
44+
api = AgentRuntimeControlAPI(config=mock_config)
45+
mock_client = create_autospec(AgentRunClient, instance=True)
46+
47+
for attr in dir(AgentRunClient):
48+
if "with_options" in attr:
49+
getattr(mock_client, attr).return_value = mock_response
50+
51+
with patch.object(api, "_get_client", return_value=mock_client):
52+
yield api, mock_client
53+
54+
55+
class TestAgentRuntimeControlAPISignatures:
56+
57+
def test_create_agent_runtime(self, api_and_client):
58+
api, client = api_and_client
59+
input_data = MagicMock(spec=CreateAgentRuntimeInput)
60+
api.create_agent_runtime(input_data)
61+
client.create_agent_runtime_with_options.assert_called_once()
62+
63+
def test_delete_agent_runtime(self, api_and_client):
64+
api, client = api_and_client
65+
api.delete_agent_runtime("agent-123")
66+
client.delete_agent_runtime_with_options.assert_called_once()
67+
68+
def test_update_agent_runtime(self, api_and_client):
69+
api, client = api_and_client
70+
input_data = MagicMock(spec=UpdateAgentRuntimeInput)
71+
api.update_agent_runtime("agent-123", input_data)
72+
client.update_agent_runtime_with_options.assert_called_once()
73+
74+
def test_get_agent_runtime(self, api_and_client):
75+
api, client = api_and_client
76+
input_data = MagicMock(spec=GetAgentRuntimeRequest)
77+
api.get_agent_runtime("agent-123", input_data)
78+
client.get_agent_runtime_with_options.assert_called_once()
79+
80+
def test_list_agent_runtimes(self, api_and_client):
81+
api, client = api_and_client
82+
input_data = MagicMock(spec=ListAgentRuntimesRequest)
83+
api.list_agent_runtimes(input_data)
84+
client.list_agent_runtimes_with_options.assert_called_once()
85+
86+
def test_create_agent_runtime_endpoint(self, api_and_client):
87+
api, client = api_and_client
88+
input_data = MagicMock(spec=CreateAgentRuntimeEndpointInput)
89+
api.create_agent_runtime_endpoint("agent-123", input_data)
90+
client.create_agent_runtime_endpoint_with_options.assert_called_once()
91+
92+
def test_delete_agent_runtime_endpoint(self, api_and_client):
93+
api, client = api_and_client
94+
api.delete_agent_runtime_endpoint("agent-123", "endpoint-456")
95+
client.delete_agent_runtime_endpoint_with_options.assert_called_once()
96+
97+
def test_update_agent_runtime_endpoint(self, api_and_client):
98+
api, client = api_and_client
99+
input_data = MagicMock(spec=UpdateAgentRuntimeEndpointInput)
100+
api.update_agent_runtime_endpoint(
101+
"agent-123", "endpoint-456", input_data
102+
)
103+
client.update_agent_runtime_endpoint_with_options.assert_called_once()
104+
105+
def test_get_agent_runtime_endpoint(self, api_and_client):
106+
api, client = api_and_client
107+
api.get_agent_runtime_endpoint("agent-123", "endpoint-456")
108+
client.get_agent_runtime_endpoint_with_options.assert_called_once()
109+
110+
def test_list_agent_runtime_endpoints(self, api_and_client):
111+
api, client = api_and_client
112+
input_data = MagicMock(spec=ListAgentRuntimeEndpointsRequest)
113+
api.list_agent_runtime_endpoints("agent-123", input_data)
114+
client.list_agent_runtime_endpoints_with_options.assert_called_once()
115+
116+
def test_list_agent_runtime_versions(self, api_and_client):
117+
api, client = api_and_client
118+
input_data = MagicMock(spec=ListAgentRuntimeVersionsRequest)
119+
api.list_agent_runtime_versions("agent-123", input_data)
120+
client.list_agent_runtime_versions_with_options.assert_called_once()
121+
122+
123+
class TestAgentRuntimeControlAPIAsyncSignatures:
124+
125+
@pytest.mark.asyncio
126+
async def test_create_agent_runtime_async(self, api_and_client):
127+
api, client = api_and_client
128+
input_data = MagicMock(spec=CreateAgentRuntimeInput)
129+
await api.create_agent_runtime_async(input_data)
130+
client.create_agent_runtime_with_options_async.assert_called_once()
131+
132+
@pytest.mark.asyncio
133+
async def test_delete_agent_runtime_async(self, api_and_client):
134+
api, client = api_and_client
135+
await api.delete_agent_runtime_async("agent-123")
136+
client.delete_agent_runtime_with_options_async.assert_called_once()
137+
138+
@pytest.mark.asyncio
139+
async def test_update_agent_runtime_async(self, api_and_client):
140+
api, client = api_and_client
141+
input_data = MagicMock(spec=UpdateAgentRuntimeInput)
142+
await api.update_agent_runtime_async("agent-123", input_data)
143+
client.update_agent_runtime_with_options_async.assert_called_once()
144+
145+
@pytest.mark.asyncio
146+
async def test_get_agent_runtime_async(self, api_and_client):
147+
api, client = api_and_client
148+
input_data = MagicMock(spec=GetAgentRuntimeRequest)
149+
await api.get_agent_runtime_async("agent-123", input_data)
150+
client.get_agent_runtime_with_options_async.assert_called_once()
151+
152+
@pytest.mark.asyncio
153+
async def test_list_agent_runtimes_async(self, api_and_client):
154+
api, client = api_and_client
155+
input_data = MagicMock(spec=ListAgentRuntimesRequest)
156+
await api.list_agent_runtimes_async(input_data)
157+
client.list_agent_runtimes_with_options_async.assert_called_once()
158+
159+
@pytest.mark.asyncio
160+
async def test_create_agent_runtime_endpoint_async(self, api_and_client):
161+
api, client = api_and_client
162+
input_data = MagicMock(spec=CreateAgentRuntimeEndpointInput)
163+
await api.create_agent_runtime_endpoint_async("agent-123", input_data)
164+
client.create_agent_runtime_endpoint_with_options_async.assert_called_once()
165+
166+
@pytest.mark.asyncio
167+
async def test_delete_agent_runtime_endpoint_async(self, api_and_client):
168+
api, client = api_and_client
169+
await api.delete_agent_runtime_endpoint_async(
170+
"agent-123", "endpoint-456"
171+
)
172+
client.delete_agent_runtime_endpoint_with_options_async.assert_called_once()
173+
174+
@pytest.mark.asyncio
175+
async def test_update_agent_runtime_endpoint_async(self, api_and_client):
176+
api, client = api_and_client
177+
input_data = MagicMock(spec=UpdateAgentRuntimeEndpointInput)
178+
await api.update_agent_runtime_endpoint_async(
179+
"agent-123", "endpoint-456", input_data
180+
)
181+
client.update_agent_runtime_endpoint_with_options_async.assert_called_once()
182+
183+
@pytest.mark.asyncio
184+
async def test_get_agent_runtime_endpoint_async(self, api_and_client):
185+
api, client = api_and_client
186+
await api.get_agent_runtime_endpoint_async("agent-123", "endpoint-456")
187+
client.get_agent_runtime_endpoint_with_options_async.assert_called_once()
188+
189+
@pytest.mark.asyncio
190+
async def test_list_agent_runtime_endpoints_async(self, api_and_client):
191+
api, client = api_and_client
192+
input_data = MagicMock(spec=ListAgentRuntimeEndpointsRequest)
193+
await api.list_agent_runtime_endpoints_async("agent-123", input_data)
194+
client.list_agent_runtime_endpoints_with_options_async.assert_called_once()
195+
196+
@pytest.mark.asyncio
197+
async def test_list_agent_runtime_versions_async(self, api_and_client):
198+
api, client = api_and_client
199+
input_data = MagicMock(spec=ListAgentRuntimeVersionsRequest)
200+
await api.list_agent_runtime_versions_async("agent-123", input_data)
201+
client.list_agent_runtime_versions_with_options_async.assert_called_once()

tests/unittests/credential/api/__init__.py

Whitespace-only changes.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""
2+
测试 CredentialControlAPI 对底层 SDK client 的调用是否与 SDK 方法签名匹配。
3+
"""
4+
5+
from unittest.mock import create_autospec, MagicMock, patch
6+
7+
from alibabacloud_agentrun20250910.client import Client as AgentRunClient
8+
from alibabacloud_agentrun20250910.models import (
9+
CreateCredentialInput,
10+
ListCredentialsRequest,
11+
UpdateCredentialInput,
12+
)
13+
import pytest
14+
15+
from agentrun.credential.api.control import CredentialControlAPI
16+
from agentrun.utils.config import Config
17+
18+
19+
@pytest.fixture
20+
def mock_config():
21+
return Config(
22+
access_key_id="test-ak",
23+
access_key_secret="test-sk",
24+
region_id="cn-hangzhou",
25+
control_endpoint="https://agentrun.cn-hangzhou.aliyuncs.com",
26+
)
27+
28+
29+
@pytest.fixture
30+
def mock_response():
31+
response = MagicMock()
32+
response.body.request_id = "test-request-id"
33+
response.body.data = MagicMock()
34+
return response
35+
36+
37+
@pytest.fixture
38+
def api_and_client(mock_config, mock_response):
39+
api = CredentialControlAPI(config=mock_config)
40+
mock_client = create_autospec(AgentRunClient, instance=True)
41+
42+
for attr in dir(AgentRunClient):
43+
if "with_options" in attr:
44+
getattr(mock_client, attr).return_value = mock_response
45+
46+
with patch.object(api, "_get_client", return_value=mock_client):
47+
yield api, mock_client
48+
49+
50+
class TestCredentialControlAPISignatures:
51+
52+
def test_create_credential(self, api_and_client):
53+
api, client = api_and_client
54+
input_data = MagicMock(spec=CreateCredentialInput)
55+
api.create_credential(input_data)
56+
client.create_credential_with_options.assert_called_once()
57+
58+
def test_delete_credential(self, api_and_client):
59+
api, client = api_and_client
60+
api.delete_credential("test-cred")
61+
client.delete_credential_with_options.assert_called_once()
62+
63+
def test_update_credential(self, api_and_client):
64+
api, client = api_and_client
65+
input_data = MagicMock(spec=UpdateCredentialInput)
66+
api.update_credential("test-cred", input_data)
67+
client.update_credential_with_options.assert_called_once()
68+
69+
def test_get_credential(self, api_and_client):
70+
api, client = api_and_client
71+
api.get_credential("test-cred")
72+
client.get_credential_with_options.assert_called_once()
73+
74+
def test_list_credentials(self, api_and_client):
75+
api, client = api_and_client
76+
input_data = MagicMock(spec=ListCredentialsRequest)
77+
api.list_credentials(input_data)
78+
client.list_credentials_with_options.assert_called_once()
79+
80+
81+
class TestCredentialControlAPIAsyncSignatures:
82+
83+
@pytest.mark.asyncio
84+
async def test_create_credential_async(self, api_and_client):
85+
api, client = api_and_client
86+
input_data = MagicMock(spec=CreateCredentialInput)
87+
await api.create_credential_async(input_data)
88+
client.create_credential_with_options_async.assert_called_once()
89+
90+
@pytest.mark.asyncio
91+
async def test_delete_credential_async(self, api_and_client):
92+
api, client = api_and_client
93+
await api.delete_credential_async("test-cred")
94+
client.delete_credential_with_options_async.assert_called_once()
95+
96+
@pytest.mark.asyncio
97+
async def test_update_credential_async(self, api_and_client):
98+
api, client = api_and_client
99+
input_data = MagicMock(spec=UpdateCredentialInput)
100+
await api.update_credential_async("test-cred", input_data)
101+
client.update_credential_with_options_async.assert_called_once()
102+
103+
@pytest.mark.asyncio
104+
async def test_get_credential_async(self, api_and_client):
105+
api, client = api_and_client
106+
await api.get_credential_async("test-cred")
107+
client.get_credential_with_options_async.assert_called_once()
108+
109+
@pytest.mark.asyncio
110+
async def test_list_credentials_async(self, api_and_client):
111+
api, client = api_and_client
112+
input_data = MagicMock(spec=ListCredentialsRequest)
113+
await api.list_credentials_async(input_data)
114+
client.list_credentials_with_options_async.assert_called_once()

0 commit comments

Comments
 (0)