test: use SQLite sessions in unit misc (#39120)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Asuka Minatoandautofix-ci[bot] authored and GitHub committed 2026-07-21 01:51:24 +00:00
1 parent 75f7069541
commit 891b2dc537
3 files changed
+85 -48

No files matched your search

+24 -10
View File
@@ -1,9 +1,12 @@
import json
import operator
from collections.abc import Iterator
from unittest.mock import Mock, patch
import httpx
import pytest
from sqlalchemy import Engine
from sqlalchemy.orm import Session
from core.tools.__base.tool_runtime import ToolRuntime
from core.tools.custom_tool.tool import ApiTool
@@ -16,6 +19,13 @@ from core.tools.entities.tool_entities import (
)
@pytest.fixture
def database_session(sqlite_engine: Engine) -> Iterator[Session]:
"""Yield the live ORM session required by the shared tool invocation contract."""
with Session(sqlite_engine) as session:
yield session
def _get_message_by_type[T](msgs: list[ToolInvokeMessage], msg_type: type[T]) -> ToolInvokeMessage | None:
return next((i for i in msgs if isinstance(i.message, msg_type)), None)
@@ -57,7 +67,9 @@ class TestApiToolInvoke:
)
@patch("core.tools.custom_tool.tool.ssrf_proxy.get")
def test_invoke_with_json_response_creates_text_message_with_serialized_json(self, mock_get: Mock) -> None:
def test_invoke_with_json_response_creates_text_message_with_serialized_json(
self, mock_get: Mock, database_session: Session
) -> None:
"""Test that when upstream returns JSON, the output Text message contains JSON-serialized string."""
# Setup mock response with JSON content
json_response_data = {
@@ -74,7 +86,7 @@ class TestApiToolInvoke:
mock_get.return_value = mock_response
# Invoke the tool
result_generator = self.api_tool._invoke(session=Mock(), user_id="test_user", tool_parameters={})
result_generator = self.api_tool._invoke(session=database_session, user_id="test_user", tool_parameters={})
# Get the result from the generator
result = list(result_generator)
@@ -135,7 +147,7 @@ class TestApiToolInvoke:
ids=operator.itemgetter(0),
)
def test_invoke_with_non_dict_json_response_creates_text_message_with_serialized_json(
self, mock_get: Mock, test_case
self, mock_get: Mock, test_case, database_session: Session
) -> None:
"""Test that when upstream returns a non-dict JSON, the output Text message contains JSON-serialized string."""
# Setup mock response with non-dict JSON content
@@ -149,7 +161,7 @@ class TestApiToolInvoke:
mock_get.return_value = mock_response
# Invoke the tool
result_generator = self.api_tool._invoke(session=Mock(), user_id="test_user", tool_parameters={})
result_generator = self.api_tool._invoke(session=database_session, user_id="test_user", tool_parameters={})
# Get the result from the generator
result = list(result_generator)
@@ -173,7 +185,9 @@ class TestApiToolInvoke:
assert json_message is None, "_invoke should not yield a JSON message for JSON array response"
@patch("core.tools.custom_tool.tool.ssrf_proxy.get")
def test_invoke_with_text_response_creates_text_message_with_original_text(self, mock_get: Mock) -> None:
def test_invoke_with_text_response_creates_text_message_with_original_text(
self, mock_get: Mock, database_session: Session
) -> None:
"""Test that when upstream returns plain text, the output Text message contains the original text."""
# Setup mock response with plain text content
text_response_data = "This is a plain text response"
@@ -186,7 +200,7 @@ class TestApiToolInvoke:
mock_get.return_value = mock_response
# Invoke the tool
result_generator = self.api_tool._invoke(session=Mock(), user_id="test_user", tool_parameters={})
result_generator = self.api_tool._invoke(session=database_session, user_id="test_user", tool_parameters={})
# Get the result from the generator
result = list(result_generator)
@@ -202,7 +216,7 @@ class TestApiToolInvoke:
assert message.message.text == text_response_data
@patch("core.tools.custom_tool.tool.ssrf_proxy.get")
def test_invoke_with_empty_response(self, mock_get: Mock) -> None:
def test_invoke_with_empty_response(self, mock_get: Mock, database_session: Session) -> None:
"""Test that empty responses are handled correctly."""
# Setup mock response with empty content
mock_response = Mock(spec=httpx.Response)
@@ -212,7 +226,7 @@ class TestApiToolInvoke:
mock_get.return_value = mock_response
# Invoke the tool
result_generator = self.api_tool._invoke(session=Mock(), user_id="test_user", tool_parameters={})
result_generator = self.api_tool._invoke(session=database_session, user_id="test_user", tool_parameters={})
# Get the result from the generator
result = list(result_generator)
@@ -228,7 +242,7 @@ class TestApiToolInvoke:
assert "Empty response from the tool" in message.message.text
@patch("core.tools.custom_tool.tool.ssrf_proxy.get")
def test_invoke_with_error_response(self, mock_get: Mock) -> None:
def test_invoke_with_error_response(self, mock_get: Mock, database_session: Session) -> None:
"""Test that error responses are handled correctly."""
# Setup mock response with error status code
mock_response = Mock(spec=httpx.Response)
@@ -236,7 +250,7 @@ class TestApiToolInvoke:
mock_response.text = "Not Found"
mock_get.return_value = mock_response
result_generator = self.api_tool._invoke(session=Mock(), user_id="test_user", tool_parameters={})
result_generator = self.api_tool._invoke(session=database_session, user_id="test_user", tool_parameters={})
# Invoke the tool and expect an error
with pytest.raises(Exception) as exc_info: