searxng/tests/unit/test_engines_init.py
Markus Heiser 86b4d2f2d0 [mod] activate pyright checks (in CI)
We have been using a static type checker (pyright) for a long time, but its
check was not yet a prerequisite for passing the quality gate.  It was checked
in the CI, but the error messages were only logged.

As is always the case in life, with checks that you have to do but which have no
consequences; you neglect them :-)

We didn't activate the checks back then because we (even today) have too much
monkey patching in our code (not only in the engines, httpx and others objects
are also affected).

We want to replace monkey patching with clear interfaces for a long time, the
basis for this is increased typing and we can only achieve this if we make type
checking an integral part of the quality gate.

  This PR activates the type check; in order to pass the check, a few typings
  were corrected in the code, but most type inconsistencies were deactivated via
  inline comments.

This was particularly necessary in places where the code uses properties that
stick to the objects (monkey patching).  The sticking of properties only happens
in a few places, but the access to these properties extends over the entire
code, which is why there are many `# type: ignore` markers in the code ... which
we will hopefully be able to remove again successively in the future.

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
2024-04-27 18:31:52 +02:00

81 lines
3.4 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
# pylint: disable=missing-module-docstring
from searx import settings, engines
from tests import SearxTestCase
class TestEnginesInit(SearxTestCase): # pylint: disable=missing-class-docstring
@classmethod
def tearDownClass(cls):
settings['outgoing']['using_tor_proxy'] = False
settings['outgoing']['extra_proxy_timeout'] = 0
def test_initialize_engines_default(self):
engine_list = [
{'engine': 'dummy', 'name': 'engine1', 'shortcut': 'e1'},
{'engine': 'dummy', 'name': 'engine2', 'shortcut': 'e2'},
]
engines.load_engines(engine_list)
self.assertEqual(len(engines.engines), 2)
self.assertIn('engine1', engines.engines)
self.assertIn('engine2', engines.engines)
def test_initialize_engines_exclude_onions(self): # pylint: disable=invalid-name
settings['outgoing']['using_tor_proxy'] = False
engine_list = [
{'engine': 'dummy', 'name': 'engine1', 'shortcut': 'e1', 'categories': 'general'},
{'engine': 'dummy', 'name': 'engine2', 'shortcut': 'e2', 'categories': 'onions'},
]
engines.load_engines(engine_list)
self.assertEqual(len(engines.engines), 1)
self.assertIn('engine1', engines.engines)
self.assertNotIn('onions', engines.categories)
def test_initialize_engines_include_onions(self): # pylint: disable=invalid-name
settings['outgoing']['using_tor_proxy'] = True
settings['outgoing']['extra_proxy_timeout'] = 100.0
engine_list = [
{
'engine': 'dummy',
'name': 'engine1',
'shortcut': 'e1',
'categories': 'general',
'timeout': 20.0,
'onion_url': 'http://engine1.onion',
},
{'engine': 'dummy', 'name': 'engine2', 'shortcut': 'e2', 'categories': 'onions'},
]
engines.load_engines(engine_list)
self.assertEqual(len(engines.engines), 2)
self.assertIn('engine1', engines.engines)
self.assertIn('engine2', engines.engines)
self.assertIn('onions', engines.categories)
self.assertIn('http://engine1.onion', engines.engines['engine1'].search_url) # type: ignore
self.assertEqual(engines.engines['engine1'].timeout, 120.0)
def test_missing_name_field(self):
settings['outgoing']['using_tor_proxy'] = False
engine_list = [
{'engine': 'dummy', 'shortcut': 'e1', 'categories': 'general'},
]
with self.assertLogs('searx.engines', level='ERROR') as cm: # pylint: disable=invalid-name
engines.load_engines(engine_list)
self.assertEqual(len(engines.engines), 0)
self.assertEqual(cm.output, ['ERROR:searx.engines:An engine does not have a "name" field'])
def test_missing_engine_field(self):
settings['outgoing']['using_tor_proxy'] = False
engine_list = [
{'name': 'engine2', 'shortcut': 'e2', 'categories': 'onions'},
]
with self.assertLogs('searx.engines', level='ERROR') as cm: # pylint: disable=invalid-name
engines.load_engines(engine_list)
self.assertEqual(len(engines.engines), 0)
self.assertEqual(
cm.output, ['ERROR:searx.engines:The "engine" field is missing for the engine named "engine2"']
)