| 5 | from tools.search_engine import search |
| 6 | |
| 7 | class TestSearchEngine(unittest.TestCase): |
| 8 | def setUp(self): |
| 9 | # Capture stdout and stderr for testing |
| 10 | self.stdout = StringIO() |
| 11 | self.stderr = StringIO() |
| 12 | self.old_stdout = sys.stdout |
| 13 | self.old_stderr = sys.stderr |
| 14 | sys.stdout = self.stdout |
| 15 | sys.stderr = self.stderr |
| 16 | |
| 17 | def tearDown(self): |
| 18 | # Restore stdout and stderr |
| 19 | sys.stdout = self.old_stdout |
| 20 | sys.stderr = self.old_stderr |
| 21 | |
| 22 | @patch('tools.search_engine.DDGS') |
| 23 | def test_successful_search(self, mock_ddgs): |
| 24 | # Mock search results |
| 25 | mock_results = [ |
| 26 | { |
| 27 | 'href': 'http://example.com', |
| 28 | 'title': 'Example Title', |
| 29 | 'body': 'Example Body' |
| 30 | }, |
| 31 | { |
| 32 | 'href': 'http://example2.com', |
| 33 | 'title': 'Example Title 2', |
| 34 | 'body': 'Example Body 2' |
| 35 | } |
| 36 | ] |
| 37 | |
| 38 | # Setup mock |
| 39 | mock_ddgs_instance = MagicMock() |
| 40 | mock_ddgs_instance.__enter__.return_value.text.return_value = mock_results |
| 41 | mock_ddgs.return_value = mock_ddgs_instance |
| 42 | |
| 43 | # Run search |
| 44 | search("test query", max_results=2) |
| 45 | |
| 46 | # Check debug output |
| 47 | expected_debug = "DEBUG: Searching for query: test query (attempt 1/3)" |
| 48 | self.assertIn(expected_debug, self.stderr.getvalue()) |
| 49 | self.assertIn("DEBUG: Found 2 results", self.stderr.getvalue()) |
| 50 | |
| 51 | # Check search results output |
| 52 | output = self.stdout.getvalue() |
| 53 | self.assertIn("=== Result 1 ===", output) |
| 54 | self.assertIn("URL: http://example.com", output) |
| 55 | self.assertIn("Title: Example Title", output) |
| 56 | self.assertIn("Snippet: Example Body", output) |
| 57 | self.assertIn("=== Result 2 ===", output) |
| 58 | self.assertIn("URL: http://example2.com", output) |
| 59 | self.assertIn("Title: Example Title 2", output) |
| 60 | self.assertIn("Snippet: Example Body 2", output) |
| 61 | |
| 62 | # Verify mock was called correctly |
| 63 | mock_ddgs_instance.__enter__.return_value.text.assert_called_once_with( |
| 64 | "test query", |
nothing calls this directly
no outgoing calls
no test coverage detected