test_plugins.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring
  3. import babel
  4. from mock import Mock
  5. from searx import plugins
  6. from tests import SearxTestCase
  7. def get_search_mock(query, **kwargs):
  8. lang = kwargs.get("lang", "en-US")
  9. kwargs["locale"] = babel.Locale.parse(lang, sep="-")
  10. return Mock(search_query=Mock(query=query, **kwargs), result_container=Mock(answers={}))
  11. class PluginMock: # pylint: disable=missing-class-docstring, too-few-public-methods
  12. default_on = False
  13. name = 'Default plugin'
  14. description = 'Default plugin description'
  15. class PluginStoreTest(SearxTestCase): # pylint: disable=missing-class-docstring
  16. def setUp(self):
  17. self.store = plugins.PluginStore()
  18. def test_init(self):
  19. self.assertEqual(0, len(self.store.plugins))
  20. self.assertIsInstance(self.store.plugins, list)
  21. def test_register(self):
  22. testplugin = PluginMock()
  23. self.store.register(testplugin)
  24. self.assertEqual(1, len(self.store.plugins))
  25. def test_call_empty(self):
  26. testplugin = PluginMock()
  27. self.store.register(testplugin)
  28. setattr(testplugin, 'asdf', Mock())
  29. request = Mock()
  30. self.store.call([], 'asdf', request, Mock())
  31. self.assertFalse(getattr(testplugin, 'asdf').called) # pylint: disable=E1101
  32. def test_call_with_plugin(self):
  33. store = plugins.PluginStore()
  34. testplugin = PluginMock()
  35. store.register(testplugin)
  36. setattr(testplugin, 'asdf', Mock())
  37. request = Mock()
  38. store.call([testplugin], 'asdf', request, Mock())
  39. self.assertTrue(getattr(testplugin, 'asdf').called) # pylint: disable=E1101