test_plugin_calculator.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.result_types import Answer
  8. from tests import SearxTestCase
  9. from .test_utils import random_string
  10. from .test_plugins import do_post_search
  11. class PluginCalculator(SearxTestCase):
  12. def setUp(self):
  13. super().setUp()
  14. engines = {}
  15. self.storage = searx.plugins.PluginStorage()
  16. self.storage.load_settings({"searx.plugins.calculator.SXNGPlugin": {"active": True}})
  17. self.storage.init(self.app)
  18. self.pref = searx.preferences.Preferences(["simple"], ["general"], engines, self.storage)
  19. self.pref.parse_dict({"locale": "en"})
  20. def test_plugin_store_init(self):
  21. self.assertEqual(1, len(self.storage))
  22. def test_pageno_1_2(self):
  23. with self.app.test_request_context():
  24. sxng_request.preferences = self.pref
  25. query = "1+1"
  26. answer = Answer(answer=f"{query} = {eval(query)}") # pylint: disable=eval-used
  27. search = do_post_search(query, self.storage, pageno=1)
  28. self.assertIn(answer, search.result_container.answers)
  29. search = do_post_search(query, self.storage, pageno=2)
  30. self.assertEqual(list(search.result_container.answers), [])
  31. def test_long_query_ignored(self):
  32. with self.app.test_request_context():
  33. sxng_request.preferences = self.pref
  34. query = f"1+1 {random_string(101)}"
  35. search = do_post_search(query, self.storage)
  36. self.assertEqual(list(search.result_container.answers), [])
  37. @parameterized.expand(
  38. [
  39. ("1+1", "2", "en"),
  40. ("1-1", "0", "en"),
  41. ("1*1", "1", "en"),
  42. ("1/1", "1", "en"),
  43. ("1**1", "1", "en"),
  44. ("1^1", "1", "en"),
  45. ("1,000.0+1,000.0", "2,000", "en"),
  46. ("1.0+1.0", "2", "en"),
  47. ("1.0-1.0", "0", "en"),
  48. ("1.0*1.0", "1", "en"),
  49. ("1.0/1.0", "1", "en"),
  50. ("1.0**1.0", "1", "en"),
  51. ("1.0^1.0", "1", "en"),
  52. ("1.000,0+1.000,0", "2.000", "de"),
  53. ("1,0+1,0", "2", "de"),
  54. ("1,0-1,0", "0", "de"),
  55. ("1,0*1,0", "1", "de"),
  56. ("1,0/1,0", "1", "de"),
  57. ("1,0**1,0", "1", "de"),
  58. ("1,0^1,0", "1", "de"),
  59. ]
  60. )
  61. def test_localized_query(self, query: str, res: str, lang: str):
  62. with self.app.test_request_context():
  63. self.pref.parse_dict({"locale": lang})
  64. sxng_request.preferences = self.pref
  65. answer = Answer(answer=f"{query} = {res}")
  66. search = do_post_search(query, self.storage)
  67. self.assertIn(answer, search.result_container.answers)
  68. @parameterized.expand(
  69. [
  70. "1/0",
  71. ]
  72. )
  73. def test_invalid_operations(self, query):
  74. with self.app.test_request_context():
  75. sxng_request.preferences = self.pref
  76. search = do_post_search(query, self.storage)
  77. self.assertEqual(list(search.result_container.answers), [])