-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tool_calling.py
More file actions
143 lines (115 loc) · 4.58 KB
/
test_tool_calling.py
File metadata and controls
143 lines (115 loc) · 4.58 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""Tests for tool calling functionality"""
import json
import pytest
import responses
from stackone_ai import StackOneTool
from stackone_ai.models import ExecuteConfig, ToolParameters
@pytest.fixture
def mock_tool():
"""Create a mock tool for testing"""
execute_config = ExecuteConfig(
name="test_tool",
method="POST",
url="https://api.example.com/test",
headers={"Content-Type": "application/json"},
)
parameters = ToolParameters(
type="object",
properties={
"name": {"type": "string", "description": "Name parameter"},
"value": {"type": "number", "description": "Value parameter"},
},
)
tool = StackOneTool(
description="Test tool",
parameters=parameters,
_execute_config=execute_config,
_api_key="test_api_key",
)
return tool
class TestToolCalling:
"""Test tool calling functionality"""
@responses.activate
def test_call_with_kwargs(self, mock_tool):
"""Test calling a tool with keyword arguments"""
# Mock the API response
responses.add(
responses.POST,
"https://api.example.com/test",
json={"success": True, "result": "test_result"},
status=200,
)
# Call the tool with kwargs
result = mock_tool.call(name="test", value=42)
# Verify the result
assert result == {"success": True, "result": "test_result"}
# Verify the request was made correctly
assert len(responses.calls) == 1
request = responses.calls[0].request
assert json.loads(request.body) == {"name": "test", "value": 42}
@responses.activate
def test_call_with_dict_arg(self, mock_tool):
"""Test calling a tool with a dictionary argument"""
# Mock the API response
responses.add(
responses.POST,
"https://api.example.com/test",
json={"success": True, "result": "test_result"},
status=200,
)
# Call the tool with a dict
result = mock_tool.call({"name": "test", "value": 42})
# Verify the result
assert result == {"success": True, "result": "test_result"}
# Verify the request
assert len(responses.calls) == 1
request = responses.calls[0].request
assert json.loads(request.body) == {"name": "test", "value": 42}
@responses.activate
def test_call_with_json_string(self, mock_tool):
"""Test calling a tool with a JSON string argument"""
# Mock the API response
responses.add(
responses.POST,
"https://api.example.com/test",
json={"success": True, "result": "test_result"},
status=200,
)
# Call the tool with a JSON string
result = mock_tool.call('{"name": "test", "value": 42}')
# Verify the result
assert result == {"success": True, "result": "test_result"}
# Verify the request
assert len(responses.calls) == 1
request = responses.calls[0].request
assert json.loads(request.body) == {"name": "test", "value": 42}
def test_call_with_both_args_and_kwargs_raises_error(self, mock_tool):
"""Test that providing both args and kwargs raises an error"""
with pytest.raises(ValueError, match="Cannot provide both positional and keyword arguments"):
mock_tool.call({"name": "test"}, value=42)
def test_call_with_multiple_args_raises_error(self, mock_tool):
"""Test that providing multiple positional arguments raises an error"""
with pytest.raises(ValueError, match="Only one positional argument is allowed"):
mock_tool.call({"name": "test"}, {"value": 42})
@responses.activate
def test_call_without_arguments(self, mock_tool):
"""Test calling a tool without any arguments"""
# Mock the API response
responses.add(
responses.POST,
"https://api.example.com/test",
json={"success": True, "result": "no_args"},
status=200,
)
# Call the tool without arguments
result = mock_tool.call()
# Verify the result
assert result == {"success": True, "result": "no_args"}
# Verify the request body is empty or contains empty JSON
assert len(responses.calls) == 1
request = responses.calls[0].request
# Handle case where body might be None for empty POST
if request.body:
assert json.loads(request.body) == {}
else:
assert request.body is None or request.body == b""