__init__.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring,disable=missing-class-docstring,invalid-name
  3. import pathlib
  4. import os
  5. import aiounittest
  6. # Before import from the searx package, we need to set up the (debug)
  7. # environment. The import of the searx package initialize the searx.settings
  8. # and this in turn takes the defaults from the environment!
  9. os.environ.pop('SEARXNG_SETTINGS_PATH', None)
  10. os.environ['SEARXNG_DEBUG'] = '1'
  11. os.environ['SEARXNG_DEBUG_LOG_LEVEL'] = 'WARNING'
  12. os.environ['SEARXNG_DISABLE_ETC_SETTINGS'] = '1'
  13. class SearxTestLayer:
  14. """Base layer for non-robot tests."""
  15. __name__ = 'SearxTestLayer'
  16. @classmethod
  17. def setUp(cls):
  18. pass
  19. @classmethod
  20. def tearDown(cls):
  21. pass
  22. @classmethod
  23. def testSetUp(cls):
  24. pass
  25. @classmethod
  26. def testTearDown(cls):
  27. pass
  28. class SearxTestCase(aiounittest.AsyncTestCase):
  29. """Base test case for non-robot tests."""
  30. layer = SearxTestLayer
  31. SETTINGS_FOLDER = pathlib.Path(__file__).parent / "unit" / "settings"
  32. TEST_SETTINGS = "test_settings.yml"
  33. def setUp(self):
  34. self.init_test_settings()
  35. def setattr4test(self, obj, attr, value):
  36. """setattr(obj, attr, value) but reset to the previous value in the
  37. cleanup."""
  38. previous_value = getattr(obj, attr)
  39. def cleanup_patch():
  40. setattr(obj, attr, previous_value)
  41. self.addCleanup(cleanup_patch)
  42. setattr(obj, attr, value)
  43. def init_test_settings(self):
  44. """Sets ``SEARXNG_SETTINGS_PATH`` environment variable an initialize
  45. global ``settings`` variable and the ``logger`` from a test config in
  46. :origin:`tests/unit/settings/`.
  47. """
  48. os.environ['SEARXNG_SETTINGS_PATH'] = str(self.SETTINGS_FOLDER / self.TEST_SETTINGS)
  49. # pylint: disable=import-outside-toplevel
  50. import searx
  51. import searx.locales
  52. import searx.plugins
  53. import searx.search
  54. import searx.webapp
  55. # https://flask.palletsprojects.com/en/stable/config/#builtin-configuration-values
  56. # searx.webapp.app.config["DEBUG"] = True
  57. searx.webapp.app.config["TESTING"] = True # to get better error messages
  58. searx.webapp.app.config["EXPLAIN_TEMPLATE_LOADING"] = True
  59. searx.init_settings()
  60. searx.plugins.initialize(searx.webapp.app)
  61. # searx.search.initialize will:
  62. # - load the engines and
  63. # - initialize searx.network, searx.metrics, searx.processors and searx.search.checker
  64. searx.search.initialize(
  65. enable_checker=True,
  66. check_network=True,
  67. enable_metrics=searx.get_setting("general.enable_metrics"), # type: ignore
  68. )
  69. # pylint: disable=attribute-defined-outside-init
  70. self.app = searx.webapp.app
  71. self.client = searx.webapp.app.test_client()