[refactor] unit tests to utilize paramaterized and break down monolithic tests

- for tests which perform the same arrange/act/assert pattern but with different
  data, the data portion has been moved to the ``paramaterized.expand`` fields

- for monolithic tests which performed multiple arrange/act/asserts,
  they have been broken up into different unit tests.

- when possible, change generic assert statements to more concise
  asserts (i.e. ``assertIsNone``)

This work ultimately is focused on creating smaller and more concise tests.
While paramaterized may make adding new configurations for existing tests
easier, that is just a beneficial side effect.  The main benefit is that smaller
tests are easier to reason about, meaning they are easier to debug when they
start failing.  This improves the developer experience in debugging what went
wrong when refactoring the project.

Total number of tests went from 192 -> 259; or, broke apart larger tests into 69
more concise ones.
This commit is contained in:
Grant Lanham 2024-09-23 23:37:30 -04:00 committed by Markus Heiser
parent 042c7190e6
commit 44a06190bb
12 changed files with 341 additions and 342 deletions

View file

@ -6,6 +6,8 @@ from pathlib import Path
import os
from unittest.mock import patch
from parameterized import parameterized
from searx.exceptions import SearxSettingsException
from searx import settings_loader
from tests import SearxTestCase
@ -31,13 +33,13 @@ class TestDefaultSettings(SearxTestCase): # pylint: disable=missing-class-docst
settings, msg = settings_loader.load_settings(load_user_settings=False)
self.assertTrue(msg.startswith('load the default settings from'))
self.assertFalse(settings['general']['debug'])
self.assertTrue(isinstance(settings['general']['instance_name'], str))
self.assertIsInstance(settings['general']['instance_name'], str)
self.assertEqual(settings['server']['secret_key'], "ultrasecretkey")
self.assertTrue(isinstance(settings['server']['port'], int))
self.assertTrue(isinstance(settings['server']['bind_address'], str))
self.assertTrue(isinstance(settings['engines'], list))
self.assertTrue(isinstance(settings['doi_resolvers'], dict))
self.assertTrue(isinstance(settings['default_doi_resolver'], str))
self.assertIsInstance(settings['server']['port'], int)
self.assertIsInstance(settings['server']['bind_address'], str)
self.assertIsInstance(settings['engines'], list)
self.assertIsInstance(settings['doi_resolvers'], dict)
self.assertIsInstance(settings['default_doi_resolver'], str)
class TestUserSettings(SearxTestCase): # pylint: disable=missing-class-docstring
@ -50,11 +52,14 @@ class TestUserSettings(SearxTestCase): # pylint: disable=missing-class-docstrin
with self.assertRaises(ValueError):
self.assertFalse(settings_loader.is_use_default_settings({'use_default_settings': 0}))
def test_user_settings_not_found(self):
with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': _settings("not_exists.yml")}):
with self.assertRaises(EnvironmentError):
_s, _m = settings_loader.load_settings()
with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': "/folder/not/exists"}):
@parameterized.expand(
[
_settings("not_exists.yml"),
"/folder/not/exists",
]
)
def test_user_settings_not_found(self, path: str):
with patch.dict(os.environ, {'SEARXNG_SETTINGS_PATH': path}):
with self.assertRaises(EnvironmentError):
_s, _m = settings_loader.load_settings()