test_standalone_searx.py 4.0 KB

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