test_plugin_calculator.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring
  3. from mock import Mock
  4. from parameterized.parameterized import parameterized
  5. from searx import plugins
  6. from tests import SearxTestCase
  7. from .test_utils import random_string
  8. from .test_plugins import get_search_mock
  9. class PluginCalculator(SearxTestCase): # pylint: disable=missing-class-docstring
  10. def setUp(self):
  11. self.store = plugins.PluginStore()
  12. plugin = plugins.load_and_initialize_plugin('searx.plugins.calculator', False, (None, {}))
  13. self.store.register(plugin)
  14. def test_plugin_store_init(self):
  15. self.assertEqual(1, len(self.store.plugins))
  16. def test_single_page_number_true(self):
  17. request = Mock(remote_addr='127.0.0.1')
  18. search = get_search_mock(query=random_string(10), pageno=2)
  19. result = self.store.call(self.store.plugins, 'post_search', request, search)
  20. self.assertTrue(result)
  21. self.assertNotIn('calculate', search.result_container.answers)
  22. def test_long_query_true(self):
  23. request = Mock(remote_addr='127.0.0.1')
  24. search = get_search_mock(query=random_string(101), pageno=1)
  25. result = self.store.call(self.store.plugins, 'post_search', request, search)
  26. self.assertTrue(result)
  27. self.assertNotIn('calculate', search.result_container.answers)
  28. def test_alpha_true(self):
  29. request = Mock(remote_addr='127.0.0.1')
  30. search = get_search_mock(query=random_string(10), pageno=1)
  31. result = self.store.call(self.store.plugins, 'post_search', request, search)
  32. self.assertTrue(result)
  33. self.assertNotIn('calculate', search.result_container.answers)
  34. @parameterized.expand(
  35. [
  36. "1+1",
  37. "1-1",
  38. "1*1",
  39. "1/1",
  40. "1**1",
  41. "1^1",
  42. ]
  43. )
  44. def test_int_operations(self, operation):
  45. request = Mock(remote_addr='127.0.0.1')
  46. search = get_search_mock(query=operation, pageno=1)
  47. result = self.store.call(self.store.plugins, 'post_search', request, search)
  48. self.assertTrue(result)
  49. self.assertIn('calculate', search.result_container.answers)
  50. @parameterized.expand(
  51. [
  52. "1/0",
  53. ]
  54. )
  55. def test_invalid_operations(self, operation):
  56. request = Mock(remote_addr='127.0.0.1')
  57. search = get_search_mock(query=operation, pageno=1)
  58. result = self.store.call(self.store.plugins, 'post_search', request, search)
  59. self.assertTrue(result)
  60. self.assertNotIn('calculate', search.result_container.answers)