test_duckduckgo.py 3.8KB

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