logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

test_duckduckgo.py (4113B)


  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import load_engine, duckduckgo
  5. from searx.testing import SearxTestCase
  6. class TestDuckduckgoEngine(SearxTestCase):
  7. def test_request(self):
  8. duckduckgo = load_engine({'engine': 'duckduckgo', 'name': 'duckduckgo'})
  9. query = 'test_query'
  10. dicto = defaultdict(dict)
  11. dicto['pageno'] = 1
  12. dicto['time_range'] = ''
  13. dicto['language'] = 'de-CH'
  14. params = duckduckgo.request(query, dicto)
  15. self.assertIn('url', params)
  16. self.assertIn(query, params['url'])
  17. self.assertIn('duckduckgo.com', params['url'])
  18. self.assertIn('ch-de', params['url'])
  19. self.assertIn('s=0', params['url'])
  20. # when ddg uses non standard codes
  21. dicto['language'] = 'zh-HK'
  22. params = duckduckgo.request(query, dicto)
  23. self.assertIn('hk-tzh', params['url'])
  24. dicto['language'] = 'en-GB'
  25. params = duckduckgo.request(query, dicto)
  26. self.assertIn('uk-en', params['url'])
  27. # no country given
  28. dicto['language'] = 'en'
  29. params = duckduckgo.request(query, dicto)
  30. self.assertIn('us-en', params['url'])
  31. def test_no_url_in_request_year_time_range(self):
  32. dicto = defaultdict(dict)
  33. query = 'test_query'
  34. dicto['time_range'] = 'year'
  35. params = duckduckgo.request(query, dicto)
  36. self.assertEqual({}, params['url'])
  37. def test_response(self):
  38. self.assertRaises(AttributeError, duckduckgo.response, None)
  39. self.assertRaises(AttributeError, duckduckgo.response, [])
  40. self.assertRaises(AttributeError, duckduckgo.response, '')
  41. self.assertRaises(AttributeError, duckduckgo.response, '[]')
  42. response = mock.Mock(text='<html></html>')
  43. self.assertEqual(duckduckgo.response(response), [])
  44. html = u"""
  45. <div class="result results_links results_links_deep web-result result--no-result">
  46. <div class="links_main links_deep result__body">
  47. <h2 class="result__title">
  48. </h2>
  49. <div class="no-results">No results</div>
  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(duckduckgo.response(response), [])
  58. html = u"""
  59. <div class="result results_links results_links_deep web-result ">
  60. <div class="links_main links_deep result__body">
  61. <h2 class="result__title">
  62. <a rel="nofollow" class="result__a" href="http://this.should.be.the.link/ű">
  63. This <b>is</b> <b>the</b> title
  64. </a>
  65. </h2>
  66. <a class="result__snippet" href="http://this.should.be.the.link/ű">
  67. <b>This</b> should be the content.
  68. </a>
  69. <div class="result__extras">
  70. </div>
  71. </div>
  72. </div>
  73. """
  74. response = mock.Mock(text=html)
  75. results = duckduckgo.response(response)
  76. self.assertEqual(type(results), list)
  77. self.assertEqual(len(results), 1)
  78. self.assertEqual(results[0]['title'], 'This is the title')
  79. self.assertEqual(results[0]['url'], u'http://this.should.be.the.link/ű')
  80. self.assertEqual(results[0]['content'], 'This should be the content.')
  81. def test_fetch_supported_languages(self):
  82. js = """some code...regions:{
  83. "wt-wt":"All Results","ar-es":"Argentina","au-en":"Australia","at-de":"Austria","be-fr":"Belgium (fr)"
  84. }some more code..."""
  85. response = mock.Mock(text=js)
  86. languages = list(duckduckgo._fetch_supported_languages(response))
  87. self.assertEqual(len(languages), 5)
  88. self.assertIn('wt-WT', languages)
  89. self.assertIn('es-AR', languages)
  90. self.assertIn('en-AU', languages)
  91. self.assertIn('de-AT', languages)
  92. self.assertIn('fr-BE', languages)