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_webutils.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_webutils.py')
-rw-r--r-- | tests/unit/test_webutils.py | 85 |
1 files changed, 49 insertions, 36 deletions
diff --git a/tests/unit/test_webutils.py b/tests/unit/test_webutils.py index c6c441dab..cf9da5f10 100644 --- a/tests/unit/test_webutils.py +++ b/tests/unit/test_webutils.py @@ -2,52 +2,59 @@ # pylint: disable=missing-module-docstring import mock +from parameterized.parameterized import parameterized from searx import webutils from tests import SearxTestCase class TestWebUtils(SearxTestCase): # pylint: disable=missing-class-docstring - def test_prettify_url(self): - data = ( + + @parameterized.expand( + [ ('https://searx.me/', 'https://searx.me/'), ('https://searx.me/ű', 'https://searx.me/ű'), ('https://searx.me/' + (100 * 'a'), 'https://searx.me/[...]aaaaaaaaaaaaaaaaa'), ('https://searx.me/' + (100 * 'ű'), 'https://searx.me/[...]űűűűűűűűűűűűűűűűű'), - ) - - for test_url, expected in data: - self.assertEqual(webutils.prettify_url(test_url, max_length=32), expected) - - def test_highlight_content(self): - self.assertEqual(webutils.highlight_content(0, None), None) - self.assertEqual(webutils.highlight_content(None, None), None) - self.assertEqual(webutils.highlight_content('', None), None) - self.assertEqual(webutils.highlight_content(False, None), None) - - contents = ['<html></html>not<'] - for content in contents: - self.assertEqual(webutils.highlight_content(content, None), content) - - content = 'a' - query = 'test' - self.assertEqual(webutils.highlight_content(content, query), 'a') - query = 'a test' - self.assertEqual(webutils.highlight_content(content, query), '<span class="highlight">a</span>') - - # pylint: disable=line-too-long - data = ( + ] + ) + def test_prettify_url(self, test_url: str, expected: str): + self.assertEqual(webutils.prettify_url(test_url, max_length=32), expected) + + @parameterized.expand( + [ + (0, None, None), + (None, None, None), + ('', None, None), + (False, None, None), + ] + ) + def test_highlight_content_none(self, content, query, expected): + self.assertEqual(webutils.highlight_content(content, query), expected) + + def test_highlight_content_same(self): + content = '<html></html>not<' + self.assertEqual(webutils.highlight_content(content, None), content) + + @parameterized.expand( + [ + ('test', 'a', 'a'), + ('a test', 'a', '<span class="highlight">a</span>'), ('" test "', 'a test string', 'a <span class="highlight">test</span> string'), ('"a"', 'this is a test string', 'this is <span class="highlight">a</span> test string'), ( 'a test', 'this is a test string that matches entire query', - 'this is <span class="highlight">a</span> <span class="highlight">test</span> string that matches entire query', + 'this is <span class="highlight">a</span>' + ' <span class="highlight">test</span>' + ' string that matches entire query', ), ( 'this a test', 'this is a string to test.', ( - '<span class="highlight">this</span> is <span class="highlight">a</span> string to <span class="highlight">test</span>.' + '<span class="highlight">this</span>' + ' is <span class="highlight">a</span>' + ' string to <span class="highlight">test</span>.' ), ), ( @@ -65,9 +72,10 @@ class TestWebUtils(SearxTestCase): # pylint: disable=missing-class-docstring 'a string with class.', '<span class="highlight">a</span> string with <span class="highlight">class</span>.', ), - ) - for query, content, expected in data: - self.assertEqual(webutils.highlight_content(content, query), expected) + ] + ) + def test_highlight_content_equal(self, query: str, content: str, expected: str): + self.assertEqual(webutils.highlight_content(content, query), expected) class TestUnicodeWriter(SearxTestCase): # pylint: disable=missing-class-docstring @@ -76,7 +84,7 @@ class TestUnicodeWriter(SearxTestCase): # pylint: disable=missing-class-docstri def test_write_row(self): row = [1, 2, 3] - self.assertEqual(self.unicode_writer.writerow(row), None) + self.assertIsNone(self.unicode_writer.writerow(row)) def test_write_rows(self): self.unicode_writer.writerow = mock.MagicMock() @@ -86,13 +94,18 @@ class TestUnicodeWriter(SearxTestCase): # pylint: disable=missing-class-docstri class TestNewHmac(SearxTestCase): # pylint: disable=missing-class-docstring - def test_bytes(self): + @parameterized.expand( + [ + b'secret', + 1, + ] + ) + def test_attribute_error(self, secret_key): data = b'http://example.com' with self.assertRaises(AttributeError): - webutils.new_hmac(b'secret', data) - - with self.assertRaises(AttributeError): - webutils.new_hmac(1, data) + webutils.new_hmac(secret_key, data) + def test_bytes(self): + data = b'http://example.com' res = webutils.new_hmac('secret', data) self.assertEqual(res, '23e2baa2404012a5cc8e4a18b4aabf0dde4cb9b56f679ddc0fd6d7c24339d819') |