test_standalone_searx.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # -*- coding: utf-8 -*-
  2. """Test utils/standalone_searx.py"""
  3. import datetime
  4. import importlib.util
  5. import sys
  6. from mock import Mock, patch
  7. from nose2.tools import params
  8. from searx.search import SearchQuery
  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. sas.parse_argument()
  26. def test_parse_argument_basic_args(self):
  27. """Test parse argument with basic args."""
  28. sas = get_standalone_searx_module()
  29. query = 'red box'
  30. exp_dict = {
  31. 'query': query, 'category': 'general', 'lang': 'all', 'pageno': 1,
  32. 'safesearch': '0', 'timerange': None}
  33. args = ['standalone_searx', query]
  34. with patch.object(sys, 'argv', args):
  35. res = sas.parse_argument()
  36. self.assertEqual(exp_dict, vars(res))
  37. res2 = sas.parse_argument(args[1:])
  38. self.assertEqual(exp_dict, vars(res2))
  39. def test_to_dict(self):
  40. """test to_dict."""
  41. sas = get_standalone_searx_module()
  42. self.assertEqual(
  43. sas.to_dict(
  44. sas.get_search_query(sas.parse_argument(['red box']))),
  45. {
  46. 'search': {
  47. 'q': 'red box', 'pageno': 1, 'lang': 'all',
  48. 'safesearch': 0, 'timerange': None
  49. },
  50. 'results': [], 'infoboxes': [], 'suggestions': [],
  51. 'answers': [], 'paging': False, 'results_number': 0
  52. }
  53. )
  54. def test_to_dict_with_mock(self):
  55. """test to dict."""
  56. sas = get_standalone_searx_module()
  57. with patch.object(sas.searx.search, 'Search') as mock_s:
  58. m_search = mock_s().search()
  59. m_sq = Mock()
  60. self.assertEqual(
  61. sas.to_dict(m_sq),
  62. {
  63. 'answers': [],
  64. 'infoboxes': m_search.infoboxes,
  65. 'paging': m_search.paging,
  66. 'results': m_search.get_ordered_results(),
  67. 'results_number': m_search.results_number(),
  68. 'search': {
  69. 'lang': m_sq.lang,
  70. 'pageno': m_sq.pageno,
  71. 'q': m_sq.query,
  72. 'safesearch': m_sq.safesearch,
  73. 'timerange': m_sq.time_range,
  74. },
  75. 'suggestions': []
  76. }
  77. )
  78. def test_get_search_query(self):
  79. """test get_search_query."""
  80. sas = get_standalone_searx_module()
  81. args = sas.parse_argument(['rain', ])
  82. search_q = sas.get_search_query(args)
  83. self.assertTrue(search_q)
  84. self.assertEqual(search_q, SearchQuery('rain', [], ['general'], 'all', 0, 1, None, None, None))
  85. def test_no_parsed_url(self):
  86. """test no_parsed_url func"""
  87. sas = get_standalone_searx_module()
  88. self.assertEqual(
  89. sas.no_parsed_url([{'parsed_url': 'http://example.com'}]),
  90. [{}]
  91. )
  92. @params(
  93. (datetime.datetime(2020, 1, 1), '2020-01-01T00:00:00'),
  94. ('a'.encode('utf8'), 'a'),
  95. (set([1]), [1])
  96. )
  97. def test_json_serial(self, arg, exp_res):
  98. """test json_serial func"""
  99. sas = get_standalone_searx_module()
  100. self.assertEqual(sas.json_serial(arg), exp_res)
  101. def test_json_serial_error(self):
  102. """test error on json_serial."""
  103. sas = get_standalone_searx_module()
  104. with self.assertRaises(TypeError):
  105. sas.json_serial('a')