test_duckduckgo.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import duckduckgo
  4. from searx.testing import SearxTestCase
  5. class TestDuckduckgoEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 1
  10. dicto['language'] = 'fr_FR'
  11. params = duckduckgo.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn(query, params['url'])
  14. self.assertIn('duckduckgo.com', params['url'])
  15. self.assertIn('fr-fr', params['url'])
  16. dicto['language'] = 'all'
  17. params = duckduckgo.request(query, dicto)
  18. self.assertIn('en-us', params['url'])
  19. def test_response(self):
  20. self.assertRaises(AttributeError, duckduckgo.response, None)
  21. self.assertRaises(AttributeError, duckduckgo.response, [])
  22. self.assertRaises(AttributeError, duckduckgo.response, '')
  23. self.assertRaises(AttributeError, duckduckgo.response, '[]')
  24. response = mock.Mock(text='<html></html>')
  25. self.assertEqual(duckduckgo.response(response), [])
  26. html = """
  27. <div class="results_links results_links_deep web-result">
  28. <div class="icon_fav" style="display: block;">
  29. <a rel="nofollow" href="https://www.test.com/">
  30. <img width="16" height="16" alt=""
  31. src="/i/www.test.com.ico" style="visibility: visible;" name="i15" />
  32. </a>
  33. </div>
  34. <div class="links_main links_deep"> <!-- This is the visible part -->
  35. <a rel="nofollow" class="large" href="http://this.should.be.the.link/">
  36. This <b>is</b> <b>the</b> title
  37. </a>
  38. <div class="snippet"><b>This</b> should be the content.</div>
  39. <div class="url">
  40. http://this.should.be.the.link/
  41. </div>
  42. </div>
  43. </div>
  44. """
  45. response = mock.Mock(text=html)
  46. results = duckduckgo.response(response)
  47. self.assertEqual(type(results), list)
  48. self.assertEqual(len(results), 1)
  49. self.assertEqual(results[0]['title'], 'This is the title')
  50. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  51. self.assertEqual(results[0]['content'], 'This should be the content.')
  52. html = """
  53. <div class="results_links results_links_deep web-result">
  54. <div class="icon_fav" style="display: block;">
  55. </div>
  56. <div class="links_main links_deep"> <!-- This is the visible part -->
  57. <div class="snippet"><b>This</b> should be the content.</div>
  58. <div class="url">
  59. http://this.should.be.the.link/
  60. </div>
  61. </div>
  62. </div>
  63. <div class="results_links results_links_deep web-result">
  64. <div class="icon_fav" style="display: block;">
  65. <img width="16" height="16" alt=""
  66. src="/i/www.test.com.ico" style="visibility: visible;" name="i15" />
  67. </div>
  68. <div class="links_main links_deep"> <!-- This is the visible part -->
  69. <a rel="nofollow" class="large" href="">
  70. This <b>is</b> <b>the</b> title
  71. </a>
  72. <div class="snippet"><b>This</b> should be the content.</div>
  73. <div class="url">
  74. http://this.should.be.the.link/
  75. </div>
  76. </div>
  77. </div>
  78. """
  79. response = mock.Mock(text=html)
  80. results = duckduckgo.response(response)
  81. self.assertEqual(type(results), list)
  82. self.assertEqual(len(results), 0)