test_search.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. from searx.testing import SearxTestCase
  3. from searx.search import SearchQuery, EngineRef
  4. import searx.search
  5. SAFESEARCH = 0
  6. PAGENO = 1
  7. PUBLIC_ENGINE_NAME = 'general dummy'
  8. TEST_ENGINES = [
  9. {
  10. 'name': PUBLIC_ENGINE_NAME,
  11. 'engine': 'dummy',
  12. 'categories': 'general',
  13. 'shortcut': 'gd',
  14. 'timeout': 3.0,
  15. 'tokens': [],
  16. },
  17. ]
  18. class SearchQueryTestCase(SearxTestCase):
  19. def test_repr(self):
  20. s = SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, '1', 5.0, 'g')
  21. self.assertEqual(repr(s),
  22. "SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, '1', 5.0, 'g')") # noqa
  23. def test_eq(self):
  24. s = SearchQuery('test', [EngineRef('bing', 'general')], 'all', 0, 1, None, None, None)
  25. t = SearchQuery('test', [EngineRef('google', 'general')], 'all', 0, 1, None, None, None)
  26. self.assertEqual(s, s)
  27. self.assertNotEqual(s, t)
  28. class SearchTestCase(SearxTestCase):
  29. @classmethod
  30. def setUpClass(cls):
  31. searx.search.initialize(TEST_ENGINES)
  32. def test_timeout_simple(self):
  33. searx.search.max_request_timeout = None
  34. search_query = SearchQuery('test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
  35. 'en-US', SAFESEARCH, PAGENO, None, None)
  36. search = searx.search.Search(search_query)
  37. search.search()
  38. self.assertEqual(search.actual_timeout, 3.0)
  39. def test_timeout_query_above_default_nomax(self):
  40. searx.search.max_request_timeout = None
  41. search_query = SearchQuery('test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
  42. 'en-US', SAFESEARCH, PAGENO, None, 5.0)
  43. search = searx.search.Search(search_query)
  44. search.search()
  45. self.assertEqual(search.actual_timeout, 3.0)
  46. def test_timeout_query_below_default_nomax(self):
  47. searx.search.max_request_timeout = None
  48. search_query = SearchQuery('test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
  49. 'en-US', SAFESEARCH, PAGENO, None, 1.0)
  50. search = searx.search.Search(search_query)
  51. search.search()
  52. self.assertEqual(search.actual_timeout, 1.0)
  53. def test_timeout_query_below_max(self):
  54. searx.search.max_request_timeout = 10.0
  55. search_query = SearchQuery('test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
  56. 'en-US', SAFESEARCH, PAGENO, None, 5.0)
  57. search = searx.search.Search(search_query)
  58. search.search()
  59. self.assertEqual(search.actual_timeout, 5.0)
  60. def test_timeout_query_above_max(self):
  61. searx.search.max_request_timeout = 10.0
  62. search_query = SearchQuery('test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
  63. 'en-US', SAFESEARCH, PAGENO, None, 15.0)
  64. search = searx.search.Search(search_query)
  65. search.search()
  66. self.assertEqual(search.actual_timeout, 10.0)
  67. def test_external_bang(self):
  68. search_query = SearchQuery('yes yes',
  69. [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
  70. 'en-US', SAFESEARCH, PAGENO, None, None,
  71. external_bang="yt")
  72. search = searx.search.Search(search_query)
  73. results = search.search()
  74. # For checking if the user redirected with the youtube external bang
  75. self.assertTrue(results.redirect_url is not None)
  76. search_query = SearchQuery('youtube never gonna give you up',
  77. [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
  78. 'en-US', SAFESEARCH, PAGENO, None, None)
  79. search = searx.search.Search(search_query)
  80. results = search.search()
  81. # This should not redirect
  82. self.assertTrue(results.redirect_url is None)