test_standalone_searx.py 4.1 KB

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