|
| 1 | +# |
| 2 | +# Copyright (C) 2017-2025 Dremio Corporation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +import pytest |
| 18 | +from dremioai.api.dremio.ai_tools import ( |
| 19 | + AiTool, |
| 20 | + InvokeToolResponse, |
| 21 | + list_tools, |
| 22 | + invoke_tool, |
| 23 | +) |
| 24 | +from mocks.http_mock import HttpMockFramework |
| 25 | + |
| 26 | + |
| 27 | +# --- list_tools tests --- |
| 28 | + |
| 29 | +@pytest.mark.asyncio |
| 30 | +async def test_list_tools_returns_tools(mock_settings_instance): |
| 31 | + with HttpMockFramework() as mock: |
| 32 | + mock.load_mock_data(r"/api/v4/ai/tools$", "ai_tools/list_tools.json") |
| 33 | + result = await list_tools() |
| 34 | + assert bool(result) |
| 35 | + assert len(result.tools) == 3 |
| 36 | + names = [t.name for t in result.tools] |
| 37 | + assert "runSql" in names |
| 38 | + assert "getTableOrViewSchema" in names |
| 39 | + assert "listEngines" in names |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.asyncio |
| 43 | +async def test_list_tools_returns_input_schema(mock_settings_instance): |
| 44 | + with HttpMockFramework() as mock: |
| 45 | + mock.load_mock_data(r"/api/v4/ai/tools$", "ai_tools/list_tools.json") |
| 46 | + result = await list_tools() |
| 47 | + run_sql = next(t for t in result.tools if t.name == "runSql") |
| 48 | + assert run_sql.input_schema["type"] == "object" |
| 49 | + assert "sqlText" in run_sql.input_schema["properties"] |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.asyncio |
| 53 | +async def test_list_tools_empty_registry(mock_settings_instance): |
| 54 | + with HttpMockFramework() as mock: |
| 55 | + mock.add_mock_response(r"/api/v4/ai/tools$", {"tools": []}) |
| 56 | + result = await list_tools() |
| 57 | + assert result.tools == [] |
| 58 | + assert bool(result) |
| 59 | + |
| 60 | + |
| 61 | +# --- invoke_tool tests --- |
| 62 | + |
| 63 | +@pytest.mark.asyncio |
| 64 | +async def test_invoke_tool_success(mock_settings_instance): |
| 65 | + with HttpMockFramework() as mock: |
| 66 | + mock.load_mock_data(r"/api/v4/ai/tools/runSql:invoke$", "ai_tools/invoke_result.json") |
| 67 | + result = await invoke_tool("runSql", {"sqlText": "SELECT 1"}) |
| 68 | + assert bool(result) |
| 69 | + assert result.result["columns"] == ["id", "name"] |
| 70 | + assert result.error is None |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.asyncio |
| 74 | +async def test_invoke_tool_error_response(mock_settings_instance): |
| 75 | + with HttpMockFramework() as mock: |
| 76 | + mock.load_mock_data(r"/api/v4/ai/tools/unknownTool:invoke$", "ai_tools/invoke_error.json") |
| 77 | + result = await invoke_tool("unknownTool", {}) |
| 78 | + assert result.error is not None |
| 79 | + assert "not found" in result.error |
| 80 | + assert result.result is None |
| 81 | + |
| 82 | + |
| 83 | +# --- Pydantic model unit tests (no HTTP) --- |
| 84 | + |
| 85 | +def test_ai_tool_model_validation(): |
| 86 | + raw = { |
| 87 | + "name": "runSql", |
| 88 | + "description": "runSql", |
| 89 | + "inputSchema": { |
| 90 | + "type": "object", |
| 91 | + "properties": {"sqlText": {"type": "string"}}, |
| 92 | + "required": ["sqlText"], |
| 93 | + }, |
| 94 | + } |
| 95 | + tool = AiTool.model_validate(raw) |
| 96 | + assert tool.name == "runSql" |
| 97 | + assert tool.input_schema["type"] == "object" |
| 98 | + assert tool.input_schema["required"] == ["sqlText"] |
| 99 | + |
| 100 | + |
| 101 | +def test_ai_tool_model_minimal_schema(): |
| 102 | + """Tools with an empty inputSchema (e.g. listEngines) should deserialize cleanly.""" |
| 103 | + raw = {"name": "listEngines", "description": "listEngines", "inputSchema": {"type": "object"}} |
| 104 | + tool = AiTool.model_validate(raw) |
| 105 | + assert tool.name == "listEngines" |
| 106 | + assert tool.input_schema == {"type": "object"} |
| 107 | + |
| 108 | + |
| 109 | +def test_invoke_tool_response_succeeded(): |
| 110 | + resp = InvokeToolResponse.model_validate({"result": {"sql": "SELECT 1"}}) |
| 111 | + assert bool(resp) is True |
| 112 | + assert resp.result == {"sql": "SELECT 1"} |
| 113 | + assert resp.error is None |
| 114 | + |
| 115 | + |
| 116 | +def test_invoke_tool_response_failed(): |
| 117 | + resp = InvokeToolResponse.model_validate({"error": "Tool not found"}) |
| 118 | + assert bool(resp) is False |
| 119 | + assert resp.result is None |
| 120 | + assert resp.error == "Tool not found" |
| 121 | + |
| 122 | + |
| 123 | +def test_invoke_tool_response_empty(): |
| 124 | + resp = InvokeToolResponse.model_validate({}) |
| 125 | + assert bool(resp) is True |
| 126 | + assert resp.result is None |
| 127 | + assert resp.error is None |
| 128 | + assert resp.is_empty is True |
| 129 | + |
| 130 | + |
| 131 | +def test_invoke_tool_response_is_empty_false_when_result(): |
| 132 | + """is_empty should be False when a result is present.""" |
| 133 | + resp = InvokeToolResponse.model_validate({"result": {"sql": "SELECT 1"}}) |
| 134 | + assert resp.is_empty is False |
| 135 | + |
| 136 | + |
| 137 | +def test_invoke_tool_response_is_empty_false_when_error(): |
| 138 | + """is_empty should be False when an error is present.""" |
| 139 | + resp = InvokeToolResponse.model_validate({"error": "Tool not found"}) |
| 140 | + assert resp.is_empty is False |
| 141 | + |
| 142 | + |
| 143 | +# --- HTTP error scenario tests --- |
| 144 | + |
| 145 | +@pytest.mark.asyncio |
| 146 | +async def test_list_tools_http_error(mock_settings_instance): |
| 147 | + """list_tools should return a response with error set on HTTP 4xx/5xx.""" |
| 148 | + with HttpMockFramework() as mock: |
| 149 | + mock.add_mock_response(r"/api/v4/ai/tools$", {"error": "Unauthorized"}, status=401) |
| 150 | + result = await list_tools() |
| 151 | + assert not bool(result) |
| 152 | + assert result.error is not None |
| 153 | + assert "401" in result.error |
| 154 | + |
| 155 | + |
| 156 | +@pytest.mark.asyncio |
| 157 | +async def test_invoke_tool_http_error(mock_settings_instance): |
| 158 | + """invoke_tool should return a response with error set on HTTP 500.""" |
| 159 | + with HttpMockFramework() as mock: |
| 160 | + mock.add_mock_response(r"/api/v4/ai/tools/runSql:invoke$", {"error": "Internal Server Error"}, status=500) |
| 161 | + result = await invoke_tool("runSql", {"sqlText": "SELECT 1"}) |
| 162 | + assert not bool(result) |
| 163 | + assert result.error is not None |
| 164 | + assert "500" in result.error |
| 165 | + |
| 166 | + |
| 167 | +@pytest.mark.asyncio |
| 168 | +async def test_invoke_tool_url_encodes_name(mock_settings_instance): |
| 169 | + """tool_name with special characters should be URL-encoded.""" |
| 170 | + with HttpMockFramework() as mock: |
| 171 | + # The encoded name "my%2Ftool" should appear in the URL |
| 172 | + mock.add_mock_response(r"/api/v4/ai/tools/my%2Ftool:invoke$", {"result": "ok", "error": None}) |
| 173 | + result = await invoke_tool("my/tool", {}) |
| 174 | + assert result.result == "ok" |
0 commit comments