test_standalone_searx.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. """Test utils/standalone_searx.py"""
  3. import datetime
  4. import importlib.util
  5. import io
  6. import sys
  7. from mock import Mock, patch
  8. from nose2.tools import params
  9. from searx.search import SearchQuery, EngineRef, initialize
  10. from searx.testing import SearxTestCase
  11. def get_standalone_searx_module():
  12. """Get standalone_searx module."""
  13. module_name = 'utils.standalone_searx'
  14. filename = 'utils/standalone_searx.py'
  15. spec = importlib.util.spec_from_file_location(module_name, filename)
  16. sas = importlib.util.module_from_spec(spec)
  17. spec.loader.exec_module(sas)
  18. return sas
  19. class StandaloneSearx(SearxTestCase):
  20. """Unit test for standalone_searx."""
  21. @classmethod
  22. def setUpClass(cls):
  23. engine_list = [{'engine': 'dummy', 'name': 'engine1', 'shortcut': 'e1'}]
  24. initialize(engine_list)
  25. def test_parse_argument_no_args(self):
  26. """Test parse argument without args."""
  27. sas = get_standalone_searx_module()
  28. with patch.object(sys, 'argv', ['standalone_searx']), \
  29. self.assertRaises(SystemExit):
  30. sys.stderr = io.StringIO()
  31. sas.parse_argument()
  32. sys.stdout = sys.__stderr__
  33. def test_parse_argument_basic_args(self):
  34. """Test parse argument with basic args."""
  35. sas = get_standalone_searx_module()
  36. query = 'red box'
  37. exp_dict = {
  38. 'query': query, 'category': 'general', 'lang': 'all', 'pageno': 1,
  39. 'safesearch': '0', 'timerange': None}
  40. args = ['standalone_searx', query]
  41. with patch.object(sys, 'argv', args):
  42. res = sas.parse_argument()
  43. self.assertEqual(exp_dict, vars(res))
  44. res2 = sas.parse_argument(args[1:])
  45. self.assertEqual(exp_dict, vars(res2))
  46. def test_to_dict(self):
  47. """test to_dict."""
  48. sas = get_standalone_searx_module()
  49. self.assertEqual(
  50. sas.to_dict(
  51. sas.get_search_query(sas.parse_argument(['red box']))),
  52. {
  53. 'search': {
  54. 'q': 'red box', 'pageno': 1, 'lang': 'all',
  55. 'safesearch': 0, 'timerange': None
  56. },
  57. 'results': [], 'infoboxes': [], 'suggestions': [],
  58. 'answers': [], 'paging': False, 'results_number': 0
  59. }
  60. )
  61. def test_to_dict_with_mock(self):
  62. """test to dict."""
  63. sas = get_standalone_searx_module()
  64. with patch.object(sas.searx.search, 'Search') as mock_s:
  65. m_search = mock_s().search()
  66. m_sq = Mock()
  67. self.assertEqual(
  68. sas.to_dict(m_sq),
  69. {
  70. 'answers': [],
  71. 'infoboxes': m_search.infoboxes,
  72. 'paging': m_search.paging,
  73. 'results': m_search.get_ordered_results(),
  74. 'results_number': m_search.results_number(),
  75. 'search': {
  76. 'lang': m_sq.lang,
  77. 'pageno': m_sq.pageno,
  78. 'q': m_sq.query,
  79. 'safesearch': m_sq.safesearch,
  80. 'timerange': m_sq.time_range,
  81. },
  82. 'suggestions': []
  83. }
  84. )
  85. def test_get_search_query(self):
  86. """test get_search_query."""
  87. sas = get_standalone_searx_module()
  88. args = sas.parse_argument(['rain', ])
  89. search_q = sas.get_search_query(args)
  90. self.assertTrue(search_q)
  91. self.assertEqual(search_q, SearchQuery('rain', [EngineRef('engine1', 'general')],
  92. 'all', 0, 1, None, None, None))
  93. def test_no_parsed_url(self):
  94. """test no_parsed_url func"""
  95. sas = get_standalone_searx_module()
  96. self.assertEqual(
  97. sas.no_parsed_url([{'parsed_url': 'http://example.com'}]),
  98. [{}]
  99. )
  100. @params(
  101. (datetime.datetime(2020, 1, 1), '2020-01-01T00:00:00'),
  102. ('a'.encode('utf8'), 'a'),
  103. (set([1]), [1])
  104. )
  105. def test_json_serial(self, arg, exp_res):
  106. """test json_serial func"""
  107. sas = get_standalone_searx_module()
  108. self.assertEqual(sas.json_serial(arg), exp_res)
  109. def test_json_serial_error(self):
  110. """test error on json_serial."""
  111. sas = get_standalone_searx_module()
  112. with self.assertRaises(TypeError):
  113. sas.json_serial('a')