test_duckduckgo.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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'] = 'de_CH'
  12. dicto['time_range'] = ''
  13. params = duckduckgo.request(query, dicto)
  14. self.assertIn('url', params)
  15. self.assertIn(query, params['url'])
  16. self.assertIn('duckduckgo.com', params['url'])
  17. self.assertIn('ch-de', params['url'])
  18. def test_response(self):
  19. self.assertRaises(AttributeError, duckduckgo.response, None)
  20. self.assertRaises(AttributeError, duckduckgo.response, [])
  21. self.assertRaises(AttributeError, duckduckgo.response, '')
  22. self.assertRaises(AttributeError, duckduckgo.response, '[]')
  23. response = mock.Mock(text='<html></html>')
  24. self.assertEqual(duckduckgo.response(response), [])
  25. html = u"""
  26. <div class="result results_links results_links_deep web-result result--no-result">
  27. <div class="links_main links_deep result__body">
  28. <h2 class="result__title">
  29. </h2>
  30. <div class="no-results">No results</div>
  31. <div class="result__extras">
  32. </div>
  33. </div>
  34. </div>
  35. """
  36. response = mock.Mock(text=html)
  37. results = duckduckgo.response(response)
  38. self.assertEqual(duckduckgo.response(response), [])
  39. html = u"""
  40. <div class="result results_links results_links_deep web-result ">
  41. <div class="links_main links_deep result__body">
  42. <h2 class="result__title">
  43. <a rel="nofollow" class="result__a" href="http://this.should.be.the.link/ű">
  44. This <b>is</b> <b>the</b> title
  45. </a>
  46. </h2>
  47. <a class="result__snippet" href="http://this.should.be.the.link/ű">
  48. <b>This</b> should be the content.
  49. </a>
  50. <div class="result__extras">
  51. </div>
  52. </div>
  53. </div>
  54. """
  55. response = mock.Mock(text=html)
  56. results = duckduckgo.response(response)
  57. self.assertEqual(type(results), list)
  58. self.assertEqual(len(results), 1)
  59. self.assertEqual(results[0]['title'], 'This is the title')
  60. self.assertEqual(results[0]['url'], u'http://this.should.be.the.link/ű')
  61. self.assertEqual(results[0]['content'], 'This should be the content.')