diff options
author | Grant Lanham <contact@grantlanham.com> | 2024-09-23 23:37:30 -0400 |
---|---|---|
committer | Markus Heiser <markus.heiser@darmarIT.de> | 2024-10-03 13:20:32 +0200 |
commit | 44a06190bbb1b412f0ed16a76b0a4aeef80975b7 (patch) | |
tree | dd6e94d656fe4b11918e6cb5bd6183535dcc7254 /tests/unit/test_utils.py | |
parent | 042c7190e6fe092a8a85997713a2511fffb09625 (diff) | |
download | searxng-44a06190bbb1b412f0ed16a76b0a4aeef80975b7.tar.gz searxng-44a06190bbb1b412f0ed16a76b0a4aeef80975b7.zip |
[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.
Diffstat (limited to 'tests/unit/test_utils.py')
-rw-r--r-- | tests/unit/test_utils.py | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 1a6fba46d..4306d0790 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -3,6 +3,7 @@ import lxml.etree from lxml import html +from parameterized.parameterized import parameterized from searx.exceptions import SearxXPathSyntaxException, SearxEngineXPathException from searx import utils @@ -66,9 +67,15 @@ class TestUtils(SearxTestCase): # pylint: disable=missing-class-docstring self.assertEqual(utils.extract_text(dom.xpath('boolean(//span)')), 'True') self.assertEqual(utils.extract_text(dom.xpath('//img/@src')), 'test.jpg') self.assertEqual(utils.extract_text(dom.xpath('//unexistingtag')), '') + + def test_extract_text_allow_none(self): self.assertEqual(utils.extract_text(None, allow_none=True), None) + + def test_extract_text_error_none(self): with self.assertRaises(ValueError): utils.extract_text(None) + + def test_extract_text_error_empty(self): with self.assertRaises(ValueError): utils.extract_text({}) @@ -103,14 +110,16 @@ class TestHTMLTextExtractor(SearxTestCase): # pylint: disable=missing-class-doc def test__init__(self): self.assertEqual(self.html_text_extractor.result, []) - def test_handle_charref(self): - self.html_text_extractor.handle_charref('xF') - self.assertIn('\x0f', self.html_text_extractor.result) - self.html_text_extractor.handle_charref('XF') - self.assertIn('\x0f', self.html_text_extractor.result) - - self.html_text_extractor.handle_charref('97') - self.assertIn('a', self.html_text_extractor.result) + @parameterized.expand( + [ + ('xF', '\x0f'), + ('XF', '\x0f'), + ('97', 'a'), + ] + ) + def test_handle_charref(self, charref: str, expected: str): + self.html_text_extractor.handle_charref(charref) + self.assertIn(expected, self.html_text_extractor.result) def test_handle_entityref(self): entity = 'test' @@ -191,7 +200,7 @@ class TestXPathUtils(SearxTestCase): # pylint: disable=missing-class-docstring self.assertEqual(utils.eval_xpath_getindex(doc, '//i/text()', 1, default='something'), 'something') # default is None - self.assertEqual(utils.eval_xpath_getindex(doc, '//i/text()', 1, default=None), None) + self.assertIsNone(utils.eval_xpath_getindex(doc, '//i/text()', 1, default=None)) # index not found with self.assertRaises(SearxEngineXPathException) as context: |