test_duckduckgo.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. # when ddg uses non standard code
  19. dicto['language'] = 'en-GB'
  20. params = duckduckgo.request(query, dicto)
  21. self.assertIn('uk-en', params['url'])
  22. # no country given
  23. duckduckgo.supported_languages = ['de-CH', 'en-US']
  24. dicto['language'] = 'de'
  25. params = duckduckgo.request(query, dicto)
  26. self.assertIn('ch-de', params['url'])
  27. def test_no_url_in_request_year_time_range(self):
  28. dicto = defaultdict(dict)
  29. query = 'test_query'
  30. dicto['time_range'] = 'year'
  31. params = duckduckgo.request(query, dicto)
  32. self.assertEqual({}, params['url'])
  33. def test_response(self):
  34. self.assertRaises(AttributeError, duckduckgo.response, None)
  35. self.assertRaises(AttributeError, duckduckgo.response, [])
  36. self.assertRaises(AttributeError, duckduckgo.response, '')
  37. self.assertRaises(AttributeError, duckduckgo.response, '[]')
  38. response = mock.Mock(text='<html></html>')
  39. self.assertEqual(duckduckgo.response(response), [])
  40. html = u"""
  41. <div class="result results_links results_links_deep web-result result--no-result">
  42. <div class="links_main links_deep result__body">
  43. <h2 class="result__title">
  44. </h2>
  45. <div class="no-results">No results</div>
  46. <div class="result__extras">
  47. </div>
  48. </div>
  49. </div>
  50. """
  51. response = mock.Mock(text=html)
  52. results = duckduckgo.response(response)
  53. self.assertEqual(duckduckgo.response(response), [])
  54. html = u"""
  55. <div class="result results_links results_links_deep web-result ">
  56. <div class="links_main links_deep result__body">
  57. <h2 class="result__title">
  58. <a rel="nofollow" class="result__a" href="http://this.should.be.the.link/ű">
  59. This <b>is</b> <b>the</b> title
  60. </a>
  61. </h2>
  62. <a class="result__snippet" href="http://this.should.be.the.link/ű">
  63. <b>This</b> should be the content.
  64. </a>
  65. <div class="result__extras">
  66. </div>
  67. </div>
  68. </div>
  69. """
  70. response = mock.Mock(text=html)
  71. results = duckduckgo.response(response)
  72. self.assertEqual(type(results), list)
  73. self.assertEqual(len(results), 1)
  74. self.assertEqual(results[0]['title'], 'This is the title')
  75. self.assertEqual(results[0]['url'], u'http://this.should.be.the.link/ű')
  76. self.assertEqual(results[0]['content'], 'This should be the content.')
  77. def test_fetch_supported_languages(self):
  78. js = """some code...regions:{
  79. "wt-wt":"All Results","ar-es":"Argentina","au-en":"Australia","at-de":"Austria","be-fr":"Belgium (fr)"
  80. }some more code..."""
  81. response = mock.Mock(text=js)
  82. languages = list(duckduckgo._fetch_supported_languages(response))
  83. self.assertEqual(len(languages), 5)
  84. self.assertIn('wt-WT', languages)
  85. self.assertIn('es-AR', languages)
  86. self.assertIn('en-AU', languages)
  87. self.assertIn('de-AT', languages)
  88. self.assertIn('fr-BE', languages)