__init__.py 2.6 KB

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