summaryrefslogtreecommitdiff
path: root/tests/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/__init__.py')
-rw-r--r--tests/__init__.py60
1 files changed, 50 insertions, 10 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
index 2ccc51847..e68d72feb 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -1,16 +1,15 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
-# pylint: disable=missing-module-docstring
+# pylint: disable=missing-module-docstring,disable=missing-class-docstring,invalid-name
+import pathlib
import os
import aiounittest
-os.environ.pop('SEARX_DEBUG', None)
-os.environ.pop('SEARX_DEBUG_LOG_LEVEL', None)
-os.environ.pop('SEARX_DISABLE_ETC_SETTINGS', None)
-os.environ.pop('SEARX_SETTINGS_PATH', None)
+# Before import from the searx package, we need to set up the (debug)
+# environment. The import of the searx package initialize the searx.settings
+# and this in turn takes the defaults from the environment!
os.environ.pop('SEARXNG_SETTINGS_PATH', None)
-
os.environ['SEARXNG_DEBUG'] = '1'
os.environ['SEARXNG_DEBUG_LOG_LEVEL'] = 'WARNING'
os.environ['SEARXNG_DISABLE_ETC_SETTINGS'] = '1'
@@ -43,11 +42,15 @@ class SearxTestCase(aiounittest.AsyncTestCase):
layer = SearxTestLayer
+ SETTINGS_FOLDER = pathlib.Path(__file__).parent / "unit" / "settings"
+ TEST_SETTINGS = "test_settings.yml"
+
+ def setUp(self):
+ self.init_test_settings()
+
def setattr4test(self, obj, attr, value):
- """
- setattr(obj, attr, value)
- but reset to the previous value in the cleanup.
- """
+ """setattr(obj, attr, value) but reset to the previous value in the
+ cleanup."""
previous_value = getattr(obj, attr)
def cleanup_patch():
@@ -55,3 +58,40 @@ class SearxTestCase(aiounittest.AsyncTestCase):
self.addCleanup(cleanup_patch)
setattr(obj, attr, value)
+
+ def init_test_settings(self):
+ """Sets ``SEARXNG_SETTINGS_PATH`` environment variable an initialize
+ global ``settings`` variable and the ``logger`` from a test config in
+ :origin:`tests/unit/settings/`.
+ """
+
+ os.environ['SEARXNG_SETTINGS_PATH'] = str(self.SETTINGS_FOLDER / self.TEST_SETTINGS)
+
+ # pylint: disable=import-outside-toplevel
+ import searx
+ import searx.locales
+ import searx.plugins
+ import searx.search
+ import searx.webapp
+
+ # https://flask.palletsprojects.com/en/stable/config/#builtin-configuration-values
+ # searx.webapp.app.config["DEBUG"] = True
+ searx.webapp.app.config["TESTING"] = True # to get better error messages
+ searx.webapp.app.config["EXPLAIN_TEMPLATE_LOADING"] = True
+
+ searx.init_settings()
+ searx.plugins.initialize(searx.webapp.app)
+
+ # searx.search.initialize will:
+ # - load the engines and
+ # - initialize searx.network, searx.metrics, searx.processors and searx.search.checker
+
+ searx.search.initialize(
+ enable_checker=True,
+ check_network=True,
+ enable_metrics=searx.get_setting("general.enable_metrics"), # type: ignore
+ )
+
+ # pylint: disable=attribute-defined-outside-init
+ self.app = searx.webapp.app
+ self.client = searx.webapp.app.test_client()