44# This source code is licensed under the terms described in the LICENSE file in
55# the root directory of this source tree.
66
7- from unittest .mock import AsyncMock , patch
7+ from unittest .mock import AsyncMock , MagicMock
88
99import httpx
1010import pytest
1515
1616@pytest .fixture
1717def tavily_search ():
18- return TavilySearchToolRuntimeImpl (TavilySearchToolConfig (api_key = "test-key" , max_results = 3 ))
18+ impl = TavilySearchToolRuntimeImpl (TavilySearchToolConfig (api_key = "test-key" , max_results = 3 ))
19+ impl ._client = MagicMock (spec = httpx .AsyncClient )
20+ return impl
1921
2022
2123@pytest .fixture
@@ -38,83 +40,83 @@ def mock_tavily_response():
3840
3941
4042async def test_invoke_with_allowed_domains (tavily_search , mock_tavily_response ):
41- with patch ( "httpx.AsyncClient .post" , new_callable = AsyncMock , return_value = mock_tavily_response ) as mock_post :
42- await tavily_search .invoke_tool (
43- "web_search" ,
44- {
45- "query" : "test query" ,
46- "allowed_domains" : ["example.com" , "docs.example.com" ],
47- },
48- )
49- call_kwargs = mock_post .call_args
50- request_body = call_kwargs .kwargs ["json" ]
51- assert request_body ["include_domains" ] == ["example.com" , "docs.example.com" ]
43+ tavily_search . _client .post = AsyncMock ( return_value = mock_tavily_response )
44+ await tavily_search .invoke_tool (
45+ "web_search" ,
46+ {
47+ "query" : "test query" ,
48+ "allowed_domains" : ["example.com" , "docs.example.com" ],
49+ },
50+ )
51+ call_kwargs = tavily_search . _client . post .call_args
52+ request_body = call_kwargs .kwargs ["json" ]
53+ assert request_body ["include_domains" ] == ["example.com" , "docs.example.com" ]
5254
5355
5456async def test_invoke_with_search_context_size (tavily_search , mock_tavily_response ):
55- with patch ( "httpx.AsyncClient .post" , new_callable = AsyncMock , return_value = mock_tavily_response ) as mock_post :
56- await tavily_search .invoke_tool (
57- "web_search" ,
58- {
59- "query" : "test query" ,
60- "search_context_size" : "high" ,
61- },
62- )
63- call_kwargs = mock_post .call_args
64- request_body = call_kwargs .kwargs ["json" ]
65- assert request_body ["max_results" ] == 10
57+ tavily_search . _client .post = AsyncMock ( return_value = mock_tavily_response )
58+ await tavily_search .invoke_tool (
59+ "web_search" ,
60+ {
61+ "query" : "test query" ,
62+ "search_context_size" : "high" ,
63+ },
64+ )
65+ call_kwargs = tavily_search . _client . post .call_args
66+ request_body = call_kwargs .kwargs ["json" ]
67+ assert request_body ["max_results" ] == 10
6668
6769
6870async def test_invoke_without_extra_params (tavily_search , mock_tavily_response ):
69- with patch ( "httpx.AsyncClient .post" , new_callable = AsyncMock , return_value = mock_tavily_response ) as mock_post :
70- await tavily_search .invoke_tool (
71- "web_search" ,
72- {"query" : "test query" },
73- )
74- call_kwargs = mock_post .call_args
75- request_body = call_kwargs .kwargs ["json" ]
76- assert request_body ["query" ] == "test query"
77- assert request_body ["api_key" ] == "test-key"
78- assert "include_domains" not in request_body
79- assert "max_results" not in request_body
71+ tavily_search . _client .post = AsyncMock ( return_value = mock_tavily_response )
72+ await tavily_search .invoke_tool (
73+ "web_search" ,
74+ {"query" : "test query" },
75+ )
76+ call_kwargs = tavily_search . _client . post .call_args
77+ request_body = call_kwargs .kwargs ["json" ]
78+ assert request_body ["query" ] == "test query"
79+ assert request_body ["api_key" ] == "test-key"
80+ assert "include_domains" not in request_body
81+ assert "max_results" not in request_body
8082
8183
8284async def test_invoke_with_user_location_ignored (tavily_search , mock_tavily_response ):
83- with patch ( "httpx.AsyncClient .post" , new_callable = AsyncMock , return_value = mock_tavily_response ) as mock_post :
84- await tavily_search .invoke_tool (
85- "web_search" ,
86- {
87- "query" : "test query" ,
88- "user_location" : {"country" : "US" , "city" : "San Francisco" },
89- },
90- )
91- call_kwargs = mock_post .call_args
92- request_body = call_kwargs .kwargs ["json" ]
93- assert "user_location" not in request_body
94- assert "country" not in request_body
95- assert "location" not in request_body
85+ tavily_search . _client .post = AsyncMock ( return_value = mock_tavily_response )
86+ await tavily_search .invoke_tool (
87+ "web_search" ,
88+ {
89+ "query" : "test query" ,
90+ "user_location" : {"country" : "US" , "city" : "San Francisco" },
91+ },
92+ )
93+ call_kwargs = tavily_search . _client . post .call_args
94+ request_body = call_kwargs .kwargs ["json" ]
95+ assert "user_location" not in request_body
96+ assert "country" not in request_body
97+ assert "location" not in request_body
9698
9799
98100async def test_invoke_with_empty_allowed_domains (tavily_search , mock_tavily_response ):
99- with patch ( "httpx.AsyncClient .post" , new_callable = AsyncMock , return_value = mock_tavily_response ) as mock_post :
100- await tavily_search .invoke_tool (
101- "web_search" ,
102- {
103- "query" : "test query" ,
104- "allowed_domains" : [],
105- },
106- )
107- call_kwargs = mock_post .call_args
108- request_body = call_kwargs .kwargs ["json" ]
109- assert "include_domains" not in request_body
101+ tavily_search . _client .post = AsyncMock ( return_value = mock_tavily_response )
102+ await tavily_search .invoke_tool (
103+ "web_search" ,
104+ {
105+ "query" : "test query" ,
106+ "allowed_domains" : [],
107+ },
108+ )
109+ call_kwargs = tavily_search . _client . post .call_args
110+ request_body = call_kwargs .kwargs ["json" ]
111+ assert "include_domains" not in request_body
110112
111113
112114async def test_invoke_returns_source_metadata (tavily_search , mock_tavily_response ):
113115 """Test that invoke_tool returns source URLs in metadata."""
114- with patch ( "httpx.AsyncClient .post" , new_callable = AsyncMock , return_value = mock_tavily_response ):
115- result = await tavily_search .invoke_tool (tool_name = "web_search" , kwargs = {"query" : "test query" })
116- assert result .metadata is not None
117- assert "sources" in result .metadata
118- assert len (result .metadata ["sources" ]) == 1
119- assert result .metadata ["sources" ][0 ]["url" ] == "https://example.com"
120- assert result .metadata ["query" ] == "test query"
116+ tavily_search . _client .post = AsyncMock ( return_value = mock_tavily_response )
117+ result = await tavily_search .invoke_tool (tool_name = "web_search" , kwargs = {"query" : "test query" })
118+ assert result .metadata is not None
119+ assert "sources" in result .metadata
120+ assert len (result .metadata ["sources" ]) == 1
121+ assert result .metadata ["sources" ][0 ]["url" ] == "https://example.com"
122+ assert result .metadata ["query" ] == "test query"
0 commit comments