test_search.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring,disable=missing-class-docstring,invalid-name
  3. from copy import copy
  4. import searx.search
  5. from searx.search import SearchQuery, EngineRef
  6. from searx import settings
  7. from tests import SearxTestCase
  8. SAFESEARCH = 0
  9. PAGENO = 1
  10. PUBLIC_ENGINE_NAME = "dummy engine" # from the ./settings/test_settings.yml
  11. class SearchQueryTestCase(SearxTestCase):
  12. def test_repr(self):
  13. s = SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, '1', 5.0, 'g')
  14. self.assertEqual(
  15. repr(s), "SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, '1', 5.0, 'g', None)"
  16. ) # noqa
  17. def test_eq(self):
  18. s = SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, None, None, None)
  19. t = SearchQuery('test', [EngineRef('google', 'general')], 'all', 0, 1, None, None, None)
  20. self.assertEqual(s, s)
  21. self.assertNotEqual(s, t)
  22. def test_copy(self):
  23. s = SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, None, None, None)
  24. t = copy(s)
  25. self.assertEqual(s, t)
  26. class SearchTestCase(SearxTestCase):
  27. def test_timeout_simple(self):
  28. settings['outgoing']['max_request_timeout'] = None
  29. search_query = SearchQuery(
  30. 'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, None
  31. )
  32. search = searx.search.Search(search_query)
  33. with self.app.test_request_context('/search'):
  34. search.search()
  35. self.assertEqual(search.actual_timeout, 3.0)
  36. def test_timeout_query_above_default_nomax(self):
  37. settings['outgoing']['max_request_timeout'] = None
  38. search_query = SearchQuery(
  39. 'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, 5.0
  40. )
  41. search = searx.search.Search(search_query)
  42. with self.app.test_request_context('/search'):
  43. search.search()
  44. self.assertEqual(search.actual_timeout, 3.0)
  45. def test_timeout_query_below_default_nomax(self):
  46. settings['outgoing']['max_request_timeout'] = None
  47. search_query = SearchQuery(
  48. 'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, 1.0
  49. )
  50. search = searx.search.Search(search_query)
  51. with self.app.test_request_context('/search'):
  52. search.search()
  53. self.assertEqual(search.actual_timeout, 1.0)
  54. def test_timeout_query_below_max(self):
  55. settings['outgoing']['max_request_timeout'] = 10.0
  56. search_query = SearchQuery(
  57. 'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, 5.0
  58. )
  59. search = searx.search.Search(search_query)
  60. with self.app.test_request_context('/search'):
  61. search.search()
  62. self.assertEqual(search.actual_timeout, 5.0)
  63. def test_timeout_query_above_max(self):
  64. settings['outgoing']['max_request_timeout'] = 10.0
  65. search_query = SearchQuery(
  66. 'test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')], 'en-US', SAFESEARCH, PAGENO, None, 15.0
  67. )
  68. search = searx.search.Search(search_query)
  69. with self.app.test_request_context('/search'):
  70. search.search()
  71. self.assertEqual(search.actual_timeout, 10.0)
  72. def test_external_bang_valid(self):
  73. search_query = SearchQuery(
  74. 'yes yes',
  75. [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
  76. 'en-US',
  77. SAFESEARCH,
  78. PAGENO,
  79. None,
  80. None,
  81. external_bang="yt",
  82. )
  83. search = searx.search.Search(search_query)
  84. results = search.search()
  85. # For checking if the user redirected with the youtube external bang
  86. self.assertIsNotNone(results.redirect_url)
  87. def test_external_bang_none(self):
  88. search_query = SearchQuery(
  89. 'youtube never gonna give you up',
  90. [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
  91. 'en-US',
  92. SAFESEARCH,
  93. PAGENO,
  94. None,
  95. None,
  96. )
  97. search = searx.search.Search(search_query)
  98. with self.app.test_request_context('/search'):
  99. results = search.search()
  100. # This should not redirect
  101. self.assertIsNone(results.redirect_url)