test_bing.py 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. dicto['language'] = 'fr_FR'
  11. params = bing.request(query, 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. dicto['language'] = 'all'
  17. params = bing.request(query, dicto)
  18. self.assertTrue('language' not in params['url'])
  19. def test_response(self):
  20. self.assertRaises(AttributeError, bing.response, None)
  21. self.assertRaises(AttributeError, bing.response, [])
  22. self.assertRaises(AttributeError, bing.response, '')
  23. self.assertRaises(AttributeError, bing.response, '[]')
  24. response = mock.Mock(text='<html></html>')
  25. self.assertEqual(bing.response(response), [])
  26. response = mock.Mock(text='<html></html>')
  27. self.assertEqual(bing.response(response), [])
  28. html = """
  29. <div class="sa_cc" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
  30. <div Class="sa_mc">
  31. <div class="sb_tlst">
  32. <h3>
  33. <a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
  34. <strong>This</strong> should be the title</a>
  35. </h3>
  36. </div>
  37. <div class="sb_meta"><cite><strong>this</strong>.meta.com</cite>
  38. <span class="c_tlbxTrg">
  39. <span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
  40. </span>
  41. </span>
  42. </div>
  43. <p><strong>This</strong> should be the content.</p>
  44. </div>
  45. </div>
  46. """
  47. response = mock.Mock(text=html)
  48. results = bing.response(response)
  49. self.assertEqual(type(results), list)
  50. self.assertEqual(len(results), 1)
  51. self.assertEqual(results[0]['title'], 'This should be the title')
  52. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  53. self.assertEqual(results[0]['content'], 'This should be the content.')
  54. html = """
  55. <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
  56. <div Class="sa_mc">
  57. <div class="sb_tlst">
  58. <h2>
  59. <a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
  60. <strong>This</strong> should be the title</a>
  61. </h2>
  62. </div>
  63. <div class="sb_meta"><cite><strong>this</strong>.meta.com</cite>
  64. <span class="c_tlbxTrg">
  65. <span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
  66. </span>
  67. </span>
  68. </div>
  69. <p><strong>This</strong> should be the content.</p>
  70. </div>
  71. </li>
  72. """
  73. response = mock.Mock(text=html)
  74. results = bing.response(response)
  75. self.assertEqual(type(results), list)
  76. self.assertEqual(len(results), 1)
  77. self.assertEqual(results[0]['title'], 'This should be the title')
  78. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  79. self.assertEqual(results[0]['content'], 'This should be the content.')