Skip to content

Commit c18fed1

Browse files
authored
chore: mock duckduckgo search test (#1017)
1 parent 0410bf0 commit c18fed1

File tree

1 file changed

+93
-16
lines changed

1 file changed

+93
-16
lines changed

test/toolkits/test_search_functions.py

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# limitations under the License.
1313
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
1414
import os
15+
from unittest import mock
1516
from unittest.mock import MagicMock, patch
1617

1718
import pytest
@@ -89,22 +90,98 @@ def test_google_api():
8990
assert result.status_code == 200
9091

9192

92-
def test_duckduckgo_api():
93-
# Test DuckDuckGo Instant Answer API
94-
95-
url = "https://api.duckduckgo.com/"
96-
params = {"q": "test", "format": "json"}
97-
result = requests.get(url, params=params)
98-
99-
assert result.status_code == 200
100-
101-
102-
def test_web_search(search_toolkit):
103-
query = "What big things are happening in 2023?"
104-
answer = search_toolkit.search_google(query)
105-
assert answer is not None
106-
answer = search_toolkit.search_duckduckgo(query)
107-
assert answer is not None
93+
search_duckduckgo = SearchToolkit().search_duckduckgo
94+
95+
96+
def test_search_duckduckgo_text():
97+
# Mock the DDGS class and its text method
98+
with mock.patch("duckduckgo_search.DDGS") as MockDDGS:
99+
# Create a mock instance of DDGS
100+
mock_ddgs_instance = MockDDGS.return_value
101+
mock_ddgs_instance.text.return_value = [
102+
{
103+
"title": "Test Title 1",
104+
"body": "Test Body 1",
105+
"href": "https://example1.com",
106+
},
107+
{
108+
"title": "Test Title 2",
109+
"body": "Test Body 2",
110+
"href": "https://example2.com",
111+
},
112+
]
113+
114+
# Call the function with the mocked DDGS
115+
result = search_duckduckgo(
116+
query="test query", source="text", max_results=2
117+
)
118+
119+
# Check if the result is as expected
120+
assert result == [
121+
{
122+
"result_id": 1,
123+
"title": "Test Title 1",
124+
"description": "Test Body 1",
125+
"url": "https://example1.com",
126+
},
127+
{
128+
"result_id": 2,
129+
"title": "Test Title 2",
130+
"description": "Test Body 2",
131+
"url": "https://example2.com",
132+
},
133+
]
134+
135+
mock_ddgs_instance.text.assert_called_once_with(
136+
keywords="test query", max_results=2
137+
)
138+
139+
140+
def test_search_duckduckgo_images():
141+
# Mock the DDGS class and its images method
142+
with mock.patch("duckduckgo_search.DDGS") as MockDDGS:
143+
mock_ddgs_instance = MockDDGS.return_value
144+
mock_ddgs_instance.images.return_value = [
145+
{
146+
"title": "Image Title 1",
147+
"image": "https://image1.com",
148+
"url": "https://example1.com",
149+
"source": "Example Source 1",
150+
},
151+
{
152+
"title": "Image Title 2",
153+
"image": "https://image2.com",
154+
"url": "https://example2.com",
155+
"source": "Example Source 2",
156+
},
157+
]
158+
159+
# Call the function with the mocked DDGS for images
160+
result = search_duckduckgo(
161+
query="test query", source="images", max_results=2
162+
)
163+
164+
# Check if the result matches the expected output
165+
assert result == [
166+
{
167+
"result_id": 1,
168+
"title": "Image Title 1",
169+
"image": "https://image1.com",
170+
"url": "https://example1.com",
171+
"source": "Example Source 1",
172+
},
173+
{
174+
"result_id": 2,
175+
"title": "Image Title 2",
176+
"image": "https://image2.com",
177+
"url": "https://example2.com",
178+
"source": "Example Source 2",
179+
},
180+
]
181+
182+
mock_ddgs_instance.images.assert_called_once_with(
183+
keywords="test query", max_results=2
184+
)
108185

109186

110187
@patch('wolframalpha.Client')

0 commit comments

Comments
 (0)