|
12 | 12 | # limitations under the License. |
13 | 13 | # =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== |
14 | 14 | import os |
| 15 | +from unittest import mock |
15 | 16 | from unittest.mock import MagicMock, patch |
16 | 17 |
|
17 | 18 | import pytest |
@@ -89,22 +90,98 @@ def test_google_api(): |
89 | 90 | assert result.status_code == 200 |
90 | 91 |
|
91 | 92 |
|
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 | + ) |
108 | 185 |
|
109 | 186 |
|
110 | 187 | @patch('wolframalpha.Client') |
|
0 commit comments