2024-03-11 13:06:26 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# pylint: disable=missing-module-docstring
|
|
|
|
|
2024-10-05 14:10:56 +00:00
|
|
|
import babel
|
2024-03-11 13:06:26 +00:00
|
|
|
from mock import Mock
|
2024-10-05 14:10:21 +00:00
|
|
|
from searx import plugins
|
2021-09-02 14:01:34 +00:00
|
|
|
from tests import SearxTestCase
|
2015-03-14 21:35:29 +00:00
|
|
|
|
|
|
|
|
2015-10-23 21:31:53 +00:00
|
|
|
def get_search_mock(query, **kwargs):
|
2024-10-05 14:10:56 +00:00
|
|
|
lang = kwargs.get("lang", "en-US")
|
|
|
|
kwargs["locale"] = babel.Locale.parse(lang, sep="-")
|
2024-03-11 13:06:26 +00:00
|
|
|
return Mock(search_query=Mock(query=query, **kwargs), result_container=Mock(answers={}))
|
2015-10-23 21:31:53 +00:00
|
|
|
|
|
|
|
|
2024-03-11 13:06:26 +00:00
|
|
|
class PluginMock: # pylint: disable=missing-class-docstring, too-few-public-methods
|
2021-09-13 17:37:51 +00:00
|
|
|
default_on = False
|
|
|
|
name = 'Default plugin'
|
|
|
|
description = 'Default plugin description'
|
|
|
|
|
|
|
|
|
2024-03-11 13:06:26 +00:00
|
|
|
class PluginStoreTest(SearxTestCase): # pylint: disable=missing-class-docstring
|
2024-09-24 03:37:30 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.store = plugins.PluginStore()
|
2015-03-14 21:35:29 +00:00
|
|
|
|
2024-09-24 03:37:30 +00:00
|
|
|
def test_init(self):
|
|
|
|
self.assertEqual(0, len(self.store.plugins))
|
|
|
|
self.assertIsInstance(self.store.plugins, list)
|
|
|
|
|
|
|
|
def test_register(self):
|
2021-09-13 17:37:51 +00:00
|
|
|
testplugin = PluginMock()
|
2024-09-24 03:37:30 +00:00
|
|
|
self.store.register(testplugin)
|
|
|
|
self.assertEqual(1, len(self.store.plugins))
|
2015-03-14 21:35:29 +00:00
|
|
|
|
2024-09-24 03:37:30 +00:00
|
|
|
def test_call_empty(self):
|
|
|
|
testplugin = PluginMock()
|
|
|
|
self.store.register(testplugin)
|
|
|
|
setattr(testplugin, 'asdf', Mock())
|
|
|
|
request = Mock()
|
|
|
|
self.store.call([], 'asdf', request, Mock())
|
|
|
|
self.assertFalse(getattr(testplugin, 'asdf').called) # pylint: disable=E1101
|
2015-03-14 21:35:29 +00:00
|
|
|
|
2024-09-24 03:37:30 +00:00
|
|
|
def test_call_with_plugin(self):
|
2015-03-14 21:35:29 +00:00
|
|
|
store = plugins.PluginStore()
|
2021-09-13 17:37:51 +00:00
|
|
|
testplugin = PluginMock()
|
2015-03-14 21:35:29 +00:00
|
|
|
store.register(testplugin)
|
|
|
|
setattr(testplugin, 'asdf', Mock())
|
2017-01-02 11:06:04 +00:00
|
|
|
request = Mock()
|
|
|
|
store.call([testplugin], 'asdf', request, Mock())
|
2024-09-24 03:37:30 +00:00
|
|
|
self.assertTrue(getattr(testplugin, 'asdf').called) # pylint: disable=E1101
|