test_duckduckgo.py 3.7KB

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