test_plugin_calculator.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring,disable=missing-class-docstring,invalid-name
  3. from parameterized.parameterized import parameterized
  4. import searx.plugins
  5. import searx.preferences
  6. from searx.extended_types import sxng_request
  7. from searx.plugins._core import _default, ModulePlugin
  8. from searx.result_types import Answer
  9. from searx.utils import load_module
  10. from tests import SearxTestCase
  11. from .test_utils import random_string
  12. from .test_plugins import do_post_search
  13. class PluginCalculator(SearxTestCase):
  14. def setUp(self):
  15. super().setUp()
  16. f = _default / "calculator.py"
  17. mod = load_module(f.name, str(f.parent))
  18. engines = {}
  19. self.storage = searx.plugins.PluginStorage()
  20. self.storage.register(ModulePlugin(mod))
  21. self.storage.init(self.app)
  22. self.pref = searx.preferences.Preferences(["simple"], ["general"], engines, self.storage)
  23. self.pref.parse_dict({"locale": "en"})
  24. def test_plugin_store_init(self):
  25. self.assertEqual(1, len(self.storage))
  26. def test_pageno_1_2(self):
  27. with self.app.test_request_context():
  28. sxng_request.preferences = self.pref
  29. query = "1+1"
  30. answer = Answer(answer=f"{query} = {eval(query)}") # pylint: disable=eval-used
  31. search = do_post_search(query, self.storage, pageno=1)
  32. self.assertIn(answer, search.result_container.answers)
  33. search = do_post_search(query, self.storage, pageno=2)
  34. self.assertEqual(list(search.result_container.answers), [])
  35. def test_long_query_ignored(self):
  36. with self.app.test_request_context():
  37. sxng_request.preferences = self.pref
  38. query = f"1+1 {random_string(101)}"
  39. search = do_post_search(query, self.storage)
  40. self.assertEqual(list(search.result_container.answers), [])
  41. @parameterized.expand(
  42. [
  43. ("1+1", "2", "en"),
  44. ("1-1", "0", "en"),
  45. ("1*1", "1", "en"),
  46. ("1/1", "1", "en"),
  47. ("1**1", "1", "en"),
  48. ("1^1", "1", "en"),
  49. ("1,000.0+1,000.0", "2,000", "en"),
  50. ("1.0+1.0", "2", "en"),
  51. ("1.0-1.0", "0", "en"),
  52. ("1.0*1.0", "1", "en"),
  53. ("1.0/1.0", "1", "en"),
  54. ("1.0**1.0", "1", "en"),
  55. ("1.0^1.0", "1", "en"),
  56. ("1.000,0+1.000,0", "2.000", "de"),
  57. ("1,0+1,0", "2", "de"),
  58. ("1,0-1,0", "0", "de"),
  59. ("1,0*1,0", "1", "de"),
  60. ("1,0/1,0", "1", "de"),
  61. ("1,0**1,0", "1", "de"),
  62. ("1,0^1,0", "1", "de"),
  63. ]
  64. )
  65. def test_localized_query(self, query: str, res: str, lang: str):
  66. with self.app.test_request_context():
  67. self.pref.parse_dict({"locale": lang})
  68. sxng_request.preferences = self.pref
  69. answer = Answer(answer=f"{query} = {res}")
  70. search = do_post_search(query, self.storage)
  71. self.assertIn(answer, search.result_container.answers)
  72. @parameterized.expand(
  73. [
  74. "1/0",
  75. ]
  76. )
  77. def test_invalid_operations(self, query):
  78. with self.app.test_request_context():
  79. sxng_request.preferences = self.pref
  80. search = do_post_search(query, self.storage)
  81. self.assertEqual(list(search.result_container.answers), [])