forked from dremio/dremio-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ai_tools.py
More file actions
221 lines (180 loc) · 7.33 KB
/
test_ai_tools.py
File metadata and controls
221 lines (180 loc) · 7.33 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#
# Copyright (C) 2017-2025 Dremio Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import pytest
from dremioai.api.dremio.ai_tools import (
AiTool,
InvokeToolResponse,
list_tools,
invoke_tool,
)
from mocks.http_mock import HttpMockFramework
# --- list_tools tests ---
@pytest.fixture
def no_project_id_settings(mock_settings_instance):
old_project_id = mock_settings_instance.dremio.project_id
mock_settings_instance.dremio.raw_project_id = None
yield mock_settings_instance
if old_project_id is not None:
mock_settings_instance.dremio.project_id = old_project_id
@pytest.mark.asyncio
async def test_list_tools_returns_tools(mock_settings_instance):
with HttpMockFramework() as mock:
mock.load_mock_data(r"/v0/projects/[^/]+/ai/tools$", "ai_tools/list_tools.json")
result = await list_tools()
assert bool(result)
assert len(result.tools) == 3
names = [t.name for t in result.tools]
assert "runSql" in names
assert "getTableOrViewSchema" in names
assert "listEngines" in names
@pytest.mark.asyncio
async def test_list_tools_returns_input_schema(mock_settings_instance):
with HttpMockFramework() as mock:
mock.load_mock_data(r"/v0/projects/[^/]+/ai/tools$", "ai_tools/list_tools.json")
result = await list_tools()
run_sql = next(t for t in result.tools if t.name == "runSql")
assert run_sql.input_schema["type"] == "object"
assert "sqlText" in run_sql.input_schema["properties"]
@pytest.mark.asyncio
async def test_list_tools_empty_registry(mock_settings_instance):
with HttpMockFramework() as mock:
mock.add_mock_response(r"/v0/projects/[^/]+/ai/tools$", {"tools": []})
result = await list_tools()
assert result.tools == []
assert bool(result)
# --- invoke_tool tests ---
@pytest.mark.asyncio
async def test_invoke_tool_success(mock_settings_instance):
with HttpMockFramework() as mock:
mock.load_mock_data(
r"/v0/projects/[^/]+/ai/tools/runSql:invoke$",
"ai_tools/invoke_result.json",
)
result = await invoke_tool("runSql", {"sqlText": "SELECT 1"})
assert bool(result)
assert result.result["columns"] == ["id", "name"]
assert result.error is None
@pytest.mark.asyncio
async def test_invoke_tool_error_response(mock_settings_instance):
with HttpMockFramework() as mock:
mock.load_mock_data(
r"/v0/projects/[^/]+/ai/tools/unknownTool:invoke$",
"ai_tools/invoke_error.json",
)
result = await invoke_tool("unknownTool", {})
assert result.error is not None
assert "not found" in result.error
assert result.result is None
# --- Pydantic model unit tests (no HTTP) ---
def test_ai_tool_model_validation():
raw = {
"name": "runSql",
"description": "runSql",
"inputSchema": {
"type": "object",
"properties": {"sqlText": {"type": "string"}},
"required": ["sqlText"],
},
}
tool = AiTool.model_validate(raw)
assert tool.name == "runSql"
assert tool.input_schema["type"] == "object"
assert tool.input_schema["required"] == ["sqlText"]
def test_ai_tool_model_minimal_schema():
"""Tools with an empty inputSchema (e.g. listEngines) should deserialize cleanly."""
raw = {"name": "listEngines", "description": "listEngines", "inputSchema": {"type": "object"}}
tool = AiTool.model_validate(raw)
assert tool.name == "listEngines"
assert tool.input_schema == {"type": "object"}
def test_invoke_tool_response_succeeded():
resp = InvokeToolResponse.model_validate({"result": {"sql": "SELECT 1"}})
assert bool(resp) is True
assert resp.result == {"sql": "SELECT 1"}
assert resp.error is None
def test_invoke_tool_response_failed():
resp = InvokeToolResponse.model_validate({"error": "Tool not found"})
assert bool(resp) is False
assert resp.result is None
assert resp.error == "Tool not found"
def test_invoke_tool_response_empty():
resp = InvokeToolResponse.model_validate({})
assert bool(resp) is True
assert resp.result is None
assert resp.error is None
assert resp.is_empty is True
def test_invoke_tool_response_is_empty_false_when_result():
"""is_empty should be False when a result is present."""
resp = InvokeToolResponse.model_validate({"result": {"sql": "SELECT 1"}})
assert resp.is_empty is False
def test_invoke_tool_response_is_empty_false_when_error():
"""is_empty should be False when an error is present."""
resp = InvokeToolResponse.model_validate({"error": "Tool not found"})
assert resp.is_empty is False
# --- HTTP error scenario tests ---
@pytest.mark.asyncio
async def test_list_tools_http_error(mock_settings_instance):
"""list_tools should return a response with error set on HTTP 4xx/5xx."""
with HttpMockFramework() as mock:
mock.add_mock_response(
r"/v0/projects/[^/]+/ai/tools$",
{"error": "Unauthorized"},
status=401,
)
result = await list_tools()
assert not bool(result)
assert result.error is not None
assert "401" in result.error
@pytest.mark.asyncio
async def test_invoke_tool_http_error(mock_settings_instance):
"""invoke_tool should return a response with error set on HTTP 500."""
with HttpMockFramework() as mock:
mock.add_mock_response(
r"/v0/projects/[^/]+/ai/tools/runSql:invoke$",
{"error": "Internal Server Error"},
status=500,
)
result = await invoke_tool("runSql", {"sqlText": "SELECT 1"})
assert not bool(result)
assert result.error is not None
assert "500" in result.error
@pytest.mark.asyncio
async def test_invoke_tool_url_encodes_name(mock_settings_instance):
"""tool_name with special characters should be URL-encoded."""
with HttpMockFramework() as mock:
# The encoded name "my%2Ftool" should appear in the URL
mock.add_mock_response(
r"/v0/projects/[^/]+/ai/tools/my%2Ftool:invoke$",
{"result": "ok", "error": None},
)
result = await invoke_tool("my/tool", {})
assert result.result == "ok"
@pytest.mark.asyncio
async def test_list_tools_uses_api_v3_without_project_id(no_project_id_settings):
with HttpMockFramework() as mock:
mock.load_mock_data(r"/api/v3/ai/tools$", "ai_tools/list_tools.json")
result = await list_tools()
assert bool(result)
assert len(result.tools) == 3
@pytest.mark.asyncio
async def test_invoke_tool_uses_api_v3_without_project_id(no_project_id_settings):
with HttpMockFramework() as mock:
mock.load_mock_data(
r"/api/v3/ai/tools/runSql:invoke$",
"ai_tools/invoke_result.json",
)
result = await invoke_tool("runSql", {"sqlText": "SELECT 1"})
assert bool(result)
assert result.result["columns"] == ["id", "name"]