|
2 | 2 | from unittest.mock import MagicMock, patch |
3 | 3 | from mini_copilot import web_search |
4 | 4 |
|
| 5 | + |
5 | 6 | class TestWebSearch(unittest.TestCase): |
6 | 7 | @patch("mini_copilot.web_search.requests.get") |
7 | 8 | def test_search_ddg(self, mock_get): |
8 | 9 | # Match the .result__title .result__a selector |
9 | | - mock_get.return_value = MagicMock(ok=True, text='<html><div class="result__title"><a class="result__a" href="u">T</a></div></html>') |
| 10 | + mock_get.return_value = MagicMock( |
| 11 | + ok=True, |
| 12 | + text='<html><div class="result__title"><a class="result__a" href="u">T</a></div></html>', |
| 13 | + ) |
10 | 14 | results = web_search.search_ddg("q", num_results=1) |
11 | 15 | self.assertEqual(len(results), 1) |
12 | 16 | self.assertEqual(results[0]["url"], "u") |
13 | 17 |
|
| 18 | + @patch("mini_copilot.web_search.requests.get") |
| 19 | + def test_search_startpage(self, mock_get): |
| 20 | + # Match the .result and a.result-link, .wgl-title selectors |
| 21 | + mock_get.return_value = MagicMock( |
| 22 | + ok=True, |
| 23 | + text='<html><div class="result"><a class="result-link" href="http://u"><div class="wgl-title">T</div></a></div></html>', |
| 24 | + ) |
| 25 | + results = web_search.search_startpage("q", num_results=1) |
| 26 | + self.assertEqual(len(results), 1) |
| 27 | + self.assertEqual(results[0]["url"], "http://u") |
| 28 | + self.assertEqual(results[0]["title"], "T") |
| 29 | + |
| 30 | + @patch("mini_copilot.web_search.requests.Session") |
| 31 | + def test_search_bing(self, mock_session): |
| 32 | + # Match the li.b_algo and h2 a selectors |
| 33 | + mock_s = MagicMock() |
| 34 | + mock_session.return_value = mock_s |
| 35 | + mock_s.get.return_value = MagicMock( |
| 36 | + ok=True, |
| 37 | + text='<html><li class="b_algo"><h2><a href="http://u">T</a></h2></li></html>', |
| 38 | + ) |
| 39 | + results = web_search.search_bing("q", num_results=1) |
| 40 | + self.assertEqual(len(results), 1) |
| 41 | + self.assertEqual(results[0]["url"], "http://u") |
| 42 | + self.assertEqual(results[0]["title"], "T") |
| 43 | + |
14 | 44 | @patch("mini_copilot.web_search.requests.Session") |
15 | 45 | def test_extract_text(self, mock_session): |
16 | 46 | mock_s = MagicMock() |
17 | 47 | mock_session.return_value = mock_s |
18 | | - mock_s.get.return_value = MagicMock(ok=True, status_code=200, text='<html><div id="firstHeading">T</div></html>', apparent_encoding='u8') |
19 | | - mock_s.get.return_value.apparent_encoding = 'u8' |
20 | | - self.assertIn("T", web_search.extract_text_from_url("https://en.wikipedia.org/wiki/T")) |
| 48 | + mock_s.get.return_value = MagicMock( |
| 49 | + ok=True, |
| 50 | + status_code=200, |
| 51 | + text='<html><div id="firstHeading">T</div></html>', |
| 52 | + apparent_encoding="u8", |
| 53 | + ) |
| 54 | + mock_s.get.return_value.apparent_encoding = "u8" |
| 55 | + self.assertIn( |
| 56 | + "T", web_search.extract_text_from_url("https://en.wikipedia.org/wiki/T") |
| 57 | + ) |
| 58 | + |
21 | 59 |
|
22 | 60 | if __name__ == "__main__": |
23 | 61 | unittest.main() |
0 commit comments