test_bing.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import bing
  4. from searx.testing import SearxTestCase
  5. class TestBingEngine(SearxTestCase):
  6. def test_request(self):
  7. query = u'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. dicto['language'] = 'fr-FR'
  11. params = bing.request(query.encode('utf-8'), dicto)
  12. self.assertTrue('url' in params)
  13. self.assertTrue(query in params['url'])
  14. self.assertTrue('language%3AFR' in params['url'])
  15. self.assertTrue('bing.com' in params['url'])
  16. def test_response(self):
  17. self.assertRaises(AttributeError, bing.response, None)
  18. self.assertRaises(AttributeError, bing.response, [])
  19. self.assertRaises(AttributeError, bing.response, '')
  20. self.assertRaises(AttributeError, bing.response, '[]')
  21. response = mock.Mock(text='<html></html>')
  22. self.assertEqual(bing.response(response), [])
  23. response = mock.Mock(text='<html></html>')
  24. self.assertEqual(bing.response(response), [])
  25. html = """
  26. <div class="sa_cc" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
  27. <div Class="sa_mc">
  28. <div class="sb_tlst">
  29. <h3>
  30. <a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
  31. <strong>This</strong> should be the title</a>
  32. </h3>
  33. </div>
  34. <div class="sb_meta"><cite><strong>this</strong>.meta.com</cite>
  35. <span class="c_tlbxTrg">
  36. <span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
  37. </span>
  38. </span>
  39. </div>
  40. <p><strong>This</strong> should be the content.</p>
  41. </div>
  42. </div>
  43. """
  44. response = mock.Mock(text=html)
  45. results = bing.response(response)
  46. self.assertEqual(type(results), list)
  47. self.assertEqual(len(results), 1)
  48. self.assertEqual(results[0]['title'], 'This should be the title')
  49. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  50. self.assertEqual(results[0]['content'], 'This should be the content.')
  51. html = """
  52. <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
  53. <div Class="sa_mc">
  54. <div class="sb_tlst">
  55. <h2>
  56. <a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
  57. <strong>This</strong> should be the title</a>
  58. </h2>
  59. </div>
  60. <div class="sb_meta"><cite><strong>this</strong>.meta.com</cite>
  61. <span class="c_tlbxTrg">
  62. <span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
  63. </span>
  64. </span>
  65. </div>
  66. <p><strong>This</strong> should be the content.</p>
  67. </div>
  68. </li>
  69. """
  70. response = mock.Mock(text=html)
  71. results = bing.response(response)
  72. self.assertEqual(type(results), list)
  73. self.assertEqual(len(results), 1)
  74. self.assertEqual(results[0]['title'], 'This should be the title')
  75. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  76. self.assertEqual(results[0]['content'], 'This should be the content.')
  77. def test_fetch_supported_languages(self):
  78. html = """<html></html>"""
  79. response = mock.Mock(text=html)
  80. results = bing._fetch_supported_languages(response)
  81. self.assertEqual(type(results), list)
  82. self.assertEqual(len(results), 0)
  83. html = """
  84. <html>
  85. <body>
  86. <form>
  87. <div id="limit-languages">
  88. <div>
  89. <div><input id="es" value="es"></input></div>
  90. </div>
  91. <div>
  92. <div><input id="pt_BR" value="pt_BR"></input></div>
  93. <div><input id="pt_PT" value="pt_PT"></input></div>
  94. </div>
  95. </div>
  96. </form>
  97. </body>
  98. </html>
  99. """
  100. response = mock.Mock(text=html)
  101. languages = bing._fetch_supported_languages(response)
  102. self.assertEqual(type(languages), list)
  103. self.assertEqual(len(languages), 3)
  104. self.assertIn('es', languages)
  105. self.assertIn('pt-BR', languages)
  106. self.assertIn('pt-PT', languages)