logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

test_google.py (9959B)


  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. import lxml
  5. from searx.engines import google
  6. from searx.testing import SearxTestCase
  7. class TestGoogleEngine(SearxTestCase):
  8. def mock_response(self, text):
  9. response = mock.Mock(text=text, url='https://www.google.com/search?q=test&start=0&gbv=1&gws_rd=cr')
  10. response.search_params = mock.Mock()
  11. response.search_params.get = mock.Mock(return_value='www.google.com')
  12. return response
  13. def test_request(self):
  14. google.supported_languages = ['en', 'fr', 'zh-CN']
  15. query = 'test_query'
  16. dicto = defaultdict(dict)
  17. dicto['pageno'] = 1
  18. dicto['language'] = 'fr-FR'
  19. dicto['time_range'] = ''
  20. params = google.request(query, dicto)
  21. self.assertIn('url', params)
  22. self.assertIn(query, params['url'])
  23. self.assertIn('google.fr', params['url'])
  24. self.assertIn('fr', params['url'])
  25. self.assertIn('fr', params['headers']['Accept-Language'])
  26. dicto['language'] = 'en-US'
  27. params = google.request(query, dicto)
  28. self.assertIn('google.com', params['url'])
  29. self.assertIn('en', params['url'])
  30. self.assertIn('en', params['headers']['Accept-Language'])
  31. dicto['language'] = 'zh'
  32. params = google.request(query, dicto)
  33. self.assertIn('google.com', params['url'])
  34. self.assertIn('zh-CN', params['url'])
  35. self.assertIn('zh-CN', params['headers']['Accept-Language'])
  36. def test_response(self):
  37. self.assertRaises(AttributeError, google.response, None)
  38. self.assertRaises(AttributeError, google.response, [])
  39. self.assertRaises(AttributeError, google.response, '')
  40. self.assertRaises(AttributeError, google.response, '[]')
  41. response = self.mock_response('<html></html>')
  42. self.assertEqual(google.response(response), [])
  43. html = """
  44. <div class="g">
  45. <h3 class="r">
  46. <a href="http://this.should.be.the.link/">
  47. <b>This</b> is <b>the</b> title
  48. </a>
  49. </h3>
  50. <div class="s">
  51. <div class="kv" style="margin-bottom:2px">
  52. <cite>
  53. <b>test</b>.psychologies.com/
  54. </cite>
  55. <div class="_nBb">‎
  56. <div style="display:inline" onclick="google.sham(this);" aria-expanded="false"
  57. aria-haspopup="true" tabindex="0" data-ved="0CBUQ7B0wAA">
  58. <span class="_O0">
  59. </span>
  60. </div>
  61. <div style="display:none" class="am-dropdown-menu" role="menu" tabindex="-1">
  62. <ul>
  63. <li class="_Ykb">
  64. <a class="_Zkb" href="http://www.google.fr/url?url=http://webcache.googleusercontent
  65. .com/search%3Fcache:R1Z_4pGXjuIJ:http://test.psychologies.com/">
  66. En cache
  67. </a>
  68. </li>
  69. <li class="_Ykb">
  70. <a class="_Zkb" href="/search?safe=off&amp;q=related:test.psy.com/">
  71. Pages similaires
  72. </a>
  73. </li>
  74. </ul>
  75. </div>
  76. </div>
  77. </div>
  78. <span class="st">
  79. This should be the content.
  80. </span>
  81. <br>
  82. <div class="osl">‎
  83. <a href="http://www.google.fr/url?url=http://test.psychologies.com/tests/">
  84. Test Personnalité
  85. </a> - ‎
  86. <a href="http://www.google.fr/url?url=http://test.psychologies.com/test/">
  87. Tests - Moi
  88. </a> - ‎
  89. <a href="http://www.google.fr/url?url=http://test.psychologies.com/test/tests-couple">
  90. Test Couple
  91. </a>
  92. - ‎
  93. <a href="http://www.google.fr/url?url=http://test.psychologies.com/tests/tests-amour">
  94. Test Amour
  95. </a>
  96. </div>
  97. </div>
  98. </div>
  99. <div class="g">
  100. <h3 class="r">
  101. <a href="http://www.google.com/images?q=toto">
  102. <b>This</b>
  103. </a>
  104. </h3>
  105. </div>
  106. <div class="g">
  107. <h3 class="r">
  108. <a href="http://www.google.com/search?q=toto">
  109. <b>This</b> is
  110. </a>
  111. </h3>
  112. </div>
  113. <div class="g">
  114. <h3 class="r">
  115. <a href="€">
  116. <b>This</b> is <b>the</b>
  117. </a>
  118. </h3>
  119. </div>
  120. <div class="g">
  121. <h3 class="r">
  122. <a href="/url?q=url">
  123. <b>This</b> is <b>the</b>
  124. </a>
  125. </h3>
  126. </div>
  127. <p class="_Bmc" style="margin:3px 8px">
  128. <a href="/search?num=20&amp;safe=off&amp;q=t&amp;revid=1754833769&amp;sa=X&amp;ei=-&amp;ved=">
  129. suggestion <b>title</b>
  130. </a>
  131. </p>
  132. """
  133. response = self.mock_response(html)
  134. results = google.response(response)
  135. self.assertEqual(type(results), list)
  136. self.assertEqual(len(results), 2)
  137. self.assertEqual(results[0]['title'], 'This is the title')
  138. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  139. self.assertEqual(results[0]['content'], 'This should be the content.')
  140. self.assertEqual(results[1]['suggestion'], 'suggestion title')
  141. html = """
  142. <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
  143. </li>
  144. """
  145. response = self.mock_response(html)
  146. results = google.response(response)
  147. self.assertEqual(type(results), list)
  148. self.assertEqual(len(results), 0)
  149. response = mock.Mock(text='<html></html>', url='https://sorry.google.com')
  150. response.search_params = mock.Mock()
  151. response.search_params.get = mock.Mock(return_value='www.google.com')
  152. self.assertRaises(RuntimeWarning, google.response, response)
  153. response = mock.Mock(text='<html></html>', url='https://www.google.com/sorry/IndexRedirect')
  154. response.search_params = mock.Mock()
  155. response.search_params.get = mock.Mock(return_value='www.google.com')
  156. self.assertRaises(RuntimeWarning, google.response, response)
  157. def test_parse_images(self):
  158. html = """
  159. <li>
  160. <div>
  161. <a href="http://www.google.com/url?q=http://this.is.the.url/">
  162. <img style="margin:3px 0;margin-right:6px;padding:0" height="90"
  163. src="https://this.is.the.image/image.jpg" width="60" align="middle" alt="" border="0">
  164. </a>
  165. </div>
  166. </li>
  167. """
  168. dom = lxml.html.fromstring(html)
  169. results = google.parse_images(dom, 'www.google.com')
  170. self.assertEqual(type(results), list)
  171. self.assertEqual(len(results), 1)
  172. self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
  173. self.assertEqual(results[0]['title'], '')
  174. self.assertEqual(results[0]['content'], '')
  175. self.assertEqual(results[0]['img_src'], 'https://this.is.the.image/image.jpg')
  176. def test_fetch_supported_languages(self):
  177. html = """<html></html>"""
  178. response = mock.Mock(text=html)
  179. languages = google._fetch_supported_languages(response)
  180. self.assertEqual(type(languages), dict)
  181. self.assertEqual(len(languages), 0)
  182. html = u"""
  183. <html>
  184. <body>
  185. <table>
  186. <tbody>
  187. <tr>
  188. <td>
  189. <font>
  190. <label>
  191. <span id="ten">English</span>
  192. </label>
  193. </font>
  194. </td>
  195. <td>
  196. <font>
  197. <label>
  198. <span id="tzh-CN">中文 (简体)</span>
  199. </label>
  200. <label>
  201. <span id="tzh-TW">中文 (繁體)</span>
  202. </label>
  203. </font>
  204. </td>
  205. </tr>
  206. </tbody>
  207. </table>
  208. </body>
  209. </html>
  210. """
  211. response = mock.Mock(text=html)
  212. languages = google._fetch_supported_languages(response)
  213. self.assertEqual(type(languages), dict)
  214. self.assertEqual(len(languages), 3)
  215. self.assertIn('en', languages)
  216. self.assertIn('zh-CN', languages)
  217. self.assertIn('zh-TW', languages)
  218. self.assertEquals(type(languages['en']), dict)
  219. self.assertEquals(type(languages['zh-CN']), dict)
  220. self.assertEquals(type(languages['zh-TW']), dict)
  221. self.assertIn('name', languages['en'])
  222. self.assertIn('name', languages['zh-CN'])
  223. self.assertIn('name', languages['zh-TW'])
  224. self.assertEquals(languages['en']['name'], 'English')
  225. self.assertEquals(languages['zh-CN']['name'], u'中文 (简体)')
  226. self.assertEquals(languages['zh-TW']['name'], u'中文 (繁體)')