From 21b59cc1d0928dc04dcf5eac24470aaa05013b2d Mon Sep 17 00:00:00 2001 From: tanaka takashi Date: Tue, 23 Jun 2026 18:37:46 +0900 Subject: 基本機能実装 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_tools.py | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 tests/test_tools.py (limited to 'tests/test_tools.py') diff --git a/tests/test_tools.py b/tests/test_tools.py new file mode 100644 index 0000000..897c597 --- /dev/null +++ b/tests/test_tools.py @@ -0,0 +1,100 @@ +"""Tests for tool registry and built-in tools.""" + +from __future__ import annotations + +import json +from unittest.mock import MagicMock, patch + +from ollama_web.tools.registry import ToolRegistry, default_registry + + +def test_registry_register_and_execute(): + reg = ToolRegistry() + + def add(a: int, b: int) -> int: + """Add two numbers. + + Args: + a: First number. + b: Second number. + + Returns: + The sum. + """ + return a + b + + reg.register(add) + assert reg.get("add") is add + result = reg.execute("add", {"a": 2, "b": 3}) + assert result == "5" + + +def test_registry_execute_unknown(): + reg = ToolRegistry() + out = reg.execute("nonexistent", {}) + assert "error" in json.loads(out) + + +def test_registry_execute_invalid_json_args(): + reg = ToolRegistry() + + def echo(x: str) -> str: + return x + + reg.register(echo) + out = reg.execute("echo", "{not json") + assert "error" in json.loads(out) + + +def test_registry_execute_handles_exceptions(): + reg = ToolRegistry() + + def boom() -> None: + raise ValueError("boom") + + reg.register(boom) + out = reg.execute("boom", {}) + assert "error" in json.loads(out) + + +def test_default_registry_has_tools(): + reg = default_registry() + assert "web_search" in reg.names + assert "scrape_url" in reg.names + + +def test_web_search_empty_query(): + from ollama_web.tools.search import web_search + + assert web_search("") == [] + + +def test_web_search_mocked(): + from ollama_web.tools import search as search_mod + + fake = [{"title": "T", "href": "http://x", "body": "B"}] + + class FakeDDGS: + def __enter__(self): + return self + + def __exit__(self, *a): + return None + + def text(self, q, max_results=5): + return fake + + monkey = MagicMock() + monkey.DDGS = FakeDDGS + with patch.object(search_mod, "DDGS", FakeDDGS): + from ollama_web.tools.search import web_search + + result = web_search("test", max_results=1) + assert len(result) == 1 + assert result[0]["title"] == "T" + + +def test_scrape_url_empty(): + from ollama_web.tools.scrape import scrape_url + + assert scrape_url("") == "Invalid URL." \ No newline at end of file -- cgit v1.2.3