test: add background node unit test

Change-Id: I9aabcf02ff04fda40c56f3ea22abe6b8f93bf9b6
This commit is contained in:
laundry
2025-05-19 15:36:24 +08:00
parent c2b8dd8e6a
commit e927b556d6

View File

@@ -22,22 +22,29 @@ def mock_state():
"background_investigation_results": None, "background_investigation_results": None,
} }
@pytest.fixture @pytest.fixture
def mock_configurable(): def mock_configurable():
mock = MagicMock() mock = MagicMock()
mock.max_search_results = 5 mock.max_search_results = 5
return mock return mock
@pytest.fixture @pytest.fixture
def mock_config(): def mock_config():
# 你可以根据实际需要返回一个 MagicMock 或 dict # 你可以根据实际需要返回一个 MagicMock 或 dict
return MagicMock() return MagicMock()
@pytest.fixture @pytest.fixture
def patch_config_from_runnable_config(mock_configurable): def patch_config_from_runnable_config(mock_configurable):
with patch("src.graph.nodes.Configuration.from_runnable_config", return_value=mock_configurable): with patch(
"src.graph.nodes.Configuration.from_runnable_config",
return_value=mock_configurable,
):
yield yield
@pytest.fixture @pytest.fixture
def mock_tavily_search(): def mock_tavily_search():
with patch("src.graph.nodes.LoggedTavilySearch") as mock: with patch("src.graph.nodes.LoggedTavilySearch") as mock:
@@ -48,6 +55,7 @@ def mock_tavily_search():
] ]
yield mock yield mock
@pytest.fixture @pytest.fixture
def mock_web_search_tool(): def mock_web_search_tool():
with patch("src.graph.nodes.get_web_search_tool") as mock: with patch("src.graph.nodes.get_web_search_tool") as mock:
@@ -58,9 +66,15 @@ def mock_web_search_tool():
] ]
yield mock yield mock
@pytest.mark.parametrize("search_engine", [SearchEngine.TAVILY, "other"]) @pytest.mark.parametrize("search_engine", [SearchEngine.TAVILY, "other"])
def test_background_investigation_node_tavily( def test_background_investigation_node_tavily(
mock_state, mock_tavily_search, mock_web_search_tool, search_engine, patch_config_from_runnable_config, mock_config mock_state,
mock_tavily_search,
mock_web_search_tool,
search_engine,
patch_config_from_runnable_config,
mock_config,
): ):
"""Test background_investigation_node with Tavily search engine""" """Test background_investigation_node with Tavily search engine"""
with patch("src.graph.nodes.SELECTED_SEARCH_ENGINE", search_engine): with patch("src.graph.nodes.SELECTED_SEARCH_ENGINE", search_engine):
@@ -86,9 +100,12 @@ def test_background_investigation_node_tavily(
assert results[0]["title"] == "Test Title 1" assert results[0]["title"] == "Test Title 1"
assert results[0]["content"] == "Test Content 1" assert results[0]["content"] == "Test Content 1"
else: else:
mock_web_search_tool.return_value.invoke.assert_called_once_with("test query") mock_web_search_tool.return_value.invoke.assert_called_once_with(
"test query"
)
assert len(results) == 2 assert len(results) == 2
def test_background_investigation_node_malformed_response( def test_background_investigation_node_malformed_response(
mock_state, mock_tavily_search, patch_config_from_runnable_config, mock_config mock_state, mock_tavily_search, patch_config_from_runnable_config, mock_config
): ):