test_duckduckgo.py 2.6KB

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