test_standalone_searx.py 3.7 KB

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