test_plugin_calculator.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring
  3. import flask
  4. from parameterized.parameterized import parameterized
  5. from searx import plugins
  6. from searx import preferences
  7. from tests import SearxTestCase
  8. from .test_utils import random_string
  9. from .test_plugins import get_search_mock
  10. class PluginCalculator(SearxTestCase): # pylint: disable=missing-class-docstring
  11. def setUp(self):
  12. from searx import webapp # pylint: disable=import-outside-toplevel
  13. self.webapp = webapp
  14. self.store = plugins.PluginStore()
  15. plugin = plugins.load_and_initialize_plugin('searx.plugins.calculator', False, (None, {}))
  16. self.store.register(plugin)
  17. self.preferences = preferences.Preferences(["simple"], ["general"], {}, self.store)
  18. self.preferences.parse_dict({"locale": "en"})
  19. def test_plugin_store_init(self):
  20. self.assertEqual(1, len(self.store.plugins))
  21. def test_single_page_number_true(self):
  22. with self.webapp.app.test_request_context():
  23. flask.request.preferences = self.preferences
  24. search = get_search_mock(query=random_string(10), pageno=2)
  25. result = self.store.call(self.store.plugins, 'post_search', flask.request, search)
  26. self.assertTrue(result)
  27. self.assertNotIn('calculate', search.result_container.answers)
  28. def test_long_query_true(self):
  29. with self.webapp.app.test_request_context():
  30. flask.request.preferences = self.preferences
  31. search = get_search_mock(query=random_string(101), pageno=1)
  32. result = self.store.call(self.store.plugins, 'post_search', flask.request, search)
  33. self.assertTrue(result)
  34. self.assertNotIn('calculate', search.result_container.answers)
  35. def test_alpha_true(self):
  36. with self.webapp.app.test_request_context():
  37. flask.request.preferences = self.preferences
  38. search = get_search_mock(query=random_string(10), pageno=1)
  39. result = self.store.call(self.store.plugins, 'post_search', flask.request, search)
  40. self.assertTrue(result)
  41. self.assertNotIn('calculate', search.result_container.answers)
  42. @parameterized.expand(
  43. [
  44. ("1+1", "2", "en"),
  45. ("1-1", "0", "en"),
  46. ("1*1", "1", "en"),
  47. ("1/1", "1", "en"),
  48. ("1**1", "1", "en"),
  49. ("1^1", "1", "en"),
  50. ("1,000.0+1,000.0", "2,000", "en"),
  51. ("1.0+1.0", "2", "en"),
  52. ("1.0-1.0", "0", "en"),
  53. ("1.0*1.0", "1", "en"),
  54. ("1.0/1.0", "1", "en"),
  55. ("1.0**1.0", "1", "en"),
  56. ("1.0^1.0", "1", "en"),
  57. ("1.000,0+1.000,0", "2.000", "de"),
  58. ("1,0+1,0", "2", "de"),
  59. ("1,0-1,0", "0", "de"),
  60. ("1,0*1,0", "1", "de"),
  61. ("1,0/1,0", "1", "de"),
  62. ("1,0**1,0", "1", "de"),
  63. ("1,0^1,0", "1", "de"),
  64. ]
  65. )
  66. def test_localized_query(self, operation: str, contains_result: str, lang: str):
  67. with self.webapp.app.test_request_context():
  68. self.preferences.parse_dict({"locale": lang})
  69. flask.request.preferences = self.preferences
  70. search = get_search_mock(query=operation, lang=lang, pageno=1)
  71. result = self.store.call(self.store.plugins, 'post_search', flask.request, search)
  72. self.assertTrue(result)
  73. self.assertIn('calculate', search.result_container.answers)
  74. self.assertIn(contains_result, search.result_container.answers['calculate']['answer'])
  75. @parameterized.expand(
  76. [
  77. "1/0",
  78. ]
  79. )
  80. def test_invalid_operations(self, operation):
  81. with self.webapp.app.test_request_context():
  82. flask.request.preferences = self.preferences
  83. search = get_search_mock(query=operation, pageno=1)
  84. result = self.store.call(self.store.plugins, 'post_search', flask.request, search)
  85. self.assertTrue(result)
  86. self.assertNotIn('calculate', search.result_container.answers)